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

The following examples show how to use android.icu.util.TimeZone#getOffset() . 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: TimeZoneTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestGetOffsetDate() {
    Calendar cal = Calendar.getInstance();
    cal.set(1997, Calendar.JANUARY, 30);
    long date = cal.getTimeInMillis();

TimeZone tz_icu = TimeZone.getTimeZone("America/Los_Angeles");
int offset = tz_icu.getOffset(date);
if (offset != -28800000) {
    errln("expected offset -28800000, got: " + offset);
}

cal.set(1997, Calendar.JULY, 30);
date = cal.getTimeInMillis();
offset = tz_icu.getOffset(date);
if (offset != -25200000) {
    errln("expected offset -25200000, got: " + offset);
}
}
 
Example 2
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestJohannesburg() {
    String j_id="Africa/Johannesburg";
    TimeZone johannesburg = TimeZone.getTimeZone(j_id);
    final int ONE_HOUR = 60*60*1000;
    int expectedOffset = ONE_HOUR*2;  // GMT+2 - NO DST
    int offset = johannesburg.getOffset(GregorianCalendar.AD,2007,Calendar.JULY,5,Calendar.THURSDAY,0);        
    
    if(offset != expectedOffset) {
        errln("FAIL: zone " + j_id +" returned offset in July " + offset +", expected "+expectedOffset);
    } else {
        logln("OK: zone " + j_id +" returned offset in July: " + offset);
    }

    int offset2 = johannesburg.getOffset(GregorianCalendar.AD,2007,Calendar.DECEMBER,12,Calendar.WEDNESDAY,0);

    if(offset2 != expectedOffset) {
        errln("FAIL: zone " + j_id +" returned offset in December " + offset2 +", expected "+expectedOffset);
    } else {
        logln("OK: zone " + j_id +" returned offset in December: " + offset2);
    }
}
 
Example 3
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 4
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestMark(){
    String tzid = "America/Argentina/ComodRivadavia";
    TimeZone tz = TimeZone.getTimeZone(tzid);
    int offset = tz.getOffset(new Date().getTime());
    logln(tzid + ":\t" + offset);
    List list = Arrays.asList(TimeZone.getAvailableIDs());
    if(!list.contains(tzid)){
        errln("Could create the time zone but it is not in getAvailableIDs");
    }
}
 
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
/**
 * The expected behavior of TimeZone around the boundaries is:
 * (Assume transition time of 2:00 AM)
 *    day of onset 1:59 AM STD  = display name 1:59 AM ST
 *                 2:00 AM STD  = display name 3:00 AM DT
 *    day of end   0:59 AM STD  = display name 1:59 AM DT
 *                 1:00 AM STD  = display name 1:00 AM ST
 */
@Test
public void Test4084933() {
    TimeZone tz = TimeZone.getTimeZone("PST");

    long offset1 = tz.getOffset(1,
        1997, Calendar.OCTOBER, 26, Calendar.SUNDAY, (2*60*60*1000));
    long offset2 = tz.getOffset(1,
        1997, Calendar.OCTOBER, 26, Calendar.SUNDAY, (2*60*60*1000)-1);

    long offset3 = tz.getOffset(1,
        1997, Calendar.OCTOBER, 26, Calendar.SUNDAY, (1*60*60*1000));
    long offset4 = tz.getOffset(1,
        1997, Calendar.OCTOBER, 26, Calendar.SUNDAY, (1*60*60*1000)-1);

    /*
     *  The following was added just for consistency.  It shows that going *to* Daylight
     *  Savings Time (PDT) does work at 2am.
     */

    long offset5 = tz.getOffset(1,
        1997, Calendar.APRIL, 6, Calendar.SUNDAY, (2*60*60*1000));
    long offset6 = tz.getOffset(1,
        1997, Calendar.APRIL, 6, Calendar.SUNDAY, (2*60*60*1000)-1);

    long offset7 = tz.getOffset(1,
        1997, Calendar.APRIL, 6, Calendar.SUNDAY, (1*60*60*1000));
    long offset8 = tz.getOffset(1,
        1997, Calendar.APRIL, 6, Calendar.SUNDAY, (1*60*60*1000)-1);

    long SToffset = -8 * 60*60*1000L;
    long DToffset = -7 * 60*60*1000L;
    if (offset1 != SToffset || offset2 != SToffset ||
        offset3 != SToffset || offset4 != DToffset ||
        offset5 != DToffset || offset6 != SToffset ||
        offset7 != SToffset || offset8 != SToffset)
        warnln("Fail: TimeZone misbehaving");
}
 
