]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/utmscale.cpp
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / utmscale.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
374ca955
A
3/*
4*******************************************************************************
51004dcb 5* Copyright (C) 2004-2012, International Business Machines Corporation and
374ca955
A
6* others. All Rights Reserved.
7*******************************************************************************
8*/
9
10#include "unicode/utypes.h"
11
12#if !UCONFIG_NO_FORMATTING
13
14#include "unicode/utmscale.h"
15
16#define ticks INT64_C(1)
17#define microseconds (ticks * 10)
18#define milliseconds (microseconds * 1000)
19#define seconds (milliseconds * 1000)
20#define minutes (seconds * 60)
21#define hours (minutes * 60)
22#define days (hours * 24)
23
73c04bcf
A
24/* Constants generated by ICU4J com.ibm.icu.dev.tool.timescale.GenerateCTimeScaleData. */
25static const int64_t timeScaleTable[UDTS_MAX_SCALE][UTSV_MAX_SCALE_VALUE] = {
374ca955 26 /* units epochOffset fromMin fromMax toMin toMax epochOffsetP1 epochOffsetM1 unitsRound minRound maxRound */
73c04bcf
A
27 {milliseconds, INT64_C(62135596800000), INT64_C(-984472800485477), INT64_C(860201606885477), INT64_C(-9223372036854774999), INT64_C(9223372036854774999), INT64_C(62135596800001), INT64_C(62135596799999), INT64_C(5000), INT64_C(-9223372036854770808), INT64_C(9223372036854770807)},
28 {seconds, INT64_C(62135596800), INT64_C(-984472800485), INT64_C(860201606885), U_INT64_MIN, U_INT64_MAX, INT64_C(62135596801), INT64_C(62135596799), INT64_C(5000000), INT64_C(-9223372036849775808), INT64_C(9223372036849775807)},
29 {milliseconds, INT64_C(62135596800000), INT64_C(-984472800485477), INT64_C(860201606885477), INT64_C(-9223372036854774999), INT64_C(9223372036854774999), INT64_C(62135596800001), INT64_C(62135596799999), INT64_C(5000), INT64_C(-9223372036854770808), INT64_C(9223372036854770807)},
30 {ticks, INT64_C(504911232000000000), U_INT64_MIN, INT64_C(8718460804854775807), INT64_C(-8718460804854775808), U_INT64_MAX, INT64_C(504911232000000000), INT64_C(504911232000000000), INT64_C(0), U_INT64_MIN, U_INT64_MAX},
31 {ticks, INT64_C(0), U_INT64_MIN, U_INT64_MAX, U_INT64_MIN, U_INT64_MAX, INT64_C(0), INT64_C(0), INT64_C(0), U_INT64_MIN, U_INT64_MAX},
32 {seconds, INT64_C(60052752000), INT64_C(-982389955685), INT64_C(862284451685), U_INT64_MIN, U_INT64_MAX, INT64_C(60052752001), INT64_C(60052751999), INT64_C(5000000), INT64_C(-9223372036849775808), INT64_C(9223372036849775807)},
33 {seconds, INT64_C(63113904000), INT64_C(-985451107685), INT64_C(859223299685), U_INT64_MIN, U_INT64_MAX, INT64_C(63113904001), INT64_C(63113903999), INT64_C(5000000), INT64_C(-9223372036849775808), INT64_C(9223372036849775807)},
34 {days, INT64_C(693594), INT64_C(-11368793), INT64_C(9981605), U_INT64_MIN, U_INT64_MAX, INT64_C(693595), INT64_C(693593), INT64_C(432000000000), INT64_C(-9223371604854775808), INT64_C(9223371604854775807)},
35 {days, INT64_C(693594), INT64_C(-11368793), INT64_C(9981605), U_INT64_MIN, U_INT64_MAX, INT64_C(693595), INT64_C(693593), INT64_C(432000000000), INT64_C(-9223371604854775808), INT64_C(9223371604854775807)},
46f4442e 36 {microseconds, INT64_C(62135596800000000), INT64_C(-984472800485477580), INT64_C(860201606885477580), INT64_C(-9223372036854775804), INT64_C(9223372036854775804), INT64_C(62135596800000001), INT64_C(62135596799999999), INT64_C(5), INT64_C(-9223372036854775803), INT64_C(9223372036854775802)},
374ca955
A
37};
38
39U_CAPI int64_t U_EXPORT2
40utmscale_getTimeScaleValue(UDateTimeScale timeScale, UTimeScaleValue value, UErrorCode *status)
41{
374ca955
A
42 if (status == NULL || U_FAILURE(*status)) {
43 return 0;
44 }
45
73c04bcf
A
46 if (timeScale < UDTS_JAVA_TIME || UDTS_MAX_SCALE <= timeScale
47 || value < UTSV_UNITS_VALUE || UTSV_MAX_SCALE_VALUE <= value)
374ca955 48 {
374ca955
A
49 *status = U_ILLEGAL_ARGUMENT_ERROR;
50 return 0;
51 }
73c04bcf
A
52
53 return timeScaleTable[timeScale][value];
374ca955
A
54}
55
56U_CAPI int64_t U_EXPORT2
57utmscale_fromInt64(int64_t otherTime, UDateTimeScale timeScale, UErrorCode *status)
58{
73c04bcf 59 const int64_t *data;
374ca955
A
60
61 if (status == NULL || U_FAILURE(*status)) {
62 return 0;
63 }
64
51004dcb 65 if ((int32_t)timeScale < 0 || timeScale >= UDTS_MAX_SCALE) {
374ca955
A
66 *status = U_ILLEGAL_ARGUMENT_ERROR;
67 return 0;
68 }
69
73c04bcf 70 data = (const int64_t *)(&timeScaleTable[timeScale]);
374ca955 71
73c04bcf 72 if (otherTime < data[UTSV_FROM_MIN_VALUE] || otherTime > data[UTSV_FROM_MAX_VALUE]) {
374ca955
A
73 *status = U_ILLEGAL_ARGUMENT_ERROR;
74 return 0;
75 }
76
73c04bcf 77 return (otherTime + data[UTSV_EPOCH_OFFSET_VALUE]) * data[UTSV_UNITS_VALUE];
374ca955
A
78}
79
80U_CAPI int64_t U_EXPORT2
81utmscale_toInt64(int64_t universalTime, UDateTimeScale timeScale, UErrorCode *status)
82{
73c04bcf 83 const int64_t *data;
374ca955
A
84
85 if (status == NULL || U_FAILURE(*status)) {
86 return 0;
87 }
88
51004dcb 89 if ((int32_t)timeScale < 0 || timeScale >= UDTS_MAX_SCALE) {
374ca955
A
90 *status = U_ILLEGAL_ARGUMENT_ERROR;
91 return 0;
92 }
93
73c04bcf 94 data = (const int64_t *)(&timeScaleTable[timeScale]);
374ca955 95
73c04bcf 96 if (universalTime < data[UTSV_TO_MIN_VALUE] || universalTime > data[UTSV_TO_MAX_VALUE]) {
374ca955
A
97 *status = U_ILLEGAL_ARGUMENT_ERROR;
98 return 0;
99 }
100
101 if (universalTime < 0) {
73c04bcf
A
102 if (universalTime < data[UTSV_MIN_ROUND_VALUE]) {
103 return (universalTime + data[UTSV_UNITS_ROUND_VALUE]) / data[UTSV_UNITS_VALUE] - data[UTSV_EPOCH_OFFSET_PLUS_1_VALUE];
374ca955
A
104 }
105
73c04bcf 106 return (universalTime - data[UTSV_UNITS_ROUND_VALUE]) / data[UTSV_UNITS_VALUE] - data[UTSV_EPOCH_OFFSET_VALUE];
374ca955
A
107 }
108
73c04bcf
A
109 if (universalTime > data[UTSV_MAX_ROUND_VALUE]) {
110 return (universalTime - data[UTSV_UNITS_ROUND_VALUE]) / data[UTSV_UNITS_VALUE] - data[UTSV_EPOCH_OFFSET_MINUS_1_VALUE];
374ca955
A
111 }
112
73c04bcf 113 return (universalTime + data[UTSV_UNITS_ROUND_VALUE]) / data[UTSV_UNITS_VALUE] - data[UTSV_EPOCH_OFFSET_VALUE];
374ca955
A
114}
115
116#endif /* #if !UCONFIG_NO_FORMATTING */