Search in sources :

Example 81 with TimeZone

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

the class DateFormatTest method TestWallyWedel.

// Test written by Wally Wedel and emailed to me.
@Test
public void TestWallyWedel() {
    /*
         * Instantiate a TimeZone so we can get the ids.
         */
    // TimeZone tz = new SimpleTimeZone(7, ""); //The variable is never used
    /*
         * Computational variables.
         */
    int offset, hours, minutes, seconds;
    /*
         * Instantiate a SimpleDateFormat set up to produce a full time
         zone name.
         */
    SimpleDateFormat sdf = new SimpleDateFormat("zzzz");
    /*
         * A String array for the time zone ids.
         */
    final String[] ids = TimeZone.getAvailableIDs();
    // when fixed the bug should comment it out
    int ids_length = ids.length;
    /*
         * How many ids do we have?
         */
    logln("Time Zone IDs size:" + ids_length);
    /*
         * Column headings (sort of)
         */
    logln("Ordinal ID offset(h:m) name");
    /*
         * Loop through the tzs.
         */
    Date today = new Date();
    Calendar cal = Calendar.getInstance();
    for (int i = 0; i < ids_length; i++) {
        logln(i + " " + ids[i]);
        TimeZone ttz = TimeZone.getTimeZone(ids[i]);
        // offset = ttz.getRawOffset();
        cal.setTimeZone(ttz);
        cal.setTime(today);
        offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
        // logln(i + " " + ids[i] + " offset " + offset);
        String sign = "+";
        if (offset < 0) {
            sign = "-";
            offset = -offset;
        }
        hours = offset / 3600000;
        minutes = (offset % 3600000) / 60000;
        seconds = (offset % 60000) / 1000;
        String dstOffset = sign + (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes;
        if (seconds != 0) {
            dstOffset += ":" + (seconds < 10 ? "0" : "") + seconds;
        }
        /*
             * Instantiate a date so we can display the time zone name.
             */
        sdf.setTimeZone(ttz);
        /*
             * Format the output.
             */
        StringBuffer fmtOffset = new StringBuffer("");
        FieldPosition pos = new FieldPosition(0);
        try {
            fmtOffset = sdf.format(today, fmtOffset, pos);
        } catch (Exception e) {
            logln("Exception:" + e);
            continue;
        }
        // UnicodeString fmtOffset = tzS.toString();
        String fmtDstOffset = null;
        if (fmtOffset.toString().startsWith("GMT")) {
            // fmtDstOffset = fmtOffset.substring(3);
            fmtDstOffset = fmtOffset.substring(3, fmtOffset.length());
        }
        /*
             * Show our result.
             */
        boolean ok = fmtDstOffset == null || fmtDstOffset.equals("") || fmtDstOffset.equals(dstOffset);
        if (ok) {
            logln(i + " " + ids[i] + " " + dstOffset + " " + fmtOffset + (fmtDstOffset != null ? " ok" : " ?"));
        } else {
            errln(i + " " + ids[i] + " " + dstOffset + " " + fmtOffset + " *** FAIL ***");
        }
    }
}
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) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 82 with TimeZone

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

the class IBMCalendarTest method TestTimeZoneTransitionAdd.

/**
 * Make sure that when adding a day, we actually wind up in a
 * different day.  The DST adjustments we use to keep the hour
 * constant across DST changes can backfire and change the day.
 */
