public final class UniversalTimeScale extends Object
averageTime = (time1 + time2)/2
, there will be overflow even with dates
beginning in 2004. Moreover, even if these problems don't occur, there is the issue of
conversion back and forth between different systems.
Binary datetimes differ in a number of ways: the datatype, the unit, and the epoch (origin). We refer to these as time scales.
ICU implements a universal time scale that is similar to the .NET framework's System.DateTime. The universal time scale is a 64-bit integer that holds ticks since midnight, January 1st, 0001. (One tick is 100 nanoseconds.) Negative values are supported. This has enough range to guarantee that calculations involving dates around the present are safe.
The universal time scale always measures time according to the proleptic Gregorian calendar. That is, the Gregorian calendar's leap year rules are used for all times, even before 1582 when it was introduced. (This is different from the default ICU calendar which switches from the Julian to the Gregorian calendar in 1582. See GregorianCalendar.setGregorianChange() and ucal_setGregorianChange().) ICU provides conversion functions to and from all other major time scales, allowing datetimes in any time scale to be converted to the universal time scale, safely manipulated, and converted back to any other datetime time scale.
For more details and background, see the Universal Time Scale chapter in the ICU User Guide.
Modifier and Type | Field and Description |
---|---|
static int |
DB2_TIME
Used in DB2.
|
static int |
DOTNET_DATE_TIME
Used in the .NET framework's
System.DateTime structure. |
static int |
EPOCH_OFFSET_MINUS_1_VALUE
Deprecated.
This API is ICU internal only.
|
static int |
EPOCH_OFFSET_PLUS_1_VALUE
The constant used to select the epoch plus one value
for a time scale.
|
static int |
EPOCH_OFFSET_VALUE
The constant used to select the epoch offset value
for a time scale.
|
static int |
EXCEL_TIME
Used in Excel.
|
static int |
FROM_MAX_VALUE
The constant used to select the maximum from value
for a time scale.
|
static int |
FROM_MIN_VALUE
The constant used to select the minimum from value
for a time scale.
|
static int |
ICU4C_TIME
Used in the ICU4C.
|
static int |
JAVA_TIME
Used in the JDK.
|
static int |
MAC_OLD_TIME
Used in older Macintosh systems.
|
static int |
MAC_TIME
Used in the JDK.
|
static int |
MAX_ROUND_VALUE
Deprecated.
This API is ICU internal only.
|
static int |
MAX_SCALE
Deprecated.
ICU 59
|
static int |
MAX_SCALE_VALUE
Deprecated.
This API is ICU internal only.
|
static int |
MIN_ROUND_VALUE
Deprecated.
This API is ICU internal only.
|
static int |
TO_MAX_VALUE
The constant used to select the maximum to value
for a time scale.
|
static int |
TO_MIN_VALUE
The constant used to select the minimum to value
for a time scale.
|
static int |
UNITS_ROUND_VALUE
Deprecated.
This API is ICU internal only.
|
static int |
UNITS_VALUE
The constant used to select the units value
for a time scale.
|
static int |
UNIX_MICROSECONDS_TIME
Data is a
long . |
static int |
UNIX_TIME
Used in Unix systems.
|
static int |
WINDOWS_FILE_TIME
Used in Windows for file times.
|
Modifier and Type | Method and Description |
---|---|
static BigDecimal |
bigDecimalFrom(BigDecimal otherTime,
int timeScale)
Convert a
BigDecimal datetime from the given time scale to the universal time scale. |
static BigDecimal |
bigDecimalFrom(double otherTime,
int timeScale)
Convert a
double datetime from the given time scale to the universal time scale. |
static BigDecimal |
bigDecimalFrom(long otherTime,
int timeScale)
Convert a
long datetime from the given time scale to the universal time scale. |
static long |
from(long otherTime,
int timeScale)
Convert a
long datetime from the given time scale to the universal time scale. |
static long |
getTimeScaleValue(int scale,
int value)
Get a value associated with a particular time scale.
|
static BigDecimal |
toBigDecimal(BigDecimal universalTime,
int timeScale)
Convert a datetime from the universal time scale to a
BigDecimal in the given time scale. |
static BigDecimal |
toBigDecimal(long universalTime,
int timeScale)
Convert a datetime from the universal time scale to a
BigDecimal in the given time scale. |
static BigDecimal |
toBigDecimalTrunc(BigDecimal universalTime,
int timeScale)
Deprecated.
This API is ICU internal only.
|
static long |
toLong(long universalTime,
int timeScale)
Convert a datetime from the universal time scale stored as a
BigDecimal to a
long in the given time scale. |
public static final int JAVA_TIME
long
. Value
is milliseconds since January 1, 1970.public static final int UNIX_TIME
int
or a long
. Value
is seconds since January 1, 1970.public static final int ICU4C_TIME
double
. Value
is milliseconds since January 1, 1970.public static final int WINDOWS_FILE_TIME
long
. Value
is ticks (1 tick == 100 nanoseconds) since January 1, 1601.public static final int DOTNET_DATE_TIME
System.DateTime
structure.
Data is a long
. Value is ticks (1 tick == 100 nanoseconds) since January 1, 0001.public static final int MAC_OLD_TIME
int
. Value
is seconds since January 1, 1904.public static final int MAC_TIME
double
. Value
is milliseconds since January 1, 2001.public static final int EXCEL_TIME
?unknown?
. Value
is days since December 31, 1899.public static final int DB2_TIME
?unknown?
. Value
is days since December 31, 1899.public static final int UNIX_MICROSECONDS_TIME
long
. Value is microseconds since January 1, 1970.
Similar to Unix time (linear value from 1970) and struct timeval
(microseconds resolution).@Deprecated public static final int MAX_SCALE
public static final int UNITS_VALUE
public static final int EPOCH_OFFSET_VALUE
getTimeScaleValue(int, int)
,
Constant Field Valuespublic static final int FROM_MIN_VALUE
getTimeScaleValue(int, int)
,
Constant Field Valuespublic static final int FROM_MAX_VALUE
getTimeScaleValue(int, int)
,
Constant Field Valuespublic static final int TO_MIN_VALUE
getTimeScaleValue(int, int)
,
Constant Field Valuespublic static final int TO_MAX_VALUE
getTimeScaleValue(int, int)
,
Constant Field Valuespublic static final int EPOCH_OFFSET_PLUS_1_VALUE
getTimeScaleValue(int, int)
,
Constant Field Values@Deprecated public static final int EPOCH_OFFSET_MINUS_1_VALUE
getTimeScaleValue(int, int)
,
Constant Field Values@Deprecated public static final int UNITS_ROUND_VALUE
getTimeScaleValue(int, int)
,
Constant Field Values@Deprecated public static final int MIN_ROUND_VALUE
getTimeScaleValue(int, int)
,
Constant Field Values@Deprecated public static final int MAX_ROUND_VALUE
getTimeScaleValue(int, int)
,
Constant Field Values@Deprecated public static final int MAX_SCALE_VALUE
getTimeScaleValue(int, int)
,
Constant Field Valuespublic static long from(long otherTime, int timeScale)
long
datetime from the given time scale to the universal time scale.otherTime
- The long
datetimetimeScale
- The time scale to convert frompublic static BigDecimal bigDecimalFrom(double otherTime, int timeScale)
double
datetime from the given time scale to the universal time scale.
All calculations are done using BigDecimal
to guarantee that the value
does not go out of range.otherTime
- The double
datetimetimeScale
- The time scale to convert frompublic static BigDecimal bigDecimalFrom(long otherTime, int timeScale)
long
datetime from the given time scale to the universal time scale.
All calculations are done using BigDecimal
to guarantee that the value
does not go out of range.otherTime
- The long
datetimetimeScale
- The time scale to convert frompublic static BigDecimal bigDecimalFrom(BigDecimal otherTime, int timeScale)
BigDecimal
datetime from the given time scale to the universal time scale.
All calculations are done using BigDecimal
to guarantee that the value
does not go out of range.otherTime
- The BigDecimal
datetimetimeScale
- The time scale to convert frompublic static long toLong(long universalTime, int timeScale)
BigDecimal
to a
long
in the given time scale.
Since this calculation requires a divide, we must round. The straight forward
way to round by adding half of the divisor will push the sum out of range for values
within have the divisor of the limits of the precision of a long
. To get around this, we do
the rounding like this:
(universalTime - units + units/2) / units + 1
(i.e. we subtract units first to guarantee that we'll still be in range when we
add units/2
. We then need to add one to the quotent to make up for the extra subtraction.
This simplifies to:
(universalTime - units/2) / units - 1
For negative values to round away from zero, we need to flip the signs:
(universalTime + units/2) / units + 1
Since we also need to subtract the epochOffset, we fold the +/- 1
into the offset value. (i.e. epochOffsetP1
, epochOffsetM1
.)
universalTime
- The datetime in the universal time scaletimeScale
- The time scale to convert topublic static BigDecimal toBigDecimal(long universalTime, int timeScale)
BigDecimal
in the given time scale.universalTime
- The datetime in the universal time scaletimeScale
- The time scale to convert topublic static BigDecimal toBigDecimal(BigDecimal universalTime, int timeScale)
BigDecimal
in the given time scale.universalTime
- The datetime in the universal time scaletimeScale
- The time scale to convert topublic static long getTimeScaleValue(int scale, int value)
scale
- - the time scalevalue
- - a constant representing the value to get@Deprecated public static BigDecimal toBigDecimalTrunc(BigDecimal universalTime, int timeScale)
universalTime
- the time in the Universal Time scaletimeScale
- the time scale to convert toCopyright © 2016 Unicode, Inc. and others.