use of android.icu.util.VTimeZone in project j2objc by google.
the class TimeZoneRuleTest method TestVTimeZoneRoundTrip.
/*
* Write out time zone rules of OlsonTimeZone into VTIMEZONE format, create a new
* VTimeZone from the VTIMEZONE data, then compare transitions
*/
@Test
public void TestVTimeZoneRoundTrip() {
long startTime = getUTCMillis(1850, Calendar.JANUARY, 1);
long endTime = getUTCMillis(2050, Calendar.JANUARY, 1);
String[] tzids = getTestZIDs();
for (int i = 0; i < tzids.length; i++) {
BasicTimeZone olsontz = (BasicTimeZone) TimeZone.getTimeZone(tzids[i], TimeZone.TIMEZONE_ICU);
VTimeZone vtz_org = VTimeZone.create(tzids[i]);
vtz_org.setTZURL("http://source.icu-project.org/timezone");
vtz_org.setLastModified(new Date());
VTimeZone vtz_new = null;
try {
// Write out VTIMEZONE
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(baos);
vtz_org.write(writer);
writer.close();
byte[] vtzdata = baos.toByteArray();
// Read VTIMEZONE
ByteArrayInputStream bais = new ByteArrayInputStream(vtzdata);
InputStreamReader reader = new InputStreamReader(bais);
vtz_new = VTimeZone.create(reader);
reader.close();
// Write out VTIMEZONE one more time
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
OutputStreamWriter writer1 = new OutputStreamWriter(baos1);
vtz_new.write(writer1);
writer1.close();
byte[] vtzdata1 = baos1.toByteArray();
// Make sure VTIMEZONE data is exactly same with the first one
if (vtzdata.length != vtzdata1.length) {
errln("FAIL: different VTIMEZONE data length");
}
for (int j = 0; j < vtzdata.length; j++) {
if (vtzdata[j] != vtzdata1[j]) {
errln("FAIL: different VTIMEZONE data");
break;
}
}
} catch (IOException ioe) {
errln("FAIL: IO error while writing/reading VTIMEZONE data");
}
// VTIMEZONE.
if (vtz_new.getOffset(startTime) != olsontz.getOffset(startTime)) {
errln("FAIL: VTimeZone for " + tzids[i] + " is not equivalent to its OlsonTimeZone corresponding at " + startTime);
}
TimeZoneTransition tzt = olsontz.getNextTransition(startTime, false);
if (tzt != null) {
if (!vtz_new.hasEquivalentTransitions(olsontz, tzt.getTime(), endTime, true)) {
int maxDelta = 1000;
if (!hasEquivalentTransitions(vtz_new, olsontz, tzt.getTime() + maxDelta, endTime, true, maxDelta)) {
errln("FAIL: VTimeZone for " + tzids[i] + " is not equivalent to its OlsonTimeZone corresponding.");
} else {
logln("VTimeZone for " + tzids[i] + " differs from its OlsonTimeZone corresponding with maximum transition time delta - " + maxDelta);
}
}
if (!vtz_new.hasEquivalentTransitions(olsontz, tzt.getTime(), endTime, false)) {
logln("VTimeZone for " + tzids[i] + " is not equivalent to its OlsonTimeZone corresponding in strict comparison mode.");
}
}
}
}
use of android.icu.util.VTimeZone in project j2objc by google.
the class TimeZoneTest method checkFrozen.
private void checkFrozen(TimeZone[] frozenZones, String zaName) {
for (int i = 0; i < frozenZones.length; i++) {
if (!frozenZones[i].isFrozen()) {
errln("Fail: " + zaName + "[" + i + "] is not frozen.");
}
// clone
TimeZone copy = (TimeZone) frozenZones[i].clone();
if (frozenZones[i] != copy) {
errln("Fail: " + zaName + "[" + i + "] - clone does not return the object itself.");
}
// cloneAsThawed
TimeZone thawed = (TimeZone) frozenZones[i].cloneAsThawed();
if (thawed.isFrozen() || !frozenZones[i].equals(thawed)) {
errln("Fail: " + zaName + "[" + i + "] - cloneAsThawed does not work.");
}
// setID
try {
String newID = "foo";
frozenZones[i].setID(newID);
errln("Fail: " + zaName + "[" + i + "] - setID must throw UnsupportedOperationException.");
} catch (UnsupportedOperationException e) {
// OK
}
// setRawOffset
if (!(frozenZones[i] instanceof RuleBasedTimeZone)) {
// RuleBasedTimeZone does not supprot setRawOffset
try {
int newOffset = -3600000;
frozenZones[i].setRawOffset(newOffset);
errln("Fail: " + zaName + "[" + i + "] - setRawOffset must throw UnsupportedOperationException.");
} catch (UnsupportedOperationException e) {
// OK
}
}
if (frozenZones[i] instanceof SimpleTimeZone) {
SimpleTimeZone stz = (SimpleTimeZone) frozenZones[i];
// setDSTSavings
try {
int newDSTSavings = 1800000;
stz.setDSTSavings(newDSTSavings);
errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setDSTSavings must throw UnsupportedOperationException.");
} catch (UnsupportedOperationException e) {
// OK
}
// setStartRule
try {
stz.setStartRule(Calendar.JANUARY, -1, Calendar.SUNDAY, 0);
errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartRule must throw UnsupportedOperationException.");
} catch (UnsupportedOperationException e) {
// OK
}
// setEndRule
try {
stz.setEndRule(Calendar.DECEMBER, 1, Calendar.SUNDAY, 0);
errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setEndRule must throw UnsupportedOperationException.");
} catch (UnsupportedOperationException e) {
// OK
}
// setStartYear
try {
stz.setStartYear(2000);
errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartYear must throw UnsupportedOperationException.");
} catch (UnsupportedOperationException e) {
// OK
}
} else if (frozenZones[i] instanceof RuleBasedTimeZone) {
RuleBasedTimeZone rbtz = (RuleBasedTimeZone) frozenZones[i];
// addTransitionRule
try {
TimeArrayTimeZoneRule tr1 = new TimeArrayTimeZoneRule("tr1", 7200000, 0, new long[] { 0 }, DateTimeRule.UTC_TIME);
rbtz.addTransitionRule(tr1);
errln("Fail: (RuleBasedTimeZone)" + zaName + "[" + i + "] - addTransitionRule must throw UnsupportedOperationException.");
} catch (UnsupportedOperationException e) {
// OK
}
} else if (frozenZones[i] instanceof VTimeZone) {
VTimeZone vtz = (VTimeZone) frozenZones[i];
// setTZURL
try {
String tzUrl = "http://icu-project.org/timezone";
vtz.setTZURL(tzUrl);
errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setTZURL must throw UnsupportedOperationException.");
} catch (UnsupportedOperationException e) {
// OK
}
// setLastModified
try {
Date d = new Date();
vtz.setLastModified(d);
errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setLastModified must throw UnsupportedOperationException.");
} catch (UnsupportedOperationException e) {
// OK
}
}
}
}
use of android.icu.util.VTimeZone in project j2objc by google.
the class TimeZoneTest method checkThawed.
private void checkThawed(TimeZone[] thawedZones, String zaName) {
for (int i = 0; i < thawedZones.length; i++) {
if (thawedZones[i].isFrozen()) {
errln("Fail: " + zaName + "[" + i + "] is frozen.");
}
// clone
TimeZone copy = (TimeZone) thawedZones[i].clone();
if (thawedZones[i] == copy || !thawedZones[i].equals(copy)) {
errln("Fail: " + zaName + "[" + i + "] - clone does not work.");
}
// cloneAsThawed
TimeZone thawed = (TimeZone) thawedZones[i].cloneAsThawed();
if (thawed.isFrozen() || !thawedZones[i].equals(thawed)) {
errln("Fail: " + zaName + "[" + i + "] - cloneAsThawed does not work.");
}
// setID
try {
String newID = "foo";
thawedZones[i].setID(newID);
if (!thawedZones[i].getID().equals(newID)) {
errln("Fail: " + zaName + "[" + i + "] - setID(\"" + newID + "\") does not work.");
}
} catch (UnsupportedOperationException e) {
errln("Fail: " + zaName + "[" + i + "] - setID throws UnsupportedOperationException.");
}
// setRawOffset
// J2ObjC: RuleBasedTimeZone and NativeTimeZone do not support setRawOffset.
boolean isNativeTimeZone = Optional.of(thawedZones[i]).filter(JavaTimeZone.class::isInstance).map(JavaTimeZone.class::cast).map(JavaTimeZone::unwrap).filter(NativeTimeZone.class::isInstance).isPresent();
if (!(thawedZones[i] instanceof RuleBasedTimeZone) && !isNativeTimeZone) {
try {
int newOffset = -3600000;
thawedZones[i].setRawOffset(newOffset);
if (thawedZones[i].getRawOffset() != newOffset) {
errln("Fail: " + zaName + "[" + i + "] - setRawOffset(" + newOffset + ") does not work.");
}
} catch (UnsupportedOperationException e) {
errln("Fail: " + zaName + "[" + i + "] - setRawOffset throws UnsupportedOperationException.");
}
}
if (thawedZones[i] instanceof SimpleTimeZone) {
SimpleTimeZone stz = (SimpleTimeZone) thawedZones[i];
// setDSTSavings
try {
int newDSTSavings = 1800000;
stz.setDSTSavings(newDSTSavings);
if (stz.getDSTSavings() != newDSTSavings) {
errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setDSTSavings(" + newDSTSavings + ") does not work.");
}
} catch (UnsupportedOperationException e) {
errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setDSTSavings throws UnsupportedOperationException.");
}
// setStartRule
try {
stz.setStartRule(Calendar.JANUARY, -1, Calendar.SUNDAY, 0);
} catch (UnsupportedOperationException e) {
errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartRule throws UnsupportedOperationException.");
}
// setEndRule
try {
stz.setEndRule(Calendar.DECEMBER, 1, Calendar.SUNDAY, 0);
} catch (UnsupportedOperationException e) {
errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setEndRule throws UnsupportedOperationException.");
}
// setStartYear
try {
stz.setStartYear(2000);
} catch (UnsupportedOperationException e) {
errln("Fail: (SimpleTimeZone)" + zaName + "[" + i + "] - setStartYear throws UnsupportedOperationException.");
}
} else if (thawedZones[i] instanceof RuleBasedTimeZone) {
RuleBasedTimeZone rbtz = (RuleBasedTimeZone) thawedZones[i];
// addTransitionRule
try {
TimeArrayTimeZoneRule tr1 = new TimeArrayTimeZoneRule("tr1", 7200000, 0, new long[] { 0 }, DateTimeRule.UTC_TIME);
rbtz.addTransitionRule(tr1);
} catch (UnsupportedOperationException e) {
errln("Fail: (RuleBasedTimeZone)" + zaName + "[" + i + "] - addTransitionRule throws UnsupportedOperationException.");
}
} else if (thawedZones[i] instanceof VTimeZone) {
VTimeZone vtz = (VTimeZone) thawedZones[i];
// setTZURL
try {
String tzUrl = "http://icu-project.org/timezone";
vtz.setTZURL(tzUrl);
if (!vtz.getTZURL().equals(tzUrl)) {
errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setTZURL does not work.");
}
} catch (UnsupportedOperationException e) {
errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setTZURL throws UnsupportedOperationException.");
}
// setLastModified
try {
Date d = new Date();
vtz.setLastModified(d);
if (!vtz.getLastModified().equals(d)) {
errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setLastModified does not work.");
}
} catch (UnsupportedOperationException e) {
errln("Fail: (VTimeZone)" + zaName + "[" + i + "] - setLastModified throws UnsupportedOperationException.");
}
}
}
}
use of android.icu.util.VTimeZone in project j2objc by google.
the class TimeZoneRuleTest method TestVTimeZoneSimpleWrite.
/*
* Write out simple time zone rules from an OlsonTimeZone at various time into VTIMEZONE
* format and create a new VTimeZone from the VTIMEZONE data, then make sure the raw offset
* and DST savings are same in these two time zones.
*/
@Test
public void TestVTimeZoneSimpleWrite() {
long[] testTimes = new long[] { getUTCMillis(2006, Calendar.JANUARY, 1), getUTCMillis(2006, Calendar.MARCH, 15), getUTCMillis(2006, Calendar.MARCH, 31), getUTCMillis(2006, Calendar.APRIL, 5), getUTCMillis(2006, Calendar.OCTOBER, 25), getUTCMillis(2006, Calendar.NOVEMBER, 1), getUTCMillis(2006, Calendar.NOVEMBER, 5), getUTCMillis(2007, Calendar.JANUARY, 1) };
String[] tzids = getTestZIDs();
for (int n = 0; n < testTimes.length; n++) {
long time = testTimes[n];
int[] offsets1 = new int[2];
int[] offsets2 = new int[2];
for (int i = 0; i < tzids.length; i++) {
VTimeZone vtz_org = VTimeZone.create(tzids[i]);
VTimeZone vtz_new = null;
try {
// Write out VTIMEZONE
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(baos);
vtz_org.writeSimple(writer, time);
writer.close();
byte[] vtzdata = baos.toByteArray();
// Read VTIMEZONE
ByteArrayInputStream bais = new ByteArrayInputStream(vtzdata);
InputStreamReader reader = new InputStreamReader(bais);
vtz_new = VTimeZone.create(reader);
reader.close();
} catch (IOException ioe) {
errln("FAIL: IO error while writing/reading VTIMEZONE data");
}
// Check equivalency
vtz_org.getOffset(time, false, offsets1);
vtz_new.getOffset(time, false, offsets2);
if (offsets1[0] != offsets2[0] || offsets1[1] != offsets2[1]) {
errln("FAIL: VTimeZone writeSimple for " + tzids[i] + " at time " + time + " failed to the round trip.");
}
}
}
}
use of android.icu.util.VTimeZone in project j2objc by google.
the class TimeZoneRuleTest method TestT6216.
@Test
public void TestT6216() {
// Test case in #6216
String tokyoTZ = "BEGIN:VCALENDAR\r\n" + "VERSION:2.0\r\n" + "PRODID:-//PYVOBJECT//NONSGML Version 1//EN\r\n" + "BEGIN:VTIMEZONE\r\n" + "TZID:Asia/Tokyo\r\n" + "BEGIN:STANDARD\r\n" + "DTSTART:20000101T000000\r\n" + "RRULE:FREQ=YEARLY;BYMONTH=1\r\n" + "TZNAME:Asia/Tokyo\r\n" + "TZOFFSETFROM:+0900\r\n" + "TZOFFSETTO:+0900\r\n" + "END:STANDARD\r\n" + "END:VTIMEZONE\r\n" + "END:VCALENDAR";
// Single final rule, overlapping with another
String finalOverlap = "BEGIN:VCALENDAR\r\n" + "BEGIN:VTIMEZONE\r\n" + "TZID:FinalOverlap\r\n" + "BEGIN:STANDARD\r\n" + "TZOFFSETFROM:-0200\r\n" + "TZOFFSETTO:-0300\r\n" + "TZNAME:STD\r\n" + "DTSTART:20001029T020000\r\n" + "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10\r\n" + "END:STANDARD\r\n" + "BEGIN:DAYLIGHT\r\n" + "TZOFFSETFROM:-0300\r\n" + "TZOFFSETTO:-0200\r\n" + "TZNAME:DST\r\n" + "DTSTART:19990404T020000\r\n" + "RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4;UNTIL=20050403T040000Z\r\n" + "END:DAYLIGHT\r\n" + "END:VTIMEZONE\r\n" + "END:VCALENDAR";
// Single final rule, no overlapping with another
String finalNonOverlap = "BEGIN:VCALENDAR\r\n" + "BEGIN:VTIMEZONE\r\n" + "TZID:FinalNonOverlap\r\n" + "BEGIN:STANDARD\r\n" + "TZOFFSETFROM:-0200\r\n" + "TZOFFSETTO:-0300\r\n" + "TZNAME:STD\r\n" + "DTSTART:20001029T020000\r\n" + "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10;UNTIL=20041031T040000Z\r\n" + "END:STANDARD\r\n" + "BEGIN:DAYLIGHT\r\n" + "TZOFFSETFROM:-0300\r\n" + "TZOFFSETTO:-0200\r\n" + "TZNAME:DST\r\n" + "DTSTART:19990404T020000\r\n" + "RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4;UNTIL=20050403T040000Z\r\n" + "END:DAYLIGHT\r\n" + "BEGIN:STANDARD\r\n" + "TZOFFSETFROM:-0200\r\n" + "TZOFFSETTO:-0300\r\n" + "TZNAME:STDFINAL\r\n" + "DTSTART:20071028T020000\r\n" + "RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10\r\n" + "END:STANDARD\r\n" + "END:VTIMEZONE\r\n" + "END:VCALENDAR";
int[][] TestDates = { { 1995, Calendar.JANUARY, 1 }, { 1995, Calendar.JULY, 1 }, { 2000, Calendar.JANUARY, 1 }, { 2000, Calendar.JULY, 1 }, { 2005, Calendar.JANUARY, 1 }, { 2005, Calendar.JULY, 1 }, { 2010, Calendar.JANUARY, 1 }, { 2010, Calendar.JULY, 1 } };
String[] TestZones = { tokyoTZ, finalOverlap, finalNonOverlap };
int[][] Expected = { // JAN90 JUL90 JAN00 JUL00 JAN05 JUL05 JAN10 JUL10
{ 32400000, 32400000, 32400000, 32400000, 32400000, 32400000, 32400000, 32400000 }, { -10800000, -10800000, -7200000, -7200000, -10800000, -7200000, -10800000, -10800000 }, { -10800000, -10800000, -7200000, -7200000, -10800000, -7200000, -10800000, -10800000 } };
// Get test times
long[] times = new long[TestDates.length];
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("Etc/GMT"));
for (int i = 0; i < TestDates.length; i++) {
cal.clear();
cal.set(TestDates[i][0], TestDates[i][1], TestDates[i][2]);
times[i] = cal.getTimeInMillis();
}
for (int i = 0; i < TestZones.length; i++) {
try {
VTimeZone vtz = VTimeZone.create(new StringReader(TestZones[i]));
for (int j = 0; j < times.length; j++) {
int offset = vtz.getOffset(times[j]);
if (offset != Expected[i][j]) {
errln("FAIL: Invalid offset at time(" + times[j] + "):" + offset + " Expected:" + Expected[i][j]);
}
}
} catch (Exception e) {
errln("FAIL: Failed to calculate the offset for VTIMEZONE data " + i);
}
}
}
Aggregations