1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ********************************************************************************* 5 * Copyright (C) 2004-2016, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ********************************************************************************* 8 * 9 */ 10 11 package com.ibm.icu.util; 12 13 import com.ibm.icu.math.BigDecimal; 14 15 /** 16 * There are quite a few different conventions for binary datetime, depending on different 17 * platforms and protocols. Some of these have severe drawbacks. For example, people using 18 * Unix time (seconds since Jan 1, 1970, usually in a 32-bit integer) 19 * 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 * beginning in 2004. Moreover, even if these problems don't occur, there is the issue of 24 * conversion back and forth between different systems. 25 * 26 * <p>Binary datetimes differ in a number of ways: the datatype, the unit, 27 * and the epoch (origin). We refer to these as time scales. 28 * 29 * <p>ICU implements a universal time scale that is similar to the 30 * .NET framework's System.DateTime. The universal time scale is a 31 * 64-bit integer that holds ticks since midnight, January 1st, 0001. 32 * (One tick is 100 nanoseconds.) 33 * Negative values are supported. This has enough range to guarantee that 34 * calculations involving dates around the present are safe. 35 * 36 * <p>The universal time scale always measures time according to the 37 * proleptic Gregorian calendar. That is, the Gregorian calendar's 38 * leap year rules are used for all times, even before 1582 when it was 39 * introduced. (This is different from the default ICU calendar which 40 * switches from the Julian to the Gregorian calendar in 1582. 41 * See GregorianCalendar.setGregorianChange() and ucal_setGregorianChange().) 42 * 43 * ICU provides conversion functions to and from all other major time 44 * scales, allowing datetimes in any time scale to be converted to the 45 * universal time scale, safely manipulated, and converted back to any other 46 * datetime time scale. 47 * 48 * <p>For more details and background, see the 49 * <a href="http://www.icu-project.org/userguide/universalTimeScale.html">Universal Time Scale</a> 50 * chapter in the ICU User Guide. 51 * 52 * @stable ICU 3.2 53 */ 54 55 public final class UniversalTimeScale 56 { 57 /** 58 * Used in the JDK. Data is a <code>long</code>. Value 59 * is milliseconds since January 1, 1970. 60 * 61 * @stable ICU 3.2 62 */ 63 public static final int JAVA_TIME = 0; 64 65 /** 66 * Used in Unix systems. Data is an <code>int</code> or a <code>long</code>. Value 67 * is seconds since January 1, 1970. 68 * 69 * @stable ICU 3.2 70 */ 71 public static final int UNIX_TIME = 1; 72 73 /** 74 * Used in the ICU4C. Data is a <code>double</code>. Value 75 * is milliseconds since January 1, 1970. 76 * 77 * @stable ICU 3.2 78 */ 79 public static final int ICU4C_TIME = 2; 80 81 /** 82 * Used in Windows for file times. Data is a <code>long</code>. Value 83 * is ticks (1 tick == 100 nanoseconds) since January 1, 1601. 84 * 85 * @stable ICU 3.2 86 */ 87 public static final int WINDOWS_FILE_TIME = 3; 88 89 /** 90 * Used in the .NET framework's <code>System.DateTime</code> structure. 91 * Data is a <code>long</code>. Value is ticks (1 tick == 100 nanoseconds) since January 1, 0001. 92 * 93 * @stable ICU 3.2 94 */ 95 public static final int DOTNET_DATE_TIME = 4; 96 97 /** 98 * Used in older Macintosh systems. Data is an <code>int</code>. Value 99 * is seconds since January 1, 1904. 100 * 101 * @stable ICU 3.2 102 */ 103 public static final int MAC_OLD_TIME = 5; 104 105 /** 106 * Used in the JDK. Data is a <code>double</code>. Value 107 * is milliseconds since January 1, 2001. 108 * 109 * @stable ICU 3.2 110 */ 111 public static final int MAC_TIME = 6; 112 113 /** 114 * Used in Excel. Data is a <code>?unknown?</code>. Value 115 * is days since December 31, 1899. 116 * 117 * @stable ICU 3.2 118 */ 119 public static final int EXCEL_TIME = 7; 120 121 /** 122 * Used in DB2. Data is a <code>?unknown?</code>. Value 123 * is days since December 31, 1899. 124 * 125 * @stable ICU 3.2 126 */ 127 public static final int DB2_TIME = 8; 128 129 /** 130 * Data is a <code>long</code>. Value is microseconds since January 1, 1970. 131 * Similar to Unix time (linear value from 1970) and struct timeval 132 * (microseconds resolution). 133 * 134 * @stable ICU 3.8 135 */ 136 public static final int UNIX_MICROSECONDS_TIME = 9; 137 138 /** 139 * This is the first unused time scale value. 140 * 141 * @deprecated ICU 59 142 */ 143 @Deprecated 144 public static final int MAX_SCALE = 10; 145 146 /** 147 * The constant used to select the units value 148 * for a time scale. 149 * 150 * 151 * @stable ICU 3.2 152 */ 153 public static final int UNITS_VALUE = 0; 154 155 /** 156 * The constant used to select the epoch offset value 157 * for a time scale. 158 * 159 * @see #getTimeScaleValue 160 * 161 * @stable ICU 3.2 162 */ 163 public static final int EPOCH_OFFSET_VALUE = 1; 164 165 /** 166 * The constant used to select the minimum from value 167 * for a time scale. 168 * 169 * @see #getTimeScaleValue 170 * 171 * @stable ICU 3.2 172 */ 173 public static final int FROM_MIN_VALUE = 2; 174 175 /** 176 * The constant used to select the maximum from value 177 * for a time scale. 178 * 179 * @see #getTimeScaleValue 180 * 181 * @stable ICU 3.2 182 */ 183 public static final int FROM_MAX_VALUE = 3; 184 185 /** 186 * The constant used to select the minimum to value 187 * for a time scale. 188 * 189 * @see #getTimeScaleValue 190 * 191 * @stable ICU 3.2 192 */ 193 public static final int TO_MIN_VALUE = 4; 194 195 /** 196 * The constant used to select the maximum to value 197 * for a time scale. 198 * 199 * @see #getTimeScaleValue 200 * 201 * @stable ICU 3.2 202 */ 203 public static final int TO_MAX_VALUE = 5; 204 205 /** 206 * The constant used to select the epoch plus one value 207 * for a time scale. 208 * 209 * NOTE: This is an internal value. DO NOT USE IT. May not 210 * actually be equal to the epoch offset value plus one. 211 * 212 * @see #getTimeScaleValue 213 * 214 * @stable ICU 3.2 215 */ 216 public static final int EPOCH_OFFSET_PLUS_1_VALUE = 6; 217 218 /** 219 * The constant used to select the epoch offset minus one value 220 * for a time scale. 221 * 222 * NOTE: This is an internal value. DO NOT USE IT. May not 223 * actually be equal to the epoch offset value minus one. 224 * 225 * @see #getTimeScaleValue 226 * 227 * @internal 228 * @deprecated This API is ICU internal only. 229 */ 230 @Deprecated 231 public static final int EPOCH_OFFSET_MINUS_1_VALUE = 7; 232 233 /** 234 * The constant used to select the units round value 235 * for a time scale. 236 * 237 * NOTE: This is an internal value. DO NOT USE IT. 238 * 239 * @see #getTimeScaleValue 240 * 241 * @internal 242 * @deprecated This API is ICU internal only. 243 */ 244 @Deprecated 245 public static final int UNITS_ROUND_VALUE = 8; 246 247 /** 248 * The constant used to select the minimum safe rounding value 249 * for a time scale. 250 * 251 * NOTE: This is an internal value. DO NOT USE IT. 252 * 253 * @see #getTimeScaleValue 254 * 255 * @internal 256 * @deprecated This API is ICU internal only. 257 */ 258 @Deprecated 259 public static final int MIN_ROUND_VALUE = 9; 260 261 /** 262 * The constant used to select the maximum safe rounding value 263 * for a time scale. 264 * 265 * NOTE: This is an internal value. DO NOT USE IT. 266 * 267 * @see #getTimeScaleValue 268 * 269 * @internal 270 * @deprecated This API is ICU internal only. 271 */ 272 @Deprecated 273 public static final int MAX_ROUND_VALUE = 10; 274 275 /** 276 * The number of time scale values. 277 * 278 * NOTE: This is an internal value. DO NOT USE IT. 279 * 280 * @see #getTimeScaleValue 281 * 282 * @internal 283 * @deprecated This API is ICU internal only. 284 */ 285 @Deprecated 286 public static final int MAX_SCALE_VALUE = 11; 287 288 private static final long ticks = 1; 289 private static final long microseconds = ticks * 10; 290 private static final long milliseconds = microseconds * 1000; 291 private static final long seconds = milliseconds * 1000; 292 private static final long minutes = seconds * 60; 293 private static final long hours = minutes * 60; 294 private static final long days = hours * 24; 295 296 /** 297 * This class holds the data that describes a particular 298 * time scale. 299 */ 300 private static final class TimeScaleData 301 { TimeScaleData(long theUnits, long theEpochOffset, long theToMin, long theToMax, long theFromMin, long theFromMax)302 TimeScaleData(long theUnits, long theEpochOffset, 303 long theToMin, long theToMax, 304 long theFromMin, long theFromMax) 305 { 306 units = theUnits; 307 unitsRound = theUnits / 2; 308 309 minRound = Long.MIN_VALUE + unitsRound; 310 maxRound = Long.MAX_VALUE - unitsRound; 311 312 epochOffset = theEpochOffset / theUnits; 313 314 if (theUnits == 1) { 315 epochOffsetP1 = epochOffsetM1 = epochOffset; 316 } else { 317 epochOffsetP1 = epochOffset + 1; 318 epochOffsetM1 = epochOffset - 1; 319 } 320 321 toMin = theToMin; 322 toMax = theToMax; 323 324 fromMin = theFromMin; 325 fromMax = theFromMax; 326 } 327 328 long units; 329 long epochOffset; 330 long fromMin; 331 long fromMax; 332 long toMin; 333 long toMax; 334 335 long epochOffsetP1; 336 long epochOffsetM1; 337 long unitsRound; 338 long minRound; 339 long maxRound; 340 } 341 342 private static final TimeScaleData[] timeScaleTable = { 343 new TimeScaleData(milliseconds, 621355968000000000L, -9223372036854774999L, 9223372036854774999L, -984472800485477L, 860201606885477L), // JAVA_TIME 344 new TimeScaleData(seconds, 621355968000000000L, -9223372036854775808L, 9223372036854775807L, -984472800485L, 860201606885L), // UNIX_TIME 345 new TimeScaleData(milliseconds, 621355968000000000L, -9223372036854774999L, 9223372036854774999L, -984472800485477L, 860201606885477L), // ICU4C_TIME 346 new TimeScaleData(ticks, 504911232000000000L, -8718460804854775808L, 9223372036854775807L, -9223372036854775808L, 8718460804854775807L), // WINDOWS_FILE_TIME 347 new TimeScaleData(ticks, 000000000000000000L, -9223372036854775808L, 9223372036854775807L, -9223372036854775808L, 9223372036854775807L), // DOTNET_DATE_TIME 348 new TimeScaleData(seconds, 600527520000000000L, -9223372036854775808L, 9223372036854775807L, -982389955685L, 862284451685L), // MAC_OLD_TIME 349 new TimeScaleData(seconds, 631139040000000000L, -9223372036854775808L, 9223372036854775807L, -985451107685L, 859223299685L), // MAC_TIME 350 new TimeScaleData(days, 599265216000000000L, -9223372036854775808L, 9223372036854775807L, -11368793L, 9981605L), // EXCEL_TIME 351 new TimeScaleData(days, 599265216000000000L, -9223372036854775808L, 9223372036854775807L, -11368793L, 9981605L), // DB2_TIME 352 new TimeScaleData(microseconds, 621355968000000000L, -9223372036854775804L, 9223372036854775804L, -984472800485477580L, 860201606885477580L) // UNIX_MICROSECONDS_TIME 353 }; 354 355 356 /* 357 * Prevent construction of this class. 358 */ 359 ///CLOVER:OFF UniversalTimeScale()360 private UniversalTimeScale() 361 { 362 // nothing to do 363 } 364 ///CLOVER:ON 365 366 /** 367 * Convert a <code>long</code> datetime from the given time scale to the universal time scale. 368 * 369 * @param otherTime The <code>long</code> datetime 370 * @param timeScale The time scale to convert from 371 * 372 * @return The datetime converted to the universal time scale 373 * 374 * @stable ICU 3.2 375 */ from(long otherTime, int timeScale)376 public static long from(long otherTime, int timeScale) 377 { 378 TimeScaleData data = fromRangeCheck(otherTime, timeScale); 379 380 return (otherTime + data.epochOffset) * data.units; 381 } 382 383 /** 384 * Convert a <code>double</code> datetime from the given time scale to the universal time scale. 385 * All calculations are done using <code>BigDecimal</code> to guarantee that the value 386 * does not go out of range. 387 * 388 * @param otherTime The <code>double</code> datetime 389 * @param timeScale The time scale to convert from 390 * 391 * @return The datetime converted to the universal time scale 392 * 393 * @stable ICU 3.2 394 */ bigDecimalFrom(double otherTime, int timeScale)395 public static BigDecimal bigDecimalFrom(double otherTime, int timeScale) 396 { 397 TimeScaleData data = getTimeScaleData(timeScale); 398 BigDecimal other = new BigDecimal(String.valueOf(otherTime)); 399 BigDecimal units = new BigDecimal(data.units); 400 BigDecimal epochOffset = new BigDecimal(data.epochOffset); 401 402 return other.add(epochOffset).multiply(units); 403 } 404 405 /** 406 * Convert a <code>long</code> datetime from the given time scale to the universal time scale. 407 * All calculations are done using <code>BigDecimal</code> to guarantee that the value 408 * does not go out of range. 409 * 410 * @param otherTime The <code>long</code> datetime 411 * @param timeScale The time scale to convert from 412 * 413 * @return The datetime converted to the universal time scale 414 * 415 * @stable ICU 3.2 416 */ bigDecimalFrom(long otherTime, int timeScale)417 public static BigDecimal bigDecimalFrom(long otherTime, int timeScale) 418 { 419 TimeScaleData data = getTimeScaleData(timeScale); 420 BigDecimal other = new BigDecimal(otherTime); 421 BigDecimal units = new BigDecimal(data.units); 422 BigDecimal epochOffset = new BigDecimal(data.epochOffset); 423 424 return other.add(epochOffset).multiply(units); 425 } 426 427 /** 428 * Convert a <code>BigDecimal</code> datetime from the given time scale to the universal time scale. 429 * All calculations are done using <code>BigDecimal</code> to guarantee that the value 430 * does not go out of range. 431 * 432 * @param otherTime The <code>BigDecimal</code> datetime 433 * @param timeScale The time scale to convert from 434 * 435 * @return The datetime converted to the universal time scale 436 * 437 * @stable ICU 3.2 438 */ bigDecimalFrom(BigDecimal otherTime, int timeScale)439 public static BigDecimal bigDecimalFrom(BigDecimal otherTime, int timeScale) 440 { 441 TimeScaleData data = getTimeScaleData(timeScale); 442 443 BigDecimal units = new BigDecimal(data.units); 444 BigDecimal epochOffset = new BigDecimal(data.epochOffset); 445 446 return otherTime.add(epochOffset).multiply(units); 447 } 448 449 /** 450 * Convert a datetime from the universal time scale stored as a <code>BigDecimal</code> to a 451 * <code>long</code> in the given time scale. 452 * 453 * Since this calculation requires a divide, we must round. The straight forward 454 * way to round by adding half of the divisor will push the sum out of range for values 455 * within have the divisor of the limits of the precision of a <code>long</code>. To get around this, we do 456 * the rounding like this: 457 * 458 * <p><code> 459 * (universalTime - units + units/2) / units + 1 460 * </code> 461 * 462 * <p> 463 * (i.e. we subtract units first to guarantee that we'll still be in range when we 464 * add <code>units/2</code>. We then need to add one to the quotent to make up for the extra subtraction. 465 * This simplifies to: 466 * 467 * <p><code> 468 * (universalTime - units/2) / units - 1 469 * </code> 470 * 471 * <p> 472 * For negative values to round away from zero, we need to flip the signs: 473 * 474 * <p><code> 475 * (universalTime + units/2) / units + 1 476 * </code> 477 * 478 * <p> 479 * Since we also need to subtract the epochOffset, we fold the <code>+/- 1</code> 480 * into the offset value. (i.e. <code>epochOffsetP1</code>, <code>epochOffsetM1</code>.) 481 * 482 * @param universalTime The datetime in the universal time scale 483 * @param timeScale The time scale to convert to 484 * 485 * @return The datetime converted to the given time scale 486 * 487 * @stable ICU 3.2 488 */ toLong(long universalTime, int timeScale)489 public static long toLong(long universalTime, int timeScale) 490 { 491 TimeScaleData data = toRangeCheck(universalTime, timeScale); 492 493 if (universalTime < 0) { 494 if (universalTime < data.minRound) { 495 return (universalTime + data.unitsRound) / data.units - data.epochOffsetP1; 496 } 497 498 return (universalTime - data.unitsRound) / data.units - data.epochOffset; 499 } 500 501 if (universalTime > data.maxRound) { 502 return (universalTime - data.unitsRound) / data.units - data.epochOffsetM1; 503 } 504 505 return (universalTime + data.unitsRound) / data.units - data.epochOffset; 506 } 507 508 /** 509 * Convert a datetime from the universal time scale to a <code>BigDecimal</code> in the given time scale. 510 * 511 * @param universalTime The datetime in the universal time scale 512 * @param timeScale The time scale to convert to 513 * 514 * @return The datetime converted to the given time scale 515 * 516 * @stable ICU 3.2 517 */ toBigDecimal(long universalTime, int timeScale)518 public static BigDecimal toBigDecimal(long universalTime, int timeScale) 519 { 520 TimeScaleData data = getTimeScaleData(timeScale); 521 BigDecimal universal = new BigDecimal(universalTime); 522 BigDecimal units = new BigDecimal(data.units); 523 BigDecimal epochOffset = new BigDecimal(data.epochOffset); 524 525 return universal.divide(units, BigDecimal.ROUND_HALF_UP).subtract(epochOffset); 526 } 527 528 /** 529 * Convert a datetime from the universal time scale to a <code>BigDecimal</code> in the given time scale. 530 * 531 * @param universalTime The datetime in the universal time scale 532 * @param timeScale The time scale to convert to 533 * 534 * @return The datetime converted to the given time scale 535 * 536 * @stable ICU 3.2 537 */ toBigDecimal(BigDecimal universalTime, int timeScale)538 public static BigDecimal toBigDecimal(BigDecimal universalTime, int timeScale) 539 { 540 TimeScaleData data = getTimeScaleData(timeScale); 541 BigDecimal units = new BigDecimal(data.units); 542 BigDecimal epochOffset = new BigDecimal(data.epochOffset); 543 544 return universalTime.divide(units, BigDecimal.ROUND_HALF_UP).subtract(epochOffset); 545 } 546 547 /** 548 * Return the <code>TimeScaleData</code> object for the given time 549 * scale. 550 * 551 * @param scale - the time scale 552 * @return the <code>TimeScaleData</code> object for the given time scale 553 */ getTimeScaleData(int scale)554 private static TimeScaleData getTimeScaleData(int scale) 555 { 556 if (scale < 0 || scale >= MAX_SCALE) { 557 throw new IllegalArgumentException("scale out of range: " + scale); 558 } 559 560 return timeScaleTable[scale]; 561 } 562 563 /** 564 * Get a value associated with a particular time scale. 565 * 566 * @param scale - the time scale 567 * @param value - a constant representing the value to get 568 * 569 * @return - the value. 570 * 571 * @stable ICU 3.2 572 */ getTimeScaleValue(int scale, int value)573 public static long getTimeScaleValue(int scale, int value) 574 { 575 TimeScaleData data = getTimeScaleData(scale); 576 577 switch (value) 578 { 579 case UNITS_VALUE: 580 return data.units; 581 582 case EPOCH_OFFSET_VALUE: 583 return data.epochOffset; 584 585 case FROM_MIN_VALUE: 586 return data.fromMin; 587 588 case FROM_MAX_VALUE: 589 return data.fromMax; 590 591 case TO_MIN_VALUE: 592 return data.toMin; 593 594 case TO_MAX_VALUE: 595 return data.toMax; 596 597 case EPOCH_OFFSET_PLUS_1_VALUE: 598 return data.epochOffsetP1; 599 600 case EPOCH_OFFSET_MINUS_1_VALUE: 601 return data.epochOffsetM1; 602 603 case UNITS_ROUND_VALUE: 604 return data.unitsRound; 605 606 case MIN_ROUND_VALUE: 607 return data.minRound; 608 609 case MAX_ROUND_VALUE: 610 return data.maxRound; 611 612 default: 613 throw new IllegalArgumentException("value out of range: " + value); 614 } 615 } 616 toRangeCheck(long universalTime, int scale)617 private static TimeScaleData toRangeCheck(long universalTime, int scale) 618 { 619 TimeScaleData data = getTimeScaleData(scale); 620 621 if (universalTime >= data.toMin && universalTime <= data.toMax) { 622 return data; 623 } 624 625 throw new IllegalArgumentException("universalTime out of range:" + universalTime); 626 } 627 fromRangeCheck(long otherTime, int scale)628 private static TimeScaleData fromRangeCheck(long otherTime, int scale) 629 { 630 TimeScaleData data = getTimeScaleData(scale); 631 632 if (otherTime >= data.fromMin && otherTime <= data.fromMax) { 633 return data; 634 } 635 636 throw new IllegalArgumentException("otherTime out of range:" + otherTime); 637 } 638 639 /** 640 * Convert a time in the Universal Time Scale into another time 641 * scale. The division used to do the conversion rounds down. 642 * 643 * NOTE: This is an internal routine used by the tool that 644 * generates the to and from limits. Use it at your own risk. 645 * 646 * @param universalTime the time in the Universal Time scale 647 * @param timeScale the time scale to convert to 648 * @return the time in the given time scale 649 * 650 * @internal 651 * @deprecated This API is ICU internal only. 652 */ 653 @Deprecated toBigDecimalTrunc(BigDecimal universalTime, int timeScale)654 public static BigDecimal toBigDecimalTrunc(BigDecimal universalTime, int timeScale) 655 { 656 TimeScaleData data = getTimeScaleData(timeScale); 657 BigDecimal units = new BigDecimal(data.units); 658 BigDecimal epochOffset = new BigDecimal(data.epochOffset); 659 660 return universalTime.divide(units, BigDecimal.ROUND_DOWN).subtract(epochOffset); 661 } 662 } 663