Example 7
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 8
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * TimeZone broken in last hour of year
 */
@Test
public void Test4173604() {
    TimeZone pst = TimeZone.getTimeZone("PST");
    int o22 = pst.getOffset(1, 1998, 11, 31, Calendar.THURSDAY, 22*60*60*1000);
    int o23 = pst.getOffset(1, 1998, 11, 31, Calendar.THURSDAY, 23*60*60*1000);
    int o00 = pst.getOffset(1, 1999, 0, 1, Calendar.FRIDAY, 0);
    if (o22 != o23 || o22 != o00) {
        errln("Offsets should be the same (for PST), but got: " +
              "12/31 22:00 " + o22 +
              ", 12/31 23:00 " + o23 +
              ", 01/01 00:00 " + o00);
    }

    GregorianCalendar cal = new GregorianCalendar();
    cal.setTimeZone(pst);
    cal.clear();
    cal.set(1998, Calendar.JANUARY, 1);
    int lastDST = cal.get(Calendar.DST_OFFSET);
    int transitions = 0;
    int delta = 5;
    while (cal.get(Calendar.YEAR) < 2000) {
        cal.add(Calendar.MINUTE, delta);
        if (cal.get(Calendar.DST_OFFSET) != lastDST) {
            ++transitions;
            Calendar t = (Calendar)cal.clone();
            t.add(Calendar.MINUTE, -delta);
            logln(t.getTime() + "  " + t.get(Calendar.DST_OFFSET));
            logln(cal.getTime() + "  " + (lastDST=cal.get(Calendar.DST_OFFSET)));
        }
    }
    if (transitions != 4) {
        errln("Saw " + transitions + " transitions; should have seen 4");
    }
}
 
Example 9
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestFebruary() {
    // Time zone with daylight savings time from the first Sunday in November
    // to the last Sunday in February.
    // Similar to the new rule for Brazil (Sao Paulo) in tzdata2006n.
    //
    // Note: In tzdata2007h, the rule had changed, so no actual zones uses
    // lastSun in Feb anymore.
    SimpleTimeZone tz1 = new SimpleTimeZone(
                       -3 * MILLIS_PER_HOUR,                    // raw offset: 3h before (west of) GMT
                       "nov-feb",
                       Calendar.NOVEMBER, 1, Calendar.SUNDAY,   // start: November, first, Sunday
                       0,                                       //        midnight wall time
                       Calendar.FEBRUARY, -1, Calendar.SUNDAY,  // end:   February, last, Sunday
                       0);                                      //        midnight wall time

    // Now hardcode the same rules as for Brazil in tzdata 2006n, so that
    // we cover the intended code even when in the future zoneinfo hardcodes
    // these transition dates.
    SimpleTimeZone tz2= new SimpleTimeZone(
                       -3 * MILLIS_PER_HOUR,                    // raw offset: 3h before (west of) GMT
                       "nov-feb2",
                       Calendar.NOVEMBER, 1, -Calendar.SUNDAY,  // start: November, 1 or after, Sunday
                       0,                                       //        midnight wall time
                       Calendar.FEBRUARY, -29, -Calendar.SUNDAY,// end:   February, 29 or before, Sunday
                       0);                                      //        midnight wall time

    // Gregorian calendar with the UTC time zone for getting sample test date/times.
    GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("Etc/GMT"));
    // "Unable to create the UTC calendar: %s"

    int[] data = {
        // UTC time (6 fields) followed by
        // expected time zone offset in hours after GMT (negative=before GMT).
        // int year, month, day, hour, minute, second, offsetHours
        2006, Calendar.NOVEMBER,  5, 02, 59, 59, -3,
        2006, Calendar.NOVEMBER,  5, 03, 00, 00, -2,
        2007, Calendar.FEBRUARY, 25, 01, 59, 59, -2,
        2007, Calendar.FEBRUARY, 25, 02, 00, 00, -3,

        2007, Calendar.NOVEMBER,  4, 02, 59, 59, -3,
        2007, Calendar.NOVEMBER,  4, 03, 00, 00, -2,
        2008, Calendar.FEBRUARY, 24, 01, 59, 59, -2,
        2008, Calendar.FEBRUARY, 24, 02, 00, 00, -3,

        2008, Calendar.NOVEMBER,  2, 02, 59, 59, -3,
        2008, Calendar.NOVEMBER,  2, 03, 00, 00, -2,
        2009, Calendar.FEBRUARY, 22, 01, 59, 59, -2,
        2009, Calendar.FEBRUARY, 22, 02, 00, 00, -3,

        2009, Calendar.NOVEMBER,  1, 02, 59, 59, -3,
        2009, Calendar.NOVEMBER,  1, 03, 00, 00, -2,
        2010, Calendar.FEBRUARY, 28, 01, 59, 59, -2,
        2010, Calendar.FEBRUARY, 28, 02, 00, 00, -3
    };

    TimeZone timezones[] = { tz1, tz2 };

    TimeZone tz;
    Date dt;
    int t, i, raw, dst;
    int[] offsets = new int[2]; // raw = offsets[0], dst = offsets[1]
    for (t = 0; t < timezones.length; ++t) {
        tz = timezones[t];
        for (i = 0; i < data.length; i+=7) {
            gc.set(data[i], data[i+1], data[i+2],
                   data[i+3], data[i+4], data[i+5]);
            dt = gc.getTime();
            tz.getOffset(dt.getTime(), false, offsets);
            raw = offsets[0];
            dst = offsets[1];
            if ((raw + dst) != data[i+6] * MILLIS_PER_HOUR) {
                errln("test case " + t + "." + (i/7) + ": " +
                      "tz.getOffset(" + data[i] + "-" + (data[i+1] + 1) + "-" + data[i+2] + " " +
                      data[i+3] + ":" + data[i+4] + ":" + data[i+5] +
                      ") returns " + raw + "+" + dst + " != " + data[i+6] * MILLIS_PER_HOUR);
            }
        }
    }
}
 
Example 10
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestSetDefault() {
    java.util.TimeZone save = java.util.TimeZone.getDefault();

    /*
     * America/Caracs (Venezuela) changed the base offset from -4:00 to
     * -4:30 on Dec 9, 2007.
     */

    TimeZone icuCaracas = TimeZone.getTimeZone("America/Caracas", TimeZone.TIMEZONE_ICU);
    java.util.TimeZone jdkCaracas = java.util.TimeZone.getTimeZone("America/Caracas");

    // Set JDK America/Caracas as the default
    java.util.TimeZone.setDefault(jdkCaracas);

    java.util.Calendar jdkCal = java.util.Calendar.getInstance();
    jdkCal.clear();
    jdkCal.set(2007, java.util.Calendar.JANUARY, 1);

    int rawOffset = jdkCal.get(java.util.Calendar.ZONE_OFFSET);
    int dstSavings = jdkCal.get(java.util.Calendar.DST_OFFSET);

    int[] offsets = new int[2];
    icuCaracas.getOffset(jdkCal.getTime().getTime()/*jdkCal.getTimeInMillis()*/, false, offsets);

    boolean isTimeZoneSynchronized = true;

    if (rawOffset != offsets[0] || dstSavings != offsets[1]) {
        // JDK time zone rule is out of sync...
        logln("Rule for JDK America/Caracas is not same with ICU.  Skipping the rest.");
        isTimeZoneSynchronized = false;
    }

    if (isTimeZoneSynchronized) {
        // If JDK America/Caracas uses the same rule with ICU,
        // the following code should work well.
        TimeZone.setDefault(icuCaracas);

        // Create a new JDK calendar instance again.
        // This calendar should reflect the new default
        // set by ICU TimeZone#setDefault.
        jdkCal = java.util.Calendar.getInstance();
        jdkCal.clear();
        jdkCal.set(2007, java.util.Calendar.JANUARY, 1);

        rawOffset = jdkCal.get(java.util.Calendar.ZONE_OFFSET);
        dstSavings = jdkCal.get(java.util.Calendar.DST_OFFSET);

        if (rawOffset != offsets[0] || dstSavings != offsets[1]) {
            errln("ERROR: Got offset [raw:" + rawOffset + "/dst:" + dstSavings
                      + "] Expected [raw:" + offsets[0] + "/dst:" + offsets[1] + "]");
        }
    }

    // Restore the original JDK time zone
    java.util.TimeZone.setDefault(save);
}
 
