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