2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FLclGregorianCalendar.h
19 * @brief This is the header file for the %GregorianCalendar class.
21 * This header file contains the declarations of the %GregorianCalendar class.
23 #ifndef _FLCL_GREGORIAN_CALENDAR_H_
24 #define _FLCL_GREGORIAN_CALENDAR_H_
28 #include <FLclCalendar.h>
29 #include <FLclLocale.h>
30 #include <FLclTimeZone.h>
32 namespace Tizen { namespace Locales
36 * @class GregorianCalendar
37 * @brief This class is a concrete subclass of calendar and provides the Gregorian calendar that is the most commonly used
42 * @final This class is not intended for extension.
44 * The %GregorianCalendar class is a concrete subclass of calendar and provides the Gregorian calendar that is the most commonly used standard calendar.
46 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/locales/gregorian_calendar.htm">Gregorian Calendar</a>.
48 * @see Tizen::Locales::Locale
49 * @see Tizen::Locales::TimeZone
51 * The following example demonstrates how to use the %GregorianCalendar class.
56 using namespace Tizen::Base;
57 using namespace Tizen::Locales;
60 MyClass::GregorianCalendarExample(void)
62 Calendar* pGregorianCalendar = Calendar::CreateInstanceN(CALENDAR_GREGORIAN);
64 pGregorianCalendar->SetTimeField(TIME_FIELD_YEAR, 2005);
65 pGregorianCalendar->SetTimeField(TIME_FIELD_MONTH, JANUARY);
66 pGregorianCalendar->SetTimeField(TIME_FIELD_DAY_OF_MONTH, 3);
68 int dayOfWeek = pGregorianCalendar->GetTimeField(TIME_FIELD_DAY_OF_WEEK);// It is Monday.
70 pGregorianCalendar->Clear();
71 pGregorianCalendar->SetTimeField(TIME_FIELD_YEAR, 1972);
72 pGregorianCalendar->SetTimeField(TIME_FIELD_MONTH, OCTOBER);
73 pGregorianCalendar->SetTimeField(TIME_FIELD_DAY_OF_MONTH, 26); // It is Thursday
75 pGregorianCalendar->AddTimeField(TIME_FIELD_DAY_OF_MONTH, 1);
76 dayOfWeek = pGregorianCalendar->GetTimeField(TIME_FIELD_DAY_OF_WEEK); // It is Friday.
82 class _OSP_EXPORT_ GregorianCalendar
87 * This is the default constructor for this class. @n
88 * The object is not fully constructed after this constructor is called. For full construction, the Construct() method must
89 * be called right after calling this constructor.
93 GregorianCalendar(void);
97 * This is the destructor for this class. @n
98 * This destructor overrides Tizen::Locales::Calendar::~Calendar().
102 virtual ~GregorianCalendar(void);
106 * Initializes this instance of %GregorianCalendar with the current GMT time zone and the system locale. @n
107 * The time zone is used for the zone offset and the DST offset.
108 * An instance has weekdata which is the first of week, minimal days in the first week, weekend on set and weekend cease.
109 * The weekdata is set to default values by the locale.
113 * @return An error code
114 * @exception E_SUCCESS The method is successful.
115 * @exception E_OUT_OF_MEMORY The memory is insufficient.
117 result Construct(void);
121 * Initializes this instance of %GregorianCalendar with the specified parameter.
125 * @return An error code
126 * @param[in] gc An instance of %GregorianCalendar to copy
127 * @exception E_SUCCESS The method is successful.
128 * @exception E_OUT_OF_MEMORY The memory is insufficient.
130 result Construct(const GregorianCalendar& gc);
134 * Initializes this instance of %GregorianCalendar based on the current time in the specified time zone with the system locale. @n
135 * The @c timeZone is used for the zone offset and the DST offset.
136 * An instance has weekdata which is the first of week, minimal days in the first week, weekend on set, and weekend cease.
137 * The weekdata is set to default values by the locale.
141 * @return An error code
142 * @param[in] timeZone An instance of TimeZone
143 * @exception E_SUCCESS The method is successful.
144 * @exception E_OUT_OF_MEMORY The memory is insufficient.
146 result Construct(const TimeZone& timeZone);
150 * Initializes this instance of %GregorianCalendar based on the current time in the GMT time zone with the specified @c locale.
151 * The time zone is used for the zone offset and the DST offset.
152 * An instance has weekdata which is the first of week, minimal days in the first week, weekend on set, and weekend cease.
153 * The weekdata is set to default values by the specified @c locale.
156 * @brief <i> [Compatibility] </i>
160 * @compatibility This method has compatibility issues with OSP compatible applications. @n
161 * For more information, see @ref CompGregorianCalendarConstructPage "here".
164 * @return An error code
165 * @param[in] locale An instance of Locale
166 * @exception E_SUCCESS The method is successful.
167 * @exception E_OUT_OF_MEMORY The memory is insufficient.
168 * @exception E_INVALID_ARG The specified @c locale is invalid.
170 result Construct(const Locale& locale);
174 * Initializes this instance of %GregorianCalendar based on the current time in the specified time zone with the specified system locale. @n
175 * The @c timeZone is used for the zone offset and the DST offset.
176 * An instance has weekdata which is the first of week, minimal days in the first week, weekend on set, and weekend cease.
177 * The weekdata is set to default values by the specified @c locale.
180 * @brief <i> [Compatibility] </i>
184 * @compatibility This method has compatibility issues with OSP compatible applications. @n
185 * For more information, see @ref CompGregorianCalendarConstructPage "here".
188 * @return An error code
189 * @param[in] timeZone An instance of TimeZone
190 * @param[in] locale An instance of Locale
191 * @exception E_SUCCESS The method is successful.
192 * @exception E_OUT_OF_MEMORY The memory is insufficient.
193 * @exception E_INVALID_ARG The specified @c locale is invalid.
195 result Construct(const TimeZone& timeZone, const Locale& locale);
199 * @page CompGregorianCalendarConstructPage Compatibility for Construct()
200 * @section CompGregorianCalendarConstructIssueSection Issues
201 * Implementation of this method in OSP compatible applications has the following issue: @n
202 * -# The method returns @c E_UNSUPPORTED_OPERATION if the @c locale is invalid.
204 * @section CompGregorianCalendarConstructSolutionSection Resolutions
205 * This issue has been resolved in Tizen.
206 * @par When working in Tizen:
207 * -# The method returns @c E_INVALID_ARG if the @c locale is invalid.
212 * Initializes this instance of %GregorianCalendar based on the specified @c year, @c month, @c day, @c hour, @c minute, and @c second
213 * in the GMT time zone with the system locale. @n
214 * The time zone is used for the zone offset and the DST offset.
215 * An instance has weekdata which is the first of week, minimal days in the first week, weekend on set, and weekend cease.
216 * The weekdata is set to default values by the locale.
217 * The Gregorian calendar is lenient; time fields are normalized when the user calls the Calendar::GetTimeField() method.
221 * @return An error code
222 * @param[in] year The year value
223 * @param[in] month The month value @n
224 * The indexing is 1-based. Therefore, 1 means January.
225 * @param[in] day The date value
226 * @param[in] hour The hour value
227 * @param[in] minute The minute value
228 * @param[in] second The second value
229 * @exception E_SUCCESS The method is successful.
230 * @exception E_INVALID_ARG A specified argument is invalid.
231 * @exception E_OUT_OF_MEMORY The memory is insufficient.
232 * @exception E_OUT_OF_RANGE In this method, the time fields of this instance are calculated. @n
233 * If the value of the time fields go out of range, this exception is returned.
235 result Construct(int year, int month, int day, int hour = 0, int minute = 0, int second = 0);
239 * Initializes this instance of %GregorianCalendar based on the specified date and time in the GMT time zone with the system locale. @n
240 * The time zone is used for the zone offset and the DST offset.
241 * An instance has weekdata which is the first of week, minimal days in the first week, weekend on set, and weekend cease.
242 * The weekdata is set to default values by the locale.
246 * @return An error code
247 * @param[in] dateTime An instance of Tizen::Base::DateTime
248 * @exception E_SUCCESS The method is successful.
249 * @exception E_OUT_OF_MEMORY The memory is insufficient.
250 * @exception E_INVALID_ARG The specified @c dateTime is invalid.
251 * @exception E_OUT_OF_RANGE In this method, the time fields of this instance are calculated. @n
252 * If the value of the time fields go out of range, this exception is returned.
254 result Construct(const Tizen::Base::DateTime& dateTime);
258 * Adds the specified amount to the specified time field, based on the calendar rules. @n
259 * It is equivalent to calling Set(field, GetTimeField(field)+amount) with the following two adjustments: @n
261 * @b Add @b Rule1: The value of @c field after the call minus the value of @c field before the call is delta, modulo any
262 * overflow that has occurred in @c field. Overflow occurs when the @c field value exceeds its range and, as a result,
263 * the next larger field is incremented or decremented and the @c field value is adjusted back into its range. @n
265 * @b Add @b Rule2: If a smaller field is expected to be invariant, but it is impossible for it to be equal to its prior
266 * value because of changes in its minimum or maximum value after the @c field value is changed, then its value is adjusted
267 * to the closest possible value. @n
269 * A smaller field represents a smaller unit of time. The @c TIME_FIELD_HOUR field is smaller than the @c TIME_FIELD_DAY_OF_MONTH field.
270 * No adjustment is made to smaller fields that are not expected to be invariant.
271 * The calendar system determines what fields are expected to be invariant. @n
273 * In addition, this method forces re-computation of the calendar's milliseconds and all the time fields immediately.
275 * For example, consider a %GregorianCalendar set to Oct. 31. 2004. Calling AddTimeField(@c TIME_FIELD_MONTH, 13) sets the calendar to Nov. 30. 2005.
276 * The @c TIME_FIELD_MONTH field is set to @c TIME_FIELD_NOVEMBER by @b Rule1, since adding 13 months to October gives the month
277 * value as November of the next year. Since the @c TIME_FIELD_DAY_OF_MONTH cannot be 31 in November in a %GregorianCalendar,
278 * @c TIME_FIELD_DAY_OF_MONTH is set to 30 by @b Rule2.
282 * @return An error code
283 * @param[in] field The time field
284 * @param[in] amount The amount to add
285 * @exception E_SUCCESS The method is successful.
286 * @exception E_INVALID_ARG The specified @c field is invalid.
287 * @exception E_OUT_OF_RANGE In this method, the time fields of this instance are calculated. @n
288 * If the value of the time fields go out of range, this exception is returned.
289 * @remarks The @c TIME_FIELD_EXTENDED_YEAR and @c TIME_FIELD_YEAR_WOY fields cannot be added.
291 virtual result AddTimeField(TimeField field, int amount);
295 * Gets the actual minimum value that the specified time field can have, given the current date. @n
296 * For the Gregorian calendar, this is the same as Calendar::GetMinTimeField() and Calendar::GetGreatestMinTimeField().
300 * @return An integer value indicating the actual minimum value
301 * @param[in] field The time field
302 * @exception E_SUCCESS The method is successful.
303 * @exception E_INVALID_ARG The specified time field is invalid.
304 * @remarks The specific error code can be accessed using the GetLastResult() method.
306 virtual int GetActualMinTimeField(TimeField field) const;
310 * Gets the actual maximum value that the specified time field can have, given the current date. @n
311 * For example, if the current date is "Feb. 3. 2004", then the maximum value of the DATE field is 29.
315 * @return An integer value indicating the actual maximum value
316 * @param[in] field The time field
317 * @exception E_SUCCESS The method is successful.
318 * @exception E_INVALID_ARG The specified time field is invalid.
319 * @remarks The specific error code can be accessed using the GetLastResult() method.
321 virtual int GetActualMaxTimeField(TimeField field) const;
325 * Gets the Gregorian Calendar change date. @n
326 * This is the point when the switch from Julian dates to Gregorian dates occurred.
327 * Default is 00:00:00 local time, October 15, 1582. Previous to this, the time and date are Julian dates.
331 * @return The Gregorian cutover time for this calendar in milliseconds
333 long long GetGregorianChange(void) const;
337 * Gets the calendar type of the current instance.
341 * @return An instance of CalendarType representing the calendar type of the current instance of Calendar @n
342 * In this case, @c CALENDAR_GREGORIAN.
344 virtual CalendarType GetType(void) const;
347 * Checks whether the current date for the current instance is in Daylight Saving Time (DST).
351 * @return An error code
352 * @param[out] isInDst Set to @c true if the current date is in DST, @n
354 * @exception E_SUCCESS The method is successful.
355 * @exception E_OUT_OF_RANGE In this method, time fields of this instance are calculated. @n
356 * If the value of the time fields go out of range, this exception is returned.
357 * @exception E_INVALID_STATE In this method, time fields of this instance are calculated. @n
358 * If any time field value previously set is invalid, this exception is returned.
360 virtual result IsInDst(bool& isInDst) const;
364 * Checks whether the specified year is a leap year.
368 * @return @c true if the specified year is a leap year, @n
370 * @param[in] year The year to check
372 bool IsLeapYear(int year) const;
376 * Sets the Gregorian Calendar change date. @n
377 * This is the point when the switch from Julian dates to Gregorian dates occurred.
378 * Default is 00:00:00 local time, Oct 15, 1582. Previous to this, dates are in the Julian calendar.
380 * To obtain a pure Julian calendar, set @c change to @c MAX_MILLISECOND.
381 * To obtain a pure Gregorian calendar, set @c change to @c MIN_MILLISECOND.
385 * @return An error code
386 * @param[in] change The Gregorian cutover date
387 * @exception E_SUCCESS The method is successful.
388 * @exception E_OUT_OF_MEMORY The memory is insufficient.
390 result SetGregorianChange(long long change);
393 * Rolls up or down as per the specified amount in the specified field. @n
394 * This method overrides Calendar().Roll(TimeField, int). @n
395 * For more details, refer to Tizen::Locales::Calendar::Roll(TimeField, int).
399 * @return An error code
400 * @param[in] field The time field
401 * @param[in] amount The amount to roll up/down
402 * @exception E_SUCCESS The method is successful.
403 * @exception E_INVALID_STATE In this method, time fields of this instance are calculated. @n
404 * If any time field value previously set is invalid, this exception is returned.
405 * @exception E_INVALID_ARG The specified @c field is invalid (for example, @c TIME_FIELD_DST_OFFSET, @c TIME_FIELD_ZONE_OFFSET).
406 * @exception E_OUT_OF_RANGE In this method, time fields of this instance are calculated. @n
407 * If the value of the time fields go out of range, this exception is returned.
409 virtual result Roll(TimeField field, int amount);
413 * Gets the midnight, local time at or before the Gregorian cutover in milliseconds.
417 * @return The midnight, local time at or before the Gregorian cutover in milliseconds. @n
418 * If this method fails, @c -1 is returned. @n
419 * The specific error code can be accessed using the GetLastResult() method.
421 long long GetNormalizedGregorianCutover(void) const;
424 * Gets the year of the Gregorian cutover, with @c 0 representing 1 BC, @c -1 representing 2 BC, and so on.
428 * @return The year of the Gregorian cutover @n
429 * If this method fails, @c -1 is returned. @n
430 * The specific error code can be accessed using the GetLastResult() method.
432 int GetGregorianCutoverYear(void) const;
435 * Gets the Julian day number of the Gregorian cutover.
439 * @return The Julian day number of the Gregorian cutover
441 int GetCutoverJulianDay(void) const;
445 * Checks whether the current calendar is a Gregorian calendar or not.
449 * @return @c true if the current calendar is a Gregorian calendar, @n
452 bool IsGregorian() const;
456 * Gets the current time of the instance in milliseconds from epoch.
460 * @return An error code
461 * @param[out] millisec The current time in milliseconds from the epoch
462 * @exception E_SUCCESS The method is successful.
463 * @exception E_INVALID_STATE If any time field value previously set is invalid, this exception is returned.
464 * @exception E_OUT_OF_RANGE A time field or its value is out of range.
466 result GetTimeInMillisecFromEpoch(long long& millisec) const;
470 * Sets the current time of the instance with the specified milliseconds value.
474 * @return An error code
475 * @param[in] millisec The new time in milliseconds from the epoch
476 * @exception E_OUT_OF_RANGE A time field or its value is out of range.
478 result SetTimeInMillisecFromEpoch(long long millisec);
482 * Converts the 1970-epoch day number to the Gregorian year, month, date, day of week, and day of year.
486 * @param[in] day The 1970-epoch day number
487 * @param[out] year The year to convert
488 * @param[out] month The month to convert
489 * @param[out] dayOfMonth The date to convert
490 * @param[out] dayOfWeek The day of week to convert
491 * @param[out] dayOfYear The day of year to convert
493 static void DayToFields(int day, int& year, int& month, int& dayOfMonth, int& dayOfWeek, int& dayOfYear);
497 * @enum GregorianCalendarEras
499 * Defines Era for Gregorian calendar. The indexing is 0-based.
501 enum GregorianCalendarEras
503 GREGORIAN_CALENDAR_BC = 0, // BC
504 GREGORIAN_CALENDAR_AD = 1, // AD
506 GREGORIAN_CALENDAR_BCE = 0, // BCE
507 GREGORIAN_CALENDAR_CE = 1, // CE
511 * The implementation of this copy constructor is intentionally blank and declared as private to
512 * prohibit copying of objects.
514 GregorianCalendar(const GregorianCalendar& gregorianCalendar);
517 * The implementation of this copy assignment operator is intentionally blank and declared as private
518 * to prohibit copying of objects.
520 GregorianCalendar& operator =(const GregorianCalendar& gregorianCalendar);
522 // added for backward compatibility
523 virtual result RollWithSingleUnit(TimeField field, bool up);
524 virtual result ComputeTimeFields(void);
525 virtual result ComputeTime(void);
526 virtual int GetMonthLength(int extendedYear, int month) const;
527 virtual int HandleGetLimit(TimeField field, CalendarLimitType limitType) const;
528 virtual Calendar* CloneN(void) const;
532 * The length of months.
536 static const int MONTH_LENGTH[24];
539 * The days of a year.
543 static const int NUM_DAYS[24];
546 * The Gregorian cutover number for Julian day.
550 static const int CUTOVER_JULIAN_DAY = 2299161; // Oct 15, 1582
557 static const int EPOCH_YEAR = 1970;
560 * The epoch start for Julian day.
564 static const int EPOCH_START_AS_JULIAN_DAY = 2440588; // Jan 1, 1970(Gregorian)
567 * Jan 1, 1 epoch start for Julian day.
571 static const int JAN_1_1_JULIAN_DAY = 1721426; // Jan 1, 1 CE Gregorian
574 * Temporary value for the Gregorian cutover.
578 static const long long PAPAL_CUTOVER =
579 ((long long) (GregorianCalendar::CUTOVER_JULIAN_DAY -
580 GregorianCalendar::EPOCH_START_AS_JULIAN_DAY) * (long long) ONE_DAY_IN_MILLISEC);
583 * Maximum value for longlong.
587 static const long long MAX_LONGLONG = (2 ^ 63) - 1;
590 * Minimum value for longlong.
594 static const long long MIN_LONGLONG = -1 * (2 ^ 63);
597 * Maximum value for Julian day.
601 static const int MAX_JULIAN = 0x7F000000;
604 * Minimum value for Julian day.
608 static const int MIN_JULIAN = -0x7F000000;
611 * Maximum millisecond value for Julian day.
615 static const long long MAX_MILLISEC = ((GregorianCalendar::MAX_JULIAN - GregorianCalendar::EPOCH_START_AS_JULIAN_DAY) * (long long) ONE_DAY_IN_MILLISEC);
618 * Minimum millisecond value for Julian day.
622 static const long long MIN_MILLISEC = ((GregorianCalendar::MIN_JULIAN - GregorianCalendar::EPOCH_START_AS_JULIAN_DAY) * (long long) ONE_DAY_IN_MILLISEC);
625 * Epoch offset in millisecond value.
629 static const long long EPOCH_OFFSET_IN_MILLISEC = 62135596800000LL; // Jan 1, 1970
632 * Default Gregorian cutover.
636 static const long long DEFAULT_GREGORIAN_CUTOVER = -12219292800000LL;
639 long long __normalizedGregorianCutover; // = __gregorianCutover;
640 int __gregorianCutoverYear; // 1582
641 int __cutoverJulianDay;
643 friend class _GregorianCalendarImpl;
644 class _GregorianCalendarImpl* __pGregorianCalendarImpl;
646 }; // GregorianCalendar
650 #endif //_FLCL_GREGORIAN_CALENDAR_H_