michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2004 - 2008, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #ifndef UTMSCALE_H michael@0: #define UTMSCALE_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C API: Universal Time Scale michael@0: * michael@0: * There are quite a few different conventions for binary datetime, depending on different michael@0: * platforms and protocols. Some of these have severe drawbacks. For example, people using michael@0: * Unix time (seconds since Jan 1, 1970) think that they are safe until near the year 2038. michael@0: * But cases can and do arise where arithmetic manipulations causes serious problems. Consider michael@0: * the computation of the average of two datetimes, for example: if one calculates them with michael@0: * averageTime = (time1 + time2)/2, there will be overflow even with dates michael@0: * around the present. Moreover, even if these problems don't occur, there is the issue of michael@0: * conversion back and forth between different systems. michael@0: * michael@0: *

michael@0: * Binary datetimes differ in a number of ways: the datatype, the unit, michael@0: * and the epoch (origin). We'll refer to these as time scales. For example: michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: *
Table 1: Binary Time Scales
SourceDatatypeUnitEpoch
UDTS_JAVA_TIMEint64_tmillisecondsJan 1, 1970
UDTS_UNIX_TIMEint32_t or int64_tsecondsJan 1, 1970
UDTS_ICU4C_TIMEdoublemillisecondsJan 1, 1970
UDTS_WINDOWS_FILE_TIMEint64_tticks (100 nanoseconds)Jan 1, 1601
UDTS_DOTNET_DATE_TIMEint64_tticks (100 nanoseconds)Jan 1, 0001
UDTS_MAC_OLD_TIMEint32_t or int64_tsecondsJan 1, 1904
UDTS_MAC_TIMEdoublesecondsJan 1, 2001
UDTS_EXCEL_TIME?daysDec 31, 1899
UDTS_DB2_TIME?daysDec 31, 1899
UDTS_UNIX_MICROSECONDS_TIMEint64_tmicrosecondsJan 1, 1970
michael@0: * michael@0: *

michael@0: * All of the epochs start at 00:00 am (the earliest possible time on the day in question), michael@0: * and are assumed to be UTC. michael@0: * michael@0: *

michael@0: * The ranges for different datatypes are given in the following table (all values in years). michael@0: * The range of years includes the entire range expressible with positive and negative michael@0: * values of the datatype. The range of years for double is the range that would be allowed michael@0: * without losing precision to the corresponding unit. michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: * michael@0: *
Unitsint64_tdoubleint32_t
1 sec5.84542x1011285,420,920.94136.10
1 millisecond584,542,046.09285,420.920.14
1 microsecond584,542.05285.420.00
100 nanoseconds (tick)58,454.2028.540.00
1 nanosecond584.54204610.28540.00
michael@0: * michael@0: *

michael@0: * These functions implement a universal time scale which can be used as a 'pivot', michael@0: * and provide conversion functions to and from all other major time scales. michael@0: * This datetimes to be converted to the pivot time, safely manipulated, michael@0: * and converted back to any other datetime time scale. michael@0: * michael@0: *

michael@0: * So what to use for this pivot? Java time has plenty of range, but cannot represent michael@0: * .NET System.DateTime values without severe loss of precision. ICU4C time addresses this by using a michael@0: * double that is otherwise equivalent to the Java time. However, there are disadvantages michael@0: * with doubles. They provide for much more graceful degradation in arithmetic operations. michael@0: * But they only have 53 bits of accuracy, which means that they will lose precision when michael@0: * converting back and forth to ticks. What would really be nice would be a michael@0: * long double (80 bits -- 64 bit mantissa), but that is not supported on most systems. michael@0: * michael@0: *

michael@0: * The Unix extended time uses a structure with two components: time in seconds and a michael@0: * fractional field (microseconds). However, this is clumsy, slow, and michael@0: * prone to error (you always have to keep track of overflow and underflow in the michael@0: * fractional field). BigDecimal would allow for arbitrary precision and arbitrary range, michael@0: * but we do not want to use this as the normal type, because it is slow and does not michael@0: * have a fixed size. michael@0: * michael@0: *

