intl/icu/source/i18n/unicode/utmscale.h

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial