use of android.icu.util.TimeZoneRule in project j2objc by google.
the class TimeZoneRuleTest method TestRBTZTransition.
/*
* Check if an OlsonTimeZone and its equivalent RBTZ have the exact same
* transitions.
*/
@Test
public void TestRBTZTransition() {
int[] STARTYEARS = { 1950, 1975, 2000, 2010 };
String[] zids = getTestZIDs();
for (int i = 0; i < zids.length; i++) {
TimeZone tz = TimeZone.getTimeZone(zids[i], TimeZone.TIMEZONE_ICU);
if (tz == null) {
break;
}
for (int j = 0; j < STARTYEARS.length; j++) {
long startTime = getUTCMillis(STARTYEARS[j], Calendar.JANUARY, 1);
TimeZoneRule[] rules = ((BasicTimeZone) tz).getTimeZoneRules(startTime);
RuleBasedTimeZone rbtz = new RuleBasedTimeZone(tz.getID() + "(RBTZ)", (InitialTimeZoneRule) rules[0]);
for (int k = 1; k < rules.length; k++) {
rbtz.addTransitionRule(rules[k]);
}
// Compare the original OlsonTimeZone with the RBTZ starting the startTime for 20 years
long until = getUTCMillis(STARTYEARS[j] + 20, Calendar.JANUARY, 1);
// Ascending
compareTransitionsAscending(tz, rbtz, startTime, until, false);
// Ascending/inclusive
compareTransitionsAscending(tz, rbtz, startTime + 1, until, true);
// Descending
compareTransitionsDescending(tz, rbtz, startTime, until, false);
// Descending/inclusive
compareTransitionsDescending(tz, rbtz, startTime + 1, until, true);
}
}
}
use of android.icu.util.TimeZoneRule in project j2objc by google.
the class OlsonTimeZone method initTransitionRules.
private synchronized void initTransitionRules() {
if (transitionRulesInitialized) {
return;
}
initialRule = null;
firstTZTransition = null;
firstFinalTZTransition = null;
historicRules = null;
firstTZTransitionIdx = 0;
finalZoneWithStartYear = null;
String stdName = getID() + "(STD)";
String dstName = getID() + "(DST)";
int raw, dst;
// Create initial rule
raw = initialRawOffset() * Grego.MILLIS_PER_SECOND;
dst = initialDstOffset() * Grego.MILLIS_PER_SECOND;
initialRule = new InitialTimeZoneRule((dst == 0 ? stdName : dstName), raw, dst);
if (transitionCount > 0) {
int transitionIdx, typeIdx;
// For now, keeping this code for just in case. Feb 19, 2010 Yoshito
for (transitionIdx = 0; transitionIdx < transitionCount; transitionIdx++) {
if (getInt(typeMapData[transitionIdx]) != 0) {
// type 0 is the initial type
break;
}
firstTZTransitionIdx++;
}
if (transitionIdx == transitionCount) {
// Actually no transitions...
} else {
// Build historic rule array
long[] times = new long[transitionCount];
for (typeIdx = 0; typeIdx < typeCount; typeIdx++) {
// Gather all start times for each pair of offsets
int nTimes = 0;
for (transitionIdx = firstTZTransitionIdx; transitionIdx < transitionCount; transitionIdx++) {
if (typeIdx == getInt(typeMapData[transitionIdx])) {
long tt = transitionTimes64[transitionIdx] * Grego.MILLIS_PER_SECOND;
if (tt < finalStartMillis) {
// Exclude transitions after finalMillis
times[nTimes++] = tt;
}
}
}
if (nTimes > 0) {
long[] startTimes = new long[nTimes];
System.arraycopy(times, 0, startTimes, 0, nTimes);
// Create a TimeArrayTimeZoneRule
raw = typeOffsets[typeIdx * 2] * Grego.MILLIS_PER_SECOND;
dst = typeOffsets[typeIdx * 2 + 1] * Grego.MILLIS_PER_SECOND;
if (historicRules == null) {
historicRules = new TimeArrayTimeZoneRule[typeCount];
}
historicRules[typeIdx] = new TimeArrayTimeZoneRule((dst == 0 ? stdName : dstName), raw, dst, startTimes, DateTimeRule.UTC_TIME);
}
}
// Create initial transition
typeIdx = getInt(typeMapData[firstTZTransitionIdx]);
firstTZTransition = new TimeZoneTransition(transitionTimes64[firstTZTransitionIdx] * Grego.MILLIS_PER_SECOND, initialRule, historicRules[typeIdx]);
}
}
if (finalZone != null) {
// Get the first occurrence of final rule starts
long startTime = (long) finalStartMillis;
TimeZoneRule firstFinalRule;
if (finalZone.useDaylightTime()) {
/*
* Note: When an OlsonTimeZone is constructed, we should set the final year
* as the start year of finalZone. However, the boundary condition used for
* getting offset from finalZone has some problems. So setting the start year
* in the finalZone will cause a problem. For now, we do not set the valid
* start year when the construction time and create a clone and set the
* start year when extracting rules.
*/
finalZoneWithStartYear = (SimpleTimeZone) finalZone.clone();
finalZoneWithStartYear.setStartYear(finalStartYear);
TimeZoneTransition tzt = finalZoneWithStartYear.getNextTransition(startTime, false);
firstFinalRule = tzt.getTo();
startTime = tzt.getTime();
} else {
finalZoneWithStartYear = finalZone;
firstFinalRule = new TimeArrayTimeZoneRule(finalZone.getID(), finalZone.getRawOffset(), 0, new long[] { startTime }, DateTimeRule.UTC_TIME);
}
TimeZoneRule prevRule = null;
if (transitionCount > 0) {
prevRule = historicRules[getInt(typeMapData[transitionCount - 1])];
}
if (prevRule == null) {
// No historic transitions, but only finalZone available
prevRule = initialRule;
}
firstFinalTZTransition = new TimeZoneTransition(startTime, prevRule, firstFinalRule);
}
transitionRulesInitialized = true;
}
use of android.icu.util.TimeZoneRule in project j2objc by google.
the class OlsonTimeZone method getTimeZoneRules.
/* (non-Javadoc)
* @see android.icu.util.BasicTimeZone#getTimeZoneRules()
*/
@Override
public TimeZoneRule[] getTimeZoneRules() {
initTransitionRules();
int size = 1;
if (historicRules != null) {
// includes non transition data.
for (int i = 0; i < historicRules.length; i++) {
if (historicRules[i] != null) {
size++;
}
}
}
if (finalZone != null) {
if (finalZone.useDaylightTime()) {
size += 2;
} else {
size++;
}
}
TimeZoneRule[] rules = new TimeZoneRule[size];
int idx = 0;
rules[idx++] = initialRule;
if (historicRules != null) {
for (int i = 0; i < historicRules.length; i++) {
if (historicRules[i] != null) {
rules[idx++] = historicRules[i];
}
}
}
if (finalZone != null) {
if (finalZone.useDaylightTime()) {
TimeZoneRule[] stzr = finalZoneWithStartYear.getTimeZoneRules();
// Adding only transition rules
rules[idx++] = stzr[1];
rules[idx++] = stzr[2];
} else {
// Create a TimeArrayTimeZoneRule at finalMillis
rules[idx++] = new TimeArrayTimeZoneRule(getID() + "(STD)", finalZone.getRawOffset(), 0, new long[] { (long) finalStartMillis }, DateTimeRule.UTC_TIME);
}
}
return rules;
}
use of android.icu.util.TimeZoneRule in project j2objc by google.
the class OlsonTimeZone method setRawOffset.
/* (non-Javadoc)
* @see android.icu.util.TimeZone#setRawOffset(int)
*/
@Override
public void setRawOffset(int offsetMillis) {
if (isFrozen()) {
throw new UnsupportedOperationException("Attempt to modify a frozen OlsonTimeZone instance.");
}
if (getRawOffset() == offsetMillis) {
return;
}
long current = System.currentTimeMillis();
if (current < finalStartMillis) {
SimpleTimeZone stz = new SimpleTimeZone(offsetMillis, getID());
boolean bDst = useDaylightTime();
if (bDst) {
TimeZoneRule[] currentRules = getSimpleTimeZoneRulesNear(current);
if (currentRules.length != 3) {
// DST was observed at the beginning of this year, so useDaylightTime
// returned true. getSimpleTimeZoneRulesNear requires at least one
// future transition for making a pair of rules. This implementation
// rolls back the time before the latest offset transition.
TimeZoneTransition tzt = getPreviousTransition(current, false);
if (tzt != null) {
currentRules = getSimpleTimeZoneRulesNear(tzt.getTime() - 1);
}
}
if (currentRules.length == 3 && (currentRules[1] instanceof AnnualTimeZoneRule) && (currentRules[2] instanceof AnnualTimeZoneRule)) {
// A pair of AnnualTimeZoneRule
AnnualTimeZoneRule r1 = (AnnualTimeZoneRule) currentRules[1];
AnnualTimeZoneRule r2 = (AnnualTimeZoneRule) currentRules[2];
DateTimeRule start, end;
int offset1 = r1.getRawOffset() + r1.getDSTSavings();
int offset2 = r2.getRawOffset() + r2.getDSTSavings();
int sav;
if (offset1 > offset2) {
start = r1.getRule();
end = r2.getRule();
sav = offset1 - offset2;
} else {
start = r2.getRule();
end = r1.getRule();
sav = offset2 - offset1;
}
// getSimpleTimeZoneRulesNear always return rules using DOW / WALL_TIME
stz.setStartRule(start.getRuleMonth(), start.getRuleWeekInMonth(), start.getRuleDayOfWeek(), start.getRuleMillisInDay());
stz.setEndRule(end.getRuleMonth(), end.getRuleWeekInMonth(), end.getRuleDayOfWeek(), end.getRuleMillisInDay());
// set DST saving amount and start year
stz.setDSTSavings(sav);
} else {
// This could only happen if last rule is DST
// and the rule used forever. For example, Asia/Dhaka
// in tzdata2009i stays in DST forever.
// Hack - set DST starting at midnight on Jan 1st,
// ending 23:59:59.999 on Dec 31st
stz.setStartRule(0, 1, 0);
stz.setEndRule(11, 31, Grego.MILLIS_PER_DAY - 1);
}
}
int[] fields = Grego.timeToFields(current, null);
finalStartYear = fields[0];
finalStartMillis = Grego.fieldsToDay(fields[0], 0, 1);
if (bDst) {
// we probably do not need to set start year of final rule
// to finalzone itself, but we always do this for now.
stz.setStartYear(finalStartYear);
}
finalZone = stz;
} else {
finalZone.setRawOffset(offsetMillis);
}
transitionRulesInitialized = false;
}
use of android.icu.util.TimeZoneRule in project j2objc by google.
the class TimeZoneRuleTest method TestVTimeZoneCoverage.
/*
* API coverage test for VTimeZone
*/
@Test
public void TestVTimeZoneCoverage() {
final String TZID = "Europe/Moscow";
BasicTimeZone otz = (BasicTimeZone) TimeZone.getTimeZone(TZID, TimeZone.TIMEZONE_ICU);
VTimeZone vtz = VTimeZone.create(TZID);
// getOffset(era, year, month, day, dayOfWeek, milliseconds)
int offset1 = otz.getOffset(GregorianCalendar.AD, 2007, Calendar.JULY, 1, Calendar.SUNDAY, 0);
int offset2 = vtz.getOffset(GregorianCalendar.AD, 2007, Calendar.JULY, 1, Calendar.SUNDAY, 0);
if (offset1 != offset2) {
errln("FAIL: getOffset(int,int,int,int,int,int) returned different results in VTimeZone and OlsonTimeZone");
}
// getOffset(date, local, offsets)
int[] offsets1 = new int[2];
int[] offsets2 = new int[2];
long t = System.currentTimeMillis();
otz.getOffset(t, false, offsets1);
vtz.getOffset(t, false, offsets2);
if (offsets1[0] != offsets2[0] || offsets1[1] != offsets2[1]) {
errln("FAIL: getOffset(long,boolean,int[]) returned different results in VTimeZone and OlsonTimeZone");
}
// getRawOffset
if (otz.getRawOffset() != vtz.getRawOffset()) {
errln("FAIL: getRawOffset returned different results in VTimeZone and OlsonTimeZone");
}
// inDaylightTime
Date d = new Date();
if (otz.inDaylightTime(d) != vtz.inDaylightTime(d)) {
errln("FAIL: inDaylightTime returned different results in VTimeZone and OlsonTimeZone");
}
// useDaylightTime
if (otz.useDaylightTime() != vtz.useDaylightTime()) {
errln("FAIL: useDaylightTime returned different results in VTimeZone and OlsonTimeZone");
}
// setRawOffset
final int RAW = -10 * HOUR;
VTimeZone tmpvtz = (VTimeZone) vtz.clone();
tmpvtz.setRawOffset(RAW);
if (tmpvtz.getRawOffset() != RAW) {
logln("setRawOffset is implemented");
}
// hasSameRules
boolean bSame = otz.hasSameRules(vtz);
logln("OlsonTimeZone#hasSameRules(VTimeZone) should return false always for now - actual: " + bSame);
// getTZURL/setTZURL
final String TZURL = "http://icu-project.org/timezone";
String tzurl = vtz.getTZURL();
if (tzurl != null) {
errln("FAIL: getTZURL returned non-null value");
}
vtz.setTZURL(TZURL);
tzurl = vtz.getTZURL();
if (!TZURL.equals(tzurl)) {
errln("FAIL: URL returned by getTZURL does not match the one set by setTZURL");
}
// getLastModified/setLastModified
Date lastmod = vtz.getLastModified();
if (lastmod != null) {
errln("FAIL: getLastModified returned non-null value");
}
Date newdate = new Date();
vtz.setLastModified(newdate);
lastmod = vtz.getLastModified();
if (!newdate.equals(lastmod)) {
errln("FAIL: Date returned by getLastModified does not match the one set by setLastModified");
}
// getNextTransition/getPreviousTransition
long base = getUTCMillis(2007, Calendar.JULY, 1);
TimeZoneTransition tzt1 = otz.getNextTransition(base, true);
TimeZoneTransition tzt2 = vtz.getNextTransition(base, true);
if (tzt1.equals(tzt2)) {
errln("FAIL: getNextTransition returned different results in VTimeZone and OlsonTimeZone");
}
tzt1 = otz.getPreviousTransition(base, false);
tzt2 = vtz.getPreviousTransition(base, false);
if (tzt1.equals(tzt2)) {
errln("FAIL: getPreviousTransition returned different results in VTimeZone and OlsonTimeZone");
}
// hasEquivalentTransitions
long time1 = getUTCMillis(1950, Calendar.JANUARY, 1);
long time2 = getUTCMillis(2020, Calendar.JANUARY, 1);
if (!vtz.hasEquivalentTransitions(otz, time1, time2)) {
errln("FAIL: hasEquivalentTransitons returned false for the same time zone");
}
// getTimeZoneRules
TimeZoneRule[] rulesetAll = vtz.getTimeZoneRules();
TimeZoneRule[] ruleset1 = vtz.getTimeZoneRules(time1);
TimeZoneRule[] ruleset2 = vtz.getTimeZoneRules(time2);
if (rulesetAll.length < ruleset1.length || ruleset1.length < ruleset2.length) {
errln("FAIL: Number of rules returned by getRules is invalid");
}
int[] offsets_vtzc = new int[2];
VTimeZone vtzc = VTimeZone.create("PST");
vtzc.getOffsetFromLocal(Calendar.getInstance(vtzc).getTimeInMillis(), VTimeZone.LOCAL_STD, VTimeZone.LOCAL_STD, offsets_vtzc);
if (offsets_vtzc[0] > offsets_vtzc[1]) {
errln("Error getOffsetFromLocal()");
}
}
Aggregations