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 standard calendar.
41 * @final This class is not intended for extension.
43 * The %GregorianCalendar class is a concrete subclass of calendar and provides the Gregorian calendar that is the most commonly used standard calendar.
45 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/locales/gregorian_calendar.htm">Gregorian Calendar</a>.
47 * @see Tizen::Locales::Locale
48 * @see Tizen::Locales::TimeZone
50 * The following example demonstrates how to use the %GregorianCalendar class.
55 using namespace Tizen::Base;
56 using namespace Tizen::Locales;
59 MyClass::GregorianCalendarExample(void)
61 Calendar* pGregorianCalendar = Calendar::CreateInstanceN(CALENDAR_GREGORIAN);
63 pGregorianCalendar->SetTimeField(TIME_FIELD_YEAR, 2005);
64 pGregorianCalendar->SetTimeField(TIME_FIELD_MONTH, JANUARY);
65 pGregorianCalendar->SetTimeField(TIME_FIELD_DAY_OF_MONTH, 3);
67 int dayOfWeek = pGregorianCalendar->GetTimeField(TIME_FIELD_DAY_OF_WEEK);// It is Monday.
69 pGregorianCalendar->Clear();
70 pGregorianCalendar->SetTimeField(TIME_FIELD_YEAR, 1972);
71 pGregorianCalendar->SetTimeField(TIME_FIELD_MONTH, OCTOBER);
72 pGregorianCalendar->SetTimeField(TIME_FIELD_DAY_OF_MONTH, 26); // It is Thursday
74 pGregorianCalendar->AddTimeField(TIME_FIELD_DAY_OF_MONTH, 1);
75 dayOfWeek = pGregorianCalendar->GetTimeField(TIME_FIELD_DAY_OF_WEEK); // It is Friday.
81 class _OSP_EXPORT_ GregorianCalendar
86 * This is the default constructor for this class. @n
87 * The object is not fully constructed after this constructor is called. For full construction, the Construct() method must 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.
107 * The time zone is used for the zone offset and the DST offset.
108 * An instance has weekdata which are the first of week, minimal days in the first week, weekend on set and weekend cease.
109 * The weekdata are set as 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 are the first of week, minimal days in the first week, weekend on set, and weekend cease.
137 * The weekdata are set as 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 are the first of week, minimal days in the first week, weekend on set, and weekend cease.
153 * The weekdata are set as 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 are the first of week, minimal days in the first week, weekend on set, and weekend cease.
177 * The weekdata are set as 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 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 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 are the first of week, minimal days in the first week, weekend on set, and weekend cease.
216 * The weekdata are set as 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 An integer value used to set the year time field
223 * @param[in] month An integer value used to set the month time field @n
224 * The indexing is 1-based. Therefore, 1 means January.
225 * @param[in] day An integer value used to set the date time field
226 * @param[in] hour An integer value used to set the hour time field
227 * @param[in] minute An integer value used to set the minute time field
228 * @param[in] second An integer value used to set the second time field
229 * @exception E_SUCCESS The method is successful.
230 * @exception E_INVALID_ARG An argument is invalid.
231 * @exception E_OUT_OF_MEMORY The memory is insufficient.
232 * @exception E_OUT_OF_RANGE In this method, time fields of this instance are calculated. @n
233 * If the value of the time fields goes 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 are the first of week, minimal days in the first week, weekend on set, and weekend cease.
242 * The weekdata are set as 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, time fields of this instance are calculated. @n
252 * If the value of the time fields goes 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 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 overflow that has occurred in @c field.
262 * Overflow occurs when the @c field value exceeds its range and, as a result, the next larger field is incremented or decremented and the @c field value is adjusted back into its range. @n
264 * @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 value because of
265 * changes in its minimum or maximum value after the @c field value is changed, then its value is adjusted to the closest possible value. @n
267 * A smaller field represents a smaller unit of time. The TIME_FIELD_HOUR field is smaller than the TIME_FIELD_DAY_OF_MONTH field.
268 * No adjustment is made to smaller fields that are not expected to be invariant.
269 * The calendar system determines what fields are expected to be invariant. @n
271 * In addition, this method forces re-computation of the calendar's milliseconds and all time fields immediately.
273 * For example, consider a %GregorianCalendar set to Oct. 31. 2004. Calling AddTimeField(TIME_FIELD_MONTH, 13) sets the calendar to Nov. 30. 2005.
274 * The TIME_FIELD_MONTH field is set to TIME_FIELD_NOVEMBER by @b Rule1, since adding 13 months to October gives November of the next year. Since
275 * the TIME_FIELD_DAY_OF_MONTH cannot be 31 in November in a %GregorianCalendar, TIME_FIELD_DAY_OF_MONTH is set to 30 by @b Rule2.
279 * @return An error code
280 * @param[in] field The time field
281 * @param[in] amount The amount to add
282 * @exception E_SUCCESS The method is successful.
283 * @exception E_INVALID_ARG The specified @c field is invalid.
284 * @exception E_OUT_OF_RANGE In this method, time fields of this instance are calculated. @n
285 * If the value of the time fields goes out of range, this exception is returned.
286 * @remarks The TIME_FIELD_EXTENDED_YEAR and TIME_FIELD_YEAR_WOY fields cannot be added.
288 virtual result AddTimeField(TimeField field, int amount);
292 * Gets the actual minimum value that the specified time field can have, given the current date. @n
293 * For the Gregorian calendar, this is the same as Calendar::GetMinTimeField() and Calendar::GetGreatestMinTimeField().
297 * @return An integer value indicating the actual minimum value
298 * @param[in] field The time field
299 * @exception E_SUCCESS The method is successful.
300 * @exception E_INVALID_ARG The specified time field is invalid.
301 * @remarks The specific error code can be accessed using the GetLastResult() method.
303 virtual int GetActualMinTimeField(TimeField field) const;
307 * Gets the actual maximum value that the specified time field can have, given the current date. @n
308 * For example, if the current date is "Feb. 3. 2004", then the maximum value of the DATE field is 29.
312 * @return An integer value indicating the actual maximum value
313 * @param[in] field The time field
314 * @exception E_SUCCESS The method is successful.
315 * @exception E_INVALID_ARG The specified time field is invalid.
317 * @remarks The specific error code can be accessed using the GetLastResult() method.
319 virtual int GetActualMaxTimeField(TimeField field) const;
323 * Gets the Gregorian Calendar change date. @n
324 * This is the point when the switch from Julian dates to Gregorian dates occurred.
325 * Default is 00:00:00 local time, October 15, 1582. Previous to this the time and date are Julian dates.
329 * @return The milliseconds which is the Gregorian cutover time for this calendar
331 long long GetGregorianChange(void) const;
335 * Gets the calendar type of the current instance.
339 * @return An instance of CalendarType representing the calendar type of the current instance of Calendar @n
340 * In this case, CALENDAR_GREGORIAN.
342 virtual CalendarType GetType(void) const;
345 * Checks whether the current date for the current instance is in Daylight Saving Time (DST).
349 * @return An error code
350 * @param[out] isInDst Set to @c true if the current date is in DST, @n
352 * @exception E_SUCCESS The method is successful.
353 * @exception E_OUT_OF_RANGE In this method, time fields of this instance are calculated. @n
354 * If the value of the time fields goes out of range, this exception is returned.
355 * @exception E_INVALID_STATE In this method, time fields of this instance are calculated. @n
356 * If any time field value previously set is invalid, this exception is returned.
358 virtual result IsInDst(bool& isInDst) const;
362 * Checks whether the specified year is a leap year.
366 * @return @c true if the specified year is a leap year, @n
368 * @param[in] year The year to check
370 bool IsLeapYear(int year) const;
374 * Sets the Gregorian Calendar change date. @n
375 * This is the point when the switch from Julian dates to Gregorian dates occurred.
376 * Default is 00:00:00 local time, Oct 15, 1582. Previous to this, dates are in the Julian calendar.
378 * To obtain a pure Julian calendar, set the change @c change to MAX_MILLISECOND.
379 * To obtain a pure Gregorian calendar, set the change @c change to MIN_MILLISECOND.
383 * @return An error code
384 * @param[in] change The given Gregorian cutover date
385 * @exception E_SUCCESS The method is successful.
386 * @exception E_OUT_OF_MEMORY The memory is insufficient.
388 result SetGregorianChange(long long change);
391 * Rolls up or down as per the specified amount in the specified field. @n
392 * This method overrides Calendar(). @n
393 * For more details, refer to Tizen::Locales::Calendar::Roll(TimeField, int).
397 * @return An error code
398 * @param[in] field The time field
399 * @param[in] amount The amount to roll up/down
400 * @exception E_SUCCESS The method is successful.
401 * @exception E_INVALID_STATE In this method, time fields of this instance are calculated. @n
402 * If any time field value previously set is invalid, this exception is returned.
403 * @exception E_INVALID_ARG The specified @c field is invalid (for example, TIME_FIELD_DST_OFFSET, TIME_FIELD_ZONE_OFFSET).
404 * @exception E_OUT_OF_RANGE In this method, time fields of this instance are calculated. @n
405 * If the value of the time fields goes out of range, this exception is returned.
407 virtual result Roll(TimeField field, int amount);
411 * Gets the millisecond which is midnight, local time at or before the Gregorian cutover.
415 * @return The millisecond which is midnight, local time at or before the Gregorian cutover
416 * If this method fails, @c -1 is returned.
417 * The specific error code can be accessed using the GetLastResult() method.
419 long long GetNormalizedGregorianCutover(void) const;
422 * Gets the year of the Gregorian cutover, with @c 0 representing 1 BC, @c -1 representing 2 BC, and so on.
426 * @return The year of the Gregorian cutover
427 * If this method fails, @c -1 is returned.
428 * The specific error code can be accessed using the GetLastResult() method.
430 int GetGregorianCutoverYear(void) const;
433 * Gets the Julian day number of the Gregorian cutover.
437 * @return The Julian day number of the Gregorian cutover
439 int GetCutoverJulianDay(void) const;
443 * Checks whether the current calendar is a Gregorian calendar or not.
447 * @return @c true if the current calendar is a Gregorian calendar, @n
450 bool IsGregorian() const;
454 * Gets the current time of the instance in milliseconds from epoch.
458 * @return An error code
459 * @param[out] millisec The current time in milliseconds from the epoch
460 * @exception E_SUCCESS The method is successful.
461 * @exception E_INVALID_STATE If any time field value previously set is invalid, this exception is returned.
462 * @exception E_OUT_OF_RANGE A time field or its value is out of range.
464 result GetTimeInMillisecFromEpoch(long long& millisec) const;
468 * Sets the current time of the instance with the specified milliseconds value.
472 * @return An error code
473 * @param[in] millisec The new time in milliseconds from the epoch
474 * @exception E_OUT_OF_RANGE A time field or its value is out of range.
476 result SetTimeInMillisecFromEpoch(long long millisec);
480 * Converts the 1970-epoch day number to the Gregorian year, month, date, day of week, and day of year.
484 * @param[in] day The 1970-epoch day number
485 * @param[out] year The year to convert
486 * @param[out] month The month to convert
487 * @param[out] dayOfMonth The date to convert
488 * @param[out] dayOfWeek The day of week to convert
489 * @param[out] dayOfYear The day of year to convert
491 static void DayToFields(int day, int& year, int& month, int& dayOfMonth, int& dayOfWeek, int& dayOfYear);
495 * @enum GregorianCalendarEras
497 * Defines Era for Gregorian calendar. The indexing is 0-based.
499 enum GregorianCalendarEras
501 GREGORIAN_CALENDAR_BC = 0, // BC
502 GREGORIAN_CALENDAR_AD = 1, // AD
504 GREGORIAN_CALENDAR_BCE = 0, // BCE
505 GREGORIAN_CALENDAR_CE = 1, // CE
509 * The implementation of this copy constructor is intentionally blank and declared as private to
510 * prohibit copying of objects.
512 GregorianCalendar(const GregorianCalendar& gregorianCalendar);
515 * The implementation of this copy assignment operator is intentionally blank and declared as private
516 * to prohibit copying of objects.
518 GregorianCalendar& operator =(const GregorianCalendar& gregorianCalendar);
520 // added for backward compatibility
521 virtual result RollWithSingleUnit(TimeField field, bool up);
522 virtual result ComputeTimeFields(void);
523 virtual result ComputeTime(void);
524 virtual int GetMonthLength(int extendedYear, int month) const;
525 virtual int HandleGetLimit(TimeField field, CalendarLimitType limitType) const;
526 virtual Calendar* CloneN(void) const;
530 * Defines the length of months.
534 static const int MONTH_LENGTH[24];
537 * Defines the days of a year.
541 static const int NUM_DAYS[24];
544 * The Gregorian cutover number for Julian day.
548 static const int CUTOVER_JULIAN_DAY = 2299161; // Oct 15, 1582
555 static const int EPOCH_YEAR = 1970;
558 * The epoch start for Julian day.
562 static const int EPOCH_START_AS_JULIAN_DAY = 2440588; // Jan 1, 1970(Gregorian)
565 * Jan 1, 1 epoch start for Julian day.
569 static const int JAN_1_1_JULIAN_DAY = 1721426; // Jan 1, 1 CE Gregorian
572 * Temporary value for the Gregorian cutover.
576 static const long long PAPAL_CUTOVER =
577 ((long long) (GregorianCalendar::CUTOVER_JULIAN_DAY -
578 GregorianCalendar::EPOCH_START_AS_JULIAN_DAY) * (long long) ONE_DAY_IN_MILLISEC);
581 * Maximum value for longlong.
585 static const long long MAX_LONGLONG = (2 ^ 63) - 1;
588 * Minimum value for longlong.
592 static const long long MIN_LONGLONG = -1 * (2 ^ 63);
595 * Maximum value for Julian day.
599 static const int MAX_JULIAN = 0x7F000000;
602 * Minimum value for Julian day.
606 static const int MIN_JULIAN = -0x7F000000;
609 * Maximum millisecond value for Julian day.
613 static const long long MAX_MILLISEC = ((GregorianCalendar::MAX_JULIAN - GregorianCalendar::EPOCH_START_AS_JULIAN_DAY) * (long long) ONE_DAY_IN_MILLISEC);
616 * Minimum millisecond value for Julian day.
620 static const long long MIN_MILLISEC = ((GregorianCalendar::MIN_JULIAN - GregorianCalendar::EPOCH_START_AS_JULIAN_DAY) * (long long) ONE_DAY_IN_MILLISEC);
623 * Epoch offset in millisecond value.
627 static const long long EPOCH_OFFSET_IN_MILLISEC = 62135596800000LL; // Jan 1, 1970
630 * Default Gregorian cutover.
634 static const long long DEFAULT_GREGORIAN_CUTOVER = -12219292800000LL;
637 long long __normalizedGregorianCutover; // = __gregorianCutover;
638 int __gregorianCutoverYear; // 1582
639 int __cutoverJulianDay;
641 friend class _GregorianCalendarImpl;
642 class _GregorianCalendarImpl* __pGregorianCalendarImpl;
644 }; // GregorianCalendar
648 #endif //_FLCL_GREGORIAN_CALENDAR_H_