Java Code Examples for android.icu.util.TimeZone#inDaylightTime()

The following examples show how to use android.icu.util.TimeZone#inDaylightTime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestEpochStartFields() {
    TimeZone z = TimeZone.getDefault();
    Calendar c = Calendar.getInstance();
    Date d = new Date(-z.getRawOffset());
    if (z.inDaylightTime(d)) {
        logln("Warning: Skipping test because " + d +
              " is in DST.");
    }
    else {
        c.setTime(d);
        c.setMinimalDaysInFirstWeek(1);
        for (int i=0; i<Calendar.ZONE_OFFSET; ++i) {
            if (c.get(i) != EPOCH_FIELDS[i])
                errln("Expected field " + i + " to have value " + EPOCH_FIELDS[i] +
                      "; saw " + c.get(i) + " instead");
        }
        if (c.get(Calendar.ZONE_OFFSET) != z.getRawOffset())
            errln("Expected field ZONE_OFFSET to have value " + z.getRawOffset() +
                  "; saw " + c.get(Calendar.ZONE_OFFSET) + " instead");
        if (c.get(Calendar.DST_OFFSET) != 0)
            errln("Expected field DST_OFFSET to have value 0" +
                  "; saw " + c.get(Calendar.DST_OFFSET) + " instead");
    }
}
 
Example 2
Source File: TimeZoneFormat.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Private method returning the time zone's specific format string.
 *
 * @param tz the time zone
 * @param stdType the name type used for standard time
 * @param dstType the name type used for daylight time
 * @param date the date
 * @param timeType when null, actual time type is set
 * @return the time zone's specific format name string
 */
private String formatSpecific(TimeZone tz, NameType stdType, NameType dstType, long date, Output<TimeType> timeType) {
    assert(stdType == NameType.LONG_STANDARD || stdType == NameType.SHORT_STANDARD);
    assert(dstType == NameType.LONG_DAYLIGHT || dstType == NameType.SHORT_DAYLIGHT);

    boolean isDaylight = tz.inDaylightTime(new Date(date));
    String name = isDaylight?
            getTimeZoneNames().getDisplayName(ZoneMeta.getCanonicalCLDRID(tz), dstType, date) :
            getTimeZoneNames().getDisplayName(ZoneMeta.getCanonicalCLDRID(tz), stdType, date);

    if (name != null && timeType != null) {
        timeType.value = isDaylight ? TimeType.DAYLIGHT : TimeType.STANDARD;
    }
    return name;
}
 
Example 3
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestDisplayName2() {
    Date now = new Date();

    String[] timezones = {"America/Chicago", "Europe/Moscow", "Europe/Rome", "Asia/Shanghai", "WET" };
    String[] locales = {"en", "fr", "de", "ja", "zh_TW", "zh_Hans" };
    for (int j = 0; j < locales.length; ++j) {
        ULocale locale = new ULocale(locales[j]);
        for (int i = 0; i < timezones.length; ++i) {
            TimeZone tz = TimeZone.getTimeZone(timezones[i]);
            String displayName0 = tz.getDisplayName(locale);
            SimpleDateFormat dt = new SimpleDateFormat("vvvv", locale);
            dt.setTimeZone(tz);
            String displayName1 = dt.format(now);  // date value _does_ matter if we fallback to GMT
            logln(locale.getDisplayName() + ", " + tz.getID() + ": " + displayName0);
            if (!displayName1.equals(displayName0)) {
                // This could happen when the date used is in DST,
                // because TimeZone.getDisplayName(ULocale) may use
                // localized GMT format for the time zone's standard
                // time.
                if (tz.inDaylightTime(now)) {
                    // Try getDisplayName with daylight argument
                    displayName0 = tz.getDisplayName(true, TimeZone.LONG_GENERIC, locale);
                }
                if (!displayName1.equals(displayName0)) {
                    errln(locale.getDisplayName() + ", " + tz.getID() + 
                            ": expected " + displayName1 + " but got: " + displayName0);
                }
            }
        }
    }
}
 
