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 FBaseDateTime.h
19 * @brief This is the header file for the %DateTime class.
21 * This header file contains the declarations of the %DateTime class.
23 #ifndef _FBASE_DATE_TIME_H_
24 #define _FBASE_DATE_TIME_H_
26 #include <FBaseObject.h>
27 #include <FBaseTimeSpan.h>
28 #include <FBaseString.h>
29 #include <FBaseDouble.h>
31 namespace Tizen { namespace Base
35 * @brief This class represents the date and time as per the Gregorian calendar.
39 * The %DateTime class represents dates and times with values ranging from 12:00:00.000 midnight,
40 * January 1, 1 to 11:59:59.999 P.M., December 31, 9999 in the Gregorian calendar. It
41 * provides methods for conversion between the time formats.
43 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/datetime_timespan.htm">DateTime and TimeSpan</a>.
45 * The following example demonstrates how to use the %DateTime class.
51 * using namespace Tizen::Base;
53 * // This method sets both current local time and UTC time.
55 * MyClass::DateTimeSample(void)
58 * int year, month, day;
60 * dt.SetValue(2009, 10, 25);
67 * year = dt.GetYear(); // 2020
68 * month = dt.GetMonth(); // 1
69 * day = dt.GetDay(); // 30
73 class _OSP_EXPORT_ DateTime
78 * This is the default constructor for this class.
82 * @remarks The value of the instance is the same as the value of the instance returned by the GetMinValue() method.
87 * Copying of objects using this copy constructor is allowed.
91 * @param[in] value The %DateTime instance to copy
93 DateTime(const DateTime& value);
96 * This destructor overrides Tizen::Base::Object::~Object().
100 virtual ~DateTime(void);
103 * Sets the current instance of %DateTime to the value of the specified instance of TimeSpan since the minimum date (GetMinValue()).
107 * @param[in] value An instance of TimeSpan
108 * @exception E_SUCCESS The method is successful.
109 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
110 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
111 * is less than the value returned by GetMinValue().
113 result SetValue(const TimeSpan& value);
116 * Sets the current instance of %DateTime to the value of the specified instance of %DateTime.
120 * @param[in] value An instance of %DateTime
122 void SetValue(const DateTime& value);
125 * Sets the current instance of %DateTime to the specified @c year, @c month, @c day, @c hour, @c minute, and @c second.
129 * @return An error code
130 * @param[in] year The year to set
131 * @param[in] month The month to set
132 * @param[in] day The day to set
133 * @param[in] hour The hour to set
134 * @param[in] minute The minute set
135 * @param[in] second The second to set
136 * @exception E_SUCCESS The method is successful.
137 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
138 * Either the arguments are greater than the value returned by GetMaxValue() or
139 * are less than the value returned by GetMinValue(), or
140 * the arguments contain invalid values.
141 * For example, day is 31 when month is 2.
143 result SetValue(int year, int month, int day, int hour = 0, int minute = 0, int second = 0);
146 * Sets the current instance of %DateTime to the specified @c year, @c month, @c day, @c hour, @c minute, @c second, and @c millisecond.
150 * @return An error code
151 * @param[in] year The year to set
152 * @param[in] month The month to set
153 * @param[in] day The day to set
154 * @param[in] hour The hour to set
155 * @param[in] minute The minute set
156 * @param[in] second The second to set
157 * @param[in] millisecond The millisecond to set
158 * @exception E_SUCCESS The method is successful.
159 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
160 * Either the arguments are greater than the value returned by GetMaxValue() or
161 * are less than the value returned by GetMinValue(), or
162 * the arguments contain invalid values.
163 * For example, day is 31 when month is 2.
165 result SetValue(int year, int month, int day, int hour, int minute, int second, int millisecond);
168 * Sets the current instance of %DateTime with the specified number of %ticks.
169 * The tick value of type @c long @c long represents dates and times ranging from January 1, 1 A.D. 00:00:00.000 am.
173 * @return An error code
174 * @param[in] ticks The number of ticks
175 * @exception E_SUCCESS The method is successful.
176 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method.
177 * Either the arguments are greater than the value returned by GetMaxValue() or
178 * are less than the value returned by GetMinValue(), or
179 * the arguments contain invalid values.
180 * For example, day is 31 when month is 2.
182 result SetValue(long long ticks);
185 * Copying of objects using this copy assignment operator is allowed.
189 * @return A reference to the current object
190 * @param[in] rhs An instance of %DateTime
192 DateTime& operator =(const DateTime& rhs);
195 * Checks whether the current instance of %DateTime is equivalent to the specified instance of %DateTime.
199 * @return @c true if the current instance is equivalent to the specified instance, @n
201 * @param[in] rhs An instance of %DateTime
203 bool operator ==(const DateTime& rhs) const;
206 * Checks whether the current instance of %DateTime is not equivalent to the specified instance of %DateTime.
210 * @return @c true if the current instance is not equivalent to the specified instance, @n
212 * @param[in] rhs An instance of %DateTime
214 bool operator !=(const DateTime& rhs) const;
217 * Checks whether the value of the current instance of %DateTime is less than the value of the specified instance of %DateTime.
221 * @return @c true if the value of the current instance is less than the value of the specified instance, @n
223 * @param[in] rhs An instance of %DateTime
225 bool operator <(const DateTime& rhs) const;
228 * Checks whether the value of the current instance of %DateTime is greater than the value of the specified instance of %DateTime.
232 * @return @c true if the value of the current instance is greater than the value of the specified instance, @n
234 * @param[in] rhs An instance of %DateTime
236 bool operator >(const DateTime& rhs) const;
239 * Checks whether the value of the current instance of %DateTime is less than or equal to the value of the specified instance of %DateTime.
243 * @return @c true if the value of the current instance is less than or equal to the value of the specified instance, @n
245 * @param[in] rhs An instance of %DateTime
247 bool operator <=(const DateTime& rhs) const;
250 * Checks whether the value of the current instance of %DateTime is greater than or equal to the value of the specified instance of %DateTime.
254 * @return @c true if the value of the current instance is greater than or equal to the value of the specified instance, @n
256 * @param[in] rhs An instance of %DateTime
258 bool operator >=(const DateTime& rhs) const;
261 * Adds the specified time span to the instance of %DateTime.
265 * @return An error code
266 * @param[in] t The time span to add
267 * @exception E_SUCCESS The method is successful.
268 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
269 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
270 * is less than the value returned by GetMinValue().
272 result Add(const TimeSpan& t);
275 * Adds the specified number of days to the instance of %DateTime.
279 * @return An error code
280 * @param[in] days The number of days to add
281 * @exception E_SUCCESS The method is successful.
282 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
283 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
284 * is less than the value returned by GetMinValue().
286 result AddDays(int days);
289 * Adds the specified number of hours to the instance of %DateTime.
293 * @return An error code
294 * @param[in] hours The number of hours to add
295 * @exception E_SUCCESS The method is successful.
296 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
297 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
298 * is less than the value returned by GetMinValue().
300 result AddHours(int hours);
303 * Adds the specified number of minutes to the instance of %DateTime.
307 * @return An error code
308 * @param[in] minutes The number of minutes to add
309 * @exception E_SUCCESS The method is successful.
310 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
311 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
312 * is less than the value returned by GetMinValue().
314 result AddMinutes(int minutes);
317 * Adds the specified number of months to the instance of %DateTime.
321 * @return An error code
322 * @param[in] months The number of months to add
323 * @exception E_SUCCESS The method is successful.
324 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
325 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
326 * is less than the value returned by GetMinValue().
328 result AddMonths(int months);
331 * Adds the specified number of seconds to the instance of %DateTime.
335 * @return An error code
336 * @param[in] seconds The number of seconds to add
337 * @exception E_SUCCESS The method is successful.
338 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
339 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
340 * is less than the value returned by GetMinValue().
342 result AddSeconds(int seconds);
345 * Adds a specified number of milliseconds to the instance of %DateTime.
349 * @return An error code
350 * @param[in] milliseconds The number of milliseconds to add
351 * @exception E_SUCCESS The method is successful.
352 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
353 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
354 * is less than the value returned by GetMinValue().
356 result AddMilliseconds(long long milliseconds);
359 * Adds a specified number of ticks to the instance of %DateTime.
363 * @return An error code
364 * @param[in] ticks The number of ticks to add
365 * @exception E_SUCCESS The method is successful.
366 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
367 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
368 * is less than the value returned by GetMinValue().
370 result AddTicks(long long ticks);
373 * Adds the specified number of years to the instance of %DateTime.
377 * @return An error code
378 * @param[in] years The number of years to add
379 * @exception E_SUCCESS The method is successful.
380 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
381 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
382 * is less than the value returned by GetMinValue().
384 result AddYears(int years);
387 * Compares two specified instances of %DateTime. @n
388 * The two instances must be in the same time zone to make a meaningful comparison.
392 * @return A 32-bit @c signed integer value
394 * < 0 if the value of @c dt1 is less than the value of @c dt2
395 * == 0 if the value of @c dt1 is equal to the value of @c dt2
396 * > 0 if the value of @c dt1 is greater than the value of @c dt2
398 * @param[in] dt1 An instance of %DateTime
399 * @param[in] dt2 An instance of %DateTime
401 static int Compare(const DateTime& dt1, const DateTime& dt2);
404 * Compares the value of the current instance of %DateTime with the value of the specified instance of %DateTime.
408 * @return A 32-bit @c signed integer value
410 * < 0 if the value of the current instance is less than value of the specified instance
411 * == 0 if the value of the current instance is equal to value of the specified instance
412 * > 0 if the value of the current instance is greater than value of the specified instance
414 * @param[in] value An instance of %DateTime
416 int CompareTo(const DateTime& value) const;
419 * Checks whether the specified instance of Object is equivalent to the current instance of %DateTime.
423 * @return @c true if the specified instance of Object is equivalent to the current instance of %DateTime, @n
425 * @param[in] obj The object to compare with the current instance of %DateTime
426 * @see Tizen::Base::Object::GetHashCode()
428 virtual bool Equals(const Object& obj) const;
431 * Gets the hash value of the current instance of %DateTime. @n
432 * The hash value is calculated as (GetTime() ^ (GetTime() >> 16)).
436 * @return The hash value of the current instance of %DateTime
437 * @see Tizen::Base::Object::Equals()
439 virtual int GetHashCode(void) const;
442 * Gets the time span since the midnight of the date represented by the current instance of %DateTime.
446 * @return An instance of TimeSpan
448 TimeSpan GetTimeOfDay(void) const;
451 * Gets the number of @c days in the specified @c month of the specified @c year.
455 * @return An error code
456 * @param[in] year The year
457 * @param[in] month The month
458 * @param[out] days The number of days
459 * @exception E_SUCCESS The method is successful.
460 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
461 * @c year must be a value between @c 1 and @c 9999 and @c month must be a value between @c 1 and @c 12.
463 static result GetDaysInMonth(int year, int month, int& days);
466 * Subtracts the specified time span from the value of the current instance of %DateTime.
470 * @return An error code
471 * @param[in] t The time span to deduct
472 * @exception E_SUCCESS The method is successful.
473 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
474 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
475 * is less than the value returned by GetMinValue().
477 result Subtract(const TimeSpan& t);
480 * Represents the current instance of %DateTime as a string.
484 * @return A string containing Unicode representation of the value of the current instance of %DateTime
485 * @remarks The format of the String representation is "mm/dd/yyyy hh:mm:ss".
486 * @remarks Use the Tizen::Locale namespace for a string of the locale-specific representation.
489 String ToString(void) const;
492 * Parses the specified String representation of the date and time value.
496 * @return An error code
497 * @param[in] str A String representation of a date and time value
498 * @param[out] dt The result of the method
499 * @exception E_SUCCESS The method is successful.
500 * @exception E_INVALID_FORMAT The specified string is in an invalid format.
501 * @exception E_OUT_OF_RANGE The specified string contains an invalid value. @n
502 * 1) The resulting value of %DateTime is greater than the value returned by GetMaxValue()
503 * or is less than the value returned by GetMinValue(). @n
504 * 2) The specified string contains an invalid value.
505 * For example, day is 31 when the month is 2.
507 * - The format of the string is "mm/dd/yyyy hh:mm:ss".
508 * - This method guarantees that the original value of out-parameter is not changed when the method returns error.
510 static result Parse(const String& str, DateTime& dt);
513 * Gets the year of the current instance of %DateTime.
517 * @return An integer value indicating the year of the current instance of %DateTime
519 int GetYear(void) const;
522 * Gets the month of the current instance of %DateTime.
526 * @return An integer value indicating the month of the current instance of %DateTime
528 int GetMonth(void) const;
531 * Gets the day of the current instance of %DateTime.
535 * @return An integer value indicating the day of the current instance of %DateTime
537 int GetDay(void) const;
540 * Gets the hour of the current instance of %DateTime.
544 * @return An integer value indicating the hour of the current instance of %DateTime
546 int GetHour(void) const;
549 * Gets the minute of the current instance of %DateTime.
553 * @return An integer value indicating the minute of the current instance of %DateTime
555 int GetMinute(void) const;
558 * Gets the second of the current instance of %DateTime.
562 * @return An integer value indicating the second of the current instance of %DateTime
564 int GetSecond(void) const;
567 * Gets the millisecond of the current instance of %DateTime.
571 * @return An integer value indicating the millisecond of the current instance of %DateTime
573 int GetMillisecond(void) const;
576 * Gets the number of ticks in 1 second.
580 * @return The number of ticks in 1 second.
582 static int GetTicksPerSecond(void);
585 * Gets the tick of the current instance of %DateTime.
586 * The tick value of type @c long @c long represents dates and times ranging from January 1, 1 A.D. 00:00:00.000 am.
590 * @return A @c long @c long value indicating the tick of the current instance of %DateTime
592 long long GetTicks(void) const;
595 * Gets the number of milliseconds (in TimeSpan) since the minimum date (GetMinValue()).
599 * @return An instance of TimeSpan
600 * @remarks The returned instance is the time since the value returned by GetMinValue().
602 TimeSpan GetTime(void) const;
605 * Gets the maximum allowable value of %DateTime (that is, "December 31 9999 23:59:59.999").
609 * @return An instance of %DateTime
611 static const DateTime& GetMaxValue(void);
614 * Gets the minimum allowable value of %DateTime (that is, "January 1 1 00:00:00.000").
618 * @return An instance of %DateTime
620 static const DateTime& GetMinValue(void);
623 * Checks whether the year represented by the current instance of %DateTime is a leap year.
627 * @return @c true if the year represented by the current instance of %DateTime is a leap year, @n
630 bool IsLeapYear(void) const;
633 * Checks whether the specified @c year is a leap year.
637 * @return @c true if the specified year is a leap year, @n
639 * @param[in] year The year
641 static bool IsLeapYear(int year);
644 DateTime(int year, int month, int day, int hour, int minute, int second, int tick);
646 int CountLeapYear(int year) const
648 return static_cast< int >((year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400);
651 int CountDays(int year) const
653 return static_cast< int >((year - 1) * 365 + CountLeapYear(year));
656 int CountYears(int day) const
658 double year = (day - CountLeapYear(day / 365)) / 365.0;
662 if (year1.Equals(year2))
668 year = (year != static_cast< int >(year) ? 1 + static_cast< int >(year) : static_cast< int >(year));
669 int tempDays = day - CountDays(year);
671 // Check the boundary of days
672 if (IsLeapYear(year))
693 int year; /**<The year (1-9999)*/
694 int month; /**<The month (1-12)*/
695 int day; /**<The day (1-31)*/
696 int hour; /**<The hour (0-23)*/
697 int minute; /**<The minute (0-59)*/
698 int second; /**<The first 16 bits store tick (0-999) and the last 16 bits store second (0-59)*/
701 result ConvertTicksToDate(long long ticks, TmDateTime* pDateTime);
702 long long ConvertDateToTicks(const TmDateTime* pDateTime) const;
704 result ConvertSecondsToDate(long long seconds, TmDateTime* pDateTime);
705 long long ConvertDateToSeconds(const TmDateTime* pDateTime) const;
707 TmDateTime __dateTime;
709 friend class _DateTimeImpl;
710 class _DateTimeImpl* __pDateTimeImpl;
714 #endif // _FBASE_DATE_TIME_H_