]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/utmscale.c
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / i18n / utmscale.c
CommitLineData
374ca955
A
1/*
2*******************************************************************************
73c04bcf 3* Copyright (C) 2004-2006, International Business Machines Corporation and
374ca955
A
4* others. All Rights Reserved.
5*******************************************************************************
6*/
7
8#include "unicode/utypes.h"
9
10#if !UCONFIG_NO_FORMATTING
11
12#include "unicode/utmscale.h"
13
14#define ticks INT64_C(1)
15#define microseconds (ticks * 10)
16#define milliseconds (microseconds * 1000)
17#define seconds (milliseconds * 1000)
18#define minutes (seconds * 60)
19#define hours (minutes * 60)
20#define days (hours * 24)
21
73c04bcf
A
22/* Constants generated by ICU4J com.ibm.icu.dev.tool.timescale.GenerateCTimeScaleData. */
23static const int64_t timeScaleTable[UDTS_MAX_SCALE][UTSV_MAX_SCALE_VALUE] = {
374ca955 24 /* units epochOffset fromMin fromMax toMin toMax epochOffsetP1 epochOffsetM1 unitsRound minRound maxRound */
73c04bcf
A
25 {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)},
26 {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)},
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 {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},
29 {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},
30 {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)},
31 {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)},
32 {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)},
33 {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)},
374ca955
A
34};
35
36U_CAPI int64_t U_EXPORT2
37utmscale_getTimeScaleValue(UDateTimeScale timeScale, UTimeScaleValue value, UErrorCode *status)
38{
374ca955
A
39 if (status == NULL || U_FAILURE(*status)) {
40 return 0;
41 }
42
73c04bcf
A
43 if (timeScale < UDTS_JAVA_TIME || UDTS_MAX_SCALE <= timeScale
44 || value < UTSV_UNITS_VALUE || UTSV_MAX_SCALE_VALUE <= value)
374ca955 45 {
374ca955
A
46 *status = U_ILLEGAL_ARGUMENT_ERROR;
47 return 0;
48 }
73c04bcf
A
49
50 return timeScaleTable[timeScale][value];
374ca955
A
51}
52
53U_CAPI int64_t U_EXPORT2
54utmscale_fromInt64(int64_t otherTime, UDateTimeScale timeScale, UErrorCode *status)
55{
73c04bcf 56 const int64_t *data;
374ca955
A
57
58 if (status == NULL || U_FAILURE(*status)) {
59 return 0;
60 }
61
62 if (timeScale < 0 || timeScale >= UDTS_MAX_SCALE) {
63 *status = U_ILLEGAL_ARGUMENT_ERROR;
64 return 0;
65 }
66
73c04bcf 67 data = (const int64_t *)(&timeScaleTable[timeScale]);
374ca955 68
73c04bcf 69 if (otherTime < data[UTSV_FROM_MIN_VALUE] || otherTime > data[UTSV_FROM_MAX_VALUE]) {
374ca955
A
70 *status = U_ILLEGAL_ARGUMENT_ERROR;
71 return 0;
72 }
73
73c04bcf 74 return (otherTime + data[UTSV_EPOCH_OFFSET_VALUE]) * data[UTSV_UNITS_VALUE];
374ca955
A
75}
76
77U_CAPI int64_t U_EXPORT2
78utmscale_toInt64(int64_t universalTime, UDateTimeScale timeScale, UErrorCode *status)
79{
73c04bcf 80 const int64_t *data;
374ca955
A
81
82 if (status == NULL || U_FAILURE(*status)) {
83 return 0;
84 }
85
86 if (timeScale < 0 || timeScale >= UDTS_MAX_SCALE) {
87 *status = U_ILLEGAL_ARGUMENT_ERROR;
88 return 0;
89 }
90
73c04bcf 91 data = (const int64_t *)(&timeScaleTable[timeScale]);
374ca955 92
73c04bcf 93 if (universalTime < data[UTSV_TO_MIN_VALUE] || universalTime > data[UTSV_TO_MAX_VALUE]) {
374ca955
A
94 *status = U_ILLEGAL_ARGUMENT_ERROR;
95 return 0;
96 }
97
98 if (universalTime < 0) {
73c04bcf
A
99 if (universalTime < data[UTSV_MIN_ROUND_VALUE]) {
100 return (universalTime + data[UTSV_UNITS_ROUND_VALUE]) / data[UTSV_UNITS_VALUE] - data[UTSV_EPOCH_OFFSET_PLUS_1_VALUE];
374ca955
A
101 }
102
73c04bcf 103 return (universalTime - data[UTSV_UNITS_ROUND_VALUE]) / data[UTSV_UNITS_VALUE] - data[UTSV_EPOCH_OFFSET_VALUE];
374ca955
A
104 }
105
73c04bcf
A
106 if (universalTime > data[UTSV_MAX_ROUND_VALUE]) {
107 return (universalTime - data[UTSV_UNITS_ROUND_VALUE]) / data[UTSV_UNITS_VALUE] - data[UTSV_EPOCH_OFFSET_MINUS_1_VALUE];
374ca955
A
108 }
109
73c04bcf 110 return (universalTime + data[UTSV_UNITS_ROUND_VALUE]) / data[UTSV_UNITS_VALUE] - data[UTSV_EPOCH_OFFSET_VALUE];
374ca955
A
111}
112
113#endif /* #if !UCONFIG_NO_FORMATTING */