2 *******************************************************************************
3 * Copyright (C) 2003 - 2009, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
13 #include "gregoimp.h" //Math
17 static const int32_t LIMITS
[UCAL_FIELD_COUNT
][4] = {
18 // Minimum Greatest Least Maximum
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
45 //-------------------------------------------------------------------------
47 //-------------------------------------------------------------------------
49 CECalendar::CECalendar(const Locale
& aLocale
, UErrorCode
& success
)
50 : Calendar(TimeZone::createDefault(), aLocale
, success
)
52 setTimeInMillis(getNow(), success
);
55 CECalendar::CECalendar (const CECalendar
& other
)
60 CECalendar::~CECalendar()
65 CECalendar::operator=(const CECalendar
& right
)
67 Calendar::operator=(right
);
71 //-------------------------------------------------------------------------
73 //-------------------------------------------------------------------------
76 CECalendar::handleComputeMonthStart(int32_t eyear
,int32_t emonth
, UBool
/*useMonth*/) const
78 return ceToJD(eyear
, emonth
, 0, getJDEpochOffset());
82 CECalendar::handleGetLimit(UCalendarDateFields field
, ELimitType limitType
) const
84 return LIMITS
[field
][limitType
];
88 CECalendar::inDaylightTime(UErrorCode
& status
) const
90 if (U_FAILURE(status
) || !getTimeZone().useDaylightTime()) {
94 // Force an update of the state of the Calendar.
95 ((CECalendar
*)this)->complete(status
); // cast away const
97 return (UBool
)(U_SUCCESS(status
) ? (internalGet(UCAL_DST_OFFSET
) != 0) : FALSE
);
101 CECalendar::haveDefaultCentury() const
106 //-------------------------------------------------------------------------
107 // Calendar system Conversion methods...
108 //-------------------------------------------------------------------------
110 CECalendar::ceToJD(int32_t year
, int32_t month
, int32_t date
, int32_t jdEpochOffset
)
112 // handle month > 12, < 0 (e.g. from add/set)
118 year
+= month
/13 - 1;
119 month
= month%13
+ 12;
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)
131 CECalendar::jdToCE(int32_t julianDay
, int32_t jdEpochOffset
, int32_t& year
, int32_t& month
, int32_t& day
)
133 int32_t c4
; // number of 4 year cycle (1461 days)
134 int32_t r4
; // remainder of 4 year cycle, always positive
136 c4
= ClockMath::floorDivide(julianDay
- jdEpochOffset
, 1461, r4
);
138 year
= 4 * c4
+ (r4
/365 - r4
/1460); // 4 * <number of 4year cycle> + <years within the last cycle>
140 int32_t doy
= (r4
== 1460) ? 365 : (r4
% 365); // days in present year
142 month
= doy
/ 30; // 30 -> Coptic/Ethiopic month length up to 12th month
143 day
= (doy
% 30) + 1; // 1-based days in a month
148 #endif /* #if !UCONFIG_NO_FORMATTING */