Class UniversalTimeScale
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intUsed in DB2.static final intUsed in the .NET framework'sSystem.DateTimestructure.static final intDeprecated.This API is ICU internal only.static final intThe constant used to select the epoch plus one value for a time scale.static final intThe constant used to select the epoch offset value for a time scale.static final intUsed in Excel.static final intThe constant used to select the maximum from value for a time scale.static final intThe constant used to select the minimum from value for a time scale.static final intUsed in the ICU4C.static final intUsed in the JDK.static final intUsed in older Macintosh systems.static final intUsed in the JDK.static final intDeprecated.This API is ICU internal only.static final intDeprecated.ICU 59static final intDeprecated.This API is ICU internal only.static final intDeprecated.This API is ICU internal only.static final intThe constant used to select the maximum to value for a time scale.static final intThe constant used to select the minimum to value for a time scale.static final intDeprecated.This API is ICU internal only.static final intThe constant used to select the units value for a time scale.static final intData is along.static final intUsed in Unix systems.static final intUsed in Windows for file times. -
Method Summary
Modifier and TypeMethodDescriptionstatic BigDecimalbigDecimalFrom(double otherTime, int timeScale) Convert adoubledatetime from the given time scale to the universal time scale.static BigDecimalbigDecimalFrom(long otherTime, int timeScale) Convert alongdatetime from the given time scale to the universal time scale.static BigDecimalbigDecimalFrom(BigDecimal otherTime, int timeScale) Convert aBigDecimaldatetime from the given time scale to the universal time scale.static longfrom(long otherTime, int timeScale) Convert alongdatetime from the given time scale to the universal time scale.static longgetTimeScaleValue(int scale, int value) Get a value associated with a particular time scale.static BigDecimaltoBigDecimal(long universalTime, int timeScale) Convert a datetime from the universal time scale to aBigDecimalin the given time scale.static BigDecimaltoBigDecimal(BigDecimal universalTime, int timeScale) Convert a datetime from the universal time scale to aBigDecimalin the given time scale.static BigDecimaltoBigDecimalTrunc(BigDecimal universalTime, int timeScale) Deprecated.This API is ICU internal only.static longtoLong(long universalTime, int timeScale) Convert a datetime from the universal time scale stored as aBigDecimalto alongin the given time scale.
-
Field Details
-
JAVA_TIME
public static final int JAVA_TIMEUsed in the JDK. Data is along. Value is milliseconds since January 1, 1970.- See Also:
-
UNIX_TIME
public static final int UNIX_TIMEUsed in Unix systems. Data is anintor along. Value is seconds since January 1, 1970.- See Also:
-
ICU4C_TIME
public static final int ICU4C_TIMEUsed in the ICU4C. Data is adouble. Value is milliseconds since January 1, 1970.- See Also:
-
WINDOWS_FILE_TIME
public static final int WINDOWS_FILE_TIMEUsed in Windows for file times. Data is along. Value is ticks (1 tick == 100 nanoseconds) since January 1, 1601.- See Also:
-
DOTNET_DATE_TIME
public static final int DOTNET_DATE_TIMEUsed in the .NET framework'sSystem.DateTimestructure. Data is along. Value is ticks (1 tick == 100 nanoseconds) since January 1, 0001.- See Also:
-
MAC_OLD_TIME
public static final int MAC_OLD_TIMEUsed in older Macintosh systems. Data is anint. Value is seconds since January 1, 1904.- See Also:
-
MAC_TIME
public static final int MAC_TIMEUsed in the JDK. Data is adouble. Value is milliseconds since January 1, 2001.- See Also:
-
EXCEL_TIME
public static final int EXCEL_TIMEUsed in Excel. Data is a?unknown?. Value is days since December 31, 1899.- See Also:
-
DB2_TIME
public static final int DB2_TIMEUsed in DB2. Data is a?unknown?. Value is days since December 31, 1899.- See Also:
-
UNIX_MICROSECONDS_TIME
public static final int UNIX_MICROSECONDS_TIMEData is along. Value is microseconds since January 1, 1970. Similar to Unix time (linear value from 1970) and struct timeval (microseconds resolution).- See Also:
-
MAX_SCALE
Deprecated.ICU 59This is the first unused time scale value.- See Also:
-
UNITS_VALUE
public static final int UNITS_VALUEThe constant used to select the units value for a time scale.- See Also:
-
EPOCH_OFFSET_VALUE
public static final int EPOCH_OFFSET_VALUEThe constant used to select the epoch offset value for a time scale.- See Also:
-
FROM_MIN_VALUE
public static final int FROM_MIN_VALUEThe constant used to select the minimum from value for a time scale.- See Also:
-
FROM_MAX_VALUE
public static final int FROM_MAX_VALUEThe constant used to select the maximum from value for a time scale.- See Also:
-
TO_MIN_VALUE
public static final int TO_MIN_VALUEThe constant used to select the minimum to value for a time scale.- See Also:
-
TO_MAX_VALUE
public static final int TO_MAX_VALUEThe constant used to select the maximum to value for a time scale.- See Also:
-
EPOCH_OFFSET_PLUS_1_VALUE
public static final int EPOCH_OFFSET_PLUS_1_VALUEThe constant used to select the epoch plus one value for a time scale. NOTE: This is an internal value. DO NOT USE IT. May not actually be equal to the epoch offset value plus one.- See Also:
-
EPOCH_OFFSET_MINUS_1_VALUE
Deprecated.This API is ICU internal only.The constant used to select the epoch offset minus one value for a time scale. NOTE: This is an internal value. DO NOT USE IT. May not actually be equal to the epoch offset value minus one.- See Also:
-
UNITS_ROUND_VALUE
Deprecated.This API is ICU internal only.The constant used to select the units round value for a time scale. NOTE: This is an internal value. DO NOT USE IT.- See Also:
-
MIN_ROUND_VALUE
Deprecated.This API is ICU internal only.The constant used to select the minimum safe rounding value for a time scale. NOTE: This is an internal value. DO NOT USE IT.- See Also:
-
MAX_ROUND_VALUE
Deprecated.This API is ICU internal only.The constant used to select the maximum safe rounding value for a time scale. NOTE: This is an internal value. DO NOT USE IT.- See Also:
-
MAX_SCALE_VALUE
Deprecated.This API is ICU internal only.The number of time scale values. NOTE: This is an internal value. DO NOT USE IT.- See Also:
-
-
Method Details
-
from
public static long from(long otherTime, int timeScale) Convert alongdatetime from the given time scale to the universal time scale.- Parameters:
otherTime- ThelongdatetimetimeScale- The time scale to convert from- Returns:
- The datetime converted to the universal time scale
-
bigDecimalFrom
Convert adoubledatetime from the given time scale to the universal time scale. All calculations are done usingBigDecimalto guarantee that the value does not go out of range.- Parameters:
otherTime- ThedoubledatetimetimeScale- The time scale to convert from- Returns:
- The datetime converted to the universal time scale
-
bigDecimalFrom
Convert alongdatetime from the given time scale to the universal time scale. All calculations are done usingBigDecimalto guarantee that the value does not go out of range.- Parameters:
otherTime- ThelongdatetimetimeScale- The time scale to convert from- Returns:
- The datetime converted to the universal time scale
-
bigDecimalFrom
Convert aBigDecimaldatetime from the given time scale to the universal time scale. All calculations are done usingBigDecimalto guarantee that the value does not go out of range.- Parameters:
otherTime- TheBigDecimaldatetimetimeScale- The time scale to convert from- Returns:
- The datetime converted to the universal time scale
-
toLong
public static long toLong(long universalTime, int timeScale) Convert a datetime from the universal time scale stored as aBigDecimalto alongin 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 along. 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 - 1For negative values to round away from zero, we need to flip the signs:
(universalTime + units/2) / units + 1Since we also need to subtract the epochOffset, we fold the
+/- 1into the offset value. (i.e.epochOffsetP1,epochOffsetM1.)- Parameters:
universalTime- The datetime in the universal time scaletimeScale- The time scale to convert to- Returns:
- The datetime converted to the given time scale
-
toBigDecimal
Convert a datetime from the universal time scale to aBigDecimalin the given time scale.- Parameters:
universalTime- The datetime in the universal time scaletimeScale- The time scale to convert to- Returns:
- The datetime converted to the given time scale
-
toBigDecimal
Convert a datetime from the universal time scale to aBigDecimalin the given time scale.- Parameters:
universalTime- The datetime in the universal time scaletimeScale- The time scale to convert to- Returns:
- The datetime converted to the given time scale
-
getTimeScaleValue
public static long getTimeScaleValue(int scale, int value) Get a value associated with a particular time scale.- Parameters:
scale- - the time scalevalue- - a constant representing the value to get- Returns:
- - the value.
-
toBigDecimalTrunc
Deprecated.This API is ICU internal only.Convert a time in the Universal Time Scale into another time scale. The division used to do the conversion rounds down. NOTE: This is an internal routine used by the tool that generates the to and from limits. Use it at your own risk.- Parameters:
universalTime- the time in the Universal Time scaletimeScale- the time scale to convert to- Returns:
- the time in the given time scale
-