Search in sources :

Example 46 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class DateFormatTest method Test6880.

@Test
public void Test6880() {
    Date d1, d2, dp1, dp2, dexp1, dexp2;
    String s1, s2;
    TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
    GregorianCalendar gcal = new GregorianCalendar(tz);
    gcal.clear();
    // offset 8:05:43
    gcal.set(1900, Calendar.JANUARY, 1, 12, 00);
    d1 = gcal.getTime();
    gcal.clear();
    // offset 8:00
    gcal.set(1950, Calendar.JANUARY, 1, 12, 00);
    d2 = gcal.getTime();
    gcal.clear();
    gcal.set(1970, Calendar.JANUARY, 1, 12, 00);
    dexp2 = gcal.getTime();
    // subtract 5m43s
    dexp1 = new Date(dexp2.getTime() - (5 * 60 + 43) * 1000);
    DateFormat fmt = DateFormat.getTimeInstance(DateFormat.FULL, new ULocale("zh"));
    fmt.setTimeZone(tz);
    s1 = fmt.format(d1);
    s2 = fmt.format(d2);
    try {
        dp1 = fmt.parse(s1);
        dp2 = fmt.parse(s2);
        if (!dp1.equals(dexp1)) {
            errln("FAIL: Failed to parse " + s1 + " parsed: " + dp1 + " expected: " + dexp1);
        }
        if (!dp2.equals(dexp2)) {
            errln("FAIL: Failed to parse " + s2 + " parsed: " + dp2 + " expected: " + dexp2);
        }
    } catch (ParseException pe) {
        errln("FAIL: Parse failure");
    }
}
Also used : TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) ParseException(java.text.ParseException) Date(java.util.Date) Test(org.junit.Test)

Example 47 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class DateFormatTest method TestDateFormatNone.

/**
 * Test the formatting of dates with the 'NONE' keyword.
 */
@Test
public void TestDateFormatNone() {
    // Mon Sep 15 00:00:00 PDT 1997
    Date testDate = new Date(874306800000L);
    DateFormat dfFrench = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.NONE, Locale.FRENCH);
    // Set TimeZone = PDT
    TimeZone tz = TimeZone.getTimeZone("PST");
    dfFrench.setTimeZone(tz);
    String expectedFRENCH_JDK12 = "lundi 15 septembre 1997";
    // String expectedFRENCH = "lundi 15 septembre 1997 00 h 00 PDT";
    logln("Date set to : " + testDate);
    String out = dfFrench.format(testDate);
    logln("Date Formated with French Locale " + out);
    if (!out.equals(expectedFRENCH_JDK12))
        errln("FAIL: Expected " + expectedFRENCH_JDK12 + " Got " + out);
}
Also used : TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 48 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class DateFormatTest method TestTwoDigitYearDSTParse.

/**
 * Test the parsing of 2-digit years.
 */
@Test
public void TestTwoDigitYearDSTParse() {
    SimpleDateFormat fullFmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss.SSS zzz yyyy G");
    SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yy h:mm:ss 'o''clock' a z", Locale.ENGLISH);
    String s = "03-Apr-04 2:20:47 o'clock AM PST";
    /*
         * SimpleDateFormat(pattern, locale) Construct a SimpleDateDateFormat using
         * the given pattern, the locale and using the TimeZone.getDefault();
         * So it need to add the timezone offset on hour field.
         * ps. the Method Calendar.getTime() used by SimpleDateFormat.parse() always
         * return Date value with TimeZone.getDefault() [Richard/GCL]
         */
    TimeZone defaultTZ = TimeZone.getDefault();
    TimeZone PST = TimeZone.getTimeZone("PST");
    int defaultOffset = defaultTZ.getRawOffset();
    int PSTOffset = PST.getRawOffset();
    int hour = 2 + (defaultOffset - PSTOffset) / (60 * 60 * 1000);
    // hour is the expected hour of day, in units of seconds
    hour = ((hour < 0) ? hour + 24 : hour) * 60 * 60;
    try {
        Date d = fmt.parse(s);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        // DSTOffset
        hour += defaultTZ.inDaylightTime(d) ? 1 : 0;
        logln(s + " P> " + ((DateFormat) fullFmt).format(d));
        // hr is the actual hour of day, in units of seconds
        // adjust for DST
        int hr = cal.get(Calendar.HOUR_OF_DAY) * 60 * 60 - cal.get(Calendar.DST_OFFSET) / 1000;
        if (hr != hour)
            errln("FAIL: Hour (-DST) = " + hr / (60 * 60.0) + "; expected " + hour / (60 * 60.0));
    } catch (ParseException e) {
        errln("Parse Error:" + e.getMessage());
    }
}
Also used : TimeZone(android.icu.util.TimeZone) BuddhistCalendar(android.icu.util.BuddhistCalendar) HebrewCalendar(android.icu.util.HebrewCalendar) IslamicCalendar(android.icu.util.IslamicCalendar) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 49 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class DateIntervalFormatTest method TestFPos_SkelWithSeconds.

