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.util;
010:
011: import com.ibm.icu.math.BigDecimal;
012: import java.lang.IllegalArgumentException;
013:
014: /**
015: * There are quite a few different conventions for binary datetime, depending on different
016: * platforms and protocols. Some of these have severe drawbacks. For example, people using
017: * Unix time (seconds since Jan 1, 1970, usually in a 32-bit integer)
018: * think that they are safe until near the year 2038.
019: * But cases can and do arise where arithmetic manipulations causes serious problems. Consider
020: * the computation of the average of two datetimes, for example: if one calculates them with
021: * <code>averageTime = (time1 + time2)/2</code>, there will be overflow even with dates
022: * beginning in 2004. Moreover, even if these problems don't occur, there is the issue of
023: * conversion back and forth between different systems.
024: *
025: * <p>Binary datetimes differ in a number of ways: the datatype, the unit,
026: * and the epoch (origin). We refer to these as time scales.</p>
027: *
028: * <p>ICU implements a universal time scale that is similar to the
029: * .NET framework's System.DateTime. The universal time scale is a
030: * 64-bit integer that holds ticks since midnight, January 1st, 0001.
031: * (One tick is 100 nanoseconds.)
032: * Negative values are supported. This has enough range to guarantee that
033: * calculations involving dates around the present are safe.</p>
034: *
035: * <p>The universal time scale always measures time according to the
036: * proleptic Gregorian calendar. That is, the Gregorian calendar's
037: * leap year rules are used for all times, even before 1582 when it was
038: * introduced. (This is different from the default ICU calendar which
039: * switches from the Julian to the Gregorian calendar in 1582.
040: * See GregorianCalendar.setGregorianChange() and ucal_setGregorianChange().)</p>
041: *
042: * ICU provides conversion functions to and from all other major time
043: * scales, allowing datetimes in any time scale to be converted to the
044: * universal time scale, safely manipulated, and converted back to any other
045: * datetime time scale.</p>
046: *
047: * <p>For more details and background, see the
048: * <a href="http://icu.sourceforge.net/userguide/universalTimeScale.html">Universal Time Scale</a>
049: * chapter in the ICU User Guide.</p>
050: *
051: * @draft ICU 3.2
052: * @provisional This API might change or be removed in a future release.
053: */
054:
055: public final class UniversalTimeScale {
056: /**
057: * Used in the JDK. Data is a <code>long</code>. Value
058: * is milliseconds since January 1, 1970.
059: *
060: * @draft ICU 3.2
061: * @provisional This API might change or be removed in a future release.
062: */
063: public static final int JAVA_TIME = 0;
064:
065: /**
066: * Used in Unix systems. Data is an <code>int</code> or a <code>long</code>. Value
067: * is seconds since January 1, 1970.
068: *
069: * @draft ICU 3.2
070: * @provisional This API might change or be removed in a future release.
071: */
072: public static final int UNIX_TIME = 1;
073:
074: /**
075: * Used in the ICU4C. Data is a <code>double</code>. Value
076: * is milliseconds since January 1, 1970.
077: *
078: * @draft ICU 3.2
079: * @provisional This API might change or be removed in a future release.
080: */
081: public static final int ICU4C_TIME = 2;
082:
083: /**
084: * Used in Windows for file times. Data is a <code>long</code>. Value
085: * is ticks (1 tick == 100 nanoseconds) since January 1, 1601.
086: *
087: * @draft ICU 3.2
088: * @provisional This API might change or be removed in a future release.
089: */
090: public static final int WINDOWS_FILE_TIME = 3;
091:
092: /**
093: * Used in the .NET framework's <code>System.DateTime</code> structure.
094: * Data is a <code>long</code>. Value is ticks (1 tick == 100 nanoseconds) since January 1, 0001.
095: *
096: * @draft ICU 3.2
097: * @provisional This API might change or be removed in a future release.
098: */
099: public static final int DOTNET_DATE_TIME = 4;
100:
101: /**
102: * Used in older Macintosh systems. Data is an <code>int</code>. Value
103: * is seconds since January 1, 1904.
104: *
105: * @draft ICU 3.2
106: * @provisional This API might change or be removed in a future release.
107: */
108: public static final int MAC_OLD_TIME = 5;
109:
110: /**
111: * Used in the JDK. Data is a <code>double</code>. Value
112: * is milliseconds since January 1, 2001.
113: *
114: * @draft ICU 3.2
115: * @provisional This API might change or be removed in a future release.
116: */
117: public static final int MAC_TIME = 6;
118:
119: /**
120: * Used in Excel. Data is a <code>?unknown?</code>. Value
121: * is days since December 31, 1899.
122: *
123: * @draft ICU 3.2
124: * @provisional This API might change or be removed in a future release.
125: */
126: public static final int EXCEL_TIME = 7;
127:
128: /**
129: * Used in DB2. Data is a <code>?unknown?</code>. Value
130: * is days since December 31, 1899.
131: *
132: * @draft ICU 3.2
133: * @provisional This API might change or be removed in a future release.
134: */
135: public static final int DB2_TIME = 8;
136:
137: /**
138: * This is the first unused time scale value.
139: *
140: * @draft ICU 3.2
141: * @provisional This API might change or be removed in a future release.
142: */
143: public static final int MAX_SCALE = 9;
144:
145: /**
146: * The constant used to select the units value
147: * for a time scale.
148: *
149: *
150: * @draft ICU 3.2
151: * @provisional This API might change or be removed in a future release.
152: */
153: public static final int UNITS_VALUE = 0;
154:
155: /**
156: * The constant used to select the epoch offset value
157: * for a time scale.
158: *
159: * @see #getTimeScaleValue
160: *
161: * @draft ICU 3.2
162: * @provisional This API might change or be removed in a future release.
163: */
164: public static final int EPOCH_OFFSET_VALUE = 1;
165:
166: /**
167: * The constant used to select the minimum from value
168: * for a time scale.
169: *
170: * @see #getTimeScaleValue
171: *
172: * @draft ICU 3.2
173: * @provisional This API might change or be removed in a future release.
174: */
175: public static final int FROM_MIN_VALUE = 2;
176:
177: /**
178: * The constant used to select the maximum from value
179: * for a time scale.
180: *
181: * @see #getTimeScaleValue
182: *
183: * @draft ICU 3.2
184: * @provisional This API might change or be removed in a future release.
185: */
186: public static final int FROM_MAX_VALUE = 3;
187:
188: /**
189: * The constant used to select the minimum to value
190: * for a time scale.
191: *
192: * @see #getTimeScaleValue
193: *
194: * @draft ICU 3.2
195: * @provisional This API might change or be removed in a future release.
196: */
197: public static final int TO_MIN_VALUE = 4;
198:
199: /**
200: * The constant used to select the maximum to value
201: * for a time scale.
202: *
203: * @see #getTimeScaleValue
204: *
205: * @draft ICU 3.2
206: * @provisional This API might change or be removed in a future release.
207: */
208: public static final int TO_MAX_VALUE = 5;
209:
210: /**
211: * The constant used to select the epoch plus one value
212: * for a time scale.
213: *
214: * NOTE: This is an internal value. DO NOT USE IT. May not
215: * actually be equal to the epoch offset value plus one.
216: *
217: * @see #getTimeScaleValue
218: *
219: * @draft ICU 3.2
220: * @provisional This API might change or be removed in a future release.
221: */
222: public static final int EPOCH_OFFSET_PLUS_1_VALUE = 6;
223:
224: /**
225: * The constant used to select the epoch offset minus one value
226: * for a time scale.
227: *
228: * NOTE: This is an internal value. DO NOT USE IT. May not
229: * actually be equal to the epoch offset value minus one.
230: *
231: * @see #getTimeScaleValue
232: *
233: * @internal
234: * @deprecated This API is ICU internal only.
235: */
236: public static final int EPOCH_OFFSET_MINUS_1_VALUE = 7;
237:
238: /**
239: * The constant used to select the units round value
240: * for a time scale.
241: *
242: * NOTE: This is an internal value. DO NOT USE IT.
243: *
244: * @see #getTimeScaleValue
245: *
246: * @internal
247: * @deprecated This API is ICU internal only.
248: */
249: public static final int UNITS_ROUND_VALUE = 8;
250:
251: /**
252: * The constant used to select the minimum safe rounding value
253: * for a time scale.
254: *
255: * NOTE: This is an internal value. DO NOT USE IT.
256: *
257: * @see #getTimeScaleValue
258: *
259: * @internal
260: * @deprecated This API is ICU internal only.
261: */
262: public static final int MIN_ROUND_VALUE = 9;
263:
264: /**
265: * The constant used to select the maximum safe rounding value
266: * for a time scale.
267: *
268: * NOTE: This is an internal value. DO NOT USE IT.
269: *
270: * @see #getTimeScaleValue
271: *
272: * @internal
273: * @deprecated This API is ICU internal only.
274: */
275: public static final int MAX_ROUND_VALUE = 10;
276:
277: /**
278: * The number of time scale values.
279: *
280: * NOTE: This is an internal value. DO NOT USE IT.
281: *
282: * @see #getTimeScaleValue
283: *
284: * @internal
285: * @deprecated This API is ICU internal only.
286: */
287: public static final int MAX_SCALE_VALUE = 11;
288:
289: private static final long ticks = 1;
290: private static final long microseconds = ticks * 10;
291: private static final long milliseconds = microseconds * 1000;
292: private static final long seconds = milliseconds * 1000;
293: private static final long minutes = seconds * 60;
294: private static final long hours = minutes * 60;
295: private static final long days = hours * 24;
296:
297: /**
298: * This class holds the data that describes a particular
299: * time scale.
300: *
301: * @internal
302: * @deprecated This API is ICU internal only.
303: */
304: private static final class TimeScaleData {
305: TimeScaleData(long theUnits, long theEpochOffset,
306: long theToMin, long theToMax, long theFromMin,
307: long theFromMax) {
308: units = theUnits;
309: unitsRound = theUnits / 2;
310:
311: minRound = Long.MIN_VALUE + unitsRound;
312: maxRound = Long.MAX_VALUE - unitsRound;
313:
314: epochOffset = theEpochOffset / theUnits;
315:
316: if (theUnits == 1) {
317: epochOffsetP1 = epochOffsetM1 = epochOffset;
318: } else {
319: epochOffsetP1 = epochOffset + 1;
320: epochOffsetM1 = epochOffset - 1;
321: }
322:
323: toMin = theToMin;
324: toMax = theToMax;
325:
326: fromMin = theFromMin;
327: fromMax = theFromMax;
328: }
329:
330: long units;
331: long epochOffset;
332: long fromMin;
333: long fromMax;
334: long toMin;
335: long toMax;
336:
337: long epochOffsetP1;
338: long epochOffsetM1;
339: long unitsRound;
340: long minRound;
341: long maxRound;
342: }
343:
344: private static final TimeScaleData[] timeScaleTable = {
345: new TimeScaleData(milliseconds, 621355968000000000L,
346: -9223372036854774999L, 9223372036854774999L,
347: -984472800485477L, 860201606885477L), // JAVA_TIME
348: new TimeScaleData(seconds, 621355968000000000L,
349: -9223372036854775808L, 9223372036854775807L,
350: -984472800485L, 860201606885L), // UNIX_TIME
351: new TimeScaleData(milliseconds, 621355968000000000L,
352: -9223372036854774999L, 9223372036854774999L,
353: -984472800485477L, 860201606885477L), // ICU4C_TIME
354: new TimeScaleData(ticks, 504911232000000000L,
355: -8718460804854775808L, 9223372036854775807L,
356: -9223372036854775808L, 8718460804854775807L), // WINDOWS_FILE_TIME
357: new TimeScaleData(ticks, 000000000000000000L,
358: -9223372036854775808L, 9223372036854775807L,
359: -9223372036854775808L, 9223372036854775807L), // DOTNET_DATE_TIME
360: new TimeScaleData(seconds, 600527520000000000L,
361: -9223372036854775808L, 9223372036854775807L,
362: -982389955685L, 862284451685L), // MAC_OLD_TIME
363: new TimeScaleData(seconds, 631139040000000000L,
364: -9223372036854775808L, 9223372036854775807L,
365: -985451107685L, 859223299685L), // MAC_TIME
366: new TimeScaleData(days, 599265216000000000L,
367: -9223372036854775808L, 9223372036854775807L,
368: -11368793L, 9981605L), // EXCEL_TIME
369: new TimeScaleData(days, 599265216000000000L,
370: -9223372036854775808L, 9223372036854775807L,
371: -11368793L, 9981605L) // DB2_TIME
372: };
373:
374: /*
375: * Prevent construction of this class.
376: */
377: private UniversalTimeScale() {
378: // nothing to do
379: }
380:
381: /**
382: * Convert a <code>long</code> datetime from the given time scale to the universal time scale.
383: *
384: * @param otherTime The <code>long</code> datetime
385: * @param timeScale The time scale to convert from
386: *
387: * @return The datetime converted to the universal time scale
388: *
389: * @draft ICU 3.2
390: * @provisional This API might change or be removed in a future release.
391: */
392: public static long from(long otherTime, int timeScale) {
393: TimeScaleData data = fromRangeCheck(otherTime, timeScale);
394:
395: return (otherTime + data.epochOffset) * data.units;
396: }
397:
398: /**
399: * Convert a <code>double</code> datetime from the given time scale to the universal time scale.
400: * All calculations are done using <code>BigDecimal</code> to guarantee that the value
401: * does not go out of range.
402: *
403: * @param otherTime The <code>double</code> datetime
404: * @param timeScale The time scale to convert from
405: *
406: * @return The datetime converted to the universal time scale
407: *
408: * @draft ICU 3.2
409: * @provisional This API might change or be removed in a future release.
410: */
411: public static BigDecimal bigDecimalFrom(double otherTime,
412: int timeScale) {
413: TimeScaleData data = getTimeScaleData(timeScale);
414: BigDecimal other = new BigDecimal(String.valueOf(otherTime));
415: BigDecimal units = new BigDecimal(data.units);
416: BigDecimal epochOffset = new BigDecimal(data.epochOffset);
417:
418: return other.add(epochOffset).multiply(units);
419: }
420:
421: /**
422: * Convert a <code>long</code> datetime from the given time scale to the universal time scale.
423: * All calculations are done using <code>BigDecimal</code> to guarantee that the value
424: * does not go out of range.
425: *
426: * @param otherTime The <code>long</code> datetime
427: * @param timeScale The time scale to convert from
428: *
429: * @return The datetime converted to the universal time scale
430: *
431: * @draft ICU 3.2
432: * @provisional This API might change or be removed in a future release.
433: */
434: public static BigDecimal bigDecimalFrom(long otherTime,
435: int timeScale) {
436: TimeScaleData data = getTimeScaleData(timeScale);
437: BigDecimal other = new BigDecimal(otherTime);
438: BigDecimal units = new BigDecimal(data.units);
439: BigDecimal epochOffset = new BigDecimal(data.epochOffset);
440:
441: return other.add(epochOffset).multiply(units);
442: }
443:
444: /**
445: * Convert a <code>BigDecimal</code> datetime from the given time scale to the universal time scale.
446: * All calculations are done using <code>BigDecimal</code> to guarantee that the value
447: * does not go out of range.
448: *
449: * @param otherTime The <code>BigDecimal</code> datetime
450: * @param timeScale The time scale to convert from
451: *
452: * @return The datetime converted to the universal time scale
453: *
454: * @draft ICU 3.2
455: * @provisional This API might change or be removed in a future release.
456: */
457: public static BigDecimal bigDecimalFrom(BigDecimal otherTime,
458: int timeScale) {
459: TimeScaleData data = getTimeScaleData(timeScale);
460:
461: BigDecimal units = new BigDecimal(data.units);
462: BigDecimal epochOffset = new BigDecimal(data.epochOffset);
463:
464: return otherTime.add(epochOffset).multiply(units);
465: }
466:
467: /**
468: * Convert a datetime from the universal time scale stored as a <code>BigDecimal</code> to a
469: * <code>long</code> in the given time scale.
470: *
471: * Since this calculation requires a divide, we must round. The straight forward
472: * way to round by adding half of the divisor will push the sum out of range for values
473: * within have the divisor of the limits of the precision of a <code>long</code>. To get around this, we do
474: * the rounding like this:
475: *
476: * <p><code>
477: * (universalTime - units + units/2) / units + 1
478: * </code>
479: *
480: * <p>
481: * (i.e. we subtract units first to guarantee that we'll still be in range when we
482: * add <code>units/2</code>. We then need to add one to the quotent to make up for the extra subtraction.
483: * This simplifies to:
484: *
485: * <p><code>
486: * (universalTime - units/2) / units - 1
487: * </code>
488: *
489: * <p>
490: * For negative values to round away from zero, we need to flip the signs:
491: *
492: * <p><code>
493: * (universalTime + units/2) / units + 1
494: * </code>
495: *
496: * <p>
497: * Since we also need to subtract the epochOffset, we fold the <code>+/- 1</code>
498: * into the offset value. (i.e. <code>epochOffsetP1</code>, <code>epochOffsetM1</code>.)
499: *
500: * @param universalTime The datetime in the universal time scale
501: * @param timeScale The time scale to convert to
502: *
503: * @return The datetime converted to the given time scale
504: *
505: * @draft ICU 3.2
506: * @provisional This API might change or be removed in a future release.
507: */
508: public static long toLong(long universalTime, int timeScale) {
509: TimeScaleData data = toRangeCheck(universalTime, timeScale);
510:
511: if (universalTime < 0) {
512: if (universalTime < data.minRound) {
513: return (universalTime + data.unitsRound) / data.units
514: - data.epochOffsetP1;
515: }
516:
517: return (universalTime - data.unitsRound) / data.units
518: - data.epochOffset;
519: }
520:
521: if (universalTime > data.maxRound) {
522: return (universalTime - data.unitsRound) / data.units
523: - data.epochOffsetM1;
524: }
525:
526: return (universalTime + data.unitsRound) / data.units
527: - data.epochOffset;
528: }
529:
530: /**
531: * Convert a datetime from the universal time scale to a <code>BigDecimal</code> in the given time scale.
532: *
533: * @param universalTime The datetime in the universal time scale
534: * @param timeScale The time scale to convert to
535: *
536: * @return The datetime converted to the given time scale
537: *
538: * @draft ICU 3.2
539: * @provisional This API might change or be removed in a future release.
540: */
541: public static BigDecimal toBigDecimal(long universalTime,
542: int timeScale) {
543: TimeScaleData data = getTimeScaleData(timeScale);
544: BigDecimal universal = new BigDecimal(universalTime);
545: BigDecimal units = new BigDecimal(data.units);
546: BigDecimal epochOffset = new BigDecimal(data.epochOffset);
547:
548: return universal.divide(units, BigDecimal.ROUND_HALF_UP)
549: .subtract(epochOffset);
550: }
551:
552: /**
553: * Convert a datetime from the universal time scale to a <code>BigDecimal</code> in the given time scale.
554: *
555: * @param universalTime The datetime in the universal time scale
556: * @param timeScale The time scale to convert to
557: *
558: * @return The datetime converted to the given time scale
559: *
560: * @draft ICU 3.2
561: * @provisional This API might change or be removed in a future release.
562: */
563: public static BigDecimal toBigDecimal(BigDecimal universalTime,
564: int timeScale) {
565: TimeScaleData data = getTimeScaleData(timeScale);
566: BigDecimal units = new BigDecimal(data.units);
567: BigDecimal epochOffset = new BigDecimal(data.epochOffset);
568:
569: return universalTime.divide(units, BigDecimal.ROUND_HALF_UP)
570: .subtract(epochOffset);
571: }
572:
573: /**
574: * Return the <code>TimeScaleData</code> object for the given time
575: * scale.
576: *
577: * @param scale - the time scale
578: *
579: * @return the <code>TimeScaleData</code> object for the given time scale
580: *
581: * @internal
582: * @deprecated This API is ICU internal only.
583: */
584: private static TimeScaleData getTimeScaleData(int scale) {
585: if (scale < 0 || scale >= MAX_SCALE) {
586: throw new IllegalArgumentException("scale out of range: "
587: + scale);
588: }
589:
590: return timeScaleTable[scale];
591: }
592:
593: /**
594: * Get a value associated with a particular time scale.
595: *
596: * @param scale - the time scale
597: * @param value - a constant representing the value to get
598: *
599: * @return - the value.
600: *
601: * @draft ICU 3.2
602: * @provisional This API might change or be removed in a future release.
603: */
604: public static long getTimeScaleValue(int scale, int value) {
605: TimeScaleData data = getTimeScaleData(scale);
606:
607: switch (value) {
608: case UNITS_VALUE:
609: return data.units;
610:
611: case EPOCH_OFFSET_VALUE:
612: return data.epochOffset;
613:
614: case FROM_MIN_VALUE:
615: return data.fromMin;
616:
617: case FROM_MAX_VALUE:
618: return data.fromMax;
619:
620: case TO_MIN_VALUE:
621: return data.toMin;
622:
623: case TO_MAX_VALUE:
624: return data.toMax;
625:
626: case EPOCH_OFFSET_PLUS_1_VALUE:
627: return data.epochOffsetP1;
628:
629: case EPOCH_OFFSET_MINUS_1_VALUE:
630: return data.epochOffsetM1;
631:
632: case UNITS_ROUND_VALUE:
633: return data.unitsRound;
634:
635: case MIN_ROUND_VALUE:
636: return data.minRound;
637:
638: case MAX_ROUND_VALUE:
639: return data.maxRound;
640:
641: default:
642: throw new IllegalArgumentException("value out of range: "
643: + value);
644: }
645: }
646:
647: private static TimeScaleData toRangeCheck(long universalTime,
648: int scale) {
649: TimeScaleData data = getTimeScaleData(scale);
650:
651: if (universalTime >= data.toMin && universalTime <= data.toMax) {
652: return data;
653: }
654:
655: throw new IllegalArgumentException(
656: "universalTime out of range:" + universalTime);
657: }
658:
659: private static TimeScaleData fromRangeCheck(long otherTime,
660: int scale) {
661: TimeScaleData data = getTimeScaleData(scale);
662:
663: if (otherTime >= data.fromMin && otherTime <= data.fromMax) {
664: return data;
665: }
666:
667: throw new IllegalArgumentException("otherTime out of range:"
668: + otherTime);
669: }
670:
671: /**
672: * Convert a time in the Universal Time Scale into another time
673: * scale. The division used to do the conversion rounds down.
674: *
675: * NOTE: This is an internal routine used by the tool that
676: * generates the to and from limits. Use it at your own risk.
677: *
678: * @param universalTime the time in the Universal Time scale
679: * @param timeScale the time scale to convert to
680: * @return the time in the given time scale
681: *
682: * @internal
683: * @deprecated This API is ICU internal only.
684: */
685: public static BigDecimal toBigDecimalTrunc(
686: BigDecimal universalTime, int timeScale) {
687: TimeScaleData data = getTimeScaleData(timeScale);
688: BigDecimal units = new BigDecimal(data.units);
689: BigDecimal epochOffset = new BigDecimal(data.epochOffset);
690:
691: return universalTime.divide(units, BigDecimal.ROUND_DOWN)
692: .subtract(epochOffset);
693: }
694: }
|