Top Description Inners Fields Constructors Methods
com.ibm.icu.util

public final Class UniversalTimeScale

extends Object
Class Inheritance
Imports
com.ibm.icu.math.BigDecimal

There are quite a few different conventions for binary datetime, depending on different platforms and protocols. Some of these have severe drawbacks. For example, people using Unix time (seconds since Jan 1, 1970, usually in a 32-bit integer) think that they are safe until near the year 2038. But cases can and do arise where arithmetic manipulations causes serious problems. Consider the computation of the average of two datetimes, for example: if one calculates them with 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.

ICU Status
Stable since ICU 3.2.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
UniversalTimeScale.TimeScaleData

This class holds the data that describes a particular time scale.

Field Summary

Modifier and TypeField and Description
private static final long
public static final int
DB2_TIME

Used in DB2.

public static final int
DOTNET_DATE_TIME

Used in the .NET framework's System.DateTime structure.

public static final int
EPOCH_OFFSET_MINUS_1_VALUE

The constant used to select the epoch offset minus one value for a time scale.

public static final int
EPOCH_OFFSET_PLUS_1_VALUE

The constant used to select the epoch plus one value for a time scale.

public static final int
EPOCH_OFFSET_VALUE

The constant used to select the epoch offset value for a time scale.

public static final int
EXCEL_TIME

Used in Excel.

public static final int
FROM_MAX_VALUE

The constant used to select the maximum from value for a time scale.

public static final int
FROM_MIN_VALUE

The constant used to select the minimum from value for a time scale.

private static final long
public static final int
ICU4C_TIME

Used in the ICU4C.

public static final int
JAVA_TIME

Used in the JDK.

public static final int
MAC_OLD_TIME

Used in older Macintosh systems.

public static final int
MAC_TIME

Used in the JDK.

public static final int
MAX_ROUND_VALUE

The constant used to select the maximum safe rounding value for a time scale.

public static final int
MAX_SCALE

This is the first unused time scale value.

public static final int
MAX_SCALE_VALUE

The number of time scale values.

private static final long
private static final long
public static final int
MIN_ROUND_VALUE

The constant used to select the minimum safe rounding value for a time scale.

private static final long
private static final long
private static final long
private static final UniversalTimeScale.TimeScaleData[]
public static final int
TO_MAX_VALUE

The constant used to select the maximum to value for a time scale.

public static final int
TO_MIN_VALUE

The constant used to select the minimum to value for a time scale.

public static final int
UNITS_ROUND_VALUE

The constant used to select the units round value for a time scale.

public static final int
UNITS_VALUE

The constant used to select the units value for a time scale.

public static final int
UNIX_MICROSECONDS_TIME

Data is a long.

public static final int
UNIX_TIME

Used in Unix systems.

public static final int
WINDOWS_FILE_TIME

Used in Windows for file times.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static BigDecimal

Returns:

The datetime converted to the universal time scale
bigDecimalFrom
(double
The double datetime
otherTime
,
int
The time scale to convert from
timeScale
)

Convert a double datetime from the given time scale to the universal time scale.

public static BigDecimal

Returns:

The datetime converted to the universal time scale
bigDecimalFrom
(long
The long datetime
otherTime
,
int
The time scale to convert from
timeScale
)

Convert a long datetime from the given time scale to the universal time scale.

public static BigDecimal

Returns:

The datetime converted to the universal time scale
bigDecimalFrom
(BigDecimal
The BigDecimal datetime
otherTime
,
int
The time scale to convert from
timeScale
)

Convert a BigDecimal datetime from the given time scale to the universal time scale.

public static long

Returns:

The datetime converted to the universal time scale
from
(long
The long datetime
otherTime
,
int
The time scale to convert from
timeScale
)

Convert a long datetime from the given time scale to the universal time scale.

private static UniversalTimeScale.TimeScaleData
fromRangeCheck(long otherTime, int scale)

private static UniversalTimeScale.TimeScaleData