Example 4
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestVariousAPI518()
{
    TimeZone time_zone = TimeZone.getTimeZone("PST");
    Calendar cal = Calendar.getInstance();
    cal.set(1997, Calendar.APRIL, 30);
    Date d = cal.getTime();

    logln("The timezone is " + time_zone.getID());

    if (time_zone.inDaylightTime(d) != true)
        errln("FAIL: inDaylightTime returned false");

    if (time_zone.useDaylightTime() != true)
        errln("FAIL: useDaylightTime returned false");

    if (time_zone.getRawOffset() != -8*millisPerHour)
        errln( "FAIL: getRawOffset returned wrong value");

    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(d);
    if (time_zone.getOffset(GregorianCalendar.AD, gc.get(GregorianCalendar.YEAR), gc.get(GregorianCalendar.MONTH),
                            gc.get(GregorianCalendar.DAY_OF_MONTH),
                            gc.get(GregorianCalendar.DAY_OF_WEEK), 0)
        != -7*millisPerHour)
        errln("FAIL: getOffset returned wrong value");
}
 
Example 5
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static boolean isDaylightTimeAvailable(TimeZone tz, long start) {
    if (tz.inDaylightTime(new Date(start))) {
        return true;
    }

    long date;
    if (tz instanceof BasicTimeZone) {
        BasicTimeZone btz = (BasicTimeZone)tz;
        // check future transitions, up to 100
        date = start;
        for (int i = 0; i < 100; i++) {
            TimeZoneTransition tzt = btz.getNextTransition(date, false);
            if (tzt == null) {
                // no more transitions
                break;
            }
            if (tzt.getTo().getDSTSavings() != 0) {
                return true;
            }
            date = tzt.getTime();
        }
    } else {
        // check future times by incrementing 30 days, up to 200 times (about 16 years)
        final long inc = 30L * 24 * 60 * 60 * 1000;
        int[] offsets = new int[2];
        date = start + inc;
        for (int i = 0; i < 200; i++, date += inc) {
            tz.getOffset(date, false, offsets);
            if (offsets[1] != 0) {
                return true;
            }
        }
    }
    return false;
}
 
Example 6
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * CANNOT REPRODUDE
 *
 * Yet another _alleged_ bug in TimeZone.getOffset(), a method that never
 * should have been made public.  It's simply too hard to use correctly.
 *
 * The original test code failed to do the following:
 * (1) Call Calendar.setTime() before getting the fields!
 * (2) Use the right millis (as usual) for getOffset(); they were passing
 *     in the MILLIS field, instead of the STANDARD MILLIS IN DAY.
 * When you fix these two problems, the test passes, as expected.
 */
@Test
public void Test4126678() {
// Note: this test depends on the PST time zone.
TimeZone initialZone = TimeZone.getDefault();
    Calendar cal = Calendar.getInstance();
    TimeZone tz = TimeZone.getTimeZone("PST");
TimeZone.setDefault(tz);
    cal.setTimeZone(tz);

    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(1998, Calendar.APRIL, 5, 10, 0);
    Date dt = tempcal.getTime();
// the dt value is local time in PST.
    if (!tz.inDaylightTime(dt))
        errln("We're not in Daylight Savings Time and we should be.\n");

    cal.setTime(dt);
    int era = cal.get(Calendar.ERA);
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DATE);
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    int millis = cal.get(Calendar.MILLISECOND) +
        (cal.get(Calendar.SECOND) +
         (cal.get(Calendar.MINUTE) +
          (cal.get(Calendar.HOUR) * 60) * 60) * 1000) -
        cal.get(Calendar.DST_OFFSET);

    long offset = tz.getOffset(era, year, month, day, dayOfWeek, millis);
    long raw_offset = tz.getRawOffset();
    if (offset == raw_offset)
        errln("Offsets should not match when in DST");

// restore the initial time zone so that this test case
// doesn't affect the others.
TimeZone.setDefault(initialZone);
}
 
