Search in sources :

Example 16 with SimpleTimeZone

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

the class TimeZoneRegressionTest method TestT5432.

/*
     * Zone ID is not set by a SimpleTimeZone constructor
     */
@Test
public void TestT5432() {
    String tzid = "MyZone";
    SimpleTimeZone stz;
    // 2-arg constructor
    stz = new SimpleTimeZone(0, tzid);
    if (!tzid.equals(stz.getID())) {
        errln("FAIL: Bad zone id (" + stz.getID() + ") is returned - expected (" + tzid + ") [2-arg constructor]");
    }
    // 10-arg constructor
    stz = new SimpleTimeZone(0, tzid, 3, -1, 1, 3600000, 9, -1, 1, 3600000);
    if (!tzid.equals(stz.getID())) {
        errln("FAIL: Bad zone id (" + stz.getID() + ") is returned - expected (" + tzid + ") [10-arg constructor]");
    }
    // 11-arg constructor
    stz = new SimpleTimeZone(0, tzid, 3, -1, 1, 3600000, 9, -1, 1, 3600000, 3600000);
    if (!tzid.equals(stz.getID())) {
        errln("FAIL: Bad zone id (" + stz.getID() + ") is returned - expected (" + tzid + ") [11-arg constructor]");
    }
    // 13-arg constructor - this version had a problem reported by trac#5432
    stz = new SimpleTimeZone(0, tzid, 3, -1, 1, 3600000, SimpleTimeZone.WALL_TIME, 9, -1, 1, 3600000, SimpleTimeZone.WALL_TIME, 3600000);
    if (!tzid.equals(stz.getID())) {
        errln("FAIL: Bad zone id (" + stz.getID() + ") is returned - expected (" + tzid + ") [13-arg constructor]");
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) Test(org.junit.Test)

Example 17 with SimpleTimeZone

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

the class TimeZoneBoundaryTest method TestNewRules.

/*
      static void _testUsingMillis(Date d, boolean startsInDST)
      {
      long millis = d.getTime();
      long max = millis + (long)(370 * ONE_DAY); // A year plus extra

      boolean lastDST = startsInDST;
      while (millis < max)
      {
      cal.setTime(new Date(millis));
      boolean inDaylight = cal.inDaylightTime();

      if (inDaylight != lastDST)
      {
      logln("Switch " + (inDaylight ? "into" : "out of")
      + " DST at " + (new Date(millis)));
      lastDST = inDaylight;
      }

      millis += 15*ONE_MINUTE;
      }
      }
      */
/**
 * Test new rule formats.
 */
@Test
public void TestNewRules() {
    if (true) {
        // Doesn't matter what the default TimeZone is here, since we
        // are creating our own TimeZone objects.
        SimpleTimeZone tz;
        java.util.Calendar tempcal = java.util.Calendar.getInstance();
        tempcal.clear();
        logln("-----------------------------------------------------------------");
        logln("Aug 2ndTues .. Mar 15");
        tz = new SimpleTimeZone(-8 * ONE_HOUR, "Test_1", Calendar.AUGUST, 2, Calendar.TUESDAY, 2 * ONE_HOUR, Calendar.MARCH, 15, 0, 2 * ONE_HOUR);
        // logln(tz.toString());
        logln("========================================");
        tempcal.set(1997, 0, 1);
        _testUsingBinarySearch(tz, tempcal.getTime(), 858416400000L);
        logln("========================================");
        tempcal.set(1997, 6, 1);
        _testUsingBinarySearch(tz, tempcal.getTime(), 871380000000L);
        logln("-----------------------------------------------------------------");
        logln("Apr Wed>=14 .. Sep Sun<=20");
        tz = new SimpleTimeZone(-8 * ONE_HOUR, "Test_2", Calendar.APRIL, 14, -Calendar.WEDNESDAY, 2 * ONE_HOUR, Calendar.SEPTEMBER, -20, -Calendar.SUNDAY, 2 * ONE_HOUR);
        // logln(tz.toString());
        logln("========================================");
        tempcal.set(1997, 0, 1);
        _testUsingBinarySearch(tz, tempcal.getTime(), 861184800000L);
        logln("========================================");
        tempcal.set(1997, 6, 1);
        _testUsingBinarySearch(tz, tempcal.getTime(), 874227600000L);
    }
/*
          if (true)
          {
          logln("========================================");
          logln("Stepping using millis");
          _testUsingMillis(new Date(97,0,1), false);
          }

          if (true)
          {
          logln("========================================");
          logln("Stepping using fields");
          _testUsingFields(1997, false);
          }

          if (false)
          {
          cal.clear();
          cal.set(1997, 3, 5, 10, 0);
          //    cal.inDaylightTime();
          logln("Date = " + cal.getTime());
          logln("Millis = " + cal.getTime().getTime()/3600000);
          }
          */
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) Test(org.junit.Test)

Example 18 with SimpleTimeZone

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

the class TimeZoneRegressionTest method Test4154542.

/**
 * SimpleTimeZone constructors, setStartRule(), and setEndRule() don't
 * check for out-of-range arguments.
 */
@Test
public void Test4154542() {
    final int GOOD = 1;
    final int BAD = 0;
    final int GOOD_MONTH = Calendar.JANUARY;
    final int GOOD_DAY = 1;
    final int GOOD_DAY_OF_WEEK = Calendar.SUNDAY;
    final int GOOD_TIME = 0;
    int[] DATA = { GOOD, Integer.MIN_VALUE, 0, Integer.MAX_VALUE, Integer.MIN_VALUE, GOOD, Calendar.JANUARY, -5, Calendar.SUNDAY, 0, GOOD, Calendar.DECEMBER, 5, Calendar.SATURDAY, 24 * 60 * 60 * 1000 - 1, BAD, Calendar.DECEMBER, 5, Calendar.SATURDAY, 24 * 60 * 60 * 1000 + 1, BAD, Calendar.DECEMBER, 5, Calendar.SATURDAY, -1, BAD, Calendar.JANUARY, -6, Calendar.SUNDAY, 0, BAD, Calendar.DECEMBER, 6, Calendar.SATURDAY, 24 * 60 * 60 * 1000, GOOD, Calendar.DECEMBER, 1, 0, 0, GOOD, Calendar.DECEMBER, 31, 0, 0, BAD, Calendar.APRIL, 31, 0, 0, BAD, Calendar.DECEMBER, 32, 0, 0, BAD, Calendar.JANUARY - 1, 1, Calendar.SUNDAY, 0, BAD, Calendar.DECEMBER + 1, 1, Calendar.SUNDAY, 0, GOOD, Calendar.DECEMBER, 31, -Calendar.SUNDAY, 0, GOOD, Calendar.DECEMBER, 31, -Calendar.SATURDAY, 0, BAD, Calendar.DECEMBER, 32, -Calendar.SATURDAY, 0, BAD, Calendar.DECEMBER, -32, -Calendar.SATURDAY, 0, BAD, Calendar.DECEMBER, 31, -Calendar.SATURDAY - 1, 0 };
    SimpleTimeZone zone = new SimpleTimeZone(0, "Z");
    for (int i = 0; i < DATA.length; i += 5) {
        boolean shouldBeGood = (DATA[i] == GOOD);
        int month = DATA[i + 1];
        int day = DATA[i + 2];
        int dayOfWeek = DATA[i + 3];
        int time = DATA[i + 4];
        Exception ex = null;
        try {
            zone.setStartRule(month, day, dayOfWeek, time);
        } catch (IllegalArgumentException e) {
            ex = e;
        }
        if ((ex == null) != shouldBeGood) {
            errln("setStartRule(month=" + month + ", day=" + day + ", dayOfWeek=" + dayOfWeek + ", time=" + time + (shouldBeGood ? (") should work but throws " + ex) : ") should fail but doesn't"));
        }
        ex = null;
        try {
            zone.setEndRule(month, day, dayOfWeek, time);
        } catch (IllegalArgumentException e) {
            ex = e;
        }
        if ((ex == null) != shouldBeGood) {
            errln("setEndRule(month=" + month + ", day=" + day + ", dayOfWeek=" + dayOfWeek + ", time=" + time + (shouldBeGood ? (") should work but throws " + ex) : ") should fail but doesn't"));
        }
        ex = null;
        try {
            /*SimpleTimeZone temp =*/
            new SimpleTimeZone(0, "Z", month, day, dayOfWeek, time, GOOD_MONTH, GOOD_DAY, GOOD_DAY_OF_WEEK, GOOD_TIME);
        } catch (IllegalArgumentException e) {
            ex = e;
        }
        if ((ex == null) != shouldBeGood) {
            errln("SimpleTimeZone(month=" + month + ", day=" + day + ", dayOfWeek=" + dayOfWeek + ", time=" + time + (shouldBeGood ? (", <end>) should work but throws " + ex) : ", <end>) should fail but doesn't"));
        }
        ex = null;
        try {
            /*SimpleTimeZone temp = */
            new SimpleTimeZone(0, "Z", GOOD_MONTH, GOOD_DAY, GOOD_DAY_OF_WEEK, GOOD_TIME, month, day, dayOfWeek, time);
        // temp = null;
        } catch (IllegalArgumentException e) {
            ex = e;
        }
        if ((ex == null) != shouldBeGood) {
            errln("SimpleTimeZone(<start>, month=" + month + ", day=" + day + ", dayOfWeek=" + dayOfWeek + ", time=" + time + (shouldBeGood ? (") should work but throws " + ex) : ") should fail but doesn't"));
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException) Test(org.junit.Test)

Example 19 with SimpleTimeZone

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

the class DateTimeGeneratorTest method TestReplacingZoneString.

@Test
public void TestReplacingZoneString() {
    Date testDate = new Date();
    TimeZone testTimeZone = TimeZone.getTimeZone("America/New_York");
    TimeZone bogusTimeZone = new SimpleTimeZone(1234, "Etc/Unknown");
    Calendar calendar = Calendar.getInstance();
    ParsePosition parsePosition = new ParsePosition(0);
    ULocale[] locales = ULocale.getAvailableLocales();
    int count = 0;
    for (int i = 0; i < locales.length; ++i) {
        // skip the country locales unless we are doing exhaustive tests
        if (getExhaustiveness() < 6) {
            if (locales[i].getCountry().length() > 0) {
                continue;
            }
        }
        count++;
        // ticket#6503
        if (getExhaustiveness() <= 5 && count % 3 != 0) {
            continue;
        }
        logln(locales[i].toString());
        DateTimePatternGenerator dtpgen = DateTimePatternGenerator.getInstance(locales[i]);
        for (int style1 = DateFormat.FULL; style1 <= DateFormat.SHORT; ++style1) {
            final SimpleDateFormat oldFormat = (SimpleDateFormat) DateFormat.getTimeInstance(style1, locales[i]);
            String pattern = oldFormat.toPattern();
            // replaceZoneString(pattern, "VVVV");
            String newPattern = dtpgen.replaceFieldTypes(pattern, "VVVV");
            if (newPattern.equals(pattern)) {
                continue;
            }
            // verify that it roundtrips parsing
            SimpleDateFormat newFormat = new SimpleDateFormat(newPattern, locales[i]);
            newFormat.setTimeZone(testTimeZone);
            String formatted = newFormat.format(testDate);
            calendar.setTimeZone(bogusTimeZone);
            parsePosition.setIndex(0);
            newFormat.parse(formatted, calendar, parsePosition);
            if (parsePosition.getErrorIndex() >= 0) {
                errln("Failed parse with VVVV:\t" + locales[i] + ",\t\"" + pattern + "\",\t\"" + newPattern + "\",\t\"" + formatted.substring(0, parsePosition.getErrorIndex()) + "{}" + formatted.substring(parsePosition.getErrorIndex()) + "\"");
            } else if (!calendar.getTimeZone().getID().equals(testTimeZone.getID())) {
                errln("Failed timezone roundtrip with VVVV:\t" + locales[i] + ",\t\"" + pattern + "\",\t\"" + newPattern + "\",\t\"" + formatted + "\",\t" + calendar.getTimeZone().getID() + " != " + testTimeZone.getID());
            } else {
                logln(locales[i] + ":\t\"" + pattern + "\" => \t\"" + newPattern + "\"\t" + formatted);
            }
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 20 with SimpleTimeZone

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

the class TimeZoneFormatTest method TestTimeZoneRoundTrip.

/*
     * Test case for checking if a TimeZone is properly set in the result calendar
     * and if the result TimeZone has the expected behavior.
     */
@Test
public void TestTimeZoneRoundTrip() {
    boolean TEST_ALL = getBooleanProperty("TimeZoneRoundTripAll", false);
    TimeZone unknownZone = new SimpleTimeZone(-31415, "Etc/Unknown");
    int badDstOffset = -1234;
    int badZoneOffset = -2345;
    int[][] testDateData = { { 2007, 1, 15 }, { 2007, 6, 15 }, { 1990, 1, 15 }, { 1990, 6, 15 }, { 1960, 1, 15 }, { 1960, 6, 15 } };
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    cal.clear();
    // Set up rule equivalency test range
    long low, high;
    cal.set(1900, 0, 1);
    low = cal.getTimeInMillis();
    cal.set(2040, 0, 1);
    high = cal.getTimeInMillis();
    // Set up test dates
    Date[] DATES = new Date[testDateData.length];
    cal.clear();
    for (int i = 0; i < DATES.length; i++) {
        cal.set(testDateData[i][0], testDateData[i][1], testDateData[i][2]);
        DATES[i] = cal.getTime();
    }
    // Set up test locales
    ULocale[] LOCALES = null;
    if (TEST_ALL || TestFmwk.getExhaustiveness() > 5) {
        LOCALES = ULocale.getAvailableLocales();
    } else {
        LOCALES = new ULocale[] { new ULocale("en"), new ULocale("en_CA"), new ULocale("fr"), new ULocale("zh_Hant") };
    }
    String[] tzids;
    if (JDKTZ) {
        tzids = java.util.TimeZone.getAvailableIDs();
    } else {
        tzids = TimeZone.getAvailableIDs();
    }
    int[] inOffsets = new int[2];
    int[] outOffsets = new int[2];
    // Run the roundtrip test
    for (int locidx = 0; locidx < LOCALES.length; locidx++) {
        logln("Locale: " + LOCALES[locidx].toString());
        String localGMTString = TimeZoneFormat.getInstance(LOCALES[locidx]).formatOffsetLocalizedGMT(0);
        for (int patidx = 0; patidx < PATTERNS.length; patidx++) {
            logln("    pattern: " + PATTERNS[patidx]);
            SimpleDateFormat sdf = new SimpleDateFormat(PATTERNS[patidx], LOCALES[locidx]);
            for (int tzidx = 0; tzidx < tzids.length; tzidx++) {
                if (EXCL_TZ_PATTERN.matcher(tzids[tzidx]).matches()) {
                    continue;
                }
                TimeZone tz = TimeZone.getTimeZone(tzids[tzidx]);
                for (int datidx = 0; datidx < DATES.length; datidx++) {
                    // Format
                    sdf.setTimeZone(tz);
                    String tzstr = sdf.format(DATES[datidx]);
                    // Before parse, set unknown zone to SimpleDateFormat instance
                    // just for making sure that it does not depends on the time zone
                    // originally set.
                    sdf.setTimeZone(unknownZone);
                    // Parse
                    ParsePosition pos = new ParsePosition(0);
                    Calendar outcal = Calendar.getInstance(unknownZone);
                    outcal.set(Calendar.DST_OFFSET, badDstOffset);
                    outcal.set(Calendar.ZONE_OFFSET, badZoneOffset);
                    sdf.parse(tzstr, outcal, pos);
                    // Check the result
                    TimeZone outtz = outcal.getTimeZone();
                    tz.getOffset(DATES[datidx].getTime(), false, inOffsets);
                    outtz.getOffset(DATES[datidx].getTime(), false, outOffsets);
                    if (PATTERNS[patidx].equals("V")) {
                        // Short zone ID - should support roundtrip for canonical CLDR IDs
                        String canonicalID = TimeZone.getCanonicalID(tzids[tzidx]);
                        if (!outtz.getID().equals(canonicalID)) {
                            if (outtz.getID().equals("Etc/Unknown")) {
                                // short zone ID and "unk" is used as the fallback
                                if (REALLY_VERBOSE_LOG) {
                                    logln("Canonical round trip failed (probably as expected); tz=" + tzids[tzidx] + ", locale=" + LOCALES[locidx] + ", pattern=" + PATTERNS[patidx] + ", time=" + DATES[datidx].getTime() + ", str=" + tzstr + ", outtz=" + outtz.getID());
                                }
                            } else {
                                errln("Canonical round trip failed; tz=" + tzids[tzidx] + ", locale=" + LOCALES[locidx] + ", pattern=" + PATTERNS[patidx] + ", time=" + DATES[datidx].getTime() + ", str=" + tzstr + ", outtz=" + outtz.getID());
                            }
                        }
                    } else if (PATTERNS[patidx].equals("VV")) {
                        // Zone ID - full roundtrip support
                        if (!outtz.getID().equals(tzids[tzidx])) {
                            errln("Zone ID round trip failed; tz=" + tzids[tzidx] + ", locale=" + LOCALES[locidx] + ", pattern=" + PATTERNS[patidx] + ", time=" + DATES[datidx].getTime() + ", str=" + tzstr + ", outtz=" + outtz.getID());
                        }
                    } else if (PATTERNS[patidx].equals("VVV") || PATTERNS[patidx].equals("VVVV")) {
                        // Location: time zone rule must be preserved except
                        // zones not actually associated with a specific location.
                        String canonicalID = TimeZone.getCanonicalID(tzids[tzidx]);
                        if (canonicalID != null && !outtz.getID().equals(canonicalID)) {
                            // Canonical ID did not match - check the rules
                            boolean bFailure = false;
                            if ((tz instanceof BasicTimeZone) && (outtz instanceof BasicTimeZone)) {
                                boolean hasNoLocation = TimeZone.getRegion(tzids[tzidx]).equals("001");
                                bFailure = !hasNoLocation && !((BasicTimeZone) outtz).hasEquivalentTransitions(tz, low, high);
                            }
                            if (bFailure) {
                                errln("Canonical round trip failed; tz=" + tzids[tzidx] + ", locale=" + LOCALES[locidx] + ", pattern=" + PATTERNS[patidx] + ", time=" + DATES[datidx].getTime() + ", str=" + tzstr + ", outtz=" + outtz.getID());
                            } else if (REALLY_VERBOSE_LOG) {
                                logln("Canonical round trip failed (as expected); tz=" + tzids[tzidx] + ", locale=" + LOCALES[locidx] + ", pattern=" + PATTERNS[patidx] + ", time=" + DATES[datidx].getTime() + ", str=" + tzstr + ", outtz=" + outtz.getID());
                            }
                        }
                    } else {
                        boolean isOffsetFormat = (PATTERNS[patidx].charAt(0) == 'Z' || PATTERNS[patidx].charAt(0) == 'O' || PATTERNS[patidx].charAt(0) == 'X' || PATTERNS[patidx].charAt(0) == 'x');
                        boolean minutesOffset = false;
                        if (PATTERNS[patidx].charAt(0) == 'X' || PATTERNS[patidx].charAt(0) == 'x') {
                            minutesOffset = PATTERNS[patidx].length() <= 3;
                        }
                        if (!isOffsetFormat) {
                            // Check if localized GMT format is used as a fallback of name styles
                            int numDigits = 0;
                            for (int n = 0; n < tzstr.length(); n++) {
                                if (UCharacter.isDigit(tzstr.charAt(n))) {
                                    numDigits++;
                                }
                            }
                            isOffsetFormat = (numDigits > 0);
                        }
                        if (isOffsetFormat || tzstr.equals(localGMTString)) {
                            // Localized GMT or ISO: total offset (raw + dst) must be preserved.
                            int inOffset = inOffsets[0] + inOffsets[1];
                            int outOffset = outOffsets[0] + outOffsets[1];
                            int diff = outOffset - inOffset;
                            if (minutesOffset) {
                                diff = (diff / 60000) * 60000;
                            }
                            if (diff != 0) {
                                errln("Offset round trip failed; tz=" + tzids[tzidx] + ", locale=" + LOCALES[locidx] + ", pattern=" + PATTERNS[patidx] + ", time=" + DATES[datidx].getTime() + ", str=" + tzstr + ", inOffset=" + inOffset + ", outOffset=" + outOffset);
                            }
                        } else {
                            // Specific or generic: raw offset must be preserved.
                            if (inOffsets[0] != outOffsets[0]) {
                                if (JDKTZ && tzids[tzidx].startsWith("SystemV/")) {
                                    // ICU handles these zones as aliases of existing time zones
                                    if (REALLY_VERBOSE_LOG) {
                                        logln("Raw offset round trip failed; tz=" + tzids[tzidx] + ", locale=" + LOCALES[locidx] + ", pattern=" + PATTERNS[patidx] + ", time=" + DATES[datidx].getTime() + ", str=" + tzstr + ", inRawOffset=" + inOffsets[0] + ", outRawOffset=" + outOffsets[0]);
                                    }
                                } else {
                                    errln("Raw offset round trip failed; tz=" + tzids[tzidx] + ", locale=" + LOCALES[locidx] + ", pattern=" + PATTERNS[patidx] + ", time=" + DATES[datidx].getTime() + ", str=" + tzstr + ", inRawOffset=" + inOffsets[0] + ", outRawOffset=" + outOffsets[0]);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Calendar(android.icu.util.Calendar) Date(java.util.Date) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Aggregations

SimpleTimeZone (android.icu.util.SimpleTimeZone)39 Test (org.junit.Test)35 TimeZone (android.icu.util.TimeZone)17 Date (java.util.Date)15 GregorianCalendar (android.icu.util.GregorianCalendar)14 BasicTimeZone (android.icu.util.BasicTimeZone)10 Calendar (android.icu.util.Calendar)9 RuleBasedTimeZone (android.icu.util.RuleBasedTimeZone)9 JavaTimeZone (android.icu.impl.JavaTimeZone)7 VTimeZone (android.icu.util.VTimeZone)7 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)7 SimpleDateFormat (android.icu.text.SimpleDateFormat)6 InitialTimeZoneRule (android.icu.util.InitialTimeZoneRule)5 CalendarAstronomer (android.icu.impl.CalendarAstronomer)4 AnnualTimeZoneRule (android.icu.util.AnnualTimeZoneRule)4 TimeArrayTimeZoneRule (android.icu.util.TimeArrayTimeZoneRule)4 DateFormat (android.icu.text.DateFormat)3 DateTimeRule (android.icu.util.DateTimeRule)3 TimeZoneTransition (android.icu.util.TimeZoneTransition)3 ULocale (android.icu.util.ULocale)3