001: /*
002: *******************************************************************************
003: * Copyright (C) 2004-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: *
007: */
008:
009: package com.ibm.icu.dev.test.timescale;
010:
011: import java.util.Date;
012: import java.util.Locale;
013: import com.ibm.icu.util.GregorianCalendar;
014: import com.ibm.icu.util.SimpleTimeZone;
015: import com.ibm.icu.util.TimeZone;
016: import com.ibm.icu.util.UniversalTimeScale;
017: import com.ibm.icu.dev.test.TestFmwk;
018:
019: /**
020: * @author Owner
021: *
022: * TODO To change the template for this generated type comment go to
023: * Window - Preferences - Java - Code Style - Code Templates
024: */
025: public class TimeScaleDataTest extends TestFmwk {
026:
027: /**
028: * Default contstructor.
029: */
030: public TimeScaleDataTest() {
031: }
032:
033: private void roundTripTest(long value, int scale) {
034: long rt = UniversalTimeScale.toLong(UniversalTimeScale.from(
035: value, scale), scale);
036:
037: if (rt != value) {
038: errln("Round-trip error: time scale = " + scale
039: + ", value = " + value + ", round-trip = " + rt);
040: }
041: }
042:
043: private void toLimitTest(long toLimit, long fromLimit, int scale) {
044: long result = UniversalTimeScale.toLong(toLimit, scale);
045:
046: if (result != fromLimit) {
047: errln("toLimit failure: scale = " + scale + ", toLimit = "
048: + toLimit + ", toLong(toLimit, scale) = " + result
049: + ", fromLimit = " + fromLimit);
050: }
051: }
052:
053: private void epochOffsetTest(long epochOffset, long units, int scale) {
054: long universalEpoch = epochOffset * units;
055: long local = UniversalTimeScale.toLong(universalEpoch, scale);
056:
057: if (local != 0) {
058: errln("toLong(epochOffset, scale): scale = " + scale
059: + ", epochOffset = " + universalEpoch
060: + ", result = " + local);
061: }
062:
063: local = UniversalTimeScale.toLong(0, scale);
064:
065: if (local != -epochOffset) {
066: errln("toLong(0, scale): scale = " + scale + ", result = "
067: + local);
068: }
069:
070: long universal = UniversalTimeScale.from(-epochOffset, scale);
071:
072: if (universal != 0) {
073: errln("from(-epochOffest, scale): scale = " + scale
074: + ", epochOffset = " + epochOffset + ", result = "
075: + universal);
076: }
077:
078: universal = UniversalTimeScale.from(0, scale);
079:
080: if (universal != universalEpoch) {
081: errln("from(0, scale): scale = " + scale + ", result = "
082: + universal);
083: }
084: }
085:
086: public void TestEpochOffsets() {
087: for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
088: long units = UniversalTimeScale.getTimeScaleValue(scale,
089: UniversalTimeScale.UNITS_VALUE);
090: long epochOffset = UniversalTimeScale.getTimeScaleValue(
091: scale, UniversalTimeScale.EPOCH_OFFSET_VALUE);
092:
093: epochOffsetTest(epochOffset, units, scale);
094: }
095: }
096:
097: public void TestFromLimits() {
098: for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
099: long fromMin = UniversalTimeScale.getTimeScaleValue(scale,
100: UniversalTimeScale.FROM_MIN_VALUE);
101: long fromMax = UniversalTimeScale.getTimeScaleValue(scale,
102: UniversalTimeScale.FROM_MAX_VALUE);
103:
104: roundTripTest(fromMin, scale);
105: roundTripTest(fromMax, scale);
106: }
107: }
108:
109: public void TestToLimits() {
110: for (int scale = 0; scale < UniversalTimeScale.MAX_SCALE; scale += 1) {
111: long fromMin = UniversalTimeScale.getTimeScaleValue(scale,
112: UniversalTimeScale.FROM_MIN_VALUE);
113: long fromMax = UniversalTimeScale.getTimeScaleValue(scale,
114: UniversalTimeScale.FROM_MAX_VALUE);
115: long toMin = UniversalTimeScale.getTimeScaleValue(scale,
116: UniversalTimeScale.TO_MIN_VALUE);
117: long toMax = UniversalTimeScale.getTimeScaleValue(scale,
118: UniversalTimeScale.TO_MAX_VALUE);
119:
120: toLimitTest(toMin, fromMin, scale);
121: toLimitTest(toMax, fromMax, scale);
122: }
123: }
124:
125: // Test with data from .Net System.DateTime ---------------------------- ***
126:
127: /*
128: * This data was generated by C++.Net code like
129: * Console::WriteLine(L" {{ {0}, 1, 1, INT64_C({1}) }},", year, DateTime(year, 1, 1).Ticks);
130: * with the DateTime constructor taking int values for year, month, and date.
131: */
132: static private final long dotNetDateTimeTicks[] = {
133: /* year, month, day, ticks */
134: 100, 1, 1, 31241376000000000L, 100, 3, 1, 31292352000000000L, 200,
135: 1, 1, 62798112000000000L, 200, 3, 1, 62849088000000000L,
136: 300, 1, 1, 94354848000000000L, 300, 3, 1,
137: 94405824000000000L, 400, 1, 1, 125911584000000000L, 400, 3,
138: 1, 125963424000000000L, 500, 1, 1, 157469184000000000L,
139: 500, 3, 1, 157520160000000000L, 600, 1, 1,
140: 189025920000000000L, 600, 3, 1, 189076896000000000L, 700,
141: 1, 1, 220582656000000000L, 700, 3, 1, 220633632000000000L,
142: 800, 1, 1, 252139392000000000L, 800, 3, 1,
143: 252191232000000000L, 900, 1, 1, 283696992000000000L, 900,
144: 3, 1, 283747968000000000L, 1000, 1, 1, 315253728000000000L,
145: 1000, 3, 1, 315304704000000000L, 1100, 1, 1,
146: 346810464000000000L, 1100, 3, 1, 346861440000000000L, 1200,
147: 1, 1, 378367200000000000L, 1200, 3, 1, 378419040000000000L,
148: 1300, 1, 1, 409924800000000000L, 1300, 3, 1,
149: 409975776000000000L, 1400, 1, 1, 441481536000000000L, 1400,
150: 3, 1, 441532512000000000L, 1500, 1, 1, 473038272000000000L,
151: 1500, 3, 1, 473089248000000000L, 1600, 1, 1,
152: 504595008000000000L, 1600, 3, 1, 504646848000000000L, 1700,
153: 1, 1, 536152608000000000L, 1700, 3, 1, 536203584000000000L,
154: 1800, 1, 1, 567709344000000000L, 1800, 3, 1,
155: 567760320000000000L, 1900, 1, 1, 599266080000000000L, 1900,
156: 3, 1, 599317056000000000L, 2000, 1, 1, 630822816000000000L,
157: 2000, 3, 1, 630874656000000000L, 2100, 1, 1,
158: 662380416000000000L, 2100, 3, 1, 662431392000000000L, 2200,
159: 1, 1, 693937152000000000L, 2200, 3, 1, 693988128000000000L,
160: 2300, 1, 1, 725493888000000000L, 2300, 3, 1,
161: 725544864000000000L, 2400, 1, 1, 757050624000000000L, 2400,
162: 3, 1, 757102464000000000L, 2500, 1, 1, 788608224000000000L,
163: 2500, 3, 1, 788659200000000000L, 2600, 1, 1,
164: 820164960000000000L, 2600, 3, 1, 820215936000000000L, 2700,
165: 1, 1, 851721696000000000L, 2700, 3, 1, 851772672000000000L,
166: 2800, 1, 1, 883278432000000000L, 2800, 3, 1,
167: 883330272000000000L, 2900, 1, 1, 914836032000000000L, 2900,
168: 3, 1, 914887008000000000L, 3000, 1, 1, 946392768000000000L,
169: 3000, 3, 1, 946443744000000000L, 1, 1, 1, 0L, 1601, 1, 1,
170: 504911232000000000L, 1899, 12, 31, 599265216000000000L,
171: 1904, 1, 1, 600527520000000000L, 1970, 1, 1,
172: 621355968000000000L, 2001, 1, 1, 631139040000000000L, 9900,
173: 3, 1, 3123873216000000000L, 9999, 12, 31,
174: 3155378112000000000L };
175:
176: /*
177: * ICU's Universal Time Scale is designed to be tick-for-tick compatible with
178: * .Net System.DateTime. Verify that this is so for the
179: * .Net-supported date range (years 1-9999 AD).
180: * This requires a proleptic Gregorian calendar because that's what .Net uses.
181: * Proleptic: No Julian/Gregorian switchover, or a switchover before
182: * any date that we test, that is, before 0001 AD.
183: */
184: public void TestDotNet() {
185: TimeZone utc;
186: final long dayMillis = 86400 * 1000L; /* 1 day = 86400 seconds */
187: final long dayTicks = 86400 * 10000000L;
188: final int kYear = 0; // offset for dotNetDateTimeTicks[] field
189: final int kMonth = 1;
190: final int kDay = 2;
191: final int kTicks = 3;
192: final int kIncrement = 4;
193: GregorianCalendar cal;
194: long icuDate;
195: long ticks, millis;
196: int i;
197:
198: /* Open a proleptic Gregorian calendar. */
199: long before0001AD = -1000000 * dayMillis;
200: utc = new SimpleTimeZone(0, "UTC");
201: cal = new GregorianCalendar(utc, Locale.ENGLISH);
202: cal.setGregorianChange(new Date(before0001AD));
203: for (i = 0; i < dotNetDateTimeTicks.length; i += kIncrement) {
204: /* Test conversion from .Net/Universal time to ICU time. */
205: millis = UniversalTimeScale.toLong(dotNetDateTimeTicks[i
206: + kTicks], UniversalTimeScale.ICU4C_TIME);
207: cal.clear();
208: cal.set((int) dotNetDateTimeTicks[i + kYear],
209: (int) dotNetDateTimeTicks[i + kMonth] - 1, /* Java & ICU use January = month 0. */
210: (int) dotNetDateTimeTicks[i + kDay]);
211: icuDate = cal.getTimeInMillis();
212: if (millis != icuDate) {
213: /* Print days not millis. */
214: errln("UniversalTimeScale.toLong(ticks[" + i
215: + "], ICU4C)=" + (millis / dayMillis) + " != "
216: + (icuDate / dayMillis) + "=ucal_getMillis("
217: + dotNetDateTimeTicks[i + kYear] + "-"
218: + dotNetDateTimeTicks[i + kMonth] + "-"
219: + dotNetDateTimeTicks[i + kDay] + ")");
220: }
221:
222: /* Test conversion from ICU time to .Net/Universal time. */
223: ticks = UniversalTimeScale.from(icuDate,
224: UniversalTimeScale.ICU4C_TIME);
225: if (ticks != dotNetDateTimeTicks[i + kTicks]) {
226: /* Print days not ticks. */
227: errln("UniversalTimeScale.from(date[" + i
228: + "], ICU4C)=" + (ticks / dayTicks) + " != "
229: + dotNetDateTimeTicks[i + kTicks] / dayTicks
230: + "=.Net System.DateTime("
231: + dotNetDateTimeTicks[i + kYear] + "-"
232: + dotNetDateTimeTicks[i + kMonth] + "-"
233: + dotNetDateTimeTicks[i + kDay] + ").Ticks");
234: }
235: }
236: }
237:
238: public static void main(String[] args) {
239: new TimeScaleDataTest().run(args);
240: }
241: }
|