Example 7
Source File: TimeZoneFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void TestInheritedFormat() {
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar cal = Calendar.getInstance(tz);
    cal.setTimeInMillis(1459187377690L); // Mar 28, 2016

    StringBuffer sb = new StringBuffer();
    FieldPosition fp = new FieldPosition(DateFormat.Field.TIME_ZONE);

    TimeZoneFormat fmt = TimeZoneFormat.getInstance(ULocale.ENGLISH);

    // Test formatting a non-timezone related object
    try {
        fmt.format(new Object(), sb, fp);
        errln("ERROR: format non-timezone related object failed");
    } catch (IllegalArgumentException e) { /* Expected */ }

    // Test formatting a TimeZone object
    sb = new StringBuffer();
    fmt.format(tz, sb, fp);
    // When formatting a TimeZone object the formatter uses the current date.
    String fmtOutput = tz.inDaylightTime(new Date()) ? "GMT-07:00" : "GMT-08:00";
    if (!sb.toString().equals(fmtOutput)) {
        errln("ERROR: format TimerZone object failed. Expected: " + fmtOutput + ", actual: " + sb);
    }

    // Test formatting a Calendar object
    sb = new StringBuffer();
    fmt.format(cal, sb, fp);
    if (!sb.toString().equals("GMT-07:00")) {
        errln("ERROR: format Calendar object failed. Expected: GMT-07:00, actual: " + sb);
    }
}
 
Example 8
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
boolean checkCalendar314(GregorianCalendar testCal, TimeZone testTZ) {
    // GregorianCalendar testCal = (GregorianCalendar)aCal.clone();

    final int ONE_DAY = 24*60*60*1000;

    int tzOffset, tzRawOffset;
    Float tzOffsetFloat,tzRawOffsetFloat;
    // Here is where the user made an error.  They were passing in the value of
    // the MILLSECOND field; you need to pass in the millis in the day in STANDARD
    // time.
    int millis = testCal.get(Calendar.MILLISECOND) +
        1000 * (testCal.get(Calendar.SECOND) +
                60 * (testCal.get(Calendar.MINUTE) +
                      60 * (testCal.get(Calendar.HOUR_OF_DAY)))) -
        testCal.get(Calendar.DST_OFFSET);

    /* Fix up millis to be in range.  ASSUME THAT WE ARE NOT AT THE
     * BEGINNING OR END OF A MONTH.  We must add this code because
     * getOffset() has been changed to be more strict about the parameters
     * it receives -- it turns out that this test was passing in illegal
     * values. */
    int date = testCal.get(Calendar.DATE);
    int dow  = testCal.get(Calendar.DAY_OF_WEEK);
    while (millis < 0) {
        millis += ONE_DAY;
        --date;
        dow = Calendar.SUNDAY + ((dow - Calendar.SUNDAY + 6) % 7);
    }
    while (millis >= ONE_DAY) {
        millis -= ONE_DAY;
        ++date;
        dow = Calendar.SUNDAY + ((dow - Calendar.SUNDAY + 1) % 7);
    }

    tzOffset = testTZ.getOffset(testCal.get(Calendar.ERA),
                                testCal.get(Calendar.YEAR),
                                testCal.get(Calendar.MONTH),
                                date,
                                dow,
                                millis);
    tzRawOffset = testTZ.getRawOffset();
    tzOffsetFloat = new Float((float)tzOffset/(float)3600000);
    tzRawOffsetFloat = new Float((float)tzRawOffset/(float)3600000);

    Date testDate = testCal.getTime();

    boolean inDaylightTime = testTZ.inDaylightTime(testDate);
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
    sdf.setCalendar(testCal);
    String inDaylightTimeString;

    boolean passed;

    if (inDaylightTime)
    {
        inDaylightTimeString = " DST ";
        passed = (tzOffset == (tzRawOffset + 3600000));
    }
    else
    {
        inDaylightTimeString = "     ";
        passed = (tzOffset == tzRawOffset);
    }

    String output = testTZ.getID() + " " + sdf.format(testDate) +
        " Offset(" + tzOffsetFloat + ")" +
        " RawOffset(" + tzRawOffsetFloat + ")" +
        " " + millis/(float)3600000 + " " +
        inDaylightTimeString;

    if (passed)
        output += "     ";
    else
        output += "ERROR";

    if (passed) logln(output); else errln(output);
    return passed;
}
 
