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