2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FBaseDateTime.h
20 * @brief This is the header file for the %DateTime class.
22 * This header file contains the declarations of the %DateTime class.
24 #ifndef _FBASE_DATE_TIME_H_
25 #define _FBASE_DATE_TIME_H_
27 #include <FBaseObject.h>
28 #include <FBaseTimeSpan.h>
29 #include <FBaseString.h>
30 #include <FBaseDouble.h>
33 namespace Tizen { namespace Base
37 * @brief This class represents the date and time as per the Gregorian calendar.
41 * The %DateTime class represents dates and times with values ranging from 12:00:00.000 midnight,
42 * January 1, 1 to 11:59:59.999 P.M., December 31, 9999 in the Gregorian calendar. It
43 * provides methods for conversion between the time formats.
45 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/datetime_timespan.htm">DateTime and TimeSpan</a>.
47 * The following example demonstrates how to use the %DateTime class.
53 * using namespace Tizen::Base;
55 * // This method sets both current local time and UTC time.
57 * MyClass::DateTimeSample(void)
60 * int year, month, day;
62 * dt.SetValue(2009, 10, 25);
69 * year = dt.GetYear(); // 2020
70 * month = dt.GetMonth(); // 1
71 * day = dt.GetDay(); // 30
75 class _OSP_EXPORT_ DateTime
80 * This is the default constructor for this class.
84 * @remarks The value of the instance is the same as the value of the instance returned by the GetMinValue() method.
89 * Copying of objects using this copy constructor is allowed.
93 * @param[in] value The %DateTime instance to copy
95 DateTime(const DateTime& value);
98 * This destructor overrides Tizen::Base::Object::~Object().
102 virtual ~DateTime(void);
105 * Sets the current instance of %DateTime to the value of the specified instance of TimeSpan since the minimum date (GetMinValue()).
109 * @param[in] value An instance of TimeSpan
110 * @exception E_SUCCESS The method is successful.
111 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
112 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
113 * is less than the value returned by GetMinValue().
115 result SetValue(const TimeSpan& value);
118 * Sets the current instance of %DateTime to the value of the specified instance of %DateTime.
122 * @param[in] value An instance of %DateTime
124 void SetValue(const DateTime& value);
127 * Sets the current instance of %DateTime to the specified @c year, @c month, @c day, @c hour, @c minute, and @c second.
131 * @return An error code
132 * @param[in] year The year to set
133 * @param[in] month The month to set
134 * @param[in] day The day to set
135 * @param[in] hour The hour to set
136 * @param[in] minute The minute set
137 * @param[in] second The second to set
138 * @exception E_SUCCESS The method is successful.
139 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
140 * Either the arguments are greater than the value returned by GetMaxValue() or
141 * are less than the value returned by GetMinValue(), or
142 * the arguments contain invalid values.
143 * For example, day is 31 when month is 2.
145 result SetValue(int year, int month, int day, int hour = 0, int minute = 0, int second = 0);
148 * Sets the current instance of %DateTime to the specified @c year, @c month, @c day, @c hour, @c minute, @c second, and @c millisecond.
152 * @return An error code
153 * @param[in] year The year to set
154 * @param[in] month The month to set
155 * @param[in] day The day to set
156 * @param[in] hour The hour to set
157 * @param[in] minute The minute set
158 * @param[in] second The second to set
159 * @param[in] millisecond The millisecond to set
160 * @exception E_SUCCESS The method is successful.
161 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
162 * Either the arguments are greater than the value returned by GetMaxValue() or
163 * are less than the value returned by GetMinValue(), or
164 * the arguments contain invalid values.
165 * For example, day is 31 when month is 2.
167 result SetValue(int year, int month, int day, int hour, int minute, int second, int millisecond);
170 * Sets the current instance of %DateTime with the specified number of %ticks.
171 * 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.
175 * @return An error code
176 * @param[in] ticks The number of ticks
177 * @exception E_SUCCESS The method is successful.
178 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method.
179 * Either the arguments are greater than the value returned by GetMaxValue() or
180 * are less than the value returned by GetMinValue(), or
181 * the arguments contain invalid values.
182 * For example, day is 31 when month is 2.
184 result SetValue(long long ticks);
187 * Copying of objects using this copy assignment operator is allowed.
191 * @return A reference to the current object
192 * @param[in] rhs An instance of %DateTime
194 DateTime& operator =(const DateTime& rhs);
197 * Checks whether the current instance of %DateTime is equivalent to the specified instance of %DateTime.
201 * @return @c true if the current instance is equivalent to the specified instance, @n
203 * @param[in] rhs An instance of %DateTime
205 bool operator ==(const DateTime& rhs) const;
208 * Checks whether the current instance of %DateTime is not equivalent to the specified instance of %DateTime.
212 * @return @c true if the current instance is not equivalent to the specified instance, @n
214 * @param[in] rhs An instance of %DateTime
216 bool operator !=(const DateTime& rhs) const;
219 * Checks whether the value of the current instance of %DateTime is less than the value of the specified instance of %DateTime.
223 * @return @c true if the value of the current instance is less than the value of the specified instance, @n
225 * @param[in] rhs An instance of %DateTime
227 bool operator <(const DateTime& rhs) const;
230 * Checks whether the value of the current instance of %DateTime is greater than the value of the specified instance of %DateTime.
234 * @return @c true if the value of the current instance is greater than the value of the specified instance, @n
236 * @param[in] rhs An instance of %DateTime
238 bool operator >(const DateTime& rhs) const;
241 * Checks whether the value of the current instance of %DateTime is less than or equal to the value of the specified instance of %DateTime.
245 * @return @c true if the value of the current instance is less than or equal to the value of the specified instance, @n
247 * @param[in] rhs An instance of %DateTime
249 bool operator <=(const DateTime& rhs) const;
252 * Checks whether the value of the current instance of %DateTime is greater than or equal to the value of the specified instance of %DateTime.
256 * @return @c true if the value of the current instance is greater than or equal to the value of the specified instance, @n
258 * @param[in] rhs An instance of %DateTime
260 bool operator >=(const DateTime& rhs) const;
263 * Adds the specified time span to the instance of %DateTime.
267 * @return An error code
268 * @param[in] t The time span to add
269 * @exception E_SUCCESS The method is successful.
270 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
271 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
272 * is less than the value returned by GetMinValue().
274 result Add(const TimeSpan& t);
277 * Adds the specified number of days to the instance of %DateTime.
281 * @return An error code
282 * @param[in] days The number of days to add
283 * @exception E_SUCCESS The method is successful.
284 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
285 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
286 * is less than the value returned by GetMinValue().
288 result AddDays(int days);
291 * Adds the specified number of hours to the instance of %DateTime.
295 * @return An error code
296 * @param[in] hours The number of hours to add
297 * @exception E_SUCCESS The method is successful.
298 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
299 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
300 * is less than the value returned by GetMinValue().
302 result AddHours(int hours);
305 * Adds the specified number of minutes to the instance of %DateTime.
309 * @return An error code
310 * @param[in] minutes The number of minutes to add
311 * @exception E_SUCCESS The method is successful.
312 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
313 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
314 * is less than the value returned by GetMinValue().
316 result AddMinutes(int minutes);
319 * Adds the specified number of months to the instance of %DateTime.
323 * @return An error code
324 * @param[in] months The number of months to add
325 * @exception E_SUCCESS The method is successful.
326 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
327 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
328 * is less than the value returned by GetMinValue().
330 result AddMonths(int months);
333 * Adds the specified number of seconds to the instance of %DateTime.
337 * @return An error code
338 * @param[in] seconds The number of seconds to add
339 * @exception E_SUCCESS The method is successful.
340 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
341 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
342 * is less than the value returned by GetMinValue().
344 result AddSeconds(int seconds);
347 * Adds a specified number of milliseconds to the instance of %DateTime.
351 * @return An error code
352 * @param[in] milliseconds The number of milliseconds to add
353 * @exception E_SUCCESS The method is successful.
354 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
355 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
356 * is less than the value returned by GetMinValue().
358 result AddMilliseconds(long long milliseconds);
361 * Adds a specified number of ticks to the instance of %DateTime.
365 * @return An error code
366 * @param[in] ticks The number of ticks to add
367 * @exception E_SUCCESS The method is successful.
368 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
369 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
370 * is less than the value returned by GetMinValue().
372 result AddTicks(long long ticks);
375 * Adds the specified number of years to the instance of %DateTime.
379 * @return An error code
380 * @param[in] years The number of years to add
381 * @exception E_SUCCESS The method is successful.
382 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
383 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
384 * is less than the value returned by GetMinValue().
386 result AddYears(int years);
389 * Compares two specified instances of %DateTime. @n
390 * The two instances must be in the same time zone to make a meaningful comparison.
394 * @return A 32-bit @c signed integer value
396 * < 0 if the value of @c dt1 is less than the value of @c dt2
397 * == 0 if the value of @c dt1 is equal to the value of @c dt2
398 * > 0 if the value of @c dt1 is greater than the value of @c dt2
400 * @param[in] dt1 An instance of %DateTime
401 * @param[in] dt2 An instance of %DateTime
403 static int Compare(const DateTime& dt1, const DateTime& dt2);
406 * Compares the value of the current instance of %DateTime with the value of the specified instance of %DateTime.
410 * @return A 32-bit @c signed integer value
412 * < 0 if the value of the current instance is less than value of the specified instance
413 * == 0 if the value of the current instance is equal to value of the specified instance
414 * > 0 if the value of the current instance is greater than value of the specified instance
416 * @param[in] value An instance of %DateTime
418 int CompareTo(const DateTime& value) const;
421 * Checks whether the specified instance of Object is equivalent to the current instance of %DateTime.
425 * @return @c true if the specified instance of Object is equivalent to the current instance of %DateTime, @n
427 * @param[in] obj The object to compare with the current instance of %DateTime
428 * @see Tizen::Base::Object::GetHashCode()
430 virtual bool Equals(const Object& obj) const;
433 * Gets the hash value of the current instance of %DateTime. @n
434 * The hash value is calculated as (GetTime() ^ (GetTime() >> 16)).
438 * @return The hash value of the current instance of %DateTime
439 * @see Tizen::Base::Object::Equals()
441 virtual int GetHashCode(void) const;
444 * Gets the time span since the midnight of the date represented by the current instance of %DateTime.
448 * @return An instance of TimeSpan
450 TimeSpan GetTimeOfDay(void) const;
453 * Gets the number of @c days in the specified @c month of the specified @c year.
457 * @return An error code
458 * @param[in] year The year
459 * @param[in] month The month
460 * @param[out] days The number of days
461 * @exception E_SUCCESS The method is successful.
462 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
463 * @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.
465 static result GetDaysInMonth(int year, int month, int& days);
468 * Subtracts the specified time span from the value of the current instance of %DateTime.
472 * @return An error code
473 * @param[in] t The time span to deduct
474 * @exception E_SUCCESS The method is successful.
475 * @exception E_OUT_OF_RANGE The value of the argument is outside the valid range defined by the method. @n
476 * The resulting value of %DateTime is greater than the value returned by GetMaxValue() or @n
477 * is less than the value returned by GetMinValue().
479 result Subtract(const TimeSpan& t);
482 * Represents the current instance of %DateTime as a string.
486 * @return A string containing Unicode representation of the value of the current instance of %DateTime
487 * @remarks The format of the String representation is "mm/dd/yyyy hh:mm:ss".
488 * @remarks Use the Tizen::Locale namespace for a string of the locale-specific representation.
491 String ToString(void) const;
494 * Parses the specified String representation of the date and time value.
498 * @return An error code
499 * @param[in] str A String representation of a date and time value
500 * @param[out] dt The result of the method
501 * @exception E_SUCCESS The method is successful.
502 * @exception E_INVALID_FORMAT The specified string is in an invalid format.
503 * @exception E_OUT_OF_RANGE The specified string contains an invalid value. @n
504 * 1) The resulting value of %DateTime is greater than the value returned by GetMaxValue()
505 * or is less than the value returned by GetMinValue(). @n
506 * 2) The specified string contains an invalid value.
507 * For example, day is 31 when the month is 2.
509 * - The format of the string is "mm/dd/yyyy hh:mm:ss".
510 * - This method guarantees that the original value of out-parameter is not changed when the method returns error.
512 static result Parse(const String& str, DateTime& dt);
515 * Gets the year of the current instance of %DateTime.
519 * @return An integer value indicating the year of the current instance of %DateTime
521 int GetYear(void) const;
524 * Gets the month of the current instance of %DateTime.
528 * @return An integer value indicating the month of the current instance of %DateTime
530 int GetMonth(void) const;
533 * Gets the day of the current instance of %DateTime.
537 * @return An integer value indicating the day of the current instance of %DateTime
539 int GetDay(void) const;
542 * Gets the hour of the current instance of %DateTime.
546 * @return An integer value indicating the hour of the current instance of %DateTime
548 int GetHour(void) const;
551 * Gets the minute of the current instance of %DateTime.
555 * @return An integer value indicating the minute of the current instance of %DateTime
557 int GetMinute(void) const;
560 * Gets the second of the current instance of %DateTime.
564 * @return An integer value indicating the second of the current instance of %DateTime
566 int GetSecond(void) const;
569 * Gets the millisecond of the current instance of %DateTime.
573 * @return An integer value indicating the millisecond of the current instance of %DateTime
575 int GetMillisecond(void) const;
578 * Gets the number of ticks in 1 second.
582 * @return The number of ticks in 1 second.
584 static int GetTicksPerSecond(void);
587 * Gets the tick of the current instance of %DateTime.
588 * 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.
592 * @return A @c long @c long value indicating the tick of the current instance of %DateTime
594 long long GetTicks(void) const;
597 * Gets the number of milliseconds (in TimeSpan) since the minimum date (GetMinValue()).
601 * @return An instance of TimeSpan
602 * @remarks The returned instance is the time since the value returned by GetMinValue().
604 TimeSpan GetTime(void) const;
607 * Gets the maximum allowable value of %DateTime (that is, "December 31 9999 23:59:59.999").
611 * @return An instance of %DateTime
613 static const DateTime& GetMaxValue(void);
616 * Gets the minimum allowable value of %DateTime (that is, "January 1 1 00:00:00.000").
620 * @return An instance of %DateTime
622 static const DateTime& GetMinValue(void);
625 * Checks whether the year represented by the current instance of %DateTime is a leap year.
629 * @return @c true if the year represented by the current instance of %DateTime is a leap year, @n
632 bool IsLeapYear(void) const;
635 * Checks whether the specified @c year is a leap year.
639 * @return @c true if the specified year is a leap year, @n
641 * @param[in] year The year
643 static bool IsLeapYear(int year);
646 DateTime(int year, int month, int day, int hour, int minute, int second, int tick);
648 int CountLeapYear(int year) const
650 return static_cast< int >((year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400);
654 int CountDays(int year) const
656 return static_cast< int >((year - 1) * 365 + CountLeapYear(year));
659 int CountYears(int day) const
661 double year = (day - CountLeapYear(day / 365)) / 365.0;
665 if (year1.Equals(year2))
671 year = (year != static_cast< int >(year) ? 1 + static_cast< int >(year) : static_cast< int >(year));
672 int tempDays = day - CountDays(year);
674 // Check the boundary of days
675 if (IsLeapYear(year))
696 int year; /**<The year (1-9999)*/
697 int month; /**<The month (1-12)*/
698 int day; /**<The day (1-31)*/
699 int hour; /**<The hour (0-23)*/
700 int minute; /**<The minute (0-59)*/
701 int second; /**<The first 16 bits store tick (0-999) and the last 16 bits store second (0-59)*/
704 result ConvertTicksToDate(long long ticks, TmDateTime* pDateTime);
705 long long ConvertDateToTicks(const TmDateTime* pDateTime) const;
707 result ConvertSecondsToDate(long long seconds, TmDateTime* pDateTime);
708 long long ConvertDateToSeconds(const TmDateTime* pDateTime) const;
710 TmDateTime __dateTime;
712 friend class _DateTimeImpl;
713 class _DateTimeImpl * __pDateTimeImpl;
719 #endif // _FBASE_DATE_TIME_H_