]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/cecal.cpp
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / cecal.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2003 - 2009, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
8 */
9
10 #include "unicode/utypes.h"
11
12 #if !UCONFIG_NO_FORMATTING
13
14 #include "cecal.h"
15 #include "gregoimp.h" //Math
16
17 U_NAMESPACE_BEGIN
18
19 static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {
20 // Minimum Greatest Least Maximum
21 // Minimum Maximum
22 { 0, 0, 1, 1}, // ERA
23 { 1, 1, 5000000, 5000000}, // YEAR
24 { 0, 0, 12, 12}, // MONTH
25 { 1, 1, 52, 53}, // WEEK_OF_YEAR
26 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
27 { 1, 1, 5, 30}, // DAY_OF_MONTH
28 { 1, 1, 365, 366}, // DAY_OF_YEAR
29 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
30 { -1, -1, 1, 5}, // DAY_OF_WEEK_IN_MONTH
31 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM
32 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
33 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
34 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
35 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
36 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
37 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
38 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
39 { -5000000, -5000000, 5000000, 5000000}, // YEAR_WOY
40 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
41 { -5000000, -5000000, 5000000, 5000000}, // EXTENDED_YEAR
42 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
43 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
44 {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH
45 };
46
47 //-------------------------------------------------------------------------
48 // Constructors...
49 //-------------------------------------------------------------------------
50
51 CECalendar::CECalendar(const Locale& aLocale, UErrorCode& success)
52 : Calendar(TimeZone::createDefault(), aLocale, success)
53 {
54 setTimeInMillis(getNow(), success);
55 }
56
57 CECalendar::CECalendar (const CECalendar& other)
58 : Calendar(other)
59 {
60 }
61
62 CECalendar::~CECalendar()
63 {
64 }
65
66 CECalendar&
67 CECalendar::operator=(const CECalendar& right)
68 {
69 Calendar::operator=(right);
70 return *this;
71 }
72
73 //-------------------------------------------------------------------------
74 // Calendar framework
75 //-------------------------------------------------------------------------
76
77 int32_t
78 CECalendar::handleComputeMonthStart(int32_t eyear,int32_t emonth, UBool /*useMonth*/) const
79 {
80 return ceToJD(eyear, emonth, 0, getJDEpochOffset());
81 }
82
83 int32_t
84 CECalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
85 {
86 return LIMITS[field][limitType];
87 }
88
89 UBool
90 CECalendar::inDaylightTime(UErrorCode& status) const
91 {
92 if (U_FAILURE(status) || !getTimeZone().useDaylightTime()) {
93 return FALSE;
94 }
95
96 // Force an update of the state of the Calendar.
97 ((CECalendar*)this)->complete(status); // cast away const
98
99 return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);
100 }
101
102 UBool
103 CECalendar::haveDefaultCentury() const
104 {
105 return TRUE;
106 }
107
108 //-------------------------------------------------------------------------
109 // Calendar system Conversion methods...
110 //-------------------------------------------------------------------------
111 int32_t
112 CECalendar::ceToJD(int32_t year, int32_t month, int32_t date, int32_t jdEpochOffset)
113 {
114 // handle month > 12, < 0 (e.g. from add/set)
115 if ( month >= 0 ) {
116 year += month/13;
117 month %= 13;
118 } else {
119 ++month;
120 year += month/13 - 1;
121 month = month%13 + 12;
122 }
123 return (int32_t) (
124 jdEpochOffset // difference from Julian epoch to 1,1,1
125 + 365 * year // number of days from years
126 + ClockMath::floorDivide(year, 4) // extra day of leap year
127 + 30 * month // number of days from months (months are 0-based)
128 + date - 1 // number of days for present month (1 based)
129 );
130 }
131
132 void
133 CECalendar::jdToCE(int32_t julianDay, int32_t jdEpochOffset, int32_t& year, int32_t& month, int32_t& day)
134 {
135 int32_t c4; // number of 4 year cycle (1461 days)
136 int32_t r4; // remainder of 4 year cycle, always positive
137
138 c4 = ClockMath::floorDivide(julianDay - jdEpochOffset, 1461, r4);
139
140 year = 4 * c4 + (r4/365 - r4/1460); // 4 * <number of 4year cycle> + <years within the last cycle>
141
142 int32_t doy = (r4 == 1460) ? 365 : (r4 % 365); // days in present year
143
144 month = doy / 30; // 30 -> Coptic/Ethiopic month length up to 12th month
145 day = (doy % 30) + 1; // 1-based days in a month
146 }
147
148 U_NAMESPACE_END
149
150 #endif /* #if !UCONFIG_NO_FORMATTING */
151 //eof