Returns:

the TimeScaleData object for the given time scale
getTimeScaleData
(int
- the time scale
scale
)

Return the TimeScaleData object for the given time scale.

public static long

Returns:

- the value.
getTimeScaleValue
(int
- the time scale
scale
,
int
- a constant representing the value to get
value
)

Get a value associated with a particular time scale.

public static BigDecimal

Returns:

The datetime converted to the given time scale
toBigDecimal
(long
The datetime in the universal time scale
universalTime
,
int
The time scale to convert to
timeScale
)

Convert a datetime from the universal time scale to a BigDecimal in the given time scale.

public static BigDecimal

Returns:

The datetime converted to the given time scale
toBigDecimal
(BigDecimal
The datetime in the universal time scale
universalTime
,
int
The time scale to convert to
timeScale
)

Convert a datetime from the universal time scale to a BigDecimal in the given time scale.

public static BigDecimal

Returns:

the time in the given time scale
toBigDecimalTrunc
(BigDecimal
the time in the Universal Time scale
universalTime
,
int
the time scale to convert to
timeScale
)

Deprecated This API is ICU internal only.
Convert a time in the Universal Time Scale into another time scale.
public static long

Returns:

The datetime converted to the given time scale
toLong
(long
The datetime in the universal time scale
universalTime
,
int
The time scale to convert to
timeScale
)

Convert a datetime from the universal time scale stored as a BigDecimal to a long in the given time scale.

private static UniversalTimeScale.TimeScaleData
toRangeCheck(long universalTime, int scale)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

daysback to summary
private static final long days
DB2_TIMEback to summary
public static final int DB2_TIME

Used in DB2. Data is a ?unknown?. Value is days since December 31, 1899.

ICU Status
Stable since ICU 3.2.
DOTNET_DATE_TIMEback to summary
public static final int DOTNET_DATE_TIME

Used in the .NET framework's System.DateTime structure. Data is a long. Value is ticks (1 tick == 100 nanoseconds) since January 1, 0001.

ICU Status
Stable since ICU 3.2.
EPOCH_OFFSET_MINUS_1_VALUEback to summary
public static final int 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.

Annotations
@Deprecated
ICU Status
Internal ICU component.
See Also
getTimeScaleValue
EPOCH_OFFSET_PLUS_1_VALUEback to summary
public static final int EPOCH_OFFSET_PLUS_1_VALUE

The 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.

ICU Status
Stable since ICU 3.2.
See Also
getTimeScaleValue
EPOCH_OFFSET_VALUEback to summary
public static final int EPOCH_OFFSET_VALUE

The constant used to select the epoch offset value for a time scale.

ICU Status
Stable since ICU 3.2.
See Also
getTimeScaleValue
EXCEL_TIMEback to summary
public static final int EXCEL_TIME

Used in Excel. Data is a ?unknown?. Value is days since December 31, 1899.

ICU Status
Stable since ICU 3.2.
FROM_MAX_VALUEback to summary
public static final int FROM_MAX_VALUE

The constant used to select the maximum from value for a time scale.

ICU Status
Stable since ICU 3.2.
See Also
getTimeScaleValue
FROM_MIN_VALUEback to summary
public static final int FROM_MIN_VALUE

The constant used to select the minimum from value for a time scale.

ICU Status
Stable since ICU 3.2.
See Also
getTimeScaleValue
hoursback to summary
private static final long hours
ICU4C_TIMEback to summary
public static final int ICU4C_TIME

Used in the ICU4C. Data is a double. Value is milliseconds since January 1, 1970.

ICU Status
Stable since ICU 3.2.
JAVA_TIMEback to summary
public static final int JAVA_TIME

Used in the JDK. Data is a long. Value is milliseconds since January 1, 1970.

ICU Status
Stable since ICU 3.2.
MAC_OLD_TIMEback to summary
public static final int MAC_OLD_TIME