Example 9
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * getOffset returns wrong offset for days in early 20th century
 */
@Test
public void TestJ5134() {
    GregorianCalendar testCal = (GregorianCalendar)Calendar.getInstance();
    TimeZone icuEastern = TimeZone.getTimeZone("America/New_York");        
    testCal.setTimeZone(icuEastern);
    testCal.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
    long time = testCal.getTimeInMillis();

    int offset = icuEastern.getOffset(time);
    if (offset != -18000000) {
        errln("FAIL: UTC offset in time zone America/New_York on Jan 1, 1900 -> " + offset);
    }
    boolean isDst = icuEastern.inDaylightTime(new Date(time));
    if (isDst) {
        errln("FAIL: DST is observed in time zone America/New_York on Jan 1, 1900");
    }

    if (System.getProperty("java.vendor", "").startsWith("IBM") &&
        System.getProperty("java.version", "").equals("1.4.1")) {
        // IBM JDK 1.4.1 has a bug and fails to run this test case.
        return;
    }
    java.util.TimeZone jdkEastern = java.util.TimeZone.getTimeZone("America/New_York");
    // Compare offset and DST observation with JDK and ICU for 50 years since 1900
    testCal.add(Calendar.YEAR, 50);
    long endTime = testCal.getTimeInMillis();
    int jdkOffset;
    boolean isDstJdk;
    while (time < endTime) {
        offset = icuEastern.getOffset(time);
        jdkOffset = jdkEastern.getOffset(time);
        if (offset != jdkOffset) {
            errln("FAIL: Incompatible UTC offset -> JDK:" + jdkOffset + "/ICU:" + offset + " [" + time + "]");
        }
        Date d = new Date(time);
        isDst = icuEastern.inDaylightTime(d);
        isDstJdk = jdkEastern.inDaylightTime(d);
        if (isDst != isDstJdk) {
            errln("FAIL: Incompatible DST -> JDK:" + isDstJdk + "/ICU:" + isDst + " [" + time + "]");
        }
        time += 24*60*60*1000L; // increment 1 day
    }
}
 
Example 10
Source File: TimeZoneBoundaryTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
void findDaylightBoundaryUsingTimeZone(Date d, boolean startsInDST,
                                       long expectedBoundary, TimeZone tz)
{
    // Given a date with a year start, find the Daylight onset
    // and end.  The given date should be 1/1/xx in some year.

    // Use a binary search, assuming that we have a Standard
    // time at the midpoint.
    long min = d.getTime();
    long max = min + SIX_MONTHS;

    if (tz.inDaylightTime(d) != startsInDST)
    {
        errln("FAIL: " + tz.getID() + " inDaylightTime(" +
              d + ") != " + startsInDST);
        startsInDST = !startsInDST; // Flip over; find the apparent value
    }

    if (tz.inDaylightTime(new Date(max)) == startsInDST)
    {
        errln("FAIL: " + tz.getID() + " inDaylightTime(" +
              (new Date(max)) + ") != " + (!startsInDST));
        return;
    }

    while ((max - min) >  INTERVAL)
    {
        long mid = (min + max) >> 1;
        boolean isIn = tz.inDaylightTime(new Date(mid));
        if (isIn == startsInDST)
        {
            min = mid;
        }
        else
        {
            max = mid;
        }
    }

    logln(tz.getID() + " Before: " + showDate(min, tz));
    logln(tz.getID() + " After:  " + showDate(max, tz));

    long mindelta = expectedBoundary - min;
    // not used long maxdelta = max - expectedBoundary; 
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
    fmt.setTimeZone(tz);
    if (mindelta >= 0 && mindelta <= INTERVAL &&
        mindelta >= 0 && mindelta <= INTERVAL)
        logln("PASS: Expected boundary at " + expectedBoundary + " = " + fmt.format(new Date(expectedBoundary)));
    else
        errln("FAIL: Expected boundary at " + expectedBoundary + " = " + fmt.format(new Date(expectedBoundary)));
}
 
Example 11
Source File: TimeZoneBoundaryTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Given a date, a TimeZone, and expected values for inDaylightTime,
 * useDaylightTime, zone and DST offset, verify that this is the case.
 */