michael@0: * Because of these issues, we ended up concluding that the .NET framework's michael@0: * System.DateTime would be the best pivot. However, we use the full range michael@0: * allowed by the datatype, allowing for datetimes back to 29,000 BC and up to 29,000 AD. michael@0: * This time scale is very fine grained, does not lose precision, and covers a range that michael@0: * will meet almost all requirements. It will not handle the range that Java times do, michael@0: * but frankly, being able to handle dates before 29,000 BC or after 29,000 AD is of very limited interest. michael@0: * michael@0: */ michael@0: michael@0: /** michael@0: * UDateTimeScale values are used to specify the time scale used for michael@0: * conversion into or out if the universal time scale. michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: typedef enum UDateTimeScale { michael@0: /** michael@0: * Used in the JDK. Data is a Java long (int64_t). Value michael@0: * is milliseconds since January 1, 1970. michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UDTS_JAVA_TIME = 0, michael@0: michael@0: /** michael@0: * Used on Unix systems. Data is int32_t or int64_t. Value michael@0: * is seconds since January 1, 1970. michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UDTS_UNIX_TIME, michael@0: michael@0: /** michael@0: * Used in IUC4C. Data is a double. Value michael@0: * is milliseconds since January 1, 1970. michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UDTS_ICU4C_TIME, michael@0: michael@0: /** michael@0: * Used in Windows for file times. Data is an int64_t. Value michael@0: * is ticks (1 tick == 100 nanoseconds) since January 1, 1601. michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UDTS_WINDOWS_FILE_TIME, michael@0: michael@0: /** michael@0: * Used in the .NET framework's System.DateTime structure. Data is an int64_t. Value michael@0: * is ticks (1 tick == 100 nanoseconds) since January 1, 0001. michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UDTS_DOTNET_DATE_TIME, michael@0: michael@0: /** michael@0: * Used in older Macintosh systems. Data is int32_t or int64_t. Value michael@0: * is seconds since January 1, 1904. michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UDTS_MAC_OLD_TIME, michael@0: michael@0: /** michael@0: * Used in newer Macintosh systems. Data is a double. Value michael@0: * is seconds since January 1, 2001. michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UDTS_MAC_TIME, michael@0: michael@0: /** michael@0: * Used in Excel. Data is an ?unknown?. Value michael@0: * is days since December 31, 1899. michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UDTS_EXCEL_TIME, michael@0: michael@0: /** michael@0: * Used in DB2. Data is an ?unknown?. Value michael@0: * is days since December 31, 1899. michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UDTS_DB2_TIME, michael@0: michael@0: /** michael@0: * Data is a long. Value is microseconds since January 1, 1970. michael@0: * Similar to Unix time (linear value from 1970) and struct timeval michael@0: * (microseconds resolution). michael@0: * michael@0: * @stable ICU 3.8 michael@0: */ michael@0: UDTS_UNIX_MICROSECONDS_TIME, michael@0: michael@0: /** michael@0: * The first unused time scale value. The limit of this enum michael@0: */ michael@0: UDTS_MAX_SCALE michael@0: } UDateTimeScale; michael@0: michael@0: /** michael@0: * UTimeScaleValue values are used to specify the time scale values michael@0: * to utmscale_getTimeScaleValue. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: typedef enum UTimeScaleValue { michael@0: /** michael@0: * The constant used to select the units vale michael@0: * for a time scale. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UTSV_UNITS_VALUE = 0, michael@0: michael@0: /** michael@0: * The constant used to select the epoch offset value michael@0: * for a time scale. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UTSV_EPOCH_OFFSET_VALUE=1, michael@0: michael@0: /** michael@0: * The constant used to select the minimum from value michael@0: * for a time scale. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UTSV_FROM_MIN_VALUE=2, michael@0: michael@0: /** michael@0: * The constant used to select the maximum from value michael@0: * for a time scale. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UTSV_FROM_MAX_VALUE=3, michael@0: michael@0: /** michael@0: * The constant used to select the minimum to value michael@0: * for a time scale. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UTSV_TO_MIN_VALUE=4, michael@0: michael@0: /** michael@0: * The constant used to select the maximum to value michael@0: * for a time scale. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: UTSV_TO_MAX_VALUE=5, michael@0: michael@0: #ifndef U_HIDE_INTERNAL_API michael@0: /** michael@0: * The constant used to select the epoch plus one value michael@0: * for a time scale. michael@0: * michael@0: * NOTE: This is an internal value. DO NOT USE IT. May not michael@0: * actually be equal to the epoch offset value plus one. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: * michael@0: * @internal ICU 3.2 michael@0: */ michael@0: UTSV_EPOCH_OFFSET_PLUS_1_VALUE=6, michael@0: michael@0: /** michael@0: * The constant used to select the epoch plus one value michael@0: * for a time scale. michael@0: * michael@0: * NOTE: This is an internal value. DO NOT USE IT. May not michael@0: * actually be equal to the epoch offset value plus one. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: * michael@0: * @internal ICU 3.2 michael@0: */ michael@0: UTSV_EPOCH_OFFSET_MINUS_1_VALUE=7, michael@0: michael@0: /** michael@0: * The constant used to select the units round value michael@0: * for a time scale. michael@0: * michael@0: * NOTE: This is an internal value. DO NOT USE IT. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: * michael@0: * @internal ICU 3.2 michael@0: */ michael@0: UTSV_UNITS_ROUND_VALUE=8, michael@0: michael@0: /** michael@0: * The constant used to select the minimum safe rounding value michael@0: * for a time scale. michael@0: * michael@0: * NOTE: This is an internal value. DO NOT USE IT. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: * michael@0: * @internal ICU 3.2 michael@0: */ michael@0: UTSV_MIN_ROUND_VALUE=9, michael@0: michael@0: /** michael@0: * The constant used to select the maximum safe rounding value michael@0: * for a time scale. michael@0: * michael@0: * NOTE: This is an internal value. DO NOT USE IT. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: * michael@0: * @internal ICU 3.2 michael@0: */ michael@0: UTSV_MAX_ROUND_VALUE=10, michael@0: michael@0: #endif /* U_HIDE_INTERNAL_API */ michael@0: michael@0: /** michael@0: * The number of time scale values, in other words limit of this enum. michael@0: * michael@0: * @see utmscale_getTimeScaleValue michael@0: */ michael@0: UTSV_MAX_SCALE_VALUE=11 michael@0: michael@0: } UTimeScaleValue; michael@0: michael@0: /** michael@0: * Get a value associated with a particular time scale. michael@0: * michael@0: * @param timeScale The time scale michael@0: * @param value A constant representing the value to get michael@0: * @param status The status code. Set to U_ILLEGAL_ARGUMENT_ERROR if arguments are invalid. michael@0: * @return - the value. michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: U_STABLE int64_t U_EXPORT2 michael@0: utmscale_getTimeScaleValue(UDateTimeScale timeScale, UTimeScaleValue value, UErrorCode *status); michael@0: michael@0: /* Conversion to 'universal time scale' */ michael@0: michael@0: /** michael@0: * Convert a int64_t datetime from the given time scale to the universal time scale. michael@0: * michael@0: * @param otherTime The int64_t datetime michael@0: * @param timeScale The time scale to convert from michael@0: * @param status The status code. Set to U_ILLEGAL_ARGUMENT_ERROR if the conversion is out of range. michael@0: * michael@0: * @return The datetime converted to the universal time scale michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: U_STABLE int64_t U_EXPORT2 michael@0: utmscale_fromInt64(int64_t otherTime, UDateTimeScale timeScale, UErrorCode *status); michael@0: michael@0: /* Conversion from 'universal time scale' */ michael@0: michael@0: /** michael@0: * Convert a datetime from the universal time scale to a int64_t in the given time scale. michael@0: * michael@0: * @param universalTime The datetime in the universal time scale michael@0: * @param timeScale The time scale to convert to michael@0: * @param status The status code. Set to U_ILLEGAL_ARGUMENT_ERROR if the conversion is out of range. michael@0: * michael@0: * @return The datetime converted to the given time scale michael@0: * michael@0: * @stable ICU 3.2 michael@0: */ michael@0: U_STABLE int64_t U_EXPORT2 michael@0: utmscale_toInt64(int64_t universalTime, UDateTimeScale timeScale, UErrorCode *status); michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: #endif michael@0: