use of android.icu.util.Calendar in project j2objc by google.
the class TimeZoneTest method TestVariousAPI518.
// Test various calls
@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");
}
use of android.icu.util.Calendar in project j2objc by google.
the class TimeZoneTest method TestGetOffsetDate.
@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);
}
}
use of android.icu.util.Calendar in project j2objc by google.
the class TimeZoneOffsetLocalTest method TestGetOffsetAroundTransition.
/*
* Testing getOffset APIs around rule transition by local standard/wall time.
*/
@Test
public void TestGetOffsetAroundTransition() {
final int HOUR = 60 * 60 * 1000;
final int MINUTE = 60 * 1000;
int[][] DATES = { { 2006, Calendar.APRIL, 2, 1, 30, 1 * HOUR + 30 * MINUTE }, { 2006, Calendar.APRIL, 2, 2, 00, 2 * HOUR }, { 2006, Calendar.APRIL, 2, 2, 30, 2 * HOUR + 30 * MINUTE }, { 2006, Calendar.APRIL, 2, 3, 00, 3 * HOUR }, { 2006, Calendar.APRIL, 2, 3, 30, 3 * HOUR + 30 * MINUTE }, { 2006, Calendar.OCTOBER, 29, 0, 30, 0 * HOUR + 30 * MINUTE }, { 2006, Calendar.OCTOBER, 29, 1, 00, 1 * HOUR }, { 2006, Calendar.OCTOBER, 29, 1, 30, 1 * HOUR + 30 * MINUTE }, { 2006, Calendar.OCTOBER, 29, 2, 00, 2 * HOUR }, { 2006, Calendar.OCTOBER, 29, 2, 30, 2 * HOUR + 30 * MINUTE } };
// Expected offsets by getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds)
int[] OFFSETS1 = { // April 2, 2006
-8 * HOUR, -7 * HOUR, -7 * HOUR, -7 * HOUR, -7 * HOUR, // October 29, 2006
-7 * HOUR, -8 * HOUR, -8 * HOUR, -8 * HOUR, -8 * HOUR };
// Expected offsets by getOffset(long time, boolean local, int[] offsets) with local = true
// or getOffsetFromLocal(long time, int nonExistingTimeOpt, int duplicatedTimeOpt, int[] offsets)
// with nonExistingTimeOpt = LOCAL_STD/duplicatedTimeOpt = LOCAL_STD
int[][] OFFSETS2 = { // April 2, 2006
{ -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, // Oct 29, 2006
{ -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 } };
// Expected offsets by getOffsetFromLocal(long time, int nonExistingTimeOpt, int duplicatedTimeOpt, int[] offsets)
// with nonExistingTimeOpt = LOCAL_DST/duplicatedTimeOpt = LOCAL_DST
int[][] OFFSETS3 = { // April 2, 2006
{ -8 * HOUR, 0 }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, // October 29, 2006
{ -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 } };
int[] offsets = new int[2];
TimeZone utc = TimeZone.getTimeZone("UTC");
Calendar cal = Calendar.getInstance(utc);
cal.clear();
// Set up TimeZone objects - OlsonTimeZone, SimpleTimeZone and RuleBasedTimeZone
BasicTimeZone[] TESTZONES = new BasicTimeZone[3];
TESTZONES[0] = (BasicTimeZone) TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU);
TESTZONES[1] = new SimpleTimeZone(-8 * HOUR, "Simple Pacific Time", Calendar.APRIL, 1, Calendar.SUNDAY, 2 * HOUR, Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * HOUR);
InitialTimeZoneRule ir = new InitialTimeZoneRule(// Initial time Name
"Pacific Standard Time", // Raw offset
-8 * HOUR, // DST saving amount
0 * HOUR);
RuleBasedTimeZone rbPT = new RuleBasedTimeZone("Rule based Pacific Time", ir);
DateTimeRule dtr;
AnnualTimeZoneRule atzr;
final int STARTYEAR = 2000;
dtr = new DateTimeRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * HOUR, // 1st Sunday in April, at 2AM wall time
DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("Pacific Daylight Time", -8 * HOUR, /* rawOffset */
1 * HOUR, /* dstSavings */
dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbPT.addTransitionRule(atzr);
dtr = new DateTimeRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * HOUR, // last Sunday in October, at 2AM wall time
DateTimeRule.WALL_TIME);
atzr = new AnnualTimeZoneRule("Pacific Standard Time", -8 * HOUR, /* rawOffset */
0, /* dstSavings */
dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
rbPT.addTransitionRule(atzr);
TESTZONES[2] = rbPT;
// Calculate millis
long[] MILLIS = new long[DATES.length];
for (int i = 0; i < DATES.length; i++) {
cal.clear();
cal.set(DATES[i][0], DATES[i][1], DATES[i][2], DATES[i][3], DATES[i][4]);
MILLIS[i] = cal.getTimeInMillis();
}
DateFormat df = DateFormat.getInstance();
df.setTimeZone(utc);
// Test getOffset(int era, int year, int month, int day, int dayOfWeek, int millis)
for (int i = 0; i < TESTZONES.length; i++) {
for (int d = 0; d < DATES.length; d++) {
int offset = TESTZONES[i].getOffset(GregorianCalendar.AD, DATES[d][0], DATES[d][1], DATES[d][2], Calendar.SUNDAY, DATES[d][5]);
if (offset != OFFSETS1[d]) {
errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[d])) + "(standard) - Got: " + offset + " Expected: " + OFFSETS1[d]);
}
}
}
// Test getOffset(long time, boolean local, int[] offsets) with local=true
for (int i = 0; i < TESTZONES.length; i++) {
for (int m = 0; m < MILLIS.length; m++) {
TESTZONES[i].getOffset(MILLIS[m], true, offsets);
if (offsets[0] != OFFSETS2[m][0] || offsets[1] != OFFSETS2[m][1]) {
errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS2[m][0] + "/" + OFFSETS2[m][1]);
}
}
}
// with nonExistingTimeOpt = LOCAL_STD/duplicatedTimeOpt = LOCAL_STD
for (int i = 0; i < TESTZONES.length; i++) {
for (int m = 0; m < MILLIS.length; m++) {
TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_STD, BasicTimeZone.LOCAL_STD, offsets);
if (offsets[0] != OFFSETS2[m][0] || offsets[1] != OFFSETS2[m][1]) {
errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/STD/STD) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS2[m][0] + "/" + OFFSETS2[m][1]);
}
}
}
// with nonExistingTimeOpt = LOCAL_DST/duplicatedTimeOpt = LOCAL_DST
for (int i = 0; i < TESTZONES.length; i++) {
for (int m = 0; m < MILLIS.length; m++) {
TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_DST, BasicTimeZone.LOCAL_DST, offsets);
if (offsets[0] != OFFSETS3[m][0] || offsets[1] != OFFSETS3[m][1]) {
errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/DST/DST) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS3[m][0] + "/" + OFFSETS3[m][1]);
}
}
}
// with nonExistingTimeOpt = LOCAL_FORMER/duplicatedTimeOpt = LOCAL_LATTER
for (int i = 0; i < TESTZONES.length; i++) {
for (int m = 0; m < MILLIS.length; m++) {
TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_FORMER, BasicTimeZone.LOCAL_LATTER, offsets);
if (offsets[0] != OFFSETS2[m][0] || offsets[1] != OFFSETS2[m][1]) {
errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/FORMER/LATTER) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS2[m][0] + "/" + OFFSETS2[m][1]);
}
}
}
// with nonExistingTimeOpt = LOCAL_LATTER/duplicatedTimeOpt = LOCAL_FORMER
for (int i = 0; i < TESTZONES.length; i++) {
for (int m = 0; m < MILLIS.length; m++) {
TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_LATTER, BasicTimeZone.LOCAL_FORMER, offsets);
if (offsets[0] != OFFSETS3[m][0] || offsets[1] != OFFSETS3[m][1]) {
errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/LATTER/FORMER) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS3[m][0] + "/" + OFFSETS3[m][1]);
}
}
}
}
use of android.icu.util.Calendar in project j2objc by google.
the class RelativeDateFormat method dayDifference.
/**
* @return the number of days in "until-now"
*/
private static int dayDifference(Calendar until) {
Calendar nowCal = (Calendar) until.clone();
Date nowDate = new Date(System.currentTimeMillis());
nowCal.clear();
nowCal.setTime(nowDate);
int dayDiff = until.get(Calendar.JULIAN_DAY) - nowCal.get(Calendar.JULIAN_DAY);
return dayDiff;
}
use of android.icu.util.Calendar in project j2objc by google.
the class IBMCalendarTest method TestWeekShift.
/**
* setFirstDayOfWeek and setMinimalDaysInFirstWeek may change the
* field <=> time mapping, since they affect the interpretation of
* the WEEK_OF_MONTH or WEEK_OF_YEAR fields.
*/
@Test
public void TestWeekShift() {
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles"), new Locale("en", "US"));
// Wed Aug 08 01:00:00 PDT 2001
cal.setTime(new Date(997257600000L));
// 26 27 28 29 30 31
for (int pass = 0; pass < 2; ++pass) {
if (pass == 0) {
cal.setFirstDayOfWeek(Calendar.WEDNESDAY);
cal.setMinimalDaysInFirstWeek(4);
} else {
cal.setFirstDayOfWeek(Calendar.SUNDAY);
cal.setMinimalDaysInFirstWeek(4);
}
// Force recalc
cal.add(Calendar.DATE, 1);
cal.add(Calendar.DATE, -1);
// Get time -- should not change
Date time1 = cal.getTime();
// calendar should do so automatically.
if (pass == 0) {
cal.setFirstDayOfWeek(Calendar.THURSDAY);
} else {
cal.setMinimalDaysInFirstWeek(5);
}
int woy1 = cal.get(Calendar.WEEK_OF_YEAR);
int wom1 = cal.get(Calendar.WEEK_OF_MONTH);
// Force recalc
cal.add(Calendar.DATE, 1);
cal.add(Calendar.DATE, -1);
int woy2 = cal.get(Calendar.WEEK_OF_YEAR);
int wom2 = cal.get(Calendar.WEEK_OF_MONTH);
Date time2 = cal.getTime();
if (!time1.equals(time2)) {
errln("FAIL: shifting week should not alter time");
} else {
logln(time1.toString());
}
if (woy1 == woy2 && wom1 == wom2) {
logln("Ok: WEEK_OF_YEAR: " + woy1 + ", WEEK_OF_MONTH: " + wom1);
} else {
errln("FAIL: WEEK_OF_YEAR: " + woy1 + " => " + woy2 + ", WEEK_OF_MONTH: " + wom1 + " => " + wom2 + " after week shift");
}
}
}
Aggregations