2 *******************************************************************************
3 * Copyright (C) 2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_FORMATTING
17 * \brief C API: Universal Time Scale
19 * There are quite a few different conventions for binary datetime, depending on different
20 * platforms and protocols. Some of these have severe drawbacks. For example, people using
21 * Unix time (seconds since Jan 1, 1970) think that they are safe until near the year 2038.
22 * But cases can and do arise where arithmetic manipulations causes serious problems. Consider
23 * the computation of the average of two datetimes, for example: if one calculates them with
24 * <code>averageTime = (time1 + time2)/2</code>, there will be overflow even with dates
25 * around the present. Moreover, even if these problems don't occur, there is the issue of
26 * conversion back and forth between different systems.
29 * Binary datetimes differ in a number of ways: the datatype, the unit,
30 * and the epoch (origin). We'll refer to these as time scales. For example:
32 * <table border="1" cellspacing="0" cellpadding="4">
33 * <caption>Table 1: Binary Time Scales</caption>
35 * <th align="left">Source</th>
36 * <th align="left">Datatype</th>
37 * <th align="left">Unit</th>
38 * <th align="left">Epoch</th>
44 * <td>milliseconds</td>
45 * <td>Jan 1, 1970</td>
50 * <td>int32_t or int64_t</td>
52 * <td>Jan 1, 1970</td>
58 * <td>milliseconds</td>
59 * <td>Jan 1, 1970</td>
62 * <td>WINDOWS_FILE_TIME</td>
65 * <td>ticks (100 nanoseconds)</td>
66 * <td>Jan 1, 1601</td>
69 * <td>WINDOWS_DATE_TIME</td>
71 * <td>ticks (100 nanoseconds)</td>
73 * <td>Jan 1, 0001</td>
76 * <td>MAC_OLD_TIME</td>
79 * <td>Jan 1, 1904</td>
86 * <td>Jan 1, 2001</td>
93 * <td>Dec 31, 1899</td>
100 * <td>Dec 31, 1899</td>
105 * All of the epochs start at 00:00 am (the earliest possible time on the day in question),
106 * and are assumed to be UTC.
109 * The ranges for different datatypes are given in the following table (all values in years).
110 * The range of years includes the entire range expressible with positive and negative
111 * values of the datatype. The range of years for double is the range that would be allowed
112 * without losing precision to the corresponding unit.
114 * <table border="1" cellspacing="0" cellpadding="4">
116 * <th align="left">Units</th>
117 * <th align="left">int64_t</th>
118 * <th align="left">double</th>
119 * <th align="left">int32_t</th>
124 * <td align="right">5.84542x10<sup>11</sup></td>
125 * <td align="right">285,420,920.94</td>
126 * <td align="right">136.10</td>
130 * <td>1 millisecond</td>
131 * <td align="right">584,542,046.09</td>
132 * <td align="right">285,420.92</td>
133 * <td align="right">0.14</td>
136 * <td>1 microsecond</td>
138 * <td align="right">584,542.05</td>
139 * <td align="right">285.42</td>
140 * <td align="right">0.00</td>
143 * <td>100 nanoseconds (tick)</td>
144 * <td align="right">58,454.20</td>
145 * <td align="right">28.54</td>
146 * <td align="right">0.00</td>
149 * <td>1 nanosecond</td>
150 * <td align="right">584.5420461</td>
151 * <td align="right">0.2854</td>
152 * <td align="right">0.00</td>
157 * These functions implement a universal time scale which can be used as a 'pivot',
158 * and provide conversion functions to and from all other major time scales.
159 * This datetimes to be converted to the pivot time, safely manipulated,
160 * and converted back to any other datetime time scale.
163 * So what to use for this pivot? Java time has plenty of range, but cannot represent
164 * Windows datetimes without severe loss of precision. ICU4C time addresses this by using a
165 * <code>double</code> that is otherwise equivalent to the Java time. However, there are disadvantages
166 * with <code>doubles</code>. They provide for much more graceful degradation in arithmetic operations.
167 * But they only have 53 bits of accuracy, which means that they will lose precision when
168 * converting back and forth to ticks. What would really be nice would be a
169 * long double (80 bits -- 64 bit mantissa), but that is not supported on most systems.
172 * The Unix extended time uses a structure with two components: time in seconds and a
173 * fractional field (microseconds). However, this is clumsy, slow, and
174 * prone to error (you always have to keep track of overflow and underflow in the
175 * fractional field). <code>BigDecimal</code> would allow for arbitrary precision and arbitrary range,
176 * but we do not want to use this as the normal type, because it is slow and does not
180 * Because of these issues, we ended up concluding that the Windows datetime would be the
181 * best pivot. However, we use the full range allowed by the datatype, allowing for
182 * datetimes back to 29,000 BC and up to 29,000 AD. This time scale is very fine grained,
183 * does not lose precision, and covers a range that will meet almost all requirements.
184 * It will not handle the range that Java times do, but frankly, being able to handle dates
185 * before 29,000 BC or after 29,000 AD is of very limited interest.
190 * <code>UDateTimeScale</code> values are used to specify the time scale used for
191 * conversion into or out if the universal time scale.
195 typedef enum UDateTimeScale
{
197 * Used in the JDK. Data is a Java <code>long</code> (<code>int64_t</code>). Value
198 * is milliseconds since January 1, 1970.
205 * Used on Unix systems. Data is <code>int32_t</code> or <code>int64_t</code>. Value
206 * is seconds since January 1, 1970.
213 * Used in IUC4C. Data is a <code>double</code>. Value
214 * is milliseconds since January 1, 1970.
221 * Used in Windows for file times. Data is an <code>int64_t</code>. Value
222 * is ticks (1 tick == 100 nanoseconds) since January 1, 1601.
226 UDTS_WINDOWS_FILE_TIME
,
229 * Used in Windows for dates and times (?). Data is an <code>int64_t</code>. Value
230 * is ticks (1 tick == 100 nanoseconds) since January 1, 0001.
234 UDTS_WINDOWS_DATE_TIME
,
237 * Used in older Macintosh systems. Data is an <code>int32_t</code>. Value
238 * is seconds since January 1, 1904.
245 * Used in newer Macintosh systems. Data is a <code>double</code>. Value
246 * is seconds since January 1, 2001.
253 * Used in Excel. Data is an <code>?unknown?</code>. Value
254 * is days since December 31, 1899.
261 * Used in DB2. Data is an <code>?unknown?</code>. Value
262 * is days since December 31, 1899.
269 * The first unused time scale value.
276 typedef enum UTimeScaleValue
{
278 * The constant used to select the units vale
281 * @see utms_getTimeScaleValue
285 UTSV_UNITS_VALUE
= 0,
288 * The constant used to select the epoch offset value
291 * @see utms_getTimeScaleValue
295 UTSV_EPOCH_OFFSET_VALUE
,
298 * The constant used to select the minimum from value
301 * @see utms_getTimeScaleValue
308 * The constant used to select the maximum from value
311 * @see utms_getTimeScaleValue
318 * The constant used to select the minimum to value
321 * @see utms_getTimeScaleValue
328 * The constant used to select the maximum to value
331 * @see utms_getTimeScaleValue
338 * The constant used to select the epoch plus one value
341 * NOTE: This is an internal value. DO NOT USE IT. May not
342 * actually be equal to the epoch offset value plus one.
344 * @see utms_getTimeScaleValue
348 UTSV_EPOCH_OFFSET_PLUS_1_VALUE
,
351 * The constant used to select the epoch plus one value
354 * NOTE: This is an internal value. DO NOT USE IT. May not
355 * actually be equal to the epoch offset value plus one.
357 * @see utms_getTimeScaleValue
361 UTSV_EPOCH_OFFSET_MINUS_1_VALUE
,
364 * The constant used to select the units round value
367 * NOTE: This is an internal value. DO NOT USE IT.
369 * @see utms_getTimeScaleValue
373 UTSV_UNITS_ROUND_VALUE
,
376 * The constant used to select the minimum safe rounding value
379 * NOTE: This is an internal value. DO NOT USE IT.
381 * @see utms_getTimeScaleValue
385 UTSV_MIN_ROUND_VALUE
,
388 * The constant used to select the maximum safe rounding value
391 * NOTE: This is an internal value. DO NOT USE IT.
393 * @see utms_getTimeScaleValue
397 UTSV_MAX_ROUND_VALUE
,
400 * The number of time scale values.
402 * NOTE: This is an internal value. DO NOT USE IT.
404 * @see utms_getTimeScaleValue
412 * Get a value associated with a particular time scale.
414 * @param timeScale The time scale
415 * @param value A constant representing the value to get
416 * @param status The status code. Set to <code>U_ILLEGAL_ARGUMENT_ERROR</code> if arguments are invalid.
417 * @return - the value.
421 U_DRAFT
int64_t U_EXPORT2
422 utmscale_getTimeScaleValue(UDateTimeScale timeScale
, UTimeScaleValue value
, UErrorCode
*status
);
424 /* Conversion to 'universal time scale' */
427 * Convert a <code>int64_t</code> datetime from the given time scale to the universal time scale.
429 * @param otherTime The <code>int64_t</code> datetime
430 * @param timeScale The time scale to convert from
431 * @param status The status code. Set to <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the conversion is out of range.
433 * @return The datetime converted to the universal time scale
437 U_DRAFT
int64_t U_EXPORT2
438 utmscale_fromInt64(int64_t otherTime
, UDateTimeScale timeScale
, UErrorCode
*status
);
440 /* Conversion from 'universal time scale' */
443 * Convert a datetime from the universal time scale to a <code>int64_t</code> in the given time scale.
445 * @param universalTime The datetime in the universal time scale
446 * @param timeScale The time scale to convert to
447 * @param status The status code. Set to <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the conversion is out of range.
449 * @return The datetime converted to the given time scale
453 U_DRAFT
int64_t U_EXPORT2
454 utmscale_toInt64(int64_t universalTime
, UDateTimeScale timeScale
, UErrorCode
*status
);
456 #endif /* #if !UCONFIG_NO_FORMATTING */