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

The following examples show how to use android.icu.util.TimeZone#getRawOffset() . 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: DateFormatRegressionTestJ.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void run() {
    SimpleDateFormat sdf = (SimpleDateFormat) sdf_.clone();
    TimeZone defaultTZ = TimeZone.getDefault();
    TimeZone PST = TimeZone.getTimeZone("PST");
    int defaultOffset = defaultTZ.getRawOffset();
    int PSTOffset = PST.getRawOffset();
    int offset = defaultOffset - PSTOffset;
    long ms = UTC_LONG - offset;
    try {
        int i = 0;
        while (i < 10000) {
            Date date = sdf.parse(TIME_STRING);
            long t = date.getTime();
            i++;
            if (t != ms) {
                throw new ParseException("Parse Error: " + i + " (" + sdf.format(date) 
                          + ") " + t + " != " + ms, 0);
            }
        }
    } catch (Exception e) {
        errln("parse error: " + e.getMessage());
    }
}
 
Example 3
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestPRTOffset()
{
    TimeZone tz = TimeZone.getTimeZone( "PRT" );
    if( tz == null ) {
        errln( "FAIL: TimeZone(PRT) is null" );
    }
    else{
        if (tz.getRawOffset() != (-4*millisPerHour))
            warnln("FAIL: Offset for PRT should be -4, got " +
                  tz.getRawOffset() / (double)millisPerHour);
    }

}
 
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
@Test
public void TestOddTimeZoneNames() {
    for (int i = 0; i < timeZoneTestNames.length; i += 2) {
        String funkyName = timeZoneTestNames[i];
        String correctName = timeZoneTestNames[i+1];

        TimeZone ftz = TimeZone.getTimeZone(funkyName);
        TimeZone ctz = TimeZone.getTimeZone(correctName);

        String fdn = ftz.getDisplayName();
        long fro = ftz.getRawOffset();
        long fds = ftz.getDSTSavings();
        boolean fdy = ftz.useDaylightTime();

        String cdn = ctz.getDisplayName();
        long cro = ctz.getRawOffset();
        long cds = ctz.getDSTSavings();
        boolean cdy = ctz.useDaylightTime();

        if (!fdn.equals(cdn)) {
            logln("display name (" + funkyName + ", " + correctName + ") expected: " + cdn + " but got: " + fdn);
        } else if (fro != cro) {
            logln("offset (" + funkyName + ", " + correctName + ") expected: " + cro + " but got: " + fro);
        } else if (fds != cds) {
            logln("daylight (" + funkyName + ", " + correctName + ") expected: " + cds + " but got: " + fds);
        } else if (fdy != cdy) {
            logln("uses daylight (" + funkyName + ", " + correctName + ") expected: " + cdy + " but got: " + fdy);
        } else {
            // no error, assume we're referencing the same internal java object
        }
    }
}
 
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: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
ZoneDescriptor(TimeZone zone) {
    this.id = zone.getID();
    this.offset = zone.getRawOffset() / 60000;
    this.daylight = zone.useDaylightTime();
}
 
Example 8
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * As part of the VM fix (see CCC approved RFE 4028006, bug
 * 4044013), TimeZone.getTimeZone() has been modified to recognize
 * generic IDs of the form GMT[+-]hh:mm, GMT[+-]hhmm, and
 * GMT[+-]hh.  Test this behavior here.
 *
 * Bug 4044013
 */
@Test
public void TestCustomParse() {
    String[] DATA = {
        // ID               offset(sec)     output ID
        "GMT",              "0",            "GMT",      // system ID
        "GMT-YOUR.AD.HERE", "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT0",             "0",            "GMT0",     // system ID
        "GMT+0",            "0",            "GMT+0",    // system ID
        "GMT+1",            "3600",         "GMT+01:00",
        "GMT-0030",         "-1800",        "GMT-00:30",
        "GMT+15:99",        "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+",             "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-",             "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+0:",           "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-:",            "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+0010",         "600",          "GMT+00:10",
        "GMT-10",           "-36000",       "GMT-10:00",
        "GMT+30",           "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-3:30",         "-12600",       "GMT-03:30",
        "GMT-230",          "-9000",        "GMT-02:30",
        "GMT+05:13:05",     "18785",        "GMT+05:13:05",
        "GMT-71023",        "-25823",       "GMT-07:10:23",
        "GMT+01:23:45:67",  "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+01:234",       "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-2:31:123",     "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT+3:75",         "0",            TimeZone.UNKNOWN_ZONE_ID,
        "GMT-01010101",     "0",            TimeZone.UNKNOWN_ZONE_ID,
    };
    for (int i = 0; i < DATA.length; i += 3) {
        String id = DATA[i];
        int offset = Integer.parseInt(DATA[i+1]);
        String expId = DATA[i+2];

        TimeZone zone = TimeZone.getTimeZone(id);
        String gotID = zone.getID();
        int gotOffset = zone.getRawOffset()/1000;

        logln(id + " -> " + gotID + " " + gotOffset);

        if (offset != gotOffset) {
            errln("FAIL: Unexpected offset for " + id + " - returned:" + gotOffset + " expected:" + offset);
        }
        if (!expId.equals(gotID)) {
            if (TimeZone.getDefaultTimeZoneType() != TimeZone.TIMEZONE_ICU) {
                logln("ID for " + id + " - returned:" + gotID + " expected:" + expId);
            } else {
                errln("FAIL: Unexpected ID for " + id + " - returned:" + gotID + " expected:" + expId);
            }
        }
    }
}
 
Example 9
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 10
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Test setRawOffset works OK with system timezone
 */
@Test
public void TestT5280() {
    boolean isJdkZone = (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK);
    String[] tzids = TimeZone.getAvailableIDs();
    for (int i = 0; i < tzids.length; i++) {
        TimeZone tz = TimeZone.getTimeZone(tzids[i]);
        boolean useDst = tz.useDaylightTime();

        // Increase offset for 30 minutes
        int newRawOffset = tz.getRawOffset() + 30*60*1000;
        try {
            tz.setRawOffset(newRawOffset);
        } catch (Exception e) {
            errln("FAIL: setRawOffset throws an exception");
        }
        int offset = tz.getRawOffset();
        if (offset != newRawOffset) {
            if (isJdkZone) {
                // JDK TimeZone#setRawOffset() only update the last rule, and getRawOffset() returns
                // the current raw offset. Therefore, they might be different.
                logln("Modified zone(" + tz.getID() + ") - getRawOffset returns " + offset + "/ Expected: " + newRawOffset);
            } else {
                errln("FAIL: Modified zone(" + tz.getID() + ") - getRawOffset returns " + offset + "/ Expected: " + newRawOffset);
            }
        }
        // Ticket#5917
        // Check if DST observation status is not unexpectedly changed.
        boolean newDst = tz.useDaylightTime();
        if (useDst != newDst) {
            errln("FAIL: Modified zone(" + tz.getID() + ") - useDaylightTime has changed from " + useDst + " to " + newDst);
        }
        // Make sure the offset is preserved in a clone
        TimeZone tzClone = (TimeZone)tz.clone();
        int offsetC = tzClone.getRawOffset();
        if (offsetC != newRawOffset) {
            if (isJdkZone) {
                logln("Cloned modified zone(" + tz.getID() + ") - getRawOffset returns " + offsetC + "/ Expected: " + newRawOffset);
            } else {
                errln("FAIL: Cloned modified zone(" + tz.getID() + ") - getRawOffset returns " + offsetC + "/ Expected: " + newRawOffset);
            }
        }

        if (offset != offsetC) {
            errln("FAIL: Different raw offset - Original:" + offset + ", Cloned:" + offsetC);
        }
    }
}
 
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);
    }
}