@Test
public void TestFPos_SkelWithSeconds() {
    final long[] deltas = { // none
    0L, // 200 millisec
    200L, // 20 sec
    20000L, // 20 min
    1200000L, // 2 hrs
    7200000L, // 12 hrs
    43200000L, // 8 days
    691200000L, // 16 days,
    1382400000L, // 100 days
    8640000000L };
    class ExpectPosAndFormat {

        public int posBegin;

        public int posEnd;

        public String format;

        // Simple constructor
        public ExpectPosAndFormat(int pBegin, int pEnd, String fmt) {
            posBegin = pBegin;
            posEnd = pEnd;
            format = fmt;
        }
    }
    ;
    final ExpectPosAndFormat[] exp_en_HHmm = { new ExpectPosAndFormat(3, 5, "09:00"), new ExpectPosAndFormat(3, 5, "09:00"), new ExpectPosAndFormat(3, 5, "09:00"), new ExpectPosAndFormat(3, 5, "09:00 \u2013 09:20"), new ExpectPosAndFormat(3, 5, "09:00 \u2013 11:00"), new ExpectPosAndFormat(3, 5, "09:00 \u2013 21:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00 \u2013 11/28/2014, 09:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00 \u2013 12/6/2014, 09:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00 \u2013 2/28/2015, 09:00") };
    final ExpectPosAndFormat[] exp_en_HHmmss = { new ExpectPosAndFormat(3, 5, "09:00:00"), new ExpectPosAndFormat(3, 5, "09:00:00"), new ExpectPosAndFormat(3, 5, "09:00:00 \u2013 09:00:20"), new ExpectPosAndFormat(3, 5, "09:00:00 \u2013 09:20:00"), new ExpectPosAndFormat(3, 5, "09:00:00 \u2013 11:00:00"), new ExpectPosAndFormat(3, 5, "09:00:00 \u2013 21:00:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00:00 \u2013 11/28/2014, 09:00:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00:00 \u2013 12/6/2014, 09:00:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00:00 \u2013 2/28/2015, 09:00:00") };
    final ExpectPosAndFormat[] exp_en_yyMMdd = { new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14 \u2013 11/28/14"), new ExpectPosAndFormat(0, 0, "11/20/14 \u2013 12/6/14"), new ExpectPosAndFormat(0, 0, "11/20/14 \u2013 2/28/15") };
    final ExpectPosAndFormat[] exp_en_yyMMddHHmm = { new ExpectPosAndFormat(13, 15, "11/20/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 09:20"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 11:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 21:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 11/28/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 12/06/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 02/28/15, 09:00") };
    final ExpectPosAndFormat[] exp_en_yyMMddHHmmss = { new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 09:00:20"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 09:20:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 11:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 21:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 11/28/14, 09:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 12/06/14, 09:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 02/28/15, 09:00:00") };
    @SuppressWarnings("unused") final ExpectPosAndFormat[] exp_en_yMMMdhmmssz = { new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 9:00:20 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 9:20:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 11:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 9:00:00 PM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 Nov 28, 2014, 9:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 Dec 6, 2014, 9:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 Feb 28, 2015, 9:00:00 AM GMT") };
    final ExpectPosAndFormat[] exp_ja_yyMMddHHmm = { new ExpectPosAndFormat(11, 13, "14/11/20 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9\u664200\u5206\uFF5E9\u664220\u5206"), new ExpectPosAndFormat(11, 13, "14/11/20 9\u664200\u5206\uFF5E11\u664200\u5206"), new ExpectPosAndFormat(11, 13, "14/11/20 9\u664200\u5206\uFF5E21\u664200\u5206"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00\uFF5E14/11/28 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00\uFF5E14/12/06 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00\uFF5E15/02/28 9:00") };
    final ExpectPosAndFormat[] exp_ja_yyMMddHHmmss = { new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E9:00:20"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E9:20:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E11:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E21:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E14/11/28 9:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E14/12/06 9:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E15/02/28 9:00:00") };
    final ExpectPosAndFormat[] exp_ja_yMMMdHHmmss = { new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E9:00:20"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E9:20:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E11:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E21:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E2014\u5E7411\u670828\u65E5 9:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E2014\u5E7412\u67086\u65E5 9:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E2015\u5E742\u670828\u65E5 9:00:00") };
    class LocaleAndSkeletonItem {

        public String locale;

        public String skeleton;

        public int fieldToCheck;

        public ExpectPosAndFormat[] expected;

        // Simple constructor
        public LocaleAndSkeletonItem(String loc, String skel, int field, ExpectPosAndFormat[] exp) {
            locale = loc;
            skeleton = skel;
            fieldToCheck = field;
            expected = exp;
        }
    }
    ;
    final LocaleAndSkeletonItem[] locSkelItems = { new LocaleAndSkeletonItem("en", "HHmm", DateFormat.MINUTE_FIELD, exp_en_HHmm), new LocaleAndSkeletonItem("en", "HHmmss", DateFormat.MINUTE_FIELD, exp_en_HHmmss), new LocaleAndSkeletonItem("en", "yyMMdd", DateFormat.MINUTE_FIELD, exp_en_yyMMdd), new LocaleAndSkeletonItem("en", "yyMMddHHmm", DateFormat.MINUTE_FIELD, exp_en_yyMMddHHmm), new LocaleAndSkeletonItem("en", "yyMMddHHmmss", DateFormat.MINUTE_FIELD, exp_en_yyMMddHHmmss), // new LocaleAndSkeletonItem( "en",		"yMMMdhmmssz",  DateFormat.MINUTE_FIELD, exp_en_yMMMdhmmssz ),
    new LocaleAndSkeletonItem("ja", "yyMMddHHmm", DateFormat.MINUTE_FIELD, exp_ja_yyMMddHHmm), new LocaleAndSkeletonItem("ja", "yyMMddHHmmss", DateFormat.MINUTE_FIELD, exp_ja_yyMMddHHmmss), new LocaleAndSkeletonItem("ja", "yMMMdHHmmss", DateFormat.MINUTE_FIELD, exp_ja_yMMMdHHmmss) };
    // final String zoneGMT = "GMT";
    // 2014 Nov 20 09:00 GMT
    final long startTimeGMT = 1416474000000L;
    TimeZone localZone = TimeZone.getDefault();
    long startTime = startTimeGMT - localZone.getOffset(startTimeGMT);
    for (LocaleAndSkeletonItem item : locSkelItems) {
        DateIntervalFormat difmt = DateIntervalFormat.getInstance(item.skeleton, new ULocale(item.locale));
        int dIdx, dCount = deltas.length;
        for (dIdx = 0; dIdx < dCount; dIdx++) {
            DateInterval di = new DateInterval(startTime, startTime + deltas[dIdx]);
            StringBuffer actual = new StringBuffer(64);
            FieldPosition pos = new FieldPosition(item.fieldToCheck);
            String actualString = difmt.format(di, actual, pos).toString();
            ExpectPosAndFormat expectPosFmt = item.expected[dIdx];
            if (!actualString.equals(expectPosFmt.format) || pos.getBeginIndex() != expectPosFmt.posBegin || pos.getEndIndex() != expectPosFmt.posEnd) {
                errln("For locale " + item.locale + ", skeleton " + item.skeleton + ", delta " + deltas[dIdx] + ": expect " + expectPosFmt.posBegin + "-" + expectPosFmt.posEnd + " \"" + expectPosFmt.format + "\"; get " + pos.getBeginIndex() + "-" + pos.getEndIndex() + " \"" + actualString + "\"");
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) FieldPosition(java.text.FieldPosition) TimeZone(android.icu.util.TimeZone) Test(org.junit.Test)

Example 50 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class GlobalizationPreferencesTest method TestTimeZone.

@Test
public void TestTimeZone() {
    GlobalizationPreferences gp = new GlobalizationPreferences();
    // Set locale - zh_CN
    logln("Set locale - zh_CN");
    gp.setLocale(new ULocale("zh_CN"));
    TimeZone tz = gp.getTimeZone();
    String tzid = tz.getID();
    if (!tzid.equals("Asia/Shanghai")) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: Asia/Shanghai");
    }
    // Set locale - en
    logln("Set locale - en");
    gp.setLocale(new ULocale("en"));
    tz = gp.getTimeZone();
    tzid = tz.getID();
    if (!tzid.equals("America/New_York")) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: America/New_York");
    }
    // Set territory - GB
    logln("Set territory - GB");
    gp.setTerritory("GB");
    tz = gp.getTimeZone();
    tzid = tz.getID();
    if (!tzid.equals("Europe/London")) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: Europe/London");
    }
    // Check if getTimeZone returns a safe clone
    tz.setID("Bad_ID");
    tz = gp.getTimeZone();
    tzid = tz.getID();
    if (!tzid.equals("Europe/London")) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: Europe/London");
    }
    // Set explicit time zone
    TimeZone jst = TimeZone.getTimeZone("Asia/Tokyo");
    String customJstId = "Japan_Standard_Time";
    jst.setID(customJstId);
    gp.setTimeZone(jst);
    tz = gp.getTimeZone();
    tzid = tz.getID();
    if (!tzid.equals(customJstId)) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: " + customJstId);
    }
    // Freeze
    logln("Freeze this object");
    TimeZone cst = TimeZone.getTimeZone("Europe/Paris");
    boolean bFrozen = false;
    gp.freeze();
    try {
        gp.setTimeZone(cst);
    } catch (UnsupportedOperationException uoe) {
        logln("setTimeZone is blocked");
        bFrozen = true;
    }
    if (!bFrozen) {
        errln("FAIL: setTimeZone must be blocked");
    }
    // Modifiable clone
    logln("cloneAsThawed");
    GlobalizationPreferences gp1 = (GlobalizationPreferences) gp.cloneAsThawed();
    tz = gp1.getTimeZone();
    tzid = tz.getID();
    if (!tzid.equals(customJstId)) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: " + customJstId);
    }
    // Set explicit time zone
    gp1.setTimeZone(cst);
    tz = gp1.getTimeZone();
    tzid = tz.getID();
    if (!tzid.equals(cst.getID())) {
        errln("FAIL: Time zone ID is " + tzid + " Expected: " + cst.getID());
    }
}
Also used : TimeZone(android.icu.util.TimeZone) GlobalizationPreferences(android.icu.util.GlobalizationPreferences) ULocale(android.icu.util.ULocale) Test(org.junit.Test)

Aggregations

TimeZone (android.icu.util.TimeZone)125 Test (org.junit.Test)98 SimpleTimeZone (android.icu.util.SimpleTimeZone)76 Date (java.util.Date)63 GregorianCalendar (android.icu.util.GregorianCalendar)49 Calendar (android.icu.util.Calendar)42 BasicTimeZone (android.icu.util.BasicTimeZone)41 SimpleDateFormat (android.icu.text.SimpleDateFormat)36 ULocale (android.icu.util.ULocale)31 RuleBasedTimeZone (android.icu.util.RuleBasedTimeZone)24 VTimeZone (android.icu.util.VTimeZone)23 DateFormat (android.icu.text.DateFormat)19 JavaTimeZone (android.icu.impl.JavaTimeZone)18 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)18 JapaneseCalendar (android.icu.util.JapaneseCalendar)15 FieldPosition (java.text.FieldPosition)15 BuddhistCalendar (android.icu.util.BuddhistCalendar)13 ParsePosition (java.text.ParsePosition)13 ChineseCalendar (android.icu.util.ChineseCalendar)12 ParseException (java.text.ParseException)12