1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/unicode/utmscale.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,481 @@ 1.4 +/* 1.5 +******************************************************************************* 1.6 +* Copyright (C) 2004 - 2008, International Business Machines Corporation and 1.7 +* others. All Rights Reserved. 1.8 +******************************************************************************* 1.9 +*/ 1.10 + 1.11 +#ifndef UTMSCALE_H 1.12 +#define UTMSCALE_H 1.13 + 1.14 +#include "unicode/utypes.h" 1.15 + 1.16 +#if !UCONFIG_NO_FORMATTING 1.17 + 1.18 +/** 1.19 + * \file 1.20 + * \brief C API: Universal Time Scale 1.21 + * 1.22 + * There are quite a few different conventions for binary datetime, depending on different 1.23 + * platforms and protocols. Some of these have severe drawbacks. For example, people using 1.24 + * Unix time (seconds since Jan 1, 1970) think that they are safe until near the year 2038. 1.25 + * But cases can and do arise where arithmetic manipulations causes serious problems. Consider 1.26 + * the computation of the average of two datetimes, for example: if one calculates them with 1.27 + * <code>averageTime = (time1 + time2)/2</code>, there will be overflow even with dates 1.28 + * around the present. Moreover, even if these problems don't occur, there is the issue of 1.29 + * conversion back and forth between different systems. 1.30 + * 1.31 + * <p> 1.32 + * Binary datetimes differ in a number of ways: the datatype, the unit, 1.33 + * and the epoch (origin). We'll refer to these as time scales. For example: 1.34 + * 1.35 + * <table border="1" cellspacing="0" cellpadding="4"> 1.36 + * <caption>Table 1: Binary Time Scales</caption> 1.37 + * <tr> 1.38 + * <th align="left">Source</th> 1.39 + * <th align="left">Datatype</th> 1.40 + * <th align="left">Unit</th> 1.41 + * <th align="left">Epoch</th> 1.42 + * </tr> 1.43 + * 1.44 + * <tr> 1.45 + * <td>UDTS_JAVA_TIME</td> 1.46 + * <td>int64_t</td> 1.47 + * <td>milliseconds</td> 1.48 + * <td>Jan 1, 1970</td> 1.49 + * </tr> 1.50 + * <tr> 1.51 + * 1.52 + * <td>UDTS_UNIX_TIME</td> 1.53 + * <td>int32_t or int64_t</td> 1.54 + * <td>seconds</td> 1.55 + * <td>Jan 1, 1970</td> 1.56 + * </tr> 1.57 + * <tr> 1.58 + * <td>UDTS_ICU4C_TIME</td> 1.59 + * 1.60 + * <td>double</td> 1.61 + * <td>milliseconds</td> 1.62 + * <td>Jan 1, 1970</td> 1.63 + * </tr> 1.64 + * <tr> 1.65 + * <td>UDTS_WINDOWS_FILE_TIME</td> 1.66 + * <td>int64_t</td> 1.67 + * 1.68 + * <td>ticks (100 nanoseconds)</td> 1.69 + * <td>Jan 1, 1601</td> 1.70 + * </tr> 1.71 + * <tr> 1.72 + * <td>UDTS_DOTNET_DATE_TIME</td> 1.73 + * <td>int64_t</td> 1.74 + * <td>ticks (100 nanoseconds)</td> 1.75 + * 1.76 + * <td>Jan 1, 0001</td> 1.77 + * </tr> 1.78 + * <tr> 1.79 + * <td>UDTS_MAC_OLD_TIME</td> 1.80 + * <td>int32_t or int64_t</td> 1.81 + * <td>seconds</td> 1.82 + * <td>Jan 1, 1904</td> 1.83 + * 1.84 + * </tr> 1.85 + * <tr> 1.86 + * <td>UDTS_MAC_TIME</td> 1.87 + * <td>double</td> 1.88 + * <td>seconds</td> 1.89 + * <td>Jan 1, 2001</td> 1.90 + * </tr> 1.91 + * 1.92 + * <tr> 1.93 + * <td>UDTS_EXCEL_TIME</td> 1.94 + * <td>?</td> 1.95 + * <td>days</td> 1.96 + * <td>Dec 31, 1899</td> 1.97 + * </tr> 1.98 + * <tr> 1.99 + * 1.100 + * <td>UDTS_DB2_TIME</td> 1.101 + * <td>?</td> 1.102 + * <td>days</td> 1.103 + * <td>Dec 31, 1899</td> 1.104 + * </tr> 1.105 + * 1.106 + * <tr> 1.107 + * <td>UDTS_UNIX_MICROSECONDS_TIME</td> 1.108 + * <td>int64_t</td> 1.109 + * <td>microseconds</td> 1.110 + * <td>Jan 1, 1970</td> 1.111 + * </tr> 1.112 + * </table> 1.113 + * 1.114 + * <p> 1.115 + * All of the epochs start at 00:00 am (the earliest possible time on the day in question), 1.116 + * and are assumed to be UTC. 1.117 + * 1.118 + * <p> 1.119 + * The ranges for different datatypes are given in the following table (all values in years). 1.120 + * The range of years includes the entire range expressible with positive and negative 1.121 + * values of the datatype. The range of years for double is the range that would be allowed 1.122 + * without losing precision to the corresponding unit. 1.123 + * 1.124 + * <table border="1" cellspacing="0" cellpadding="4"> 1.125 + * <tr> 1.126 + * <th align="left">Units</th> 1.127 + * <th align="left">int64_t</th> 1.128 + * <th align="left">double</th> 1.129 + * <th align="left">int32_t</th> 1.130 + * </tr> 1.131 + * 1.132 + * <tr> 1.133 + * <td>1 sec</td> 1.134 + * <td align="right">5.84542x10<sup>11</sup></td> 1.135 + * <td align="right">285,420,920.94</td> 1.136 + * <td align="right">136.10</td> 1.137 + * </tr> 1.138 + * <tr> 1.139 + * 1.140 + * <td>1 millisecond</td> 1.141 + * <td align="right">584,542,046.09</td> 1.142 + * <td align="right">285,420.92</td> 1.143 + * <td align="right">0.14</td> 1.144 + * </tr> 1.145 + * <tr> 1.146 + * <td>1 microsecond</td> 1.147 + * 1.148 + * <td align="right">584,542.05</td> 1.149 + * <td align="right">285.42</td> 1.150 + * <td align="right">0.00</td> 1.151 + * </tr> 1.152 + * <tr> 1.153 + * <td>100 nanoseconds (tick)</td> 1.154 + * <td align="right">58,454.20</td> 1.155 + * <td align="right">28.54</td> 1.156 + * <td align="right">0.00</td> 1.157 + * </tr> 1.158 + * <tr> 1.159 + * <td>1 nanosecond</td> 1.160 + * <td align="right">584.5420461</td> 1.161 + * <td align="right">0.2854</td> 1.162 + * <td align="right">0.00</td> 1.163 + * </tr> 1.164 + * </table> 1.165 + * 1.166 + * <p> 1.167 + * These functions implement a universal time scale which can be used as a 'pivot', 1.168 + * and provide conversion functions to and from all other major time scales. 1.169 + * This datetimes to be converted to the pivot time, safely manipulated, 1.170 + * and converted back to any other datetime time scale. 1.171 + * 1.172 + *<p> 1.173 + * So what to use for this pivot? Java time has plenty of range, but cannot represent 1.174 + * .NET <code>System.DateTime</code> values without severe loss of precision. ICU4C time addresses this by using a 1.175 + * <code>double</code> that is otherwise equivalent to the Java time. However, there are disadvantages 1.176 + * with <code>doubles</code>. They provide for much more graceful degradation in arithmetic operations. 1.177 + * But they only have 53 bits of accuracy, which means that they will lose precision when 1.178 + * converting back and forth to ticks. What would really be nice would be a 1.179 + * <code>long double</code> (80 bits -- 64 bit mantissa), but that is not supported on most systems. 1.180 + * 1.181 + *<p> 1.182 + * The Unix extended time uses a structure with two components: time in seconds and a 1.183 + * fractional field (microseconds). However, this is clumsy, slow, and 1.184 + * prone to error (you always have to keep track of overflow and underflow in the 1.185 + * fractional field). <code>BigDecimal</code> would allow for arbitrary precision and arbitrary range, 1.186 + * but we do not want to use this as the normal type, because it is slow and does not 1.187 + * have a fixed size. 1.188 + * 1.189 + *<p> 1.190 + * Because of these issues, we ended up concluding that the .NET framework's 1.191 + * <code>System.DateTime</code> would be the best pivot. However, we use the full range 1.192 + * allowed by the datatype, allowing for datetimes back to 29,000 BC and up to 29,000 AD. 1.193 + * This time scale is very fine grained, does not lose precision, and covers a range that 1.194 + * will meet almost all requirements. It will not handle the range that Java times do, 1.195 + * but frankly, being able to handle dates before 29,000 BC or after 29,000 AD is of very limited interest. 1.196 + * 1.197 + */ 1.198 + 1.199 +/** 1.200 + * <code>UDateTimeScale</code> values are used to specify the time scale used for 1.201 + * conversion into or out if the universal time scale. 1.202 + * 1.203 + * @stable ICU 3.2 1.204 + */ 1.205 +typedef enum UDateTimeScale { 1.206 + /** 1.207 + * Used in the JDK. Data is a Java <code>long</code> (<code>int64_t</code>). Value 1.208 + * is milliseconds since January 1, 1970. 1.209 + * 1.210 + * @stable ICU 3.2 1.211 + */ 1.212 + UDTS_JAVA_TIME = 0, 1.213 + 1.214 + /** 1.215 + * Used on Unix systems. Data is <code>int32_t</code> or <code>int64_t</code>. Value 1.216 + * is seconds since January 1, 1970. 1.217 + * 1.218 + * @stable ICU 3.2 1.219 + */ 1.220 + UDTS_UNIX_TIME, 1.221 + 1.222 + /** 1.223 + * Used in IUC4C. Data is a <code>double</code>. Value 1.224 + * is milliseconds since January 1, 1970. 1.225 + * 1.226 + * @stable ICU 3.2 1.227 + */ 1.228 + UDTS_ICU4C_TIME, 1.229 + 1.230 + /** 1.231 + * Used in Windows for file times. Data is an <code>int64_t</code>. Value 1.232 + * is ticks (1 tick == 100 nanoseconds) since January 1, 1601. 1.233 + * 1.234 + * @stable ICU 3.2 1.235 + */ 1.236 + UDTS_WINDOWS_FILE_TIME, 1.237 + 1.238 + /** 1.239 + * Used in the .NET framework's <code>System.DateTime</code> structure. Data is an <code>int64_t</code>. Value 1.240 + * is ticks (1 tick == 100 nanoseconds) since January 1, 0001. 1.241 + * 1.242 + * @stable ICU 3.2 1.243 + */ 1.244 + UDTS_DOTNET_DATE_TIME, 1.245 + 1.246 + /** 1.247 + * Used in older Macintosh systems. Data is <code>int32_t</code> or <code>int64_t</code>. Value 1.248 + * is seconds since January 1, 1904. 1.249 + * 1.250 + * @stable ICU 3.2 1.251 + */ 1.252 + UDTS_MAC_OLD_TIME, 1.253 + 1.254 + /** 1.255 + * Used in newer Macintosh systems. Data is a <code>double</code>. Value 1.256 + * is seconds since January 1, 2001. 1.257 + * 1.258 + * @stable ICU 3.2 1.259 + */ 1.260 + UDTS_MAC_TIME, 1.261 + 1.262 + /** 1.263 + * Used in Excel. Data is an <code>?unknown?</code>. Value 1.264 + * is days since December 31, 1899. 1.265 + * 1.266 + * @stable ICU 3.2 1.267 + */ 1.268 + UDTS_EXCEL_TIME, 1.269 + 1.270 + /** 1.271 + * Used in DB2. Data is an <code>?unknown?</code>. Value 1.272 + * is days since December 31, 1899. 1.273 + * 1.274 + * @stable ICU 3.2 1.275 + */ 1.276 + UDTS_DB2_TIME, 1.277 + 1.278 + /** 1.279 + * Data is a <code>long</code>. Value is microseconds since January 1, 1970. 1.280 + * Similar to Unix time (linear value from 1970) and struct timeval 1.281 + * (microseconds resolution). 1.282 + * 1.283 + * @stable ICU 3.8 1.284 + */ 1.285 + UDTS_UNIX_MICROSECONDS_TIME, 1.286 + 1.287 + /** 1.288 + * The first unused time scale value. The limit of this enum 1.289 + */ 1.290 + UDTS_MAX_SCALE 1.291 +} UDateTimeScale; 1.292 + 1.293 +/** 1.294 + * <code>UTimeScaleValue</code> values are used to specify the time scale values 1.295 + * to <code>utmscale_getTimeScaleValue</code>. 1.296 + * 1.297 + * @see utmscale_getTimeScaleValue 1.298 + * 1.299 + * @stable ICU 3.2 1.300 + */ 1.301 +typedef enum UTimeScaleValue { 1.302 + /** 1.303 + * The constant used to select the units vale 1.304 + * for a time scale. 1.305 + * 1.306 + * @see utmscale_getTimeScaleValue 1.307 + * 1.308 + * @stable ICU 3.2 1.309 + */ 1.310 + UTSV_UNITS_VALUE = 0, 1.311 + 1.312 + /** 1.313 + * The constant used to select the epoch offset value 1.314 + * for a time scale. 1.315 + * 1.316 + * @see utmscale_getTimeScaleValue 1.317 + * 1.318 + * @stable ICU 3.2 1.319 + */ 1.320 + UTSV_EPOCH_OFFSET_VALUE=1, 1.321 + 1.322 + /** 1.323 + * The constant used to select the minimum from value 1.324 + * for a time scale. 1.325 + * 1.326 + * @see utmscale_getTimeScaleValue 1.327 + * 1.328 + * @stable ICU 3.2 1.329 + */ 1.330 + UTSV_FROM_MIN_VALUE=2, 1.331 + 1.332 + /** 1.333 + * The constant used to select the maximum from value 1.334 + * for a time scale. 1.335 + * 1.336 + * @see utmscale_getTimeScaleValue 1.337 + * 1.338 + * @stable ICU 3.2 1.339 + */ 1.340 + UTSV_FROM_MAX_VALUE=3, 1.341 + 1.342 + /** 1.343 + * The constant used to select the minimum to value 1.344 + * for a time scale. 1.345 + * 1.346 + * @see utmscale_getTimeScaleValue 1.347 + * 1.348 + * @stable ICU 3.2 1.349 + */ 1.350 + UTSV_TO_MIN_VALUE=4, 1.351 + 1.352 + /** 1.353 + * The constant used to select the maximum to value 1.354 + * for a time scale. 1.355 + * 1.356 + * @see utmscale_getTimeScaleValue 1.357 + * 1.358 + * @stable ICU 3.2 1.359 + */ 1.360 + UTSV_TO_MAX_VALUE=5, 1.361 + 1.362 +#ifndef U_HIDE_INTERNAL_API 1.363 + /** 1.364 + * The constant used to select the epoch plus one value 1.365 + * for a time scale. 1.366 + * 1.367 + * NOTE: This is an internal value. DO NOT USE IT. May not 1.368 + * actually be equal to the epoch offset value plus one. 1.369 + * 1.370 + * @see utmscale_getTimeScaleValue 1.371 + * 1.372 + * @internal ICU 3.2 1.373 + */ 1.374 + UTSV_EPOCH_OFFSET_PLUS_1_VALUE=6, 1.375 + 1.376 + /** 1.377 + * The constant used to select the epoch plus one value 1.378 + * for a time scale. 1.379 + * 1.380 + * NOTE: This is an internal value. DO NOT USE IT. May not 1.381 + * actually be equal to the epoch offset value plus one. 1.382 + * 1.383 + * @see utmscale_getTimeScaleValue 1.384 + * 1.385 + * @internal ICU 3.2 1.386 + */ 1.387 + UTSV_EPOCH_OFFSET_MINUS_1_VALUE=7, 1.388 + 1.389 + /** 1.390 + * The constant used to select the units round value 1.391 + * for a time scale. 1.392 + * 1.393 + * NOTE: This is an internal value. DO NOT USE IT. 1.394 + * 1.395 + * @see utmscale_getTimeScaleValue 1.396 + * 1.397 + * @internal ICU 3.2 1.398 + */ 1.399 + UTSV_UNITS_ROUND_VALUE=8, 1.400 + 1.401 + /** 1.402 + * The constant used to select the minimum safe rounding value 1.403 + * for a time scale. 1.404 + * 1.405 + * NOTE: This is an internal value. DO NOT USE IT. 1.406 + * 1.407 + * @see utmscale_getTimeScaleValue 1.408 + * 1.409 + * @internal ICU 3.2 1.410 + */ 1.411 + UTSV_MIN_ROUND_VALUE=9, 1.412 + 1.413 + /** 1.414 + * The constant used to select the maximum safe rounding value 1.415 + * for a time scale. 1.416 + * 1.417 + * NOTE: This is an internal value. DO NOT USE IT. 1.418 + * 1.419 + * @see utmscale_getTimeScaleValue 1.420 + * 1.421 + * @internal ICU 3.2 1.422 + */ 1.423 + UTSV_MAX_ROUND_VALUE=10, 1.424 + 1.425 +#endif /* U_HIDE_INTERNAL_API */ 1.426 + 1.427 + /** 1.428 + * The number of time scale values, in other words limit of this enum. 1.429 + * 1.430 + * @see utmscale_getTimeScaleValue 1.431 + */ 1.432 + UTSV_MAX_SCALE_VALUE=11 1.433 + 1.434 +} UTimeScaleValue; 1.435 + 1.436 +/** 1.437 + * Get a value associated with a particular time scale. 1.438 + * 1.439 + * @param timeScale The time scale 1.440 + * @param value A constant representing the value to get 1.441 + * @param status The status code. Set to <code>U_ILLEGAL_ARGUMENT_ERROR</code> if arguments are invalid. 1.442 + * @return - the value. 1.443 + * 1.444 + * @stable ICU 3.2 1.445 + */ 1.446 +U_STABLE int64_t U_EXPORT2 1.447 + utmscale_getTimeScaleValue(UDateTimeScale timeScale, UTimeScaleValue value, UErrorCode *status); 1.448 + 1.449 +/* Conversion to 'universal time scale' */ 1.450 + 1.451 +/** 1.452 + * Convert a <code>int64_t</code> datetime from the given time scale to the universal time scale. 1.453 + * 1.454 + * @param otherTime The <code>int64_t</code> datetime 1.455 + * @param timeScale The time scale to convert from 1.456 + * @param status The status code. Set to <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the conversion is out of range. 1.457 + * 1.458 + * @return The datetime converted to the universal time scale 1.459 + * 1.460 + * @stable ICU 3.2 1.461 + */ 1.462 +U_STABLE int64_t U_EXPORT2 1.463 + utmscale_fromInt64(int64_t otherTime, UDateTimeScale timeScale, UErrorCode *status); 1.464 + 1.465 +/* Conversion from 'universal time scale' */ 1.466 + 1.467 +/** 1.468 + * Convert a datetime from the universal time scale to a <code>int64_t</code> in the given time scale. 1.469 + * 1.470 + * @param universalTime The datetime in the universal time scale 1.471 + * @param timeScale The time scale to convert to 1.472 + * @param status The status code. Set to <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the conversion is out of range. 1.473 + * 1.474 + * @return The datetime converted to the given time scale 1.475 + * 1.476 + * @stable ICU 3.2 1.477 + */ 1.478 +U_STABLE int64_t U_EXPORT2 1.479 + utmscale_toInt64(int64_t universalTime, UDateTimeScale timeScale, UErrorCode *status); 1.480 + 1.481 +#endif /* #if !UCONFIG_NO_FORMATTING */ 1.482 + 1.483 +#endif 1.484 +