2 *********************************************************************************
\r
3 * Copyright (C) 2004-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *********************************************************************************
\r
9 package com.ibm.icu.util;
\r
11 import com.ibm.icu.math.BigDecimal;
\r
14 * There are quite a few different conventions for binary datetime, depending on different
\r
15 * platforms and protocols. Some of these have severe drawbacks. For example, people using
\r
16 * Unix time (seconds since Jan 1, 1970, usually in a 32-bit integer)
\r
17 * think that they are safe until near the year 2038.
\r
18 * But cases can and do arise where arithmetic manipulations causes serious problems. Consider
\r
19 * the computation of the average of two datetimes, for example: if one calculates them with
\r
20 * <code>averageTime = (time1 + time2)/2</code>, there will be overflow even with dates
\r
21 * beginning in 2004. Moreover, even if these problems don't occur, there is the issue of
\r
22 * conversion back and forth between different systems.
\r
24 * <p>Binary datetimes differ in a number of ways: the datatype, the unit,
\r
25 * and the epoch (origin). We refer to these as time scales.</p>
\r
27 * <p>ICU implements a universal time scale that is similar to the
\r
28 * .NET framework's System.DateTime. The universal time scale is a
\r
29 * 64-bit integer that holds ticks since midnight, January 1st, 0001.
\r
30 * (One tick is 100 nanoseconds.)
\r
31 * Negative values are supported. This has enough range to guarantee that
\r
32 * calculations involving dates around the present are safe.</p>
\r
34 * <p>The universal time scale always measures time according to the
\r
35 * proleptic Gregorian calendar. That is, the Gregorian calendar's
\r
36 * leap year rules are used for all times, even before 1582 when it was
\r
37 * introduced. (This is different from the default ICU calendar which
\r
38 * switches from the Julian to the Gregorian calendar in 1582.
\r
39 * See GregorianCalendar.setGregorianChange() and ucal_setGregorianChange().)</p>
\r
41 * ICU provides conversion functions to and from all other major time
\r
42 * scales, allowing datetimes in any time scale to be converted to the
\r
43 * universal time scale, safely manipulated, and converted back to any other
\r
44 * datetime time scale.</p>
\r
46 * <p>For more details and background, see the
\r
47 * <a href="http://www.icu-project.org/userguide/universalTimeScale.html">Universal Time Scale</a>
\r
48 * chapter in the ICU User Guide.</p>
\r
53 public final class UniversalTimeScale
\r
56 * Used in the JDK. Data is a <code>long</code>. Value
\r
57 * is milliseconds since January 1, 1970.
\r
61 public static final int JAVA_TIME = 0;
\r
64 * Used in Unix systems. Data is an <code>int</code> or a <code>long</code>. Value
\r
65 * is seconds since January 1, 1970.
\r
69 public static final int UNIX_TIME = 1;
\r
72 * Used in the ICU4C. Data is a <code>double</code>. Value
\r
73 * is milliseconds since January 1, 1970.
\r
77 public static final int ICU4C_TIME = 2;
\r
80 * Used in Windows for file times. Data is a <code>long</code>. Value
\r
81 * is ticks (1 tick == 100 nanoseconds) since January 1, 1601.
\r
85 public static final int WINDOWS_FILE_TIME = 3;
\r
88 * Used in the .NET framework's <code>System.DateTime</code> structure.
\r
89 * Data is a <code>long</code>. Value is ticks (1 tick == 100 nanoseconds) since January 1, 0001.
\r
93 public static final int DOTNET_DATE_TIME = 4;
\r
96 * Used in older Macintosh systems. Data is an <code>int</code>. Value
\r
97 * is seconds since January 1, 1904.
\r
101 public static final int MAC_OLD_TIME = 5;
\r
104 * Used in the JDK. Data is a <code>double</code>. Value
\r
105 * is milliseconds since January 1, 2001.
\r
109 public static final int MAC_TIME = 6;
\r
112 * Used in Excel. Data is a <code>?unknown?</code>. Value
\r
113 * is days since December 31, 1899.
\r
117 public static final int EXCEL_TIME = 7;
\r
120 * Used in DB2. Data is a <code>?unknown?</code>. Value
\r
121 * is days since December 31, 1899.
\r
125 public static final int DB2_TIME = 8;
\r
128 * Data is a <code>long</code>. Value is microseconds since January 1, 1970.
\r
129 * Similar to Unix time (linear value from 1970) and struct timeval
\r
130 * (microseconds resolution).
\r
134 public static final int UNIX_MICROSECONDS_TIME = 9;
\r
137 * This is the first unused time scale value.
\r
141 public static final int MAX_SCALE = 10;
\r
144 * The constant used to select the units value
\r
145 * for a time scale.
\r
150 public static final int UNITS_VALUE = 0;
\r
153 * The constant used to select the epoch offset value
\r
154 * for a time scale.
\r
156 * @see #getTimeScaleValue
\r
160 public static final int EPOCH_OFFSET_VALUE = 1;
\r
163 * The constant used to select the minimum from value
\r
164 * for a time scale.
\r
166 * @see #getTimeScaleValue
\r
170 public static final int FROM_MIN_VALUE = 2;
\r
173 * The constant used to select the maximum from value
\r
174 * for a time scale.
\r
176 * @see #getTimeScaleValue
\r
180 public static final int FROM_MAX_VALUE = 3;
\r
183 * The constant used to select the minimum to value
\r
184 * for a time scale.
\r
186 * @see #getTimeScaleValue
\r
190 public static final int TO_MIN_VALUE = 4;
\r
193 * The constant used to select the maximum to value
\r
194 * for a time scale.
\r
196 * @see #getTimeScaleValue
\r
200 public static final int TO_MAX_VALUE = 5;
\r
203 * The constant used to select the epoch plus one value
\r
204 * for a time scale.
\r
206 * NOTE: This is an internal value. DO NOT USE IT. May not
\r
207 * actually be equal to the epoch offset value plus one.
\r
209 * @see #getTimeScaleValue
\r
213 public static final int EPOCH_OFFSET_PLUS_1_VALUE = 6;
\r
216 * The constant used to select the epoch offset minus one value
\r
217 * for a time scale.
\r
219 * NOTE: This is an internal value. DO NOT USE IT. May not
\r
220 * actually be equal to the epoch offset value minus one.
\r
222 * @see #getTimeScaleValue
\r
225 * @deprecated This API is ICU internal only.
\r
227 public static final int EPOCH_OFFSET_MINUS_1_VALUE = 7;
\r
230 * The constant used to select the units round value
\r
231 * for a time scale.
\r
233 * NOTE: This is an internal value. DO NOT USE IT.
\r
235 * @see #getTimeScaleValue
\r
238 * @deprecated This API is ICU internal only.
\r
240 public static final int UNITS_ROUND_VALUE = 8;
\r
243 * The constant used to select the minimum safe rounding value
\r
244 * for a time scale.
\r
246 * NOTE: This is an internal value. DO NOT USE IT.
\r
248 * @see #getTimeScaleValue
\r
251 * @deprecated This API is ICU internal only.
\r
253 public static final int MIN_ROUND_VALUE = 9;
\r
256 * The constant used to select the maximum safe rounding value
\r
257 * for a time scale.
\r
259 * NOTE: This is an internal value. DO NOT USE IT.
\r
261 * @see #getTimeScaleValue
\r
264 * @deprecated This API is ICU internal only.
\r
266 public static final int MAX_ROUND_VALUE = 10;
\r
269 * The number of time scale values.
\r
271 * NOTE: This is an internal value. DO NOT USE IT.
\r
273 * @see #getTimeScaleValue
\r
276 * @deprecated This API is ICU internal only.
\r
278 public static final int MAX_SCALE_VALUE = 11;
\r
280 private static final long ticks = 1;
\r
281 private static final long microseconds = ticks * 10;
\r
282 private static final long milliseconds = microseconds * 1000;
\r
283 private static final long seconds = milliseconds * 1000;
\r
284 private static final long minutes = seconds * 60;
\r
285 private static final long hours = minutes * 60;
\r
286 private static final long days = hours * 24;
\r
289 * This class holds the data that describes a particular
\r
292 private static final class TimeScaleData
\r
294 TimeScaleData(long theUnits, long theEpochOffset,
\r
295 long theToMin, long theToMax,
\r
296 long theFromMin, long theFromMax)
\r
299 unitsRound = theUnits / 2;
\r
301 minRound = Long.MIN_VALUE + unitsRound;
\r
302 maxRound = Long.MAX_VALUE - unitsRound;
\r
304 epochOffset = theEpochOffset / theUnits;
\r
306 if (theUnits == 1) {
\r
307 epochOffsetP1 = epochOffsetM1 = epochOffset;
\r
309 epochOffsetP1 = epochOffset + 1;
\r
310 epochOffsetM1 = epochOffset - 1;
\r
316 fromMin = theFromMin;
\r
317 fromMax = theFromMax;
\r
327 long epochOffsetP1;
\r
328 long epochOffsetM1;
\r
334 private static final TimeScaleData[] timeScaleTable = {
\r
335 new TimeScaleData(milliseconds, 621355968000000000L, -9223372036854774999L, 9223372036854774999L, -984472800485477L, 860201606885477L), // JAVA_TIME
\r
336 new TimeScaleData(seconds, 621355968000000000L, -9223372036854775808L, 9223372036854775807L, -984472800485L, 860201606885L), // UNIX_TIME
\r
337 new TimeScaleData(milliseconds, 621355968000000000L, -9223372036854774999L, 9223372036854774999L, -984472800485477L, 860201606885477L), // ICU4C_TIME
\r
338 new TimeScaleData(ticks, 504911232000000000L, -8718460804854775808L, 9223372036854775807L, -9223372036854775808L, 8718460804854775807L), // WINDOWS_FILE_TIME
\r
339 new TimeScaleData(ticks, 000000000000000000L, -9223372036854775808L, 9223372036854775807L, -9223372036854775808L, 9223372036854775807L), // DOTNET_DATE_TIME
\r
340 new TimeScaleData(seconds, 600527520000000000L, -9223372036854775808L, 9223372036854775807L, -982389955685L, 862284451685L), // MAC_OLD_TIME
\r
341 new TimeScaleData(seconds, 631139040000000000L, -9223372036854775808L, 9223372036854775807L, -985451107685L, 859223299685L), // MAC_TIME
\r
342 new TimeScaleData(days, 599265216000000000L, -9223372036854775808L, 9223372036854775807L, -11368793L, 9981605L), // EXCEL_TIME
\r
343 new TimeScaleData(days, 599265216000000000L, -9223372036854775808L, 9223372036854775807L, -11368793L, 9981605L), // DB2_TIME
\r
344 new TimeScaleData(microseconds, 621355968000000000L, -9223372036854775804L, 9223372036854775804L, -984472800485477580L, 860201606885477580L) // UNIX_MICROSECONDS_TIME
\r
349 * Prevent construction of this class.
\r
352 private UniversalTimeScale()
\r
359 * Convert a <code>long</code> datetime from the given time scale to the universal time scale.
\r
361 * @param otherTime The <code>long</code> datetime
\r
362 * @param timeScale The time scale to convert from
\r
364 * @return The datetime converted to the universal time scale
\r
368 public static long from(long otherTime, int timeScale)
\r
370 TimeScaleData data = fromRangeCheck(otherTime, timeScale);
\r
372 return (otherTime + data.epochOffset) * data.units;
\r
376 * Convert a <code>double</code> datetime from the given time scale to the universal time scale.
\r
377 * All calculations are done using <code>BigDecimal</code> to guarantee that the value
\r
378 * does not go out of range.
\r
380 * @param otherTime The <code>double</code> datetime
\r
381 * @param timeScale The time scale to convert from
\r
383 * @return The datetime converted to the universal time scale
\r
387 public static BigDecimal bigDecimalFrom(double otherTime, int timeScale)
\r
389 TimeScaleData data = getTimeScaleData(timeScale);
\r
390 BigDecimal other = new BigDecimal(String.valueOf(otherTime));
\r
391 BigDecimal units = new BigDecimal(data.units);
\r
392 BigDecimal epochOffset = new BigDecimal(data.epochOffset);
\r
394 return other.add(epochOffset).multiply(units);
\r
398 * Convert a <code>long</code> datetime from the given time scale to the universal time scale.
\r
399 * All calculations are done using <code>BigDecimal</code> to guarantee that the value
\r
400 * does not go out of range.
\r
402 * @param otherTime The <code>long</code> datetime
\r
403 * @param timeScale The time scale to convert from
\r
405 * @return The datetime converted to the universal time scale
\r
409 public static BigDecimal bigDecimalFrom(long otherTime, int timeScale)
\r
411 TimeScaleData data = getTimeScaleData(timeScale);
\r
412 BigDecimal other = new BigDecimal(otherTime);
\r
413 BigDecimal units = new BigDecimal(data.units);
\r
414 BigDecimal epochOffset = new BigDecimal(data.epochOffset);
\r
416 return other.add(epochOffset).multiply(units);
\r
420 * Convert a <code>BigDecimal</code> datetime from the given time scale to the universal time scale.
\r
421 * All calculations are done using <code>BigDecimal</code> to guarantee that the value
\r
422 * does not go out of range.
\r
424 * @param otherTime The <code>BigDecimal</code> datetime
\r
425 * @param timeScale The time scale to convert from
\r
427 * @return The datetime converted to the universal time scale
\r
431 public static BigDecimal bigDecimalFrom(BigDecimal otherTime, int timeScale)
\r
433 TimeScaleData data = getTimeScaleData(timeScale);
\r
435 BigDecimal units = new BigDecimal(data.units);
\r
436 BigDecimal epochOffset = new BigDecimal(data.epochOffset);
\r
438 return otherTime.add(epochOffset).multiply(units);
\r
442 * Convert a datetime from the universal time scale stored as a <code>BigDecimal</code> to a
\r
443 * <code>long</code> in the given time scale.
\r
445 * Since this calculation requires a divide, we must round. The straight forward
\r
446 * way to round by adding half of the divisor will push the sum out of range for values
\r
447 * within have the divisor of the limits of the precision of a <code>long</code>. To get around this, we do
\r
448 * the rounding like this:
\r
451 * (universalTime - units + units/2) / units + 1
\r
455 * (i.e. we subtract units first to guarantee that we'll still be in range when we
\r
456 * add <code>units/2</code>. We then need to add one to the quotent to make up for the extra subtraction.
\r
457 * This simplifies to:
\r
460 * (universalTime - units/2) / units - 1
\r
464 * For negative values to round away from zero, we need to flip the signs:
\r
467 * (universalTime + units/2) / units + 1
\r
471 * Since we also need to subtract the epochOffset, we fold the <code>+/- 1</code>
\r
472 * into the offset value. (i.e. <code>epochOffsetP1</code>, <code>epochOffsetM1</code>.)
\r
474 * @param universalTime The datetime in the universal time scale
\r
475 * @param timeScale The time scale to convert to
\r
477 * @return The datetime converted to the given time scale
\r
481 public static long toLong(long universalTime, int timeScale)
\r
483 TimeScaleData data = toRangeCheck(universalTime, timeScale);
\r
485 if (universalTime < 0) {
\r
486 if (universalTime < data.minRound) {
\r
487 return (universalTime + data.unitsRound) / data.units - data.epochOffsetP1;
\r
490 return (universalTime - data.unitsRound) / data.units - data.epochOffset;
\r
493 if (universalTime > data.maxRound) {
\r
494 return (universalTime - data.unitsRound) / data.units - data.epochOffsetM1;
\r
497 return (universalTime + data.unitsRound) / data.units - data.epochOffset;
\r
501 * Convert a datetime from the universal time scale to a <code>BigDecimal</code> in the given time scale.
\r
503 * @param universalTime The datetime in the universal time scale
\r
504 * @param timeScale The time scale to convert to
\r
506 * @return The datetime converted to the given time scale
\r
510 public static BigDecimal toBigDecimal(long universalTime, int timeScale)
\r
512 TimeScaleData data = getTimeScaleData(timeScale);
\r
513 BigDecimal universal = new BigDecimal(universalTime);
\r
514 BigDecimal units = new BigDecimal(data.units);
\r
515 BigDecimal epochOffset = new BigDecimal(data.epochOffset);
\r
517 return universal.divide(units, BigDecimal.ROUND_HALF_UP).subtract(epochOffset);
\r
521 * Convert a datetime from the universal time scale to a <code>BigDecimal</code> in the given time scale.
\r
523 * @param universalTime The datetime in the universal time scale
\r
524 * @param timeScale The time scale to convert to
\r
526 * @return The datetime converted to the given time scale
\r
530 public static BigDecimal toBigDecimal(BigDecimal universalTime, int timeScale)
\r
532 TimeScaleData data = getTimeScaleData(timeScale);
\r
533 BigDecimal units = new BigDecimal(data.units);
\r
534 BigDecimal epochOffset = new BigDecimal(data.epochOffset);
\r
536 return universalTime.divide(units, BigDecimal.ROUND_HALF_UP).subtract(epochOffset);
\r
540 * Return the <code>TimeScaleData</code> object for the given time
\r
543 * @param scale - the time scale
\r
544 * @return the <code>TimeScaleData</code> object for the given time scale
\r
546 private static TimeScaleData getTimeScaleData(int scale)
\r
548 if (scale < 0 || scale >= MAX_SCALE) {
\r
549 throw new IllegalArgumentException("scale out of range: " + scale);
\r
552 return timeScaleTable[scale];
\r
556 * Get a value associated with a particular time scale.
\r
558 * @param scale - the time scale
\r
559 * @param value - a constant representing the value to get
\r
561 * @return - the value.
\r
565 public static long getTimeScaleValue(int scale, int value)
\r
567 TimeScaleData data = getTimeScaleData(scale);
\r
574 case EPOCH_OFFSET_VALUE:
\r
575 return data.epochOffset;
\r
577 case FROM_MIN_VALUE:
\r
578 return data.fromMin;
\r
580 case FROM_MAX_VALUE:
\r
581 return data.fromMax;
\r
589 case EPOCH_OFFSET_PLUS_1_VALUE:
\r
590 return data.epochOffsetP1;
\r
592 case EPOCH_OFFSET_MINUS_1_VALUE:
\r
593 return data.epochOffsetM1;
\r
595 case UNITS_ROUND_VALUE:
\r
596 return data.unitsRound;
\r
598 case MIN_ROUND_VALUE:
\r
599 return data.minRound;
\r
601 case MAX_ROUND_VALUE:
\r
602 return data.maxRound;
\r
605 throw new IllegalArgumentException("value out of range: " + value);
\r
609 private static TimeScaleData toRangeCheck(long universalTime, int scale)
\r
611 TimeScaleData data = getTimeScaleData(scale);
\r
613 if (universalTime >= data.toMin && universalTime <= data.toMax) {
\r
617 throw new IllegalArgumentException("universalTime out of range:" + universalTime);
\r
620 private static TimeScaleData fromRangeCheck(long otherTime, int scale)
\r
622 TimeScaleData data = getTimeScaleData(scale);
\r
624 if (otherTime >= data.fromMin && otherTime <= data.fromMax) {
\r
628 throw new IllegalArgumentException("otherTime out of range:" + otherTime);
\r
632 * Convert a time in the Universal Time Scale into another time
\r
633 * scale. The division used to do the conversion rounds down.
\r
635 * NOTE: This is an internal routine used by the tool that
\r
636 * generates the to and from limits. Use it at your own risk.
\r
638 * @param universalTime the time in the Universal Time scale
\r
639 * @param timeScale the time scale to convert to
\r
640 * @return the time in the given time scale
\r
643 * @deprecated This API is ICU internal only.
\r
645 public static BigDecimal toBigDecimalTrunc(BigDecimal universalTime, int timeScale)
\r
647 TimeScaleData data = getTimeScaleData(timeScale);
\r
648 BigDecimal units = new BigDecimal(data.units);
\r
649 BigDecimal epochOffset = new BigDecimal(data.epochOffset);
\r
651 return universalTime.divide(units, BigDecimal.ROUND_DOWN).subtract(epochOffset);
\r