utmscale.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2004 - 2008, International Business Machines Corporation and
  6. * others. All Rights Reserved.
  7. *******************************************************************************
  8. */
  9. #ifndef UTMSCALE_H
  10. #define UTMSCALE_H
  11. #include "unicode/utypes.h"
  12. #if !UCONFIG_NO_FORMATTING
  13. /**
  14. * \file
  15. * \brief C API: Universal Time Scale
  16. *
  17. * There are quite a few different conventions for binary datetime, depending on different
  18. * platforms and protocols. Some of these have severe drawbacks. For example, people using
  19. * Unix time (seconds since Jan 1, 1970) think that they are safe until near the year 2038.
  20. * But cases can and do arise where arithmetic manipulations causes serious problems. Consider
  21. * the computation of the average of two datetimes, for example: if one calculates them with
  22. * <code>averageTime = (time1 + time2)/2</code>, there will be overflow even with dates
  23. * around the present. Moreover, even if these problems don't occur, there is the issue of
  24. * conversion back and forth between different systems.
  25. *
  26. * <p>
  27. * Binary datetimes differ in a number of ways: the datatype, the unit,
  28. * and the epoch (origin). We'll refer to these as time scales. For example:
  29. *
  30. * <table border="1" cellspacing="0" cellpadding="4">
  31. * <caption>Table 1: Binary Time Scales</caption>
  32. * <tr>
  33. * <th align="left">Source</th>
  34. * <th align="left">Datatype</th>
  35. * <th align="left">Unit</th>
  36. * <th align="left">Epoch</th>
  37. * </tr>
  38. *
  39. * <tr>
  40. * <td>UDTS_JAVA_TIME</td>
  41. * <td>int64_t</td>
  42. * <td>milliseconds</td>
  43. * <td>Jan 1, 1970</td>
  44. * </tr>
  45. * <tr>
  46. *
  47. * <td>UDTS_UNIX_TIME</td>
  48. * <td>int32_t or int64_t</td>
  49. * <td>seconds</td>
  50. * <td>Jan 1, 1970</td>
  51. * </tr>
  52. * <tr>
  53. * <td>UDTS_ICU4C_TIME</td>
  54. *
  55. * <td>double</td>
  56. * <td>milliseconds</td>
  57. * <td>Jan 1, 1970</td>
  58. * </tr>
  59. * <tr>
  60. * <td>UDTS_WINDOWS_FILE_TIME</td>
  61. * <td>int64_t</td>
  62. *
  63. * <td>ticks (100 nanoseconds)</td>
  64. * <td>Jan 1, 1601</td>
  65. * </tr>
  66. * <tr>
  67. * <td>UDTS_DOTNET_DATE_TIME</td>
  68. * <td>int64_t</td>
  69. * <td>ticks (100 nanoseconds)</td>
  70. *
  71. * <td>Jan 1, 0001</td>
  72. * </tr>
  73. * <tr>
  74. * <td>UDTS_MAC_OLD_TIME</td>
  75. * <td>int32_t or int64_t</td>
  76. * <td>seconds</td>
  77. * <td>Jan 1, 1904</td>
  78. *
  79. * </tr>
  80. * <tr>
  81. * <td>UDTS_MAC_TIME</td>
  82. * <td>double</td>
  83. * <td>seconds</td>
  84. * <td>Jan 1, 2001</td>
  85. * </tr>
  86. *
  87. * <tr>
  88. * <td>UDTS_EXCEL_TIME</td>
  89. * <td>?</td>
  90. * <td>days</td>
  91. * <td>Dec 31, 1899</td>
  92. * </tr>
  93. * <tr>
  94. *
  95. * <td>UDTS_DB2_TIME</td>
  96. * <td>?</td>
  97. * <td>days</td>
  98. * <td>Dec 31, 1899</td>
  99. * </tr>
  100. *
  101. * <tr>
  102. * <td>UDTS_UNIX_MICROSECONDS_TIME</td>
  103. * <td>int64_t</td>
  104. * <td>microseconds</td>
  105. * <td>Jan 1, 1970</td>
  106. * </tr>
  107. * </table>
  108. *
  109. * <p>
  110. * All of the epochs start at 00:00 am (the earliest possible time on the day in question),
  111. * and are assumed to be UTC.
  112. *
  113. * <p>
  114. * The ranges for different datatypes are given in the following table (all values in years).
  115. * The range of years includes the entire range expressible with positive and negative
  116. * values of the datatype. The range of years for double is the range that would be allowed
  117. * without losing precision to the corresponding unit.
  118. *
  119. * <table border="1" cellspacing="0" cellpadding="4">
  120. * <tr>
  121. * <th align="left">Units</th>
  122. * <th align="left">int64_t</th>
  123. * <th align="left">double</th>
  124. * <th align="left">int32_t</th>
  125. * </tr>
  126. *
  127. * <tr>
  128. * <td>1 sec</td>
  129. * <td align="right">5.84542x10<sup>11</sup></td>
  130. * <td align="right">285,420,920.94</td>
  131. * <td align="right">136.10</td>
  132. * </tr>
  133. * <tr>
  134. *
  135. * <td>1 millisecond</td>
  136. * <td align="right">584,542,046.09</td>
  137. * <td align="right">285,420.92</td>
  138. * <td align="right">0.14</td>
  139. * </tr>
  140. * <tr>
  141. * <td>1 microsecond</td>
  142. *
  143. * <td align="right">584,542.05</td>
  144. * <td align="right">285.42</td>
  145. * <td align="right">0.00</td>
  146. * </tr>
  147. * <tr>
  148. * <td>100 nanoseconds (tick)</td>
  149. * <td align="right">58,454.20</td>
  150. * <td align="right">28.54</td>
  151. * <td align="right">0.00</td>
  152. * </tr>
  153. * <tr>
  154. * <td>1 nanosecond</td>
  155. * <td align="right">584.5420461</td>
  156. * <td align="right">0.2854</td>
  157. * <td align="right">0.00</td>
  158. * </tr>
  159. * </table>
  160. *
  161. * <p>
  162. * These functions implement a universal time scale which can be used as a 'pivot',
  163. * and provide conversion functions to and from all other major time scales.
  164. * This datetimes to be converted to the pivot time, safely manipulated,
  165. * and converted back to any other datetime time scale.
  166. *
  167. *<p>
  168. * So what to use for this pivot? Java time has plenty of range, but cannot represent
  169. * .NET <code>System.DateTime</code> values without severe loss of precision. ICU4C time addresses this by using a
  170. * <code>double</code> that is otherwise equivalent to the Java time. However, there are disadvantages
  171. * with <code>doubles</code>. They provide for much more graceful degradation in arithmetic operations.
  172. * But they only have 53 bits of accuracy, which means that they will lose precision when
  173. * converting back and forth to ticks. What would really be nice would be a
  174. * <code>long double</code> (80 bits -- 64 bit mantissa), but that is not supported on most systems.
  175. *
  176. *<p>
  177. * The Unix extended time uses a structure with two components: time in seconds and a
  178. * fractional field (microseconds). However, this is clumsy, slow, and
  179. * prone to error (you always have to keep track of overflow and underflow in the
  180. * fractional field). <code>BigDecimal</code> would allow for arbitrary precision and arbitrary range,
  181. * but we do not want to use this as the normal type, because it is slow and does not
  182. * have a fixed size.
  183. *
  184. *<p>
  185. * Because of these issues, we ended up concluding that the .NET framework's
  186. * <code>System.DateTime</code> would be the best pivot. However, we use the full range
  187. * allowed by the datatype, allowing for datetimes back to 29,000 BC and up to 29,000 AD.
  188. * This time scale is very fine grained, does not lose precision, and covers a range that
  189. * will meet almost all requirements. It will not handle the range that Java times do,
  190. * but frankly, being able to handle dates before 29,000 BC or after 29,000 AD is of very limited interest.
  191. *
  192. */
  193. /**
  194. * <code>UDateTimeScale</code> values are used to specify the time scale used for
  195. * conversion into or out if the universal time scale.
  196. *
  197. * @stable ICU 3.2
  198. */
  199. typedef enum UDateTimeScale {
  200. /**
  201. * Used in the JDK. Data is a Java <code>long</code> (<code>int64_t</code>). Value
  202. * is milliseconds since January 1, 1970.
  203. *
  204. * @stable ICU 3.2
  205. */
  206. UDTS_JAVA_TIME = 0,
  207. /**
  208. * Used on Unix systems. Data is <code>int32_t</code> or <code>int64_t</code>. Value
  209. * is seconds since January 1, 1970.
  210. *
  211. * @stable ICU 3.2
  212. */
  213. UDTS_UNIX_TIME,
  214. /**
  215. * Used in IUC4C. Data is a <code>double</code>. Value
  216. * is milliseconds since January 1, 1970.
  217. *
  218. * @stable ICU 3.2
  219. */
  220. UDTS_ICU4C_TIME,
  221. /**
  222. * Used in Windows for file times. Data is an <code>int64_t</code>. Value
  223. * is ticks (1 tick == 100 nanoseconds) since January 1, 1601.
  224. *
  225. * @stable ICU 3.2
  226. */
  227. UDTS_WINDOWS_FILE_TIME,
  228. /**
  229. * Used in the .NET framework's <code>System.DateTime</code> structure. Data is an <code>int64_t</code>. Value
  230. * is ticks (1 tick == 100 nanoseconds) since January 1, 0001.
  231. *
  232. * @stable ICU 3.2
  233. */
  234. UDTS_DOTNET_DATE_TIME,
  235. /**
  236. * Used in older Macintosh systems. Data is <code>int32_t</code> or <code>int64_t</code>. Value
  237. * is seconds since January 1, 1904.
  238. *
  239. * @stable ICU 3.2
  240. */
  241. UDTS_MAC_OLD_TIME,
  242. /**
  243. * Used in newer Macintosh systems. Data is a <code>double</code>. Value
  244. * is seconds since January 1, 2001.
  245. *
  246. * @stable ICU 3.2
  247. */
  248. UDTS_MAC_TIME,
  249. /**
  250. * Used in Excel. Data is an <code>?unknown?</code>. Value
  251. * is days since December 31, 1899.
  252. *
  253. * @stable ICU 3.2
  254. */
  255. UDTS_EXCEL_TIME,
  256. /**
  257. * Used in DB2. Data is an <code>?unknown?</code>. Value
  258. * is days since December 31, 1899.
  259. *
  260. * @stable ICU 3.2
  261. */
  262. UDTS_DB2_TIME,
  263. /**
  264. * Data is a <code>long</code>. Value is microseconds since January 1, 1970.
  265. * Similar to Unix time (linear value from 1970) and struct timeval
  266. * (microseconds resolution).
  267. *
  268. * @stable ICU 3.8
  269. */
  270. UDTS_UNIX_MICROSECONDS_TIME,
  271. #ifndef U_HIDE_DEPRECATED_API
  272. /**
  273. * The first unused time scale value. The limit of this enum
  274. * @deprecated ICU 59 The numeric value may change over time, see ICU ticket #12420.
  275. */
  276. UDTS_MAX_SCALE
  277. #endif /* U_HIDE_DEPRECATED_API */
  278. } UDateTimeScale;
  279. /**
  280. * <code>UTimeScaleValue</code> values are used to specify the time scale values
  281. * to <code>utmscale_getTimeScaleValue</code>.
  282. *
  283. * @see utmscale_getTimeScaleValue
  284. *
  285. * @stable ICU 3.2
  286. */
  287. typedef enum UTimeScaleValue {
  288. /**
  289. * The constant used to select the units vale
  290. * for a time scale.
  291. *
  292. * @see utmscale_getTimeScaleValue
  293. *
  294. * @stable ICU 3.2
  295. */
  296. UTSV_UNITS_VALUE = 0,
  297. /**
  298. * The constant used to select the epoch offset value
  299. * for a time scale.
  300. *
  301. * @see utmscale_getTimeScaleValue
  302. *
  303. * @stable ICU 3.2
  304. */
  305. UTSV_EPOCH_OFFSET_VALUE=1,
  306. /**
  307. * The constant used to select the minimum from value
  308. * for a time scale.
  309. *
  310. * @see utmscale_getTimeScaleValue
  311. *
  312. * @stable ICU 3.2
  313. */
  314. UTSV_FROM_MIN_VALUE=2,
  315. /**
  316. * The constant used to select the maximum from value
  317. * for a time scale.
  318. *
  319. * @see utmscale_getTimeScaleValue
  320. *
  321. * @stable ICU 3.2
  322. */
  323. UTSV_FROM_MAX_VALUE=3,
  324. /**
  325. * The constant used to select the minimum to value
  326. * for a time scale.
  327. *
  328. * @see utmscale_getTimeScaleValue
  329. *
  330. * @stable ICU 3.2
  331. */
  332. UTSV_TO_MIN_VALUE=4,
  333. /**
  334. * The constant used to select the maximum to value
  335. * for a time scale.
  336. *
  337. * @see utmscale_getTimeScaleValue
  338. *
  339. * @stable ICU 3.2
  340. */
  341. UTSV_TO_MAX_VALUE=5,
  342. #ifndef U_HIDE_INTERNAL_API
  343. /**
  344. * The constant used to select the epoch plus one value
  345. * for a time scale.
  346. *
  347. * NOTE: This is an internal value. DO NOT USE IT. May not
  348. * actually be equal to the epoch offset value plus one.
  349. *
  350. * @see utmscale_getTimeScaleValue
  351. *
  352. * @internal ICU 3.2
  353. */
  354. UTSV_EPOCH_OFFSET_PLUS_1_VALUE=6,
  355. /**
  356. * The constant used to select the epoch plus one value
  357. * for a time scale.
  358. *
  359. * NOTE: This is an internal value. DO NOT USE IT. May not
  360. * actually be equal to the epoch offset value plus one.
  361. *
  362. * @see utmscale_getTimeScaleValue
  363. *
  364. * @internal ICU 3.2
  365. */
  366. UTSV_EPOCH_OFFSET_MINUS_1_VALUE=7,
  367. /**
  368. * The constant used to select the units round value
  369. * for a time scale.
  370. *
  371. * NOTE: This is an internal value. DO NOT USE IT.
  372. *
  373. * @see utmscale_getTimeScaleValue
  374. *
  375. * @internal ICU 3.2
  376. */
  377. UTSV_UNITS_ROUND_VALUE=8,
  378. /**
  379. * The constant used to select the minimum safe rounding value
  380. * for a time scale.
  381. *
  382. * NOTE: This is an internal value. DO NOT USE IT.
  383. *
  384. * @see utmscale_getTimeScaleValue
  385. *
  386. * @internal ICU 3.2
  387. */
  388. UTSV_MIN_ROUND_VALUE=9,
  389. /**
  390. * The constant used to select the maximum safe rounding value
  391. * for a time scale.
  392. *
  393. * NOTE: This is an internal value. DO NOT USE IT.
  394. *
  395. * @see utmscale_getTimeScaleValue
  396. *
  397. * @internal ICU 3.2
  398. */
  399. UTSV_MAX_ROUND_VALUE=10,
  400. #endif /* U_HIDE_INTERNAL_API */
  401. #ifndef U_HIDE_DEPRECATED_API
  402. /**
  403. * The number of time scale values, in other words limit of this enum.
  404. *
  405. * @see utmscale_getTimeScaleValue
  406. * @deprecated ICU 59 The numeric value may change over time, see ICU ticket #12420.
  407. */
  408. UTSV_MAX_SCALE_VALUE=11
  409. #endif /* U_HIDE_DEPRECATED_API */
  410. } UTimeScaleValue;
  411. /**
  412. * Get a value associated with a particular time scale.
  413. *
  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.
  418. *
  419. * @stable ICU 3.2
  420. */
  421. U_CAPI int64_t U_EXPORT2
  422. utmscale_getTimeScaleValue(UDateTimeScale timeScale, UTimeScaleValue value, UErrorCode *status);
  423. /* Conversion to 'universal time scale' */
  424. /**
  425. * Convert a <code>int64_t</code> datetime from the given time scale to the universal time scale.
  426. *
  427. * @param otherTime The <code>int64_t</code> datetime
  428. * @param timeScale The time scale to convert from
  429. * @param status The status code. Set to <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the conversion is out of range.
  430. *
  431. * @return The datetime converted to the universal time scale
  432. *
  433. * @stable ICU 3.2
  434. */
  435. U_CAPI int64_t U_EXPORT2
  436. utmscale_fromInt64(int64_t otherTime, UDateTimeScale timeScale, UErrorCode *status);
  437. /* Conversion from 'universal time scale' */
  438. /**
  439. * Convert a datetime from the universal time scale to a <code>int64_t</code> in the given time scale.
  440. *
  441. * @param universalTime The datetime in the universal time scale
  442. * @param timeScale The time scale to convert to
  443. * @param status The status code. Set to <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the conversion is out of range.
  444. *
  445. * @return The datetime converted to the given time scale
  446. *
  447. * @stable ICU 3.2
  448. */
  449. U_CAPI int64_t U_EXPORT2
  450. utmscale_toInt64(int64_t universalTime, UDateTimeScale timeScale, UErrorCode *status);
  451. #endif /* #if !UCONFIG_NO_FORMATTING */
  452. #endif