Example 11
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 12
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * SimpleTimeZone.getOffset accepts illegal arguments.
 */
@Test
public void Test4154650() {
    final int GOOD=1, BAD=0;
    final int GOOD_ERA=GregorianCalendar.AD, GOOD_YEAR=1998, GOOD_MONTH=Calendar.AUGUST;
    final int GOOD_DAY=2, GOOD_DOW=Calendar.SUNDAY, GOOD_TIME=16*3600000;
    int[] DATA = {
        GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,

        GOOD, GregorianCalendar.BC, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,
        GOOD, GregorianCalendar.AD, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,
        BAD,  GregorianCalendar.BC-1, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,
        BAD,  GregorianCalendar.AD+1, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,

        GOOD, GOOD_ERA, GOOD_YEAR, Calendar.JANUARY, GOOD_DAY, GOOD_DOW, GOOD_TIME,
        GOOD, GOOD_ERA, GOOD_YEAR, Calendar.DECEMBER, GOOD_DAY, GOOD_DOW, GOOD_TIME,
        BAD,  GOOD_ERA, GOOD_YEAR, Calendar.JANUARY-1, GOOD_DAY, GOOD_DOW, GOOD_TIME,
        BAD,  GOOD_ERA, GOOD_YEAR, Calendar.DECEMBER+1, GOOD_DAY, GOOD_DOW, GOOD_TIME,

        GOOD, GOOD_ERA, GOOD_YEAR, Calendar.JANUARY, 1, GOOD_DOW, GOOD_TIME,
        GOOD, GOOD_ERA, GOOD_YEAR, Calendar.JANUARY, 31, GOOD_DOW, GOOD_TIME,
        BAD,  GOOD_ERA, GOOD_YEAR, Calendar.JANUARY, 0, GOOD_DOW, GOOD_TIME,
        BAD,  GOOD_ERA, GOOD_YEAR, Calendar.JANUARY, 32, GOOD_DOW, GOOD_TIME,

        GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, Calendar.SUNDAY, GOOD_TIME,
        GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, Calendar.SATURDAY, GOOD_TIME,
        BAD,  GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, Calendar.SUNDAY-1, GOOD_TIME,
        BAD,  GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, Calendar.SATURDAY+1, GOOD_TIME,

        GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, 0,
        GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, 24*3600000-1,
        BAD,  GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, -1,
        BAD,  GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, 24*3600000,
    };

    TimeZone tz = TimeZone.getDefault();
    for (int i=0; i<DATA.length; i+=7) {
        boolean good = DATA[i] == GOOD;
        IllegalArgumentException e = null;
        try {
            /*int offset =*/ tz.getOffset(DATA[i+1], DATA[i+2], DATA[i+3],
                                      DATA[i+4], DATA[i+5], DATA[i+6]);
            //offset = 0;
       } catch (IllegalArgumentException ex) {
            e = ex;
        }
        if (good != (e == null)) {
            errln("Fail: getOffset(" +
                  DATA[i+1] + ", " + DATA[i+2] + ", " + DATA[i+3] + ", " +
                  DATA[i+4] + ", " + DATA[i+5] + ", " + DATA[i+6] +
                  (good ? (") threw " + e) : ") accepts invalid args"));
        }
    }
}
 