Used in older Macintosh systems. Data is an int. Value is seconds since January 1, 1904.

ICU Status
Stable since ICU 3.2.
MAC_TIMEback to summary
public static final int MAC_TIME

Used in the JDK. Data is a double. Value is milliseconds since January 1, 2001.

ICU Status
Stable since ICU 3.2.
MAX_ROUND_VALUEback to summary
public static final int 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.

Annotations
@Deprecated
ICU Status
Internal ICU component.
See Also
getTimeScaleValue
MAX_SCALEback to summary
public static final int MAX_SCALE

Deprecated

ICU 59

This is the first unused time scale value.

Annotations
@Deprecated
MAX_SCALE_VALUEback to summary
public static final int 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.

Annotations
@Deprecated
ICU Status
Internal ICU component.
See Also
getTimeScaleValue
microsecondsback to summary
private static final long microseconds
millisecondsback to summary
private static final long milliseconds
MIN_ROUND_VALUEback to summary
public static final int 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.

Annotations
@Deprecated
ICU Status
Internal ICU component.
See Also
getTimeScaleValue
minutesback to summary
private static final long minutes
secondsback to summary
private static final long seconds
ticksback to summary
private static final long ticks
timeScaleTableback to summary
private static final UniversalTimeScale.TimeScaleData[] timeScaleTable
TO_MAX_VALUEback to summary
public static final int TO_MAX_VALUE

The constant used to select the maximum to value for a time scale.

ICU Status
Stable since ICU 3.2.
See Also
getTimeScaleValue
TO_MIN_VALUEback to summary
public static final int TO_MIN_VALUE

The constant used to select the minimum to value for a time scale.

ICU Status
Stable since ICU 3.2.
See Also
getTimeScaleValue
UNITS_ROUND_VALUEback to summary
public static final int 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.

Annotations
@Deprecated
ICU Status
Internal ICU component.
See Also
getTimeScaleValue
UNITS_VALUEback to summary
public static final int UNITS_VALUE

The constant used to select the units value for a time scale.

ICU Status
Stable since ICU 3.2.
UNIX_MICROSECONDS_TIMEback to summary
public static final int UNIX_MICROSECONDS_TIME

Data is a long. Value is microseconds since January 1, 1970. Similar to Unix time (linear value from 1970) and struct timeval (microseconds resolution).

ICU Status
Stable since ICU 3.8.
UNIX_TIMEback to summary
public static final int UNIX_TIME

Used in Unix systems. Data is an int or a long. Value is seconds since January 1, 1970.

ICU Status
Stable since ICU 3.2.
WINDOWS_FILE_TIMEback to summary
public static final int WINDOWS_FILE_TIME

Used in Windows for file times. Data is a long. Value is ticks (1 tick == 100 nanoseconds) since January 1, 1601.

ICU Status
Stable since ICU 3.2.

Constructor Detail

UniversalTimeScaleback to summary
private UniversalTimeScale()

Method Detail

bigDecimalFromback to summary
public static BigDecimal bigDecimalFrom(double otherTime, int timeScale)

Convert a 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.

Parameters
otherTime:double

The double datetime

timeScale:int

The time scale to convert from

Returns:BigDecimal

The datetime converted to the universal time scale

ICU Status
Stable since ICU 3.2.
bigDecimalFromback to summary
public static BigDecimal bigDecimalFrom(long otherTime, int timeScale)

Convert a 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.

Parameters
otherTime:long

The long datetime

timeScale:int

The time scale to convert from

Returns:BigDecimal

The datetime converted to the universal time scale

ICU Status
Stable since ICU 3.2.
bigDecimalFromback to summary
public static BigDecimal bigDecimalFrom(BigDecimal otherTime, int timeScale)

Convert a 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.

Parameters
otherTime:BigDecimal

The BigDecimal datetime

timeScale:int

The time scale to convert from

Returns:BigDecimal

The datetime converted to the universal time scale

ICU Status
Stable since ICU 3.2.
fromback to summary
public static long from(long otherTime, int timeScale)

Convert a long datetime from the given time scale to the universal time scale.

Parameters
otherTime:long

The long datetime

timeScale:int

The time scale to convert from

Returns:long

The datetime converted to the universal time scale

ICU Status
Stable since ICU 3.2.
fromRangeCheckback to summary
private static UniversalTimeScale.TimeScaleData fromRangeCheck(long otherTime, int scale)
getTimeScaleDataback to summary
private static UniversalTimeScale.TimeScaleData getTimeScaleData(int scale)

Return the TimeScaleData object for the given time scale.

Parameters
scale:int

- the time scale

Returns:UniversalTimeScale.TimeScaleData

the TimeScaleData object for the given time scale

getTimeScaleValueback to summary
public static long getTimeScaleValue(int scale, int value)

Get a value associated with a particular time scale.

Parameters
scale:int

- the time scale

value:int

- a constant representing the value to get

Returns:long

- the value.

ICU Status
Stable since ICU 3.2.
toBigDecimalback to summary
public static BigDecimal toBigDecimal(long universalTime, int timeScale)

Convert a datetime from the universal time scale to a BigDecimal in the given time scale.

Parameters
universalTime:long

The datetime in the universal time scale

timeScale:int

The time scale to convert to

Returns:BigDecimal

The datetime converted to the given time scale

ICU Status
Stable since ICU 3.2.
toBigDecimalback to summary
public static BigDecimal toBigDecimal(BigDecimal universalTime, int timeScale)

Convert a datetime from the universal time scale to a BigDecimal in the given time scale.

Parameters
universalTime:BigDecimal

The datetime in the universal time scale

timeScale:int

The time scale to convert to

Returns:BigDecimal

The datetime converted to the given time scale

ICU Status
Stable since ICU 3.2.
toBigDecimalTruncback to summary
public static BigDecimal toBigDecimalTrunc(BigDecimal universalTime, int timeScale)

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:BigDecimal

the time in the Universal Time scale

timeScale:int

the time scale to convert to

Returns:BigDecimal

the time in the given time scale

Annotations
@Deprecated
ICU Status
Internal ICU component.
toLongback to summary
public 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. 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.)

Parameters
universalTime:long

The datetime in the universal time scale

timeScale:int

The time scale to convert to

Returns:long

The datetime converted to the given time scale

ICU Status
Stable since ICU 3.2.
toRangeCheckback to summary
private static UniversalTimeScale.TimeScaleData toRangeCheck(long universalTime, int scale)
com.ibm.icu.util back to summary

private final Class UniversalTimeScale.TimeScaleData

extends Object
Class Inheritance

This class holds the data that describes a particular time scale.

Field Summary

Modifier and TypeField and Description
pack-priv long
pack-priv long
pack-priv long
pack-priv long
pack-priv long
pack-priv long
pack-priv long
pack-priv long
pack-priv long
pack-priv long
pack-priv long

Constructor Summary

AccessConstructor and Description
pack-priv
TimeScaleData(long theUnits, long theEpochOffset, long theToMin, long theToMax, long theFromMin, long theFromMax)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

epochOffsetback to summary
pack-priv long epochOffset
epochOffsetM1back to summary
pack-priv long epochOffsetM1
epochOffsetP1back to summary
pack-priv long epochOffsetP1
fromMaxback to summary
pack-priv long fromMax
fromMinback to summary
pack-priv long fromMin
maxRoundback to summary
pack-priv long maxRound
minRoundback to summary
pack-priv long minRound
toMaxback to summary
pack-priv long toMax
toMinback to summary
pack-priv long toMin
unitsback to summary
pack-priv long units
unitsRoundback to summary
pack-priv long unitsRound

Constructor Detail

TimeScaleDataback to summary
pack-priv TimeScaleData(long theUnits, long theEpochOffset, long theToMin, long theToMax, long theFromMin, long theFromMax)