void verifyDST(String tag, Calendar cal, TimeZone time_zone,
               boolean expUseDaylightTime, boolean expInDaylightTime,
               int expRawOffset, int expOffset)
{
    Date d = cal.getTime();

    logln("-- " + tag + ": " + d +
          " in zone " + time_zone.getID() + " (" +
          d.getTime()/3600000.0 + ")");

    if (time_zone.inDaylightTime(d) == expInDaylightTime)
        logln("PASS: inDaylightTime = " + time_zone.inDaylightTime(d));
    else
        errln("FAIL: inDaylightTime = " + time_zone.inDaylightTime(d));

    if (time_zone.useDaylightTime() == expUseDaylightTime)
        logln("PASS: useDaylightTime = " + time_zone.useDaylightTime());
    else
        errln("FAIL: useDaylightTime = " + time_zone.useDaylightTime());

    if (time_zone.getRawOffset() == expRawOffset)
        logln("PASS: getRawOffset() = " + expRawOffset/(double)ONE_HOUR);
    else
        errln("FAIL: getRawOffset() = " + time_zone.getRawOffset()/(double)ONE_HOUR +
              "; expected " + expRawOffset/(double)ONE_HOUR);

    //GregorianCalendar gc = new GregorianCalendar(time_zone);
    //gc.setTime(d);
    int offset = time_zone.getOffset(cal.get(Calendar.ERA), cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                                     cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_WEEK),
                                     ((cal.get(Calendar.HOUR_OF_DAY) * 60 +
                                       cal.get(Calendar.MINUTE)) * 60 +
                                      cal.get(Calendar.SECOND)) * 1000 +
                                     cal.get(Calendar.MILLISECOND));
    if (offset == expOffset)
        logln("PASS: getOffset() = " + offset/(double)ONE_HOUR);
    else {
        logln("era=" + cal.get(Calendar.ERA) +
              ", year=" + cal.get(Calendar.YEAR) +
              ", month=" + cal.get(Calendar.MONTH) +
              ", dom=" + cal.get(Calendar.DAY_OF_MONTH) +
              ", dow=" + cal.get(Calendar.DAY_OF_WEEK) +
              ", time-of-day=" + (((cal.get(Calendar.HOUR_OF_DAY) * 60 +
                           cal.get(Calendar.MINUTE)) * 60 +
                          cal.get(Calendar.SECOND)) * 1000 +
                         cal.get(Calendar.MILLISECOND)) / 3600000.0 +
                        " hours");
        errln("FAIL: getOffset() = " + offset/(double)ONE_HOUR +
              "; expected " + expOffset/(double)ONE_HOUR);
    }
}
 
Example 12
Source File: TimeZoneBoundaryTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Find boundaries by stepping.
 */
void findBoundariesStepwise(int year, long interval, TimeZone z, int expectedChanges)
{
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(year, Calendar.JANUARY, 1);
    Date d = tempcal.getTime();
    long time = d.getTime(); // ms
    long limit = time + ONE_YEAR + ONE_DAY;
    boolean lastState = z.inDaylightTime(d);
    int changes = 0;
    logln("-- Zone " + z.getID() + " starts in " + year + " with DST = " + lastState);
    logln("useDaylightTime = " + z.useDaylightTime());
    while (time < limit)
    {
        d.setTime(time);
        boolean state = z.inDaylightTime(d);
        if (state != lastState)
        {
            logln((state ? "Entry " : "Exit ") +
                  "at " + d);
            lastState = state;
            ++changes;
        }
        time += interval;
    }
    if (changes == 0)
    {
        if (!lastState && !z.useDaylightTime()) logln("No DST");
        else errln("FAIL: DST all year, or no DST with true useDaylightTime");
    }
    else if (changes != 2)
    {
        errln("FAIL: " + changes + " changes seen; should see 0 or 2");
    }
    else if (!z.useDaylightTime())
    {
        errln("FAIL: useDaylightTime false but 2 changes seen");
    }
    if (changes != expectedChanges)
    {
        errln("FAIL: " + changes + " changes seen; expected " + expectedChanges);
    }
}