use of android.icu.util.SimpleTimeZone in project j2objc by google.
the class AstroTest method TestSolarLongitude.
@Test
public void TestSolarLongitude() {
GregorianCalendar gc = new GregorianCalendar(new SimpleTimeZone(0, "UTC"));
CalendarAstronomer astro = new CalendarAstronomer();
// year, month, day, hour, minute, longitude (radians), ascension(radians), declination(radians)
final double[][] tests = { { 1980, 7, 27, 00, 00, 2.166442986535465, 2.2070499713207730, 0.3355704075759270 }, { 1988, 7, 27, 00, 00, 2.167484927693959, 2.2081183335606176, 0.3353093444275315 } };
logln("");
for (int i = 0; i < tests.length; i++) {
gc.clear();
gc.set((int) tests[i][0], (int) tests[i][1] - 1, (int) tests[i][2], (int) tests[i][3], (int) tests[i][4]);
astro.setDate(gc.getTime());
double longitude = astro.getSunLongitude();
if (longitude != tests[i][5]) {
if ((float) longitude == (float) tests[i][5]) {
logln("longitude(" + longitude + ") != tests[i][5](" + tests[i][5] + ") in double for test " + i);
} else {
errln("FAIL: longitude(" + longitude + ") != tests[i][5](" + tests[i][5] + ") for test " + i);
}
}
Equatorial result = astro.getSunPosition();
if (result.ascension != tests[i][6]) {
if ((float) result.ascension == (float) tests[i][6]) {
logln("result.ascension(" + result.ascension + ") != tests[i][6](" + tests[i][6] + ") in double for test " + i);
} else {
errln("FAIL: result.ascension(" + result.ascension + ") != tests[i][6](" + tests[i][6] + ") for test " + i);
}
}
if (result.declination != tests[i][7]) {
if ((float) result.declination == (float) tests[i][7]) {
logln("result.declination(" + result.declination + ") != tests[i][7](" + tests[i][7] + ") in double for test " + i);
} else {
errln("FAIL: result.declination(" + result.declination + ") != tests[i][7](" + tests[i][7] + ") for test " + i);
}
}
}
}
use of android.icu.util.SimpleTimeZone in project j2objc by google.
the class AstroTest method TestSunriseTimes.
@Test
public void TestSunriseTimes() {
// logln("Sunrise/Sunset times for San Jose, California, USA");
// CalendarAstronomer astro = new CalendarAstronomer(-121.55, 37.20);
// TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
// We'll use a table generated by the UNSO website as our reference
// From: http://aa.usno.navy.mil/
// -Location: W079 25, N43 40
// -Rise and Set for the Sun for 2001
// -Zone: 4h West of Greenwich
int[] USNO = { 6, 59, 19, 45, 6, 57, 19, 46, 6, 56, 19, 47, 6, 54, 19, 48, 6, 52, 19, 49, 6, 50, 19, 51, 6, 48, 19, 52, 6, 47, 19, 53, 6, 45, 19, 54, 6, 43, 19, 55, 6, 42, 19, 57, 6, 40, 19, 58, 6, 38, 19, 59, 6, 36, 20, 0, 6, 35, 20, 1, 6, 33, 20, 3, 6, 31, 20, 4, 6, 30, 20, 5, 6, 28, 20, 6, 6, 27, 20, 7, 6, 25, 20, 8, 6, 23, 20, 10, 6, 22, 20, 11, 6, 20, 20, 12, 6, 19, 20, 13, 6, 17, 20, 14, 6, 16, 20, 16, 6, 14, 20, 17, 6, 13, 20, 18, 6, 11, 20, 19 };
logln("Sunrise/Sunset times for Toronto, Canada");
CalendarAstronomer astro = new CalendarAstronomer(-(79 + 25 / 60), 43 + 40 / 60);
// As of ICU4J 2.8 the ICU4J time zones implement pass-through
// to the underlying JDK. Because of variation in the
// underlying JDKs, we have to use a fixed-offset
// SimpleTimeZone to get consistent behavior between JDKs.
// The offset we want is [-18000000, 3600000] (raw, dst).
// [aliu 10/15/03]
// TimeZone tz = TimeZone.getTimeZone("America/Montreal");
TimeZone tz = new SimpleTimeZone(-18000000 + 3600000, "Montreal(FIXED)");
GregorianCalendar cal = new GregorianCalendar(tz, Locale.US);
GregorianCalendar cal2 = new GregorianCalendar(tz, Locale.US);
cal.clear();
cal.set(Calendar.YEAR, 2001);
cal.set(Calendar.MONTH, Calendar.APRIL);
cal.set(Calendar.DAY_OF_MONTH, 1);
// must be near local noon for getSunRiseSet to work
cal.set(Calendar.HOUR_OF_DAY, 12);
DateFormat df = DateFormat.getTimeInstance(cal, DateFormat.MEDIUM, Locale.US);
DateFormat df2 = DateFormat.getDateTimeInstance(cal, DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US);
DateFormat day = DateFormat.getDateInstance(cal, DateFormat.MEDIUM, Locale.US);
for (int i = 0; i < 30; i++) {
astro.setDate(cal.getTime());
Date sunrise = new Date(astro.getSunRiseSet(true));
Date sunset = new Date(astro.getSunRiseSet(false));
cal2.setTime(cal.getTime());
cal2.set(Calendar.SECOND, 0);
cal2.set(Calendar.MILLISECOND, 0);
cal2.set(Calendar.HOUR_OF_DAY, USNO[4 * i + 0]);
cal2.set(Calendar.MINUTE, USNO[4 * i + 1]);
Date exprise = cal2.getTime();
cal2.set(Calendar.HOUR_OF_DAY, USNO[4 * i + 2]);
cal2.set(Calendar.MINUTE, USNO[4 * i + 3]);
Date expset = cal2.getTime();
// Compute delta of what we got to the USNO data, in seconds
int deltarise = Math.abs((int) (sunrise.getTime() - exprise.getTime()) / 1000);
int deltaset = Math.abs((int) (sunset.getTime() - expset.getTime()) / 1000);
// Allow a deviation of 0..MAX_DEV seconds
// It would be nice to get down to 60 seconds, but at this
// point that appears to be impossible without a redo of the
// algorithm using something more advanced than Duffett-Smith.
final int MAX_DEV = 180;
if (deltarise > MAX_DEV || deltaset > MAX_DEV) {
if (deltarise > MAX_DEV) {
errln("FAIL: " + day.format(cal.getTime()) + ", Sunrise: " + df2.format(sunrise) + " (USNO " + df.format(exprise) + " d=" + deltarise + "s)");
} else {
logln(day.format(cal.getTime()) + ", Sunrise: " + df.format(sunrise) + " (USNO " + df.format(exprise) + ")");
}
if (deltaset > MAX_DEV) {
errln("FAIL: " + day.format(cal.getTime()) + ", Sunset: " + df2.format(sunset) + " (USNO " + df.format(expset) + " d=" + deltaset + "s)");
} else {
logln(day.format(cal.getTime()) + ", Sunset: " + df.format(sunset) + " (USNO " + df.format(expset) + ")");
}
} else {
logln(day.format(cal.getTime()) + ", Sunrise: " + df.format(sunrise) + " (USNO " + df.format(exprise) + ")" + ", Sunset: " + df.format(sunset) + " (USNO " + df.format(expset) + ")");
}
cal.add(Calendar.DATE, 1);
}
// CalendarAstronomer a = new CalendarAstronomer(-(71+5/60), 42+37/60);
// cal.clear();
// cal.set(cal.YEAR, 1986);
// cal.set(cal.MONTH, cal.MARCH);
// cal.set(cal.DATE, 10);
// cal.set(cal.YEAR, 1988);
// cal.set(cal.MONTH, cal.JULY);
// cal.set(cal.DATE, 27);
// a.setDate(cal.getTime());
// long r = a.getSunRiseSet2(true);
}
use of android.icu.util.SimpleTimeZone in project j2objc by google.
the class DateFormatRegressionTest method Test4089106.
/**
* @bug 4089106
*/
@Test
public void Test4089106() {
TimeZone def = TimeZone.getDefault();
try {
TimeZone z = new SimpleTimeZone((int) (1.25 * 3600000), "FAKEZONE");
TimeZone.setDefault(z);
// Android patch (http://b/28949992) start.
// ICU TimeZone.setDefault() not supported on Android.
z = TimeZone.getDefault();
// Android patch (http://b/28949992) end.
SimpleDateFormat f = new SimpleDateFormat();
if (!f.getTimeZone().equals(z))
errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
} finally {
TimeZone.setDefault(def);
}
}
use of android.icu.util.SimpleTimeZone in project j2objc by google.
the class TimeZoneRuleTest method TestT6669.
@Test
public void TestT6669() {
// getNext/PreviousTransition implementation in SimpleTimeZone
// used to use a bad condition for detecting if DST is enabled or not.
SimpleTimeZone stz = new SimpleTimeZone(0, "CustomID", Calendar.JANUARY, 1, Calendar.SUNDAY, 0, Calendar.JULY, 1, Calendar.SUNDAY, 0);
// 2008-12-31T00:00:00
long t = 1230681600000L;
// 2009-01-04T00:00:00
long expectedNext = 1231027200000L;
// 2008-07-06T00:00:00
long expectedPrev = 1215298800000L;
TimeZoneTransition tzt = stz.getNextTransition(t, false);
if (tzt == null) {
errln("FAIL: No transition returned by getNextTransition.");
} else if (tzt.getTime() != expectedNext) {
errln("FAIL: Wrong transition time returned by getNextTransition - " + tzt.getTime() + " Expected: " + expectedNext);
}
tzt = stz.getPreviousTransition(t, true);
if (tzt == null) {
errln("FAIL: No transition returned by getPreviousTransition.");
} else if (tzt.getTime() != expectedPrev) {
errln("FAIL: Wrong transition time returned by getPreviousTransition - " + tzt.getTime() + " Expected: " + expectedPrev);
}
}
use of android.icu.util.SimpleTimeZone in project j2objc by google.
the class TimeZoneRuleTest method TestSimpleRuleBasedTimeZone.
/*
* RuleBasedTimeZone test cases
*/
@Test
public void TestSimpleRuleBasedTimeZone() {
SimpleTimeZone stz = new SimpleTimeZone(-1 * HOUR, "TestSTZ", Calendar.SEPTEMBER, -30, -Calendar.SATURDAY, 1 * HOUR, SimpleTimeZone.WALL_TIME, Calendar.FEBRUARY, 2, Calendar.SUNDAY, 1 * HOUR, SimpleTimeZone.WALL_TIME, 1 * HOUR);
DateTimeRule dtr;
AnnualTimeZoneRule atzr;
final int STARTYEAR = 2000;
InitialTimeZoneRule ir = new InitialTimeZoneRule(// Initial time Name
"RBTZ_Initial", // Raw offset
-1 * HOUR, // DST saving amount
1 * HOUR);
// RBTZ
RuleBasedTimeZone rbtz1 = new RuleBasedTimeZone("RBTZ1", ir);
dtr = new DateTimeRule(Calendar.SEPTEMBER, 30, Calendar.SATURDAY, false, 1 * HOUR, // SUN<=30 in September, at 1AM wall time
DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_DST1", -1 * HOUR, /* rawOffset */
1 * HOUR, /* dstSavings */
dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbtz1.addTransitionRule(atzr);
dtr = new DateTimeRule(Calendar.FEBRUARY, 2, Calendar.SUNDAY, 1 * HOUR, // 2nd Sunday in February, at 1AM wall time
DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_STD1", -1 * HOUR, /* rawOffset */
0, /* dstSavings */
dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbtz1.addTransitionRule(atzr);
// Equivalent, but different date rule type
RuleBasedTimeZone rbtz2 = new RuleBasedTimeZone("RBTZ2", ir);
dtr = new DateTimeRule(Calendar.SEPTEMBER, -1, Calendar.SATURDAY, 1 * HOUR, // Last Sunday in September at 1AM wall time
DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_DST2", -1 * HOUR, 1 * HOUR, dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbtz2.addTransitionRule(atzr);
dtr = new DateTimeRule(Calendar.FEBRUARY, 8, Calendar.SUNDAY, true, 1 * HOUR, // SUN>=8 in February, at 1AM wall time
DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_STD2", -1 * HOUR, 0, dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbtz2.addTransitionRule(atzr);
// Equivalent, but different time rule type
RuleBasedTimeZone rbtz3 = new RuleBasedTimeZone("RBTZ3", ir);
dtr = new DateTimeRule(Calendar.SEPTEMBER, 30, Calendar.SATURDAY, false, 2 * HOUR, DateTimeRule.UTC_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_DST3", -1 * HOUR, 1 * HOUR, dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbtz3.addTransitionRule(atzr);
dtr = new DateTimeRule(Calendar.FEBRUARY, 2, Calendar.SUNDAY, 0 * HOUR, DateTimeRule.STANDARD_TIME);
atzr = new AnnualTimeZoneRule("RBTZ_STD3", -1 * HOUR, 0, dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbtz3.addTransitionRule(atzr);
// Check equivalency for 10 years
long start = getUTCMillis(STARTYEAR, Calendar.JANUARY, 1);
long until = getUTCMillis(STARTYEAR + 10, Calendar.JANUARY, 1);
if (!(stz.hasEquivalentTransitions(rbtz1, start, until))) {
errln("FAIL: rbtz1 must be equivalent to the SimpleTimeZone in the time range.");
}
if (!(stz.hasEquivalentTransitions(rbtz2, start, until))) {
errln("FAIL: rbtz2 must be equivalent to the SimpleTimeZone in the time range.");
}
if (!(stz.hasEquivalentTransitions(rbtz3, start, until))) {
errln("FAIL: rbtz3 must be equivalent to the SimpleTimeZone in the time range.");
}
// hasSameRules
if (rbtz1.hasSameRules(rbtz2)) {
errln("FAIL: rbtz1 and rbtz2 have different rules, but returned true.");
}
if (rbtz1.hasSameRules(rbtz3)) {
errln("FAIL: rbtz1 and rbtz3 have different rules, but returned true.");
}
RuleBasedTimeZone rbtz1c = (RuleBasedTimeZone) rbtz1.clone();
if (!rbtz1.hasSameRules(rbtz1c)) {
errln("FAIL: Cloned RuleBasedTimeZone must have the same rules with the original.");
}
// getOffset
GregorianCalendar cal = new GregorianCalendar();
int[] offsets = new int[2];
int offset;
boolean dst;
cal.setTimeZone(rbtz1);
cal.clear();
// Jan 1, 1000 BC
cal.set(Calendar.ERA, GregorianCalendar.BC);
cal.set(1000, Calendar.JANUARY, 1);
offset = rbtz1.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.MILLISECONDS_IN_DAY));
if (offset != 0) {
errln("FAIL: Invalid time zone offset: " + offset + " /expected: 0");
}
dst = rbtz1.inDaylightTime(cal.getTime());
if (!dst) {
errln("FAIL: Invalid daylight saving time");
}
rbtz1.getOffset(cal.getTimeInMillis(), true, offsets);
if (offsets[0] != -3600000) {
errln("FAIL: Invalid time zone raw offset: " + offsets[0] + " /expected: -3600000");
}
if (offsets[1] != 3600000) {
errln("FAIL: Invalid DST amount: " + offsets[1] + " /expected: 3600000");
}
// July 1, 2000, AD
cal.set(Calendar.ERA, GregorianCalendar.AD);
cal.set(2000, Calendar.JULY, 1);
offset = rbtz1.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.MILLISECONDS_IN_DAY));
if (offset != -3600000) {
errln("FAIL: Invalid time zone offset: " + offset + " /expected: -3600000");
}
dst = rbtz1.inDaylightTime(cal.getTime());
if (dst) {
errln("FAIL: Invalid daylight saving time");
}
rbtz1.getOffset(cal.getTimeInMillis(), true, offsets);
if (offsets[0] != -3600000) {
errln("FAIL: Invalid time zone raw offset: " + offsets[0] + " /expected: -3600000");
}
if (offsets[1] != 0) {
errln("FAIL: Invalid DST amount: " + offsets[1] + " /expected: 0");
}
// July 1, 2000, AD
// Try to add 3rd final rule
dtr = new DateTimeRule(Calendar.OCTOBER, 15, 1 * HOUR, DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("3RD_ATZ", -1 * HOUR, 2 * HOUR, dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
boolean bException = false;
try {
rbtz1.addTransitionRule(atzr);
} catch (IllegalStateException ise) {
bException = true;
}
if (!bException) {
errln("FAIL: 3rd final rule must be rejected");
}
// Try to add an initial rule
bException = false;
try {
rbtz1.addTransitionRule(new InitialTimeZoneRule("Test Initial", 2 * HOUR, 0));
} catch (IllegalArgumentException iae) {
bException = true;
}
if (!bException) {
errln("FAIL: InitialTimeZoneRule must be rejected");
}
}
Aggregations