]>
Commit | Line | Data |
---|---|---|
374ca955 A |
1 | /* |
2 | ******************************************************************************* | |
73c04bcf | 3 | * Copyright (C) 2004 - 2006, International Business Machines Corporation and |
374ca955 A |
4 | * others. All Rights Reserved. |
5 | ******************************************************************************* | |
6 | */ | |
7 | ||
8 | #ifndef UTMSCALE_H | |
9 | #define UTMSCALE_H | |
10 | ||
11 | #include "unicode/utypes.h" | |
12 | ||
13 | #if !UCONFIG_NO_FORMATTING | |
14 | ||
15 | /** | |
16 | * \file | |
17 | * \brief C API: Universal Time Scale | |
18 | * | |
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. | |
27 | * | |
28 | * <p> | |
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: | |
31 | * | |
32 | * <table border="1" cellspacing="0" cellpadding="4"> | |
33 | * <caption>Table 1: Binary Time Scales</caption> | |
34 | * <tr> | |
35 | * <th align="left">Source</th> | |
36 | * <th align="left">Datatype</th> | |
37 | * <th align="left">Unit</th> | |
38 | * <th align="left">Epoch</th> | |
39 | * </tr> | |
40 | * | |
41 | * <tr> | |
73c04bcf | 42 | * <td>UDTS_JAVA_TIME</td> |
374ca955 A |
43 | * <td>int64_t</td> |
44 | * <td>milliseconds</td> | |
45 | * <td>Jan 1, 1970</td> | |
46 | * </tr> | |
47 | * <tr> | |
48 | * | |
73c04bcf | 49 | * <td>UDTS_UNIX_TIME</td> |
374ca955 A |
50 | * <td>int32_t or int64_t</td> |
51 | * <td>seconds</td> | |
52 | * <td>Jan 1, 1970</td> | |
53 | * </tr> | |
54 | * <tr> | |
73c04bcf | 55 | * <td>UDTS_ICU4C_TIME</td> |
374ca955 A |
56 | * |
57 | * <td>double</td> | |
58 | * <td>milliseconds</td> | |
59 | * <td>Jan 1, 1970</td> | |
60 | * </tr> | |
61 | * <tr> | |
73c04bcf | 62 | * <td>UDTS_WINDOWS_FILE_TIME</td> |
374ca955 A |
63 | * <td>int64_t</td> |
64 | * | |
65 | * <td>ticks (100 nanoseconds)</td> | |
66 | * <td>Jan 1, 1601</td> | |
67 | * </tr> | |
68 | * <tr> | |
73c04bcf | 69 | * <td>UDTS_DOTNET_DATE_TIME</td> |
374ca955 A |
70 | * <td>int64_t</td> |
71 | * <td>ticks (100 nanoseconds)</td> | |
72 | * | |
73 | * <td>Jan 1, 0001</td> | |
74 | * </tr> | |
75 | * <tr> | |
73c04bcf A |
76 | * <td>UDTS_MAC_OLD_TIME</td> |
77 | * <td>int32_t or int64_t</td> | |
374ca955 A |
78 | * <td>seconds</td> |
79 | * <td>Jan 1, 1904</td> | |
80 | * | |
81 | * </tr> | |
82 | * <tr> | |
73c04bcf | 83 | * <td>UDTS_MAC_TIME</td> |
374ca955 A |
84 | * <td>double</td> |
85 | * <td>seconds</td> | |
86 | * <td>Jan 1, 2001</td> | |
87 | * </tr> | |
88 | * | |
89 | * <tr> | |
73c04bcf | 90 | * <td>UDTS_EXCEL_TIME</td> |
374ca955 A |
91 | * <td>?</td> |
92 | * <td>days</td> | |
93 | * <td>Dec 31, 1899</td> | |
94 | * </tr> | |
95 | * <tr> | |
96 | * | |
73c04bcf | 97 | * <td>UDTS_DB2_TIME</td> |
374ca955 A |
98 | * <td>?</td> |
99 | * <td>days</td> | |
100 | * <td>Dec 31, 1899</td> | |
101 | * </tr> | |
102 | * </table> | |
103 | * | |
104 | * <p> | |
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. | |
107 | * | |
108 | * <p> | |
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. | |
113 | * | |
114 | * <table border="1" cellspacing="0" cellpadding="4"> | |
115 | * <tr> | |
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> | |
120 | * </tr> | |
121 | * | |
122 | * <tr> | |
123 | * <td>1 sec</td> | |
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> | |
127 | * </tr> | |
128 | * <tr> | |
129 | * | |
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> | |
134 | * </tr> | |
135 | * <tr> | |
136 | * <td>1 microsecond</td> | |
137 | * | |
138 | * <td align="right">584,542.05</td> | |
139 | * <td align="right">285.42</td> | |
140 | * <td align="right">0.00</td> | |
141 | * </tr> | |
142 | * <tr> | |
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> | |
147 | * </tr> | |
148 | * <tr> | |
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> | |
153 | * </tr> | |
154 | * </table> | |
155 | * | |
156 | * <p> | |
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. | |
161 | * | |
162 | *<p> | |
163 | * So what to use for this pivot? Java time has plenty of range, but cannot represent | |
73c04bcf | 164 | * .NET <code>System.DateTime</code> values without severe loss of precision. ICU4C time addresses this by using a |
374ca955 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 | |
73c04bcf | 169 | * <code>long double</code> (80 bits -- 64 bit mantissa), but that is not supported on most systems. |
374ca955 A |
170 | * |
171 | *<p> | |
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 | |
177 | * have a fixed size. | |
178 | * | |
179 | *<p> | |
73c04bcf A |
180 | * Because of these issues, we ended up concluding that the .NET framework's |
181 | * <code>System.DateTime</code> would be the best pivot. However, we use the full range | |
182 | * allowed by the datatype, allowing for datetimes back to 29,000 BC and up to 29,000 AD. | |
183 | * This time scale is very fine grained, does not lose precision, and covers a range that | |
184 | * will meet almost all requirements. It will not handle the range that Java times do, | |
185 | * but frankly, being able to handle dates before 29,000 BC or after 29,000 AD is of very limited interest. | |
374ca955 A |
186 | * |
187 | */ | |
188 | ||
189 | /** | |
190 | * <code>UDateTimeScale</code> values are used to specify the time scale used for | |
191 | * conversion into or out if the universal time scale. | |
192 | * | |
73c04bcf | 193 | * @stable ICU 3.2 |
374ca955 A |
194 | */ |
195 | typedef enum UDateTimeScale { | |
196 | /** | |
197 | * Used in the JDK. Data is a Java <code>long</code> (<code>int64_t</code>). Value | |
198 | * is milliseconds since January 1, 1970. | |
199 | * | |
73c04bcf | 200 | * @stable ICU 3.2 |
374ca955 A |
201 | */ |
202 | UDTS_JAVA_TIME = 0, | |
203 | ||
204 | /** | |
205 | * Used on Unix systems. Data is <code>int32_t</code> or <code>int64_t</code>. Value | |
206 | * is seconds since January 1, 1970. | |
207 | * | |
73c04bcf | 208 | * @stable ICU 3.2 |
374ca955 A |
209 | */ |
210 | UDTS_UNIX_TIME, | |
211 | ||
212 | /** | |
213 | * Used in IUC4C. Data is a <code>double</code>. Value | |
214 | * is milliseconds since January 1, 1970. | |
215 | * | |
73c04bcf | 216 | * @stable ICU 3.2 |
374ca955 A |
217 | */ |
218 | UDTS_ICU4C_TIME, | |
219 | ||
220 | /** | |
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. | |
223 | * | |
73c04bcf | 224 | * @stable ICU 3.2 |
374ca955 A |
225 | */ |
226 | UDTS_WINDOWS_FILE_TIME, | |
227 | ||
228 | /** | |
73c04bcf | 229 | * Used in the .NET framework's <code>System.DateTime</code> structure. Data is an <code>int64_t</code>. Value |
374ca955 A |
230 | * is ticks (1 tick == 100 nanoseconds) since January 1, 0001. |
231 | * | |
73c04bcf | 232 | * @stable ICU 3.2 |
374ca955 | 233 | */ |
73c04bcf | 234 | UDTS_DOTNET_DATE_TIME, |
374ca955 A |
235 | |
236 | /** | |
73c04bcf | 237 | * Used in older Macintosh systems. Data is <code>int32_t</code> or <code>int64_t</code>. Value |
374ca955 A |
238 | * is seconds since January 1, 1904. |
239 | * | |
73c04bcf | 240 | * @stable ICU 3.2 |
374ca955 A |
241 | */ |
242 | UDTS_MAC_OLD_TIME, | |
243 | ||
244 | /** | |
245 | * Used in newer Macintosh systems. Data is a <code>double</code>. Value | |
246 | * is seconds since January 1, 2001. | |
247 | * | |
73c04bcf | 248 | * @stable ICU 3.2 |
374ca955 A |
249 | */ |
250 | UDTS_MAC_TIME, | |
251 | ||
252 | /** | |
253 | * Used in Excel. Data is an <code>?unknown?</code>. Value | |
254 | * is days since December 31, 1899. | |
255 | * | |
73c04bcf | 256 | * @stable ICU 3.2 |
374ca955 A |
257 | */ |
258 | UDTS_EXCEL_TIME, | |
259 | ||
260 | /** | |
261 | * Used in DB2. Data is an <code>?unknown?</code>. Value | |
262 | * is days since December 31, 1899. | |
263 | * | |
73c04bcf | 264 | * @stable ICU 3.2 |
374ca955 A |
265 | */ |
266 | UDTS_DB2_TIME, | |
267 | ||
268 | /** | |
73c04bcf | 269 | * The first unused time scale value. The limit of this enum |
374ca955 A |
270 | */ |
271 | UDTS_MAX_SCALE | |
272 | } UDateTimeScale; | |
273 | ||
73c04bcf A |
274 | /** |
275 | * <code>UTimeScaleValue</code> values are used to specify the time scale values | |
276 | * to <code>utmscale_getTimeScaleValue</code>. | |
277 | * | |
278 | * @see utmscale_getTimeScaleValue | |
279 | * | |
280 | * @stable ICU 3.2 | |
281 | */ | |
374ca955 A |
282 | typedef enum UTimeScaleValue { |
283 | /** | |
284 | * The constant used to select the units vale | |
285 | * for a time scale. | |
286 | * | |
73c04bcf | 287 | * @see utmscale_getTimeScaleValue |
374ca955 | 288 | * |
73c04bcf | 289 | * @stable ICU 3.2 |
374ca955 A |
290 | */ |
291 | UTSV_UNITS_VALUE = 0, | |
292 | ||
293 | /** | |
294 | * The constant used to select the epoch offset value | |
295 | * for a time scale. | |
296 | * | |
73c04bcf | 297 | * @see utmscale_getTimeScaleValue |
374ca955 | 298 | * |
73c04bcf | 299 | * @stable ICU 3.2 |
374ca955 | 300 | */ |
73c04bcf | 301 | UTSV_EPOCH_OFFSET_VALUE=1, |
374ca955 A |
302 | |
303 | /** | |
304 | * The constant used to select the minimum from value | |
305 | * for a time scale. | |
306 | * | |
73c04bcf | 307 | * @see utmscale_getTimeScaleValue |
374ca955 | 308 | * |
73c04bcf | 309 | * @stable ICU 3.2 |
374ca955 | 310 | */ |
73c04bcf | 311 | UTSV_FROM_MIN_VALUE=2, |
374ca955 A |
312 | |
313 | /** | |
314 | * The constant used to select the maximum from value | |
315 | * for a time scale. | |
316 | * | |
73c04bcf | 317 | * @see utmscale_getTimeScaleValue |
374ca955 | 318 | * |
73c04bcf | 319 | * @stable ICU 3.2 |
374ca955 | 320 | */ |
73c04bcf | 321 | UTSV_FROM_MAX_VALUE=3, |
374ca955 A |
322 | |
323 | /** | |
324 | * The constant used to select the minimum to value | |
325 | * for a time scale. | |
326 | * | |
73c04bcf | 327 | * @see utmscale_getTimeScaleValue |
374ca955 | 328 | * |
73c04bcf | 329 | * @stable ICU 3.2 |
374ca955 | 330 | */ |
73c04bcf | 331 | UTSV_TO_MIN_VALUE=4, |
374ca955 A |
332 | |
333 | /** | |
334 | * The constant used to select the maximum to value | |
335 | * for a time scale. | |
336 | * | |
73c04bcf | 337 | * @see utmscale_getTimeScaleValue |
374ca955 | 338 | * |
73c04bcf | 339 | * @stable ICU 3.2 |
374ca955 | 340 | */ |
73c04bcf | 341 | UTSV_TO_MAX_VALUE=5, |
374ca955 | 342 | |
73c04bcf | 343 | #ifndef U_HIDE_INTERNAL_API |
374ca955 A |
344 | /** |
345 | * The constant used to select the epoch plus one value | |
346 | * for a time scale. | |
347 | * | |
348 | * NOTE: This is an internal value. DO NOT USE IT. May not | |
349 | * actually be equal to the epoch offset value plus one. | |
350 | * | |
73c04bcf | 351 | * @see utmscale_getTimeScaleValue |
374ca955 | 352 | * |
73c04bcf | 353 | * @internal ICU 3.2 |
374ca955 | 354 | */ |
73c04bcf | 355 | UTSV_EPOCH_OFFSET_PLUS_1_VALUE=6, |
374ca955 A |
356 | |
357 | /** | |
358 | * The constant used to select the epoch plus one value | |
359 | * for a time scale. | |
360 | * | |
361 | * NOTE: This is an internal value. DO NOT USE IT. May not | |
362 | * actually be equal to the epoch offset value plus one. | |
363 | * | |
73c04bcf | 364 | * @see utmscale_getTimeScaleValue |
374ca955 | 365 | * |
73c04bcf | 366 | * @internal ICU 3.2 |
374ca955 | 367 | */ |
73c04bcf | 368 | UTSV_EPOCH_OFFSET_MINUS_1_VALUE=7, |
374ca955 A |
369 | |
370 | /** | |
371 | * The constant used to select the units round value | |
372 | * for a time scale. | |
373 | * | |
374 | * NOTE: This is an internal value. DO NOT USE IT. | |
375 | * | |
73c04bcf | 376 | * @see utmscale_getTimeScaleValue |
374ca955 | 377 | * |
73c04bcf | 378 | * @internal ICU 3.2 |
374ca955 | 379 | */ |
73c04bcf | 380 | UTSV_UNITS_ROUND_VALUE=8, |
374ca955 A |
381 | |
382 | /** | |
383 | * The constant used to select the minimum safe rounding value | |
384 | * for a time scale. | |
385 | * | |
386 | * NOTE: This is an internal value. DO NOT USE IT. | |
387 | * | |
73c04bcf | 388 | * @see utmscale_getTimeScaleValue |
374ca955 | 389 | * |
73c04bcf | 390 | * @internal ICU 3.2 |
374ca955 | 391 | */ |
73c04bcf | 392 | UTSV_MIN_ROUND_VALUE=9, |
374ca955 A |
393 | |
394 | /** | |
395 | * The constant used to select the maximum safe rounding value | |
396 | * for a time scale. | |
397 | * | |
398 | * NOTE: This is an internal value. DO NOT USE IT. | |
399 | * | |
73c04bcf | 400 | * @see utmscale_getTimeScaleValue |
374ca955 | 401 | * |
73c04bcf | 402 | * @internal ICU 3.2 |
374ca955 | 403 | */ |
73c04bcf A |
404 | UTSV_MAX_ROUND_VALUE=10, |
405 | ||
406 | #endif /* U_HIDE_INTERNAL_API */ | |
374ca955 A |
407 | |
408 | /** | |
73c04bcf | 409 | * The number of time scale values, in other words limit of this enum. |
374ca955 | 410 | * |
73c04bcf | 411 | * @see utmscale_getTimeScaleValue |
374ca955 | 412 | */ |
73c04bcf A |
413 | UTSV_MAX_SCALE_VALUE=11 |
414 | ||
374ca955 A |
415 | } UTimeScaleValue; |
416 | ||
417 | /** | |
418 | * Get a value associated with a particular time scale. | |
419 | * | |
420 | * @param timeScale The time scale | |
421 | * @param value A constant representing the value to get | |
422 | * @param status The status code. Set to <code>U_ILLEGAL_ARGUMENT_ERROR</code> if arguments are invalid. | |
423 | * @return - the value. | |
424 | * | |
73c04bcf | 425 | * @stable ICU 3.2 |
374ca955 | 426 | */ |
73c04bcf | 427 | U_STABLE int64_t U_EXPORT2 |
374ca955 A |
428 | utmscale_getTimeScaleValue(UDateTimeScale timeScale, UTimeScaleValue value, UErrorCode *status); |
429 | ||
430 | /* Conversion to 'universal time scale' */ | |
431 | ||
432 | /** | |
433 | * Convert a <code>int64_t</code> datetime from the given time scale to the universal time scale. | |
434 | * | |
435 | * @param otherTime The <code>int64_t</code> datetime | |
436 | * @param timeScale The time scale to convert from | |
437 | * @param status The status code. Set to <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the conversion is out of range. | |
438 | * | |
439 | * @return The datetime converted to the universal time scale | |
440 | * | |
73c04bcf | 441 | * @stable ICU 3.2 |
374ca955 | 442 | */ |
73c04bcf | 443 | U_STABLE int64_t U_EXPORT2 |
374ca955 A |
444 | utmscale_fromInt64(int64_t otherTime, UDateTimeScale timeScale, UErrorCode *status); |
445 | ||
446 | /* Conversion from 'universal time scale' */ | |
447 | ||
448 | /** | |
449 | * Convert a datetime from the universal time scale to a <code>int64_t</code> in the given time scale. | |
450 | * | |
451 | * @param universalTime The datetime in the universal time scale | |
452 | * @param timeScale The time scale to convert to | |
453 | * @param status The status code. Set to <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the conversion is out of range. | |
454 | * | |
455 | * @return The datetime converted to the given time scale | |
456 | * | |
73c04bcf | 457 | * @stable ICU 3.2 |
374ca955 | 458 | */ |
73c04bcf | 459 | U_STABLE int64_t U_EXPORT2 |
374ca955 A |
460 | utmscale_toInt64(int64_t universalTime, UDateTimeScale timeScale, UErrorCode *status); |
461 | ||
462 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
463 | ||
464 | #endif | |
465 |