jpayne@69: // © 2016 and later: Unicode, Inc. and others.
jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html
jpayne@69: /*
jpayne@69: *******************************************************************************
jpayne@69: * Copyright (C) 2004 - 2008, International Business Machines Corporation and
jpayne@69: * others. All Rights Reserved.
jpayne@69: *******************************************************************************
jpayne@69: */
jpayne@69:
jpayne@69: #ifndef UTMSCALE_H
jpayne@69: #define UTMSCALE_H
jpayne@69:
jpayne@69: #include "unicode/utypes.h"
jpayne@69:
jpayne@69: #if !UCONFIG_NO_FORMATTING
jpayne@69:
jpayne@69: /**
jpayne@69: * \file
jpayne@69: * \brief C API: Universal Time Scale
jpayne@69: *
jpayne@69: * There are quite a few different conventions for binary datetime, depending on different
jpayne@69: * platforms and protocols. Some of these have severe drawbacks. For example, people using
jpayne@69: * Unix time (seconds since Jan 1, 1970) think that they are safe until near the year 2038.
jpayne@69: * But cases can and do arise where arithmetic manipulations causes serious problems. Consider
jpayne@69: * the computation of the average of two datetimes, for example: if one calculates them with
jpayne@69: * averageTime = (time1 + time2)/2
, there will be overflow even with dates
jpayne@69: * around the present. Moreover, even if these problems don't occur, there is the issue of
jpayne@69: * conversion back and forth between different systems.
jpayne@69: *
jpayne@69: *
jpayne@69: * Binary datetimes differ in a number of ways: the datatype, the unit, jpayne@69: * and the epoch (origin). We'll refer to these as time scales. For example: jpayne@69: * jpayne@69: *
Source | jpayne@69: *Datatype | jpayne@69: *Unit | jpayne@69: *Epoch | jpayne@69: *
---|---|---|---|
UDTS_JAVA_TIME | jpayne@69: *int64_t | jpayne@69: *milliseconds | jpayne@69: *Jan 1, 1970 | jpayne@69: *
UDTS_UNIX_TIME | jpayne@69: *int32_t or int64_t | jpayne@69: *seconds | jpayne@69: *Jan 1, 1970 | jpayne@69: *
UDTS_ICU4C_TIME | jpayne@69: * jpayne@69: *double | jpayne@69: *milliseconds | jpayne@69: *Jan 1, 1970 | jpayne@69: *
UDTS_WINDOWS_FILE_TIME | jpayne@69: *int64_t | jpayne@69: * jpayne@69: *ticks (100 nanoseconds) | jpayne@69: *Jan 1, 1601 | jpayne@69: *
UDTS_DOTNET_DATE_TIME | jpayne@69: *int64_t | jpayne@69: *ticks (100 nanoseconds) | jpayne@69: * jpayne@69: *Jan 1, 0001 | jpayne@69: *
UDTS_MAC_OLD_TIME | jpayne@69: *int32_t or int64_t | jpayne@69: *seconds | jpayne@69: *Jan 1, 1904 | jpayne@69: * jpayne@69: *
UDTS_MAC_TIME | jpayne@69: *double | jpayne@69: *seconds | jpayne@69: *Jan 1, 2001 | jpayne@69: *
UDTS_EXCEL_TIME | jpayne@69: *? | jpayne@69: *days | jpayne@69: *Dec 31, 1899 | jpayne@69: *
UDTS_DB2_TIME | jpayne@69: *? | jpayne@69: *days | jpayne@69: *Dec 31, 1899 | jpayne@69: *
UDTS_UNIX_MICROSECONDS_TIME | jpayne@69: *int64_t | jpayne@69: *microseconds | jpayne@69: *Jan 1, 1970 | jpayne@69: *
jpayne@69: * All of the epochs start at 00:00 am (the earliest possible time on the day in question), jpayne@69: * and are assumed to be UTC. jpayne@69: * jpayne@69: *
jpayne@69: * The ranges for different datatypes are given in the following table (all values in years). jpayne@69: * The range of years includes the entire range expressible with positive and negative jpayne@69: * values of the datatype. The range of years for double is the range that would be allowed jpayne@69: * without losing precision to the corresponding unit. jpayne@69: * jpayne@69: *
Units | jpayne@69: *int64_t | jpayne@69: *double | jpayne@69: *int32_t | jpayne@69: *
---|---|---|---|
1 sec | jpayne@69: *5.84542x1011 | jpayne@69: *285,420,920.94 | jpayne@69: *136.10 | jpayne@69: *
1 millisecond | jpayne@69: *584,542,046.09 | jpayne@69: *285,420.92 | jpayne@69: *0.14 | jpayne@69: *
1 microsecond | jpayne@69: * jpayne@69: *584,542.05 | jpayne@69: *285.42 | jpayne@69: *0.00 | jpayne@69: *
100 nanoseconds (tick) | jpayne@69: *58,454.20 | jpayne@69: *28.54 | jpayne@69: *0.00 | jpayne@69: *
1 nanosecond | jpayne@69: *584.5420461 | jpayne@69: *0.2854 | jpayne@69: *0.00 | jpayne@69: *
jpayne@69: * These functions implement a universal time scale which can be used as a 'pivot', jpayne@69: * and provide conversion functions to and from all other major time scales. jpayne@69: * This datetimes to be converted to the pivot time, safely manipulated, jpayne@69: * and converted back to any other datetime time scale. jpayne@69: * jpayne@69: *
jpayne@69: * So what to use for this pivot? Java time has plenty of range, but cannot represent
jpayne@69: * .NET System.DateTime
values without severe loss of precision. ICU4C time addresses this by using a
jpayne@69: * double
that is otherwise equivalent to the Java time. However, there are disadvantages
jpayne@69: * with doubles
. They provide for much more graceful degradation in arithmetic operations.
jpayne@69: * But they only have 53 bits of accuracy, which means that they will lose precision when
jpayne@69: * converting back and forth to ticks. What would really be nice would be a
jpayne@69: * long double
(80 bits -- 64 bit mantissa), but that is not supported on most systems.
jpayne@69: *
jpayne@69: *
jpayne@69: * The Unix extended time uses a structure with two components: time in seconds and a
jpayne@69: * fractional field (microseconds). However, this is clumsy, slow, and
jpayne@69: * prone to error (you always have to keep track of overflow and underflow in the
jpayne@69: * fractional field). BigDecimal
would allow for arbitrary precision and arbitrary range,
jpayne@69: * but we do not want to use this as the normal type, because it is slow and does not
jpayne@69: * have a fixed size.
jpayne@69: *
jpayne@69: *
jpayne@69: * Because of these issues, we ended up concluding that the .NET framework's
jpayne@69: * System.DateTime
would be the best pivot. However, we use the full range
jpayne@69: * allowed by the datatype, allowing for datetimes back to 29,000 BC and up to 29,000 AD.
jpayne@69: * This time scale is very fine grained, does not lose precision, and covers a range that
jpayne@69: * will meet almost all requirements. It will not handle the range that Java times do,
jpayne@69: * but frankly, being able to handle dates before 29,000 BC or after 29,000 AD is of very limited interest.
jpayne@69: *
jpayne@69: */
jpayne@69:
jpayne@69: /**
jpayne@69: * UDateTimeScale
values are used to specify the time scale used for
jpayne@69: * conversion into or out if the universal time scale.
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: typedef enum UDateTimeScale {
jpayne@69: /**
jpayne@69: * Used in the JDK. Data is a Java long
(int64_t
). Value
jpayne@69: * is milliseconds since January 1, 1970.
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UDTS_JAVA_TIME = 0,
jpayne@69:
jpayne@69: /**
jpayne@69: * Used on Unix systems. Data is int32_t
or int64_t
. Value
jpayne@69: * is seconds since January 1, 1970.
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UDTS_UNIX_TIME,
jpayne@69:
jpayne@69: /**
jpayne@69: * Used in IUC4C. Data is a double
. Value
jpayne@69: * is milliseconds since January 1, 1970.
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UDTS_ICU4C_TIME,
jpayne@69:
jpayne@69: /**
jpayne@69: * Used in Windows for file times. Data is an int64_t
. Value
jpayne@69: * is ticks (1 tick == 100 nanoseconds) since January 1, 1601.
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UDTS_WINDOWS_FILE_TIME,
jpayne@69:
jpayne@69: /**
jpayne@69: * Used in the .NET framework's System.DateTime
structure. Data is an int64_t
. Value
jpayne@69: * is ticks (1 tick == 100 nanoseconds) since January 1, 0001.
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UDTS_DOTNET_DATE_TIME,
jpayne@69:
jpayne@69: /**
jpayne@69: * Used in older Macintosh systems. Data is int32_t
or int64_t
. Value
jpayne@69: * is seconds since January 1, 1904.
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UDTS_MAC_OLD_TIME,
jpayne@69:
jpayne@69: /**
jpayne@69: * Used in newer Macintosh systems. Data is a double
. Value
jpayne@69: * is seconds since January 1, 2001.
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UDTS_MAC_TIME,
jpayne@69:
jpayne@69: /**
jpayne@69: * Used in Excel. Data is an ?unknown?
. Value
jpayne@69: * is days since December 31, 1899.
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UDTS_EXCEL_TIME,
jpayne@69:
jpayne@69: /**
jpayne@69: * Used in DB2. Data is an ?unknown?
. Value
jpayne@69: * is days since December 31, 1899.
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UDTS_DB2_TIME,
jpayne@69:
jpayne@69: /**
jpayne@69: * Data is a long
. Value is microseconds since January 1, 1970.
jpayne@69: * Similar to Unix time (linear value from 1970) and struct timeval
jpayne@69: * (microseconds resolution).
jpayne@69: *
jpayne@69: * @stable ICU 3.8
jpayne@69: */
jpayne@69: UDTS_UNIX_MICROSECONDS_TIME,
jpayne@69:
jpayne@69: #ifndef U_HIDE_DEPRECATED_API
jpayne@69: /**
jpayne@69: * The first unused time scale value. The limit of this enum
jpayne@69: * @deprecated ICU 59 The numeric value may change over time, see ICU ticket #12420.
jpayne@69: */
jpayne@69: UDTS_MAX_SCALE
jpayne@69: #endif /* U_HIDE_DEPRECATED_API */
jpayne@69:
jpayne@69: } UDateTimeScale;
jpayne@69:
jpayne@69: /**
jpayne@69: * UTimeScaleValue
values are used to specify the time scale values
jpayne@69: * to utmscale_getTimeScaleValue
.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: typedef enum UTimeScaleValue {
jpayne@69: /**
jpayne@69: * The constant used to select the units vale
jpayne@69: * for a time scale.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UTSV_UNITS_VALUE = 0,
jpayne@69:
jpayne@69: /**
jpayne@69: * The constant used to select the epoch offset value
jpayne@69: * for a time scale.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UTSV_EPOCH_OFFSET_VALUE=1,
jpayne@69:
jpayne@69: /**
jpayne@69: * The constant used to select the minimum from value
jpayne@69: * for a time scale.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UTSV_FROM_MIN_VALUE=2,
jpayne@69:
jpayne@69: /**
jpayne@69: * The constant used to select the maximum from value
jpayne@69: * for a time scale.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UTSV_FROM_MAX_VALUE=3,
jpayne@69:
jpayne@69: /**
jpayne@69: * The constant used to select the minimum to value
jpayne@69: * for a time scale.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UTSV_TO_MIN_VALUE=4,
jpayne@69:
jpayne@69: /**
jpayne@69: * The constant used to select the maximum to value
jpayne@69: * for a time scale.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: UTSV_TO_MAX_VALUE=5,
jpayne@69:
jpayne@69: #ifndef U_HIDE_INTERNAL_API
jpayne@69: /**
jpayne@69: * The constant used to select the epoch plus one value
jpayne@69: * for a time scale.
jpayne@69: *
jpayne@69: * NOTE: This is an internal value. DO NOT USE IT. May not
jpayne@69: * actually be equal to the epoch offset value plus one.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: *
jpayne@69: * @internal ICU 3.2
jpayne@69: */
jpayne@69: UTSV_EPOCH_OFFSET_PLUS_1_VALUE=6,
jpayne@69:
jpayne@69: /**
jpayne@69: * The constant used to select the epoch plus one value
jpayne@69: * for a time scale.
jpayne@69: *
jpayne@69: * NOTE: This is an internal value. DO NOT USE IT. May not
jpayne@69: * actually be equal to the epoch offset value plus one.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: *
jpayne@69: * @internal ICU 3.2
jpayne@69: */
jpayne@69: UTSV_EPOCH_OFFSET_MINUS_1_VALUE=7,
jpayne@69:
jpayne@69: /**
jpayne@69: * The constant used to select the units round value
jpayne@69: * for a time scale.
jpayne@69: *
jpayne@69: * NOTE: This is an internal value. DO NOT USE IT.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: *
jpayne@69: * @internal ICU 3.2
jpayne@69: */
jpayne@69: UTSV_UNITS_ROUND_VALUE=8,
jpayne@69:
jpayne@69: /**
jpayne@69: * The constant used to select the minimum safe rounding value
jpayne@69: * for a time scale.
jpayne@69: *
jpayne@69: * NOTE: This is an internal value. DO NOT USE IT.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: *
jpayne@69: * @internal ICU 3.2
jpayne@69: */
jpayne@69: UTSV_MIN_ROUND_VALUE=9,
jpayne@69:
jpayne@69: /**
jpayne@69: * The constant used to select the maximum safe rounding value
jpayne@69: * for a time scale.
jpayne@69: *
jpayne@69: * NOTE: This is an internal value. DO NOT USE IT.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: *
jpayne@69: * @internal ICU 3.2
jpayne@69: */
jpayne@69: UTSV_MAX_ROUND_VALUE=10,
jpayne@69:
jpayne@69: #endif /* U_HIDE_INTERNAL_API */
jpayne@69:
jpayne@69: #ifndef U_HIDE_DEPRECATED_API
jpayne@69: /**
jpayne@69: * The number of time scale values, in other words limit of this enum.
jpayne@69: *
jpayne@69: * @see utmscale_getTimeScaleValue
jpayne@69: * @deprecated ICU 59 The numeric value may change over time, see ICU ticket #12420.
jpayne@69: */
jpayne@69: UTSV_MAX_SCALE_VALUE=11
jpayne@69: #endif /* U_HIDE_DEPRECATED_API */
jpayne@69:
jpayne@69: } UTimeScaleValue;
jpayne@69:
jpayne@69: /**
jpayne@69: * Get a value associated with a particular time scale.
jpayne@69: *
jpayne@69: * @param timeScale The time scale
jpayne@69: * @param value A constant representing the value to get
jpayne@69: * @param status The status code. Set to U_ILLEGAL_ARGUMENT_ERROR
if arguments are invalid.
jpayne@69: * @return - the value.
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: U_STABLE int64_t U_EXPORT2
jpayne@69: utmscale_getTimeScaleValue(UDateTimeScale timeScale, UTimeScaleValue value, UErrorCode *status);
jpayne@69:
jpayne@69: /* Conversion to 'universal time scale' */
jpayne@69:
jpayne@69: /**
jpayne@69: * Convert a int64_t
datetime from the given time scale to the universal time scale.
jpayne@69: *
jpayne@69: * @param otherTime The int64_t
datetime
jpayne@69: * @param timeScale The time scale to convert from
jpayne@69: * @param status The status code. Set to U_ILLEGAL_ARGUMENT_ERROR
if the conversion is out of range.
jpayne@69: *
jpayne@69: * @return The datetime converted to the universal time scale
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: U_STABLE int64_t U_EXPORT2
jpayne@69: utmscale_fromInt64(int64_t otherTime, UDateTimeScale timeScale, UErrorCode *status);
jpayne@69:
jpayne@69: /* Conversion from 'universal time scale' */
jpayne@69:
jpayne@69: /**
jpayne@69: * Convert a datetime from the universal time scale to a int64_t
in the given time scale.
jpayne@69: *
jpayne@69: * @param universalTime The datetime in the universal time scale
jpayne@69: * @param timeScale The time scale to convert to
jpayne@69: * @param status The status code. Set to U_ILLEGAL_ARGUMENT_ERROR
if the conversion is out of range.
jpayne@69: *
jpayne@69: * @return The datetime converted to the given time scale
jpayne@69: *
jpayne@69: * @stable ICU 3.2
jpayne@69: */
jpayne@69: U_STABLE int64_t U_EXPORT2
jpayne@69: utmscale_toInt64(int64_t universalTime, UDateTimeScale timeScale, UErrorCode *status);
jpayne@69:
jpayne@69: #endif /* #if !UCONFIG_NO_FORMATTING */
jpayne@69:
jpayne@69: #endif
jpayne@69: