12ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/* GENERATED SOURCE. DO NOT MODIFY. */ 2f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert// © 2016 and later: Unicode, Inc. and others. 3f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert// License & terms of use: http://www.unicode.org/copyright.html#License 42ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/* 52ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller ********************************************************************************* 6bfab1e7fec36dff93fb980c546ad64a565faf9fcPaul Duffin * Copyright (C) 2004-2016, International Business Machines Corporation and * 72ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * others. All Rights Reserved. * 82ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller ********************************************************************************* 92ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpackage android.icu.util; 132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.math.BigDecimal; 152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/** 172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * There are quite a few different conventions for binary datetime, depending on different 182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * platforms and protocols. Some of these have severe drawbacks. For example, people using 192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Unix time (seconds since Jan 1, 1970, usually in a 32-bit integer) 202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * think that they are safe until near the year 2038. 212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * But cases can and do arise where arithmetic manipulations causes serious problems. Consider 222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * the computation of the average of two datetimes, for example: if one calculates them with 232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <code>averageTime = (time1 + time2)/2</code>, there will be overflow even with dates 242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * beginning in 2004. Moreover, even if these problems don't occur, there is the issue of 252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * conversion back and forth between different systems. 262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p>Binary datetimes differ in a number of ways: the datatype, the unit, 28bfab1e7fec36dff93fb980c546ad64a565faf9fcPaul Duffin * and the epoch (origin). We refer to these as time scales. 292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p>ICU implements a universal time scale that is similar to the 312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * .NET framework's System.DateTime. The universal time scale is a 322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 64-bit integer that holds ticks since midnight, January 1st, 0001. 332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * (One tick is 100 nanoseconds.) 342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Negative values are supported. This has enough range to guarantee that 35bfab1e7fec36dff93fb980c546ad64a565faf9fcPaul Duffin * calculations involving dates around the present are safe. 362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p>The universal time scale always measures time according to the 382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * proleptic Gregorian calendar. That is, the Gregorian calendar's 392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * leap year rules are used for all times, even before 1582 when it was 402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * introduced. (This is different from the default ICU calendar which 412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * switches from the Julian to the Gregorian calendar in 1582. 42bfab1e7fec36dff93fb980c546ad64a565faf9fcPaul Duffin * See GregorianCalendar.setGregorianChange() and ucal_setGregorianChange().) 432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * ICU provides conversion functions to and from all other major time 452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * scales, allowing datetimes in any time scale to be converted to the 462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * universal time scale, safely manipulated, and converted back to any other 47bfab1e7fec36dff93fb980c546ad64a565faf9fcPaul Duffin * datetime time scale. 482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p>For more details and background, see the 502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <a href="http://www.icu-project.org/userguide/universalTimeScale.html">Universal Time Scale</a> 51bfab1e7fec36dff93fb980c546ad64a565faf9fcPaul Duffin * chapter in the ICU User Guide. 522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpublic final class UniversalTimeScale 552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller{ 562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Used in the JDK. Data is a <code>long</code>. Value 582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * is milliseconds since January 1, 1970. 592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int JAVA_TIME = 0; 612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Used in Unix systems. Data is an <code>int</code> or a <code>long</code>. Value 642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * is seconds since January 1, 1970. 652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int UNIX_TIME = 1; 672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Used in the ICU4C. Data is a <code>double</code>. Value 702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * is milliseconds since January 1, 1970. 712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int ICU4C_TIME = 2; 732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Used in Windows for file times. Data is a <code>long</code>. Value 762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * is ticks (1 tick == 100 nanoseconds) since January 1, 1601. 772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int WINDOWS_FILE_TIME = 3; 792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Used in the .NET framework's <code>System.DateTime</code> structure. 822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Data is a <code>long</code>. Value is ticks (1 tick == 100 nanoseconds) since January 1, 0001. 832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int DOTNET_DATE_TIME = 4; 852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Used in older Macintosh systems. Data is an <code>int</code>. Value 882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * is seconds since January 1, 1904. 892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int MAC_OLD_TIME = 5; 912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Used in the JDK. Data is a <code>double</code>. Value 942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * is milliseconds since January 1, 2001. 952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int MAC_TIME = 6; 972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Used in Excel. Data is a <code>?unknown?</code>. Value 1002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * is days since December 31, 1899. 1012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 1022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int EXCEL_TIME = 7; 1032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 1042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 1052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Used in DB2. Data is a <code>?unknown?</code>. Value 1062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * is days since December 31, 1899. 1072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 1082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int DB2_TIME = 8; 1092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 1102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 1112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Data is a <code>long</code>. Value is microseconds since January 1, 1970. 1122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Similar to Unix time (linear value from 1970) and struct timeval 1132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * (microseconds resolution). 1142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 1152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int UNIX_MICROSECONDS_TIME = 9; 1162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 1172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 1182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * This is the first unused time scale value. 119495cb271e305cfb399d463f32210a371198f0abfFredrik Roubert * 120495cb271e305cfb399d463f32210a371198f0abfFredrik Roubert * @deprecated ICU 59 1212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 122495cb271e305cfb399d463f32210a371198f0abfFredrik Roubert @Deprecated 1232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int MAX_SCALE = 10; 1242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 1252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 1262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The constant used to select the units value 1272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * for a time scale. 1282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 1292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int UNITS_VALUE = 0; 1302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 1312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 1322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The constant used to select the epoch offset value 1332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * for a time scale. 1342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 1352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @see #getTimeScaleValue 1362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 1372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int EPOCH_OFFSET_VALUE = 1; 1382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 1392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 1402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The constant used to select the minimum from value 1412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * for a time scale. 1422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 1432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @see #getTimeScaleValue 1442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 1452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int FROM_MIN_VALUE = 2; 1462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 1472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 1482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The constant used to select the maximum from value 1492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * for a time scale. 1502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 1512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @see #getTimeScaleValue 1522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 1532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int FROM_MAX_VALUE = 3; 1542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 1552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 1562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The constant used to select the minimum to value 1572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * for a time scale. 1582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 1592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @see #getTimeScaleValue 1602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 1612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int TO_MIN_VALUE = 4; 1622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 1632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 1642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The constant used to select the maximum to value 1652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * for a time scale. 1662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 1672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @see #getTimeScaleValue 1682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 1692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int TO_MAX_VALUE = 5; 1702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 1712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 1722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The constant used to select the epoch plus one value 1732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * for a time scale. 1742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 1752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * NOTE: This is an internal value. DO NOT USE IT. May not 1762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * actually be equal to the epoch offset value plus one. 1772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 1782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @see #getTimeScaleValue 1792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 1802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int EPOCH_OFFSET_PLUS_1_VALUE = 6; 1812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 1822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 1832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The constant used to select the epoch offset minus one value 1842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * for a time scale. 1852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 1862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * NOTE: This is an internal value. DO NOT USE IT. May not 1872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * actually be equal to the epoch offset value minus one. 1882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 1892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @see #getTimeScaleValue 1902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 1912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @deprecated This API is ICU internal only. 192836e6b40a94ec3fb7545a76cb072960442b7eee9Neil Fuller * @hide draft / provisional / internal are hidden on Android 1932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 1942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller @Deprecated 1952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int EPOCH_OFFSET_MINUS_1_VALUE = 7; 1962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 1972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 1982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The constant used to select the units round value 1992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * for a time scale. 2002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 2012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * NOTE: This is an internal value. DO NOT USE IT. 2022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 2032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @see #getTimeScaleValue 2042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 2052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @deprecated This API is ICU internal only. 206836e6b40a94ec3fb7545a76cb072960442b7eee9Neil Fuller * @hide draft / provisional / internal are hidden on Android 2072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 2082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller @Deprecated 2092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int UNITS_ROUND_VALUE = 8; 2102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 2112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 2122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The constant used to select the minimum safe rounding value 2132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * for a time scale. 2142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 2152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * NOTE: This is an internal value. DO NOT USE IT. 2162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 2172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @see #getTimeScaleValue 2182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 2192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @deprecated This API is ICU internal only. 220836e6b40a94ec3fb7545a76cb072960442b7eee9Neil Fuller * @hide draft / provisional / internal are hidden on Android 2212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 2222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller @Deprecated 2232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int MIN_ROUND_VALUE = 9; 2242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 2252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 2262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The constant used to select the maximum safe rounding value 2272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * for a time scale. 2282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 2292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * NOTE: This is an internal value. DO NOT USE IT. 2302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 2312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @see #getTimeScaleValue 2322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 2332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @deprecated This API is ICU internal only. 234836e6b40a94ec3fb7545a76cb072960442b7eee9Neil Fuller * @hide draft / provisional / internal are hidden on Android 2352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 2362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller @Deprecated 2372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int MAX_ROUND_VALUE = 10; 2382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 2392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 2402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * The number of time scale values. 2412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 2422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * NOTE: This is an internal value. DO NOT USE IT. 2432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 2442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @see #getTimeScaleValue 2452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 2462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @deprecated This API is ICU internal only. 247836e6b40a94ec3fb7545a76cb072960442b7eee9Neil Fuller * @hide draft / provisional / internal are hidden on Android 2482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 2492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller @Deprecated 2502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static final int MAX_SCALE_VALUE = 11; 2512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 2522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private static final long ticks = 1; 2532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private static final long microseconds = ticks * 10; 2542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private static final long milliseconds = microseconds * 1000; 2552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private static final long seconds = milliseconds * 1000; 2562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private static final long minutes = seconds * 60; 2572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private static final long hours = minutes * 60; 2582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private static final long days = hours * 24; 2592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 2602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 2612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * This class holds the data that describes a particular 2622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * time scale. 2632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 2642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private static final class TimeScaleData 2652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 2662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller TimeScaleData(long theUnits, long theEpochOffset, 2672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long theToMin, long theToMax, 2682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long theFromMin, long theFromMax) 2692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 2702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller units = theUnits; 2712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller unitsRound = theUnits / 2; 2722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 2732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller minRound = Long.MIN_VALUE + unitsRound; 2742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller maxRound = Long.MAX_VALUE - unitsRound; 2752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 2762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller epochOffset = theEpochOffset / theUnits; 2772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 2782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller if (theUnits == 1) { 2792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller epochOffsetP1 = epochOffsetM1 = epochOffset; 2802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } else { 2812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller epochOffsetP1 = epochOffset + 1; 2822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller epochOffsetM1 = epochOffset - 1; 2832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 2842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 2852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller toMin = theToMin; 2862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller toMax = theToMax; 2872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 2882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller fromMin = theFromMin; 2892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller fromMax = theFromMax; 2902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 2912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 2922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long units; 2932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long epochOffset; 2942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long fromMin; 2952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long fromMax; 2962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long toMin; 2972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long toMax; 2982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 2992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long epochOffsetP1; 3002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long epochOffsetM1; 3012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long unitsRound; 3022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long minRound; 3032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller long maxRound; 3042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 3052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 3062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private static final TimeScaleData[] timeScaleTable = { 3072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller new TimeScaleData(milliseconds, 621355968000000000L, -9223372036854774999L, 9223372036854774999L, -984472800485477L, 860201606885477L), // JAVA_TIME 3082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller new TimeScaleData(seconds, 621355968000000000L, -9223372036854775808L, 9223372036854775807L, -984472800485L, 860201606885L), // UNIX_TIME 3092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller new TimeScaleData(milliseconds, 621355968000000000L, -9223372036854774999L, 9223372036854774999L, -984472800485477L, 860201606885477L), // ICU4C_TIME 3102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller new TimeScaleData(ticks, 504911232000000000L, -8718460804854775808L, 9223372036854775807L, -9223372036854775808L, 8718460804854775807L), // WINDOWS_FILE_TIME 3112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller new TimeScaleData(ticks, 000000000000000000L, -9223372036854775808L, 9223372036854775807L, -9223372036854775808L, 9223372036854775807L), // DOTNET_DATE_TIME 3122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller new TimeScaleData(seconds, 600527520000000000L, -9223372036854775808L, 9223372036854775807L, -982389955685L, 862284451685L), // MAC_OLD_TIME 3132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller new TimeScaleData(seconds, 631139040000000000L, -9223372036854775808L, 9223372036854775807L, -985451107685L, 859223299685L), // MAC_TIME 3142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller new TimeScaleData(days, 599265216000000000L, -9223372036854775808L, 9223372036854775807L, -11368793L, 9981605L), // EXCEL_TIME 3152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller new TimeScaleData(days, 599265216000000000L, -9223372036854775808L, 9223372036854775807L, -11368793L, 9981605L), // DB2_TIME 3162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller new TimeScaleData(microseconds, 621355968000000000L, -9223372036854775804L, 9223372036854775804L, -984472800485477580L, 860201606885477580L) // UNIX_MICROSECONDS_TIME 3172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller }; 3182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 3192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 3202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /* 3212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Prevent construction of this class. 3222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 3232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller ///CLOVER:OFF 3242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private UniversalTimeScale() 3252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 3262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller // nothing to do 3272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 3282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller ///CLOVER:ON 3292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 3302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 3312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Convert a <code>long</code> datetime from the given time scale to the universal time scale. 3322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 3332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param otherTime The <code>long</code> datetime 3342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param timeScale The time scale to convert from 3352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 3362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @return The datetime converted to the universal time scale 3372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 3382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static long from(long otherTime, int timeScale) 3392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 3402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller TimeScaleData data = fromRangeCheck(otherTime, timeScale); 3412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 3422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return (otherTime + data.epochOffset) * data.units; 3432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 3442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 3452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 3462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Convert a <code>double</code> datetime from the given time scale to the universal time scale. 3472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * All calculations are done using <code>BigDecimal</code> to guarantee that the value 3482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * does not go out of range. 3492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 3502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param otherTime The <code>double</code> datetime 3512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param timeScale The time scale to convert from 3522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 3532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @return The datetime converted to the universal time scale 3542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 3552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static BigDecimal bigDecimalFrom(double otherTime, int timeScale) 3562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 3572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller TimeScaleData data = getTimeScaleData(timeScale); 3582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal other = new BigDecimal(String.valueOf(otherTime)); 3592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal units = new BigDecimal(data.units); 3602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal epochOffset = new BigDecimal(data.epochOffset); 3612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 3622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return other.add(epochOffset).multiply(units); 3632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 3642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 3652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 3662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Convert a <code>long</code> datetime from the given time scale to the universal time scale. 3672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * All calculations are done using <code>BigDecimal</code> to guarantee that the value 3682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * does not go out of range. 3692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 3702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param otherTime The <code>long</code> datetime 3712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param timeScale The time scale to convert from 3722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 3732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @return The datetime converted to the universal time scale 3742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 3752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static BigDecimal bigDecimalFrom(long otherTime, int timeScale) 3762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 3772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller TimeScaleData data = getTimeScaleData(timeScale); 3782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal other = new BigDecimal(otherTime); 3792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal units = new BigDecimal(data.units); 3802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal epochOffset = new BigDecimal(data.epochOffset); 3812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 3822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return other.add(epochOffset).multiply(units); 3832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 3842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 3852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 3862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Convert a <code>BigDecimal</code> datetime from the given time scale to the universal time scale. 3872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * All calculations are done using <code>BigDecimal</code> to guarantee that the value 3882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * does not go out of range. 3892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 3902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param otherTime The <code>BigDecimal</code> datetime 3912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param timeScale The time scale to convert from 3922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 3932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @return The datetime converted to the universal time scale 3942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 3952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static BigDecimal bigDecimalFrom(BigDecimal otherTime, int timeScale) 3962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 3972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller TimeScaleData data = getTimeScaleData(timeScale); 3982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 3992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal units = new BigDecimal(data.units); 4002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal epochOffset = new BigDecimal(data.epochOffset); 4012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 4022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return otherTime.add(epochOffset).multiply(units); 4032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 4042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 4052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 4062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Convert a datetime from the universal time scale stored as a <code>BigDecimal</code> to a 4072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <code>long</code> in the given time scale. 4082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Since this calculation requires a divide, we must round. The straight forward 4102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * way to round by adding half of the divisor will push the sum out of range for values 4112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * within have the divisor of the limits of the precision of a <code>long</code>. To get around this, we do 4122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * the rounding like this: 4132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p><code> 4152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * (universalTime - units + units/2) / units + 1 4162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * </code> 4172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p> 4192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * (i.e. we subtract units first to guarantee that we'll still be in range when we 4202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * add <code>units/2</code>. We then need to add one to the quotent to make up for the extra subtraction. 4212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * This simplifies to: 4222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p><code> 4242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * (universalTime - units/2) / units - 1 4252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * </code> 4262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p> 4282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * For negative values to round away from zero, we need to flip the signs: 4292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p><code> 4312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * (universalTime + units/2) / units + 1 4322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * </code> 4332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * <p> 4352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Since we also need to subtract the epochOffset, we fold the <code>+/- 1</code> 4362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * into the offset value. (i.e. <code>epochOffsetP1</code>, <code>epochOffsetM1</code>.) 4372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param universalTime The datetime in the universal time scale 4392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param timeScale The time scale to convert to 4402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @return The datetime converted to the given time scale 4422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 4432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static long toLong(long universalTime, int timeScale) 4442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 4452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller TimeScaleData data = toRangeCheck(universalTime, timeScale); 4462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 4472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller if (universalTime < 0) { 4482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller if (universalTime < data.minRound) { 4492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return (universalTime + data.unitsRound) / data.units - data.epochOffsetP1; 4502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 4512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 4522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return (universalTime - data.unitsRound) / data.units - data.epochOffset; 4532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 4542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 4552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller if (universalTime > data.maxRound) { 4562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return (universalTime - data.unitsRound) / data.units - data.epochOffsetM1; 4572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 4582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 4592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return (universalTime + data.unitsRound) / data.units - data.epochOffset; 4602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 4612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 4622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 4632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Convert a datetime from the universal time scale to a <code>BigDecimal</code> in the given time scale. 4642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param universalTime The datetime in the universal time scale 4662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param timeScale The time scale to convert to 4672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @return The datetime converted to the given time scale 4692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 4702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static BigDecimal toBigDecimal(long universalTime, int timeScale) 4712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 4722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller TimeScaleData data = getTimeScaleData(timeScale); 4732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal universal = new BigDecimal(universalTime); 4742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal units = new BigDecimal(data.units); 4752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal epochOffset = new BigDecimal(data.epochOffset); 4762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 4772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return universal.divide(units, BigDecimal.ROUND_HALF_UP).subtract(epochOffset); 4782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 4792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 4802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 4812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Convert a datetime from the universal time scale to a <code>BigDecimal</code> in the given time scale. 4822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param universalTime The datetime in the universal time scale 4842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param timeScale The time scale to convert to 4852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 4862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @return The datetime converted to the given time scale 4872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 4882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static BigDecimal toBigDecimal(BigDecimal universalTime, int timeScale) 4892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 4902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller TimeScaleData data = getTimeScaleData(timeScale); 4912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal units = new BigDecimal(data.units); 4922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal epochOffset = new BigDecimal(data.epochOffset); 4932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 4942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return universalTime.divide(units, BigDecimal.ROUND_HALF_UP).subtract(epochOffset); 4952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 4962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 4972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 4982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Return the <code>TimeScaleData</code> object for the given time 4992ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * scale. 5002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 5012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param scale - the time scale 5022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @return the <code>TimeScaleData</code> object for the given time scale 5032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 5042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private static TimeScaleData getTimeScaleData(int scale) 5052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 5062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller if (scale < 0 || scale >= MAX_SCALE) { 5072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller throw new IllegalArgumentException("scale out of range: " + scale); 5082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 5092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return timeScaleTable[scale]; 5112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 5122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 5142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Get a value associated with a particular time scale. 5152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 5162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param scale - the time scale 5172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param value - a constant representing the value to get 5182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 5192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @return - the value. 5202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 5212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static long getTimeScaleValue(int scale, int value) 5222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 5232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller TimeScaleData data = getTimeScaleData(scale); 5242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller switch (value) 5262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 5272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller case UNITS_VALUE: 5282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data.units; 5292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller case EPOCH_OFFSET_VALUE: 5312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data.epochOffset; 5322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller case FROM_MIN_VALUE: 5342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data.fromMin; 5352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller case FROM_MAX_VALUE: 5372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data.fromMax; 5382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller case TO_MIN_VALUE: 5402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data.toMin; 5412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller case TO_MAX_VALUE: 5432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data.toMax; 5442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller case EPOCH_OFFSET_PLUS_1_VALUE: 5462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data.epochOffsetP1; 5472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5482ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller case EPOCH_OFFSET_MINUS_1_VALUE: 5492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data.epochOffsetM1; 5502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller case UNITS_ROUND_VALUE: 5522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data.unitsRound; 5532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller case MIN_ROUND_VALUE: 5552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data.minRound; 5562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller case MAX_ROUND_VALUE: 5582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data.maxRound; 5592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller default: 5612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller throw new IllegalArgumentException("value out of range: " + value); 5622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 5632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 5642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private static TimeScaleData toRangeCheck(long universalTime, int scale) 5662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 5672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller TimeScaleData data = getTimeScaleData(scale); 5682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller if (universalTime >= data.toMin && universalTime <= data.toMax) { 5702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data; 5712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 5722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller throw new IllegalArgumentException("universalTime out of range:" + universalTime); 5742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 5752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller private static TimeScaleData fromRangeCheck(long otherTime, int scale) 5772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 5782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller TimeScaleData data = getTimeScaleData(scale); 5792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller if (otherTime >= data.fromMin && otherTime <= data.fromMax) { 5812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return data; 5822ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 5832ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5842ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller throw new IllegalArgumentException("otherTime out of range:" + otherTime); 5852ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 5862ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 5872ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller /** 5882ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * Convert a time in the Universal Time Scale into another time 5892ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * scale. The division used to do the conversion rounds down. 5902ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 5912ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * NOTE: This is an internal routine used by the tool that 5922ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * generates the to and from limits. Use it at your own risk. 5932ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 5942ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param universalTime the time in the Universal Time scale 5952ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @param timeScale the time scale to convert to 5962ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @return the time in the given time scale 5972ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * 5982ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * @deprecated This API is ICU internal only. 599836e6b40a94ec3fb7545a76cb072960442b7eee9Neil Fuller * @hide draft / provisional / internal are hidden on Android 6002ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */ 6012ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller @Deprecated 6022ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller public static BigDecimal toBigDecimalTrunc(BigDecimal universalTime, int timeScale) 6032ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller { 6042ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller TimeScaleData data = getTimeScaleData(timeScale); 6052ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal units = new BigDecimal(data.units); 6062ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller BigDecimal epochOffset = new BigDecimal(data.epochOffset); 6072ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller 6082ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller return universalTime.divide(units, BigDecimal.ROUND_DOWN).subtract(epochOffset); 6092ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller } 6102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller} 611