publicvoid Test4096952() {
String[] ZONES = { "GMT", "MET", "IST" }; boolean pass = true; try { for (int i=0; i<ZONES.length; ++i) {
TimeZone zone = TimeZone.getTimeZone(ZONES[i]); if (!zone.getID().equals(ZONES[i]))
errln("Fail: Test broken; zones not instantiating");
publicvoid Test4109314() {
Locale locale = Locale.getDefault(); if (!TestUtils.usesGregorianCalendar(locale)) {
logln("Skipping this test because locale is " + locale); return;
}
// test both SimpleTimeZone and ZoneInfo objects. // @since 1.4
sub4109314(getPST());
sub4109314(TimeZone.getTimeZone("PST"));
}
@SuppressWarnings("deprecation") privatevoid sub4109314(TimeZone PST) {
GregorianCalendar testCal = (GregorianCalendar)Calendar.getInstance();
Object[] testData = {
PST, new Date(98,Calendar.APRIL,4,22,0), new Date(98, Calendar.APRIL, 5,6,0),
PST, new Date(98,Calendar.OCTOBER,24,22,0), new Date(98,Calendar.OCTOBER,25,6,0),
}; boolean pass=true; for (int i=0; i<testData.length; i+=3) {
testCal.setTimeZone((TimeZone) testData[i]); long t = ((Date)testData[i+1]).getTime();
Date end = (Date) testData[i+2]; while (t < end.getTime()) {
testCal.setTime(new Date(t)); if (!checkCalendar314(testCal, (TimeZone) testData[i]))
pass = false;
t += 60*60*1000L;
}
} if (!pass) errln("Fail: TZ API inconsistent");
}
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 *BEGINNINGORENDOFAMONTH.Wemustaddthiscodebecause *getOffset()hasbeenchangedtobemorestrictabouttheparameters *itreceives--itturnsoutthatthistestwaspassinginillegal
* 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);
}
Date dt = new Date(1998-1900, Calendar.APRIL, 5, 10, 0); // 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");
}
}
/** *TimeZone.getAvailableIDs(int)throwsexceptionforcertainvalues, *duetoafaultyconstantinTimeZone.java.
*/ publicvoid Test4151406() { int max = 0; for (int h=-28; h<=30; ++h) { // h is in half-hours from GMT; rawoffset is in millis int rawoffset = h * 1800000; int hh = (h<0) ? -h : h;
String hname = ((h<0) ? "GMT-" : "GMT+") +
((hh/2 < 10) ? "0" : "") +
(hh/2) + ':' +
((hh%2==0) ? "00" : "30"); try {
String[] ids = TimeZone.getAvailableIDs(rawoffset); if (ids.length > max) max = ids.length;
logln(hname + ' ' + ids.length +
((ids.length > 0) ? (" e.g. " + ids[0]) : ""));
} catch (Exception e) {
errln(hname + ' ' + "Fail: " + e);
}
}
logln("Maximum zones per offset = " + max);
}
publicvoid Test4151429() { try {
TimeZone tz = TimeZone.getTimeZone("GMT");
String name = tz.getDisplayName(true, Integer.MAX_VALUE,
Locale.getDefault());
errln("IllegalArgumentException not thrown by TimeZone.getDisplayName()");
} catch(IllegalArgumentException e) {}
}
/** *SimpleTimeZoneacceptsillegalDSTsavingsvalues.Thesevalues *mustbenon-zero.Thereisnoupperlimitatthistime.
*/ publicvoid Test4154525() { finalint GOOD = 1, BAD = 0; int[] DATA = { 1, GOOD, 0, BAD,
-1, BAD, 60*60*1000, GOOD,
Integer.MIN_VALUE, BAD, // Integer.MAX_VALUE, ?, // no upper limit on DST savings at this time
}; for (int i=0; i<DATA.length; i+=2) { int savings = DATA[i]; boolean valid = DATA[i+1] == GOOD;
String method = null; for (int j=0; j<2; ++j) { try { switch (j) { case0:
method = "constructor";
SimpleTimeZone z = new SimpleTimeZone(0, "id",
Calendar.JANUARY, 1, 0, 0,
Calendar.MARCH, 1, 0, 0,
savings); // <- what we're interested in break; case1:
method = "setDSTSavings()";
z = new SimpleTimeZone(0, "GMT");
z.setDSTSavings(savings); break;
} if (valid) {
logln("Pass: DST savings of " + savings + " accepted by " + method);
} else {
errln("Fail: DST savings of " + savings + " accepted by " + method);
}
} catch (IllegalArgumentException e) { if (valid) {
errln("Fail: DST savings of " + savings + " to " + method + " gave " + e);
} else {
logln("Pass: DST savings of " + savings + " to " + method + " gave " + e);
}
}
}
}
}
/** *SimpleTimeZone.hasSameRules()doesn'tworkforzoneswithnoDST *anddifferentDSTparameters.
*/ publicvoid Test4154537() { // tz1 and tz2 have no DST and different rule parameters
SimpleTimeZone tz1 = new SimpleTimeZone(0, "1", 0, 0, 0, 0, 2, 0, 0, 0);
SimpleTimeZone tz2 = new SimpleTimeZone(0, "2", 1, 0, 0, 0, 3, 0, 0, 0); // tza and tzA have the same rule params
SimpleTimeZone tza = new SimpleTimeZone(0, "a", 0, 1, 0, 0, 3, 2, 0, 0);
SimpleTimeZone tzA = new SimpleTimeZone(0, "A", 0, 1, 0, 0, 3, 2, 0, 0); // tzb differs from tza
SimpleTimeZone tzb = new SimpleTimeZone(0, "b", 0, 1, 0, 0, 3, 1, 0, 0); if (tz1.useDaylightTime() || tz2.useDaylightTime() ||
!tza.useDaylightTime() || !tzA.useDaylightTime() ||
!tzb.useDaylightTime()) {
errln("Test is broken -- rewrite it");
} if (!tza.hasSameRules(tzA) || tza.hasSameRules(tzb)) {
errln("Fail: hasSameRules() broken for zones with rules");
} if (!tz1.hasSameRules(tz2)) {
errln("Fail: hasSameRules() returns false for zones without rules");
errln("zone 1 = " + tz1);
errln("zone 2 = " + tz2);
}
}
/** *SimpleTimeZoneconstructors,setStartRule(),andsetEndRule()don't *checkforout-of-rangearguments.
*/ publicvoid Test4154542() { finalint GOOD = 1; finalint BAD = 0;
// Must construct the Date object AFTER setting the default zone int[] p = (int[])DATA[j+1];
Date d = new Date(p[0], p[1], p[2], p[3], p[4]); boolean transitionExpected = ((Boolean)DATA[j+2]).booleanValue();
logln(tz.getID() + ":"); for (int i=0; i<4; ++i) {
zone[i] = fmt.format(d);
logln("" + i + ": " + d);
d = new Date(d.getTime() + ONE_HOUR);
} if (zone[0].equals(zone[1]) &&
(zone[1].equals(zone[2]) != transitionExpected) &&
zone[2].equals(zone[3])) {
logln("Ok: transition " + transitionExpected);
} else {
errln("Fail: boundary transition incorrect");
}
}
} finally { // restore the initial time zone so that this test case // doesn't affect the others.
TimeZone.setDefault(initialZone);
}
}
/** *TimeZonebrokeninlasthourofyear
*/ publicvoid Test4173604() { // test both SimpleTimeZone and ZoneInfo objects. // @since 1.4
sub4173604(getPST());
sub4173604(TimeZone.getTimeZone("PST"));
}
privatevoid sub4173604(TimeZone 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");
}
}
/** *getDisplayNamedoesn'tworkwithunusualsavings/offsets.
*/
@SuppressWarnings("deprecation") publicvoid Test4176686() { // Construct a zone that does not observe DST but // that does have a DST savings (which should be ignored). int offset = 90 * 60000; // 1:30
SimpleTimeZone z1 = new SimpleTimeZone(offset, "_std_zone_");
z1.setDSTSavings(45 * 60000); // 0:45
// Construct a zone that observes DST for the first 6 months.
SimpleTimeZone z2 = new SimpleTimeZone(offset, "_dst_zone_");
z2.setDSTSavings(45 * 60000); // 0:45
z2.setStartRule(Calendar.JANUARY, 1, 0);
z2.setEndRule(Calendar.JULY, 1, 0);
// Also check DateFormat
DateFormat fmt1 = new SimpleDateFormat("z");
fmt1.setTimeZone(z1); // Format uses standard zone
DateFormat fmt2 = new SimpleDateFormat("z");
fmt2.setTimeZone(z2); // Format uses DST zone
Date dst = new Date(1970-1900, Calendar.FEBRUARY, 1); // Time in DST
Date std = new Date(1970-1900, Calendar.AUGUST, 1); // Time in standard
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.