@Test
public void TestTimeZoneTransitionAdd() {
    // could also be CHINA
    Locale locale = Locale.US;
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm z", locale);
    String[] tz = TimeZone.getAvailableIDs();
    for (int z = 0; z < tz.length; ++z) {
        TimeZone t = TimeZone.getTimeZone(tz[z]);
        dateFormat.setTimeZone(t);
        Calendar cal = Calendar.getInstance(t, locale);
        cal.clear();
        // Scan the year 2003, overlapping the edges of the year
        cal.set(Calendar.YEAR, 2002);
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DAY_OF_MONTH, 25);
        for (int i = 0; i < 365 + 10; ++i) {
            Date yesterday = cal.getTime();
            int yesterday_day = cal.get(Calendar.DAY_OF_MONTH);
            cal.add(Calendar.DAY_OF_MONTH, 1);
            if (yesterday_day == cal.get(Calendar.DAY_OF_MONTH)) {
                errln(tz[z] + " " + dateFormat.format(yesterday) + " +1d= " + dateFormat.format(cal.getTime()));
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) TimeZone(android.icu.util.TimeZone) ChineseCalendar(android.icu.util.ChineseCalendar) TaiwanCalendar(android.icu.util.TaiwanCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) BuddhistCalendar(android.icu.util.BuddhistCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 83 with TimeZone

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

the class IBMCalendarTest method TestFieldDifference.

@Test
public void TestFieldDifference() {
    class TFDItem {

        public String tzname;

        public String locale;

        public long start;

        public long target;

        // true to compute progressive difference for each field, false to reset calendar after each call
        public boolean progressive;

        int yDiff;

        int MDiff;

        int dDiff;

        int HDiff;

        int mDiff;

        // 0x7FFFFFFF indicates overflow error expected
        int sDiff;

        // Simple constructor
        public TFDItem(String tz, String loc, long st, long tg, boolean prg, int yD, int MD, int dD, int HD, int mD, int sD) {
            tzname = tz;
            locale = loc;
            start = st;
            target = tg;
            progressive = prg;
            yDiff = yD;
            MDiff = MD;
            dDiff = dD;
            HDiff = HD;
            mDiff = mD;
            sDiff = sD;
        }
    }
    ;
    final TFDItem[] tfdItems = { // 2010-Mar-01 08:10 -> 2010-Jun-28 17:50
    new TFDItem("US/Pacific", "en_US", 1267459800000L, 1277772600000L, true, 0, 3, 27, 9, 40, 0), // 2010-Mar-01 08:10 -> 2011-Mar-02 10:08
    new TFDItem("US/Pacific", "en_US", 1267459800000L, 1299089280000L, true, 1, 0, 1, 1, 58, 0), // 1970-Jan-01 00:00 -> 2004-Jan-10 00:00
    new TFDItem("GMT", "en_US", 0, 1073692800000L, false, 34, 408, 12427, 298248, 17894880, 1073692800), // 1970-Jan-01 00:00 -> 2004-Jan-11 00:00
    new TFDItem("GMT", "en_US", 0, 1073779200000L, false, 34, 408, 12428, 298272, 17896320, 1073779200), // 1970-Jan-01 00:00 -> 2038-Jan-19 00:00
    new TFDItem("GMT", "en_US", 0, 2147472000000L, false, 68, 816, 24855, 596520, 35791200, 2147472000), // 1970-Jan-01 00:00 -> 1935-Dec-24 00:00
    new TFDItem("GMT", "en_US", 0, -1073692800000L, false, -34, -408, -12427, -298248, -17894880, -1073692800), // 1970-Jan-01 00:00 -> 1935-Dec-23 00:00
    new TFDItem("GMT", "en_US", 0, -1073779200000L, false, -34, -408, -12428, -298272, -17896320, -1073779200), // CE   5-Dec-31 00:00 -> CE  10-Dec-30 00:00
    new TFDItem("GMT", "en_US", -61978089600000L, -61820409600000L, false, 4, 59, 1825, 43800, 2628000, 157680000), // CE  10-Dec-30 00:00 -> CE   5-Dec-31 00:00
    new TFDItem("GMT", "en_US", -61820409600000L, -61978089600000L, false, -4, -59, -1825, -43800, -2628000, -157680000), // BCE 10-Jan-04 00:00 -> BCE  5-Jan-03 00:00
    new TFDItem("GMT", "en_US", -62451129600000L, -62293449600000L, false, 4, 59, 1825, 43800, 2628000, 157680000), // BCE  5-Jan-03 00:00 -> BCE 10-Jan-04 00:00
    new TFDItem("GMT", "en_US", -62293449600000L, -62451129600000L, false, -4, -59, -1825, -43800, -2628000, -157680000), // BCE  5-Jan-03 00:00 -> CE   5-Dec-31 00:00
    new TFDItem("GMT", "en_US", -62293449600000L, -61978089600000L, false, 9, 119, 3650, 87600, 5256000, 315360000), // CE   5-Dec-31 00:00 -> BCE  5-Jan-03 00:00
    new TFDItem("GMT", "en_US", -61978089600000L, -62293449600000L, false, -9, -119, -3650, -87600, -5256000, -315360000), // MG   5-Dec-30 00:00 -> MG  10-Dec-29 00:00
    new TFDItem("GMT", "en@calendar=roc", -1672704000000L, -1515024000000L, false, 4, 59, 1825, 43800, 2628000, 157680000), // MG  10-Dec-29 00:00 -> MG   5-Dec-30 00:00
    new TFDItem("GMT", "en@calendar=roc", -1515024000000L, -1672704000000L, false, -4, -59, -1825, -43800, -2628000, -157680000), // BMG 10-Jan-03 00:00 -> BMG  5-Jan-02 00:00
    new TFDItem("GMT", "en@calendar=roc", -2145744000000L, -1988064000000L, false, 4, 59, 1825, 43800, 2628000, 157680000), // BMG  5-Jan-02 00:00 -> BMG 10-Jan-03 00:00
    new TFDItem("GMT", "en@calendar=roc", -1988064000000L, -2145744000000L, false, -4, -59, -1825, -43800, -2628000, -157680000), // BMG  5-Jan-02 00:00 -> MG   5-Dec-30 00:00
    new TFDItem("GMT", "en@calendar=roc", -1988064000000L, -1672704000000L, false, 9, 119, 3650, 87600, 5256000, 315360000), // MG   5-Dec-30 00:00 -> BMG  5-Jan-02 00:00
    new TFDItem("GMT", "en@calendar=roc", -1672704000000L, -1988064000000L, false, -9, -119, -3650, -87600, -5256000, -315360000), // Er1  5-Nas-05 00:00 -> Er1 10-Nas-04 00:00
    new TFDItem("GMT", "en@calendar=coptic", -53026531200000L, -52868851200000L, false, 4, 64, 1825, 43800, 2628000, 157680000), // Er1 10-Nas-04 00:00 -> Er1  5-Nas-05 00:00
    new TFDItem("GMT", "en@calendar=coptic", -52868851200000L, -53026531200000L, false, -4, -64, -1825, -43800, -2628000, -157680000), // Er0 10-Tou-04 00:00 -> Er0  5-Tou-02 00:00
    new TFDItem("GMT", "en@calendar=coptic", -53499571200000L, -53341891200000L, false, 4, 64, 1825, 43800, 2628000, 157680000), // Er0  5-Tou-02 00:00 -> Er0 10-Tou-04 00:00
    new TFDItem("GMT", "en@calendar=coptic", -53341891200000L, -53499571200000L, false, -4, -64, -1825, -43800, -2628000, -157680000), // Er0  5-Tou-02 00:00 -> Er1  5-Nas-05 00:00
    new TFDItem("GMT", "en@calendar=coptic", -53341891200000L, -53026531200000L, false, 9, 129, 3650, 87600, 5256000, 315360000), // Er1  5-Nas-05 00:00 -> Er0  5-Tou-02 00:00
    new TFDItem("GMT", "en@calendar=coptic", -53026531200000L, -53341891200000L, false, -9, -129, -3650, -87600, -5256000, -315360000) };
    for (TFDItem tfdItem : tfdItems) {
        TimeZone timezone = TimeZone.getFrozenTimeZone(tfdItem.tzname);
        Calendar ucal = Calendar.getInstance(timezone, new ULocale(tfdItem.locale));
        ucal.setTimeInMillis(tfdItem.target);
        Date targetDate = ucal.getTime();
        int yDf, MDf, dDf, HDf, mDf, sDf;
        if (tfdItem.progressive) {
            ucal.setTimeInMillis(tfdItem.start);
            yDf = ucal.fieldDifference(targetDate, YEAR);
            MDf = ucal.fieldDifference(targetDate, MONTH);
            dDf = ucal.fieldDifference(targetDate, DATE);
            HDf = ucal.fieldDifference(targetDate, HOUR);
            mDf = ucal.fieldDifference(targetDate, MINUTE);
            sDf = ucal.fieldDifference(targetDate, SECOND);
            if (yDf != tfdItem.yDiff || MDf != tfdItem.MDiff || dDf != tfdItem.dDiff || HDf != tfdItem.HDiff || mDf != tfdItem.mDiff || sDf != tfdItem.sDiff) {
                errln("Fail: for locale \"" + tfdItem.locale + "\", start " + tfdItem.start + ", target " + tfdItem.target + ", expected y-M-d-H-m-s progressive diffs " + tfdItem.yDiff + "," + tfdItem.MDiff + "," + tfdItem.dDiff + "," + tfdItem.HDiff + "," + tfdItem.mDiff + "," + tfdItem.sDiff + ", got " + yDf + "," + MDf + "," + dDf + "," + HDf + "," + mDf + "," + sDf);
            }
        } else {
            ucal.setTimeInMillis(tfdItem.start);
            yDf = ucal.fieldDifference(targetDate, YEAR);
            ucal.setTimeInMillis(tfdItem.start);
            MDf = ucal.fieldDifference(targetDate, MONTH);
            ucal.setTimeInMillis(tfdItem.start);
            dDf = ucal.fieldDifference(targetDate, DATE);
            ucal.setTimeInMillis(tfdItem.start);
            HDf = ucal.fieldDifference(targetDate, HOUR);
            ucal.setTimeInMillis(tfdItem.start);
            mDf = ucal.fieldDifference(targetDate, MINUTE);
            if (yDf != tfdItem.yDiff || MDf != tfdItem.MDiff || dDf != tfdItem.dDiff || HDf != tfdItem.HDiff || mDf != tfdItem.mDiff) {
                errln("Fail: for locale \"" + tfdItem.locale + "\", start " + tfdItem.start + ", target " + tfdItem.target + ", expected y-M-d-H-m total diffs " + tfdItem.yDiff + "," + tfdItem.MDiff + "," + tfdItem.dDiff + "," + tfdItem.HDiff + "," + tfdItem.mDiff + ", got " + yDf + "," + MDf + "," + dDf + "," + HDf + "," + mDf);
            }
            ucal.setTimeInMillis(tfdItem.start);
            sDf = ucal.fieldDifference(targetDate, SECOND);
            if (sDf != 0x7FFFFFFF && sDf != tfdItem.sDiff) {
                errln("Fail: for locale \"" + tfdItem.locale + "\", start " + tfdItem.start + ", target " + tfdItem.target + ", expected seconds total diffs " + tfdItem.sDiff + ", got " + sDf);
            }
        }
    }
}
Also used : TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) ChineseCalendar(android.icu.util.ChineseCalendar) TaiwanCalendar(android.icu.util.TaiwanCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) BuddhistCalendar(android.icu.util.BuddhistCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 84 with TimeZone

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

the class DateFormatRegressionTest method Test_GEec.

@Test
public void Test_GEec() {
    class PatternAndResult {

        private String pattern;

        private String result;

        PatternAndResult(String pat, String res) {
            pattern = pat;
            result = res;
        }

        public String getPattern() {
            return pattern;
        }

        public String getResult() {
            return result;
        }
    }
    final PatternAndResult[] tests = { new PatternAndResult("dd MMM yyyy GGG", "02 Jul 2008 AD"), new PatternAndResult("dd MMM yyyy GGGGG", "02 Jul 2008 A"), new PatternAndResult("e dd MMM yyyy", "4 02 Jul 2008"), new PatternAndResult("ee dd MMM yyyy", "04 02 Jul 2008"), new PatternAndResult("c dd MMM yyyy", "4 02 Jul 2008"), new PatternAndResult("cc dd MMM yyyy", "4 02 Jul 2008"), new PatternAndResult("eee dd MMM yyyy", "Wed 02 Jul 2008"), new PatternAndResult("EEE dd MMM yyyy", "Wed 02 Jul 2008"), new PatternAndResult("EE dd MMM yyyy", "Wed 02 Jul 2008"), new PatternAndResult("eeee dd MMM yyyy", "Wednesday 02 Jul 2008"), new PatternAndResult("eeeee dd MMM yyyy", "W 02 Jul 2008"), new PatternAndResult("e ww YYYY", "4 27 2008"), new PatternAndResult("c ww YYYY", "4 27 2008") };
    ULocale loc = ULocale.ENGLISH;
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar cal = new GregorianCalendar(tz, loc);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd", loc);
    for (int i = 0; i < tests.length; i++) {
        PatternAndResult item = tests[i];
        dateFormat.applyPattern(item.getPattern());
        // 2008 July 02 5 AM PDT
        cal.set(2008, 6, 2, 5, 0);
        StringBuffer buf = new StringBuffer(32);
        FieldPosition fp = new FieldPosition(DateFormat.YEAR_FIELD);
        dateFormat.format(cal, buf, fp);
        if (buf.toString().compareTo(item.getResult()) != 0) {
            errln("for pattern " + item.getPattern() + ", expected " + item.getResult() + ", got " + buf);
        }
        ParsePosition pos = new ParsePosition(0);
        dateFormat.parse(item.getResult(), cal, pos);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DATE);
        if (year != 2008 || month != 6 || day != 2) {
            errln("use pattern " + item.getPattern() + " to parse " + item.getResult() + ", expected y2008 m6 d2, got " + year + " " + month + " " + day);
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 85 with TimeZone

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

the class DateFormatRegressionTest method Test714.

@Test
public void Test714() {
    // TimeZone Offset
    TimeZone defaultTZ = TimeZone.getDefault();
    TimeZone PST = TimeZone.getTimeZone("PST");
    int defaultOffset = defaultTZ.getRawOffset();
    int PSTOffset = PST.getRawOffset();
    Date d = new Date(978103543000l - (defaultOffset - PSTOffset));
    d = new Date(d.getTime() - (defaultTZ.inDaylightTime(d) ? 3600000 : 0));
    DateFormat fmt = DateFormat.getDateTimeInstance(-1, DateFormat.MEDIUM, Locale.US);
    String tests = "7:25:43 AM";
    String s = fmt.format(d);
    if (!s.equals(tests)) {
        errln("Fail: " + s + " != " + tests);
    } else {
        logln("OK: " + s + " == " + tests);
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) 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