Example 13
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * TimeZone broken at midnight.  The TimeZone code fails to handle
 * transitions at midnight correctly.
 */
@Test
public void Test4162593() {
    SimpleDateFormat fmt = new SimpleDateFormat("z", Locale.US);
    final int ONE_HOUR = 60*60*1000;
    final float H = (float) ONE_HOUR;
    TimeZone initialZone = TimeZone.getDefault();
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm z");

    SimpleTimeZone asuncion = new SimpleTimeZone(-4*ONE_HOUR, "America/Asuncion" /*PY%sT*/,
        Calendar.OCTOBER, 1, 0 /*DOM*/, 0*ONE_HOUR,
        Calendar.MARCH, 1, 0 /*DOM*/, 0*ONE_HOUR, 1*ONE_HOUR);

    /* Zone
     * Starting time
     * Transition expected between start+1H and start+2H
     */
    Object[] DATA = {
        new SimpleTimeZone(2*ONE_HOUR, "Asia/Damascus" /*EE%sT*/,
            Calendar.APRIL, 1, 0 /*DOM*/, 0*ONE_HOUR,
            Calendar.OCTOBER, 1, 0 /*DOM*/, 0*ONE_HOUR, 1*ONE_HOUR),
        new int[] {1998, Calendar.SEPTEMBER, 30, 22, 0},
        Boolean.TRUE,

        asuncion,
        new int[] {2000, Calendar.FEBRUARY, 28, 22, 0},
        Boolean.FALSE,

        asuncion,
        new int[] {2000, Calendar.FEBRUARY, 29, 22, 0},
        Boolean.TRUE,
    };

    String[] zone = new String[4];

    for (int j=0; j<DATA.length; j+=3) {
        TimeZone tz = (TimeZone)DATA[j];
        TimeZone.setDefault(tz);
        fmt.setTimeZone(tz);
        sdf.setTimeZone(tz);

        // Must construct the Date object AFTER setting the default zone
        int[] p = (int[])DATA[j+1];
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(p[0], p[1], p[2], p[3], p[4]);
        long start = cal.getTime().getTime();
        boolean transitionExpected = ((Boolean)DATA[j+2]).booleanValue();

        logln(tz.getID() + ":");
        for (int i=0; i<4; ++i) {
            Date d = new Date(start + i*ONE_HOUR);
            zone[i] = fmt.format(d);
            logln("" + i + ": " + sdf.format(d) + " => " + zone[i] +
                  " (" + d.getTime()/H + ")");
        }
        cal.set(p[0], p[1], p[2], 0, 0);
        for (int i=0; i<4; ++i) {
            int h = 22+i;
            int dom = p[2]+(h>=24?1:0);
            h %= 24;
            int ms = h*ONE_HOUR;
            cal.clear();
            cal.set(p[0], p[1], dom, 0, 0);
            int off = tz.getOffset(GregorianCalendar.AD,
                                   cal.get(Calendar.YEAR),
                                   cal.get(Calendar.MONTH),
                                   cal.get(Calendar.DATE),
                                   cal.get(Calendar.DAY_OF_WEEK),
                                   ms);
            cal.add(Calendar.HOUR, h);
            int dstOffset = cal.get(Calendar.DST_OFFSET);
            logln("h=" + h + "; dom=" + dom +
                  "; ZONE_OFFSET=" + cal.get(Calendar.ZONE_OFFSET)/H +
                  "; DST_OFFSET=" + dstOffset/H +
                  "; getOffset()=" + off/H +
                  " (" + cal.getTime().getTime()/H + ")");
        }
        if (zone[0].equals(zone[1]) &&
            (zone[1].equals(zone[2]) != transitionExpected) &&
            zone[2].equals(zone[3])) {
            logln("Ok: transition " + transitionExpected);
        } else {
            errln("FAIL: expected " +
                  (transitionExpected?"transition":"no transition"));
        }
    }

// restore the initial time zone so that this test case
// doesn't affect the others.
TimeZone.setDefault(initialZone);
}
 
Example 14
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 15
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);
    }
}