4 * Copyright (C) 2005-2013 Team XBMC
7 * This Program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This Program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XBMC; see the file COPYING. If not, see
19 * <http://www.gnu.org/licenses/>.
23 #include "utils/IArchivable.h"
27 /*! \brief TIME_FORMAT enum/bitmask used for formatting time strings
28 Note the use of bitmasking, e.g.
29 TIME_FORMAT_HH_MM_SS = TIME_FORMAT_HH | TIME_FORMAT_MM | TIME_FORMAT_SS
30 \sa StringUtils::SecondsToTimeString
32 enum TIME_FORMAT { TIME_FORMAT_GUESS = 0,
35 TIME_FORMAT_MM_SS = 3,
37 TIME_FORMAT_HH_SS = 5, // not particularly useful
38 TIME_FORMAT_HH_MM = 6,
39 TIME_FORMAT_HH_MM_SS = 7,
40 TIME_FORMAT_XX = 8, // AM/PM
41 TIME_FORMAT_HH_MM_XX = 14,
42 TIME_FORMAT_HH_MM_SS_XX = 15,
44 TIME_FORMAT_H_MM_SS = 19,
45 TIME_FORMAT_H_MM_SS_XX = 27};
53 CDateTimeSpan(const CDateTimeSpan& span);
54 CDateTimeSpan(int day, int hour, int minute, int second);
56 bool operator >(const CDateTimeSpan& right) const;
57 bool operator >=(const CDateTimeSpan& right) const;
58 bool operator <(const CDateTimeSpan& right) const;
59 bool operator <=(const CDateTimeSpan& right) const;
60 bool operator ==(const CDateTimeSpan& right) const;
61 bool operator !=(const CDateTimeSpan& right) const;
63 CDateTimeSpan operator +(const CDateTimeSpan& right) const;
64 CDateTimeSpan operator -(const CDateTimeSpan& right) const;
66 const CDateTimeSpan& operator +=(const CDateTimeSpan& right);
67 const CDateTimeSpan& operator -=(const CDateTimeSpan& right);
69 void SetDateTimeSpan(int day, int hour, int minute, int second);
70 void SetFromPeriod(const std::string &period);
71 void SetFromTimeString(const std::string& time);
75 int GetMinutes() const;
76 int GetSeconds() const;
77 int GetSecondsTotal() const;
80 void ToULargeInt(ULARGE_INTEGER& time) const;
81 void FromULargeInt(const ULARGE_INTEGER& time);
86 friend class CDateTime;
89 /// \brief DateTime class, which uses FILETIME as it's base.
90 class CDateTime : public IArchivable
94 CDateTime(const CDateTime& time);
95 CDateTime(const SYSTEMTIME& time);
96 CDateTime(const FILETIME& time);
97 CDateTime(const time_t& time);
98 CDateTime(const tm& time);
99 CDateTime(int year, int month, int day, int hour, int minute, int second);
100 virtual ~CDateTime() {}
102 bool SetFromDateString(const std::string &date);
104 static CDateTime GetCurrentDateTime();
105 static CDateTime GetUTCDateTime();
106 static int MonthStringToMonthNum(const std::string& month);
108 const CDateTime& operator =(const SYSTEMTIME& right);
109 const CDateTime& operator =(const FILETIME& right);
110 const CDateTime& operator =(const time_t& right);
111 const CDateTime& operator =(const tm& right);
113 bool operator >(const CDateTime& right) const;
114 bool operator >=(const CDateTime& right) const;
115 bool operator <(const CDateTime& right) const;
116 bool operator <=(const CDateTime& right) const;
117 bool operator ==(const CDateTime& right) const;
118 bool operator !=(const CDateTime& right) const;
120 bool operator >(const FILETIME& right) const;
121 bool operator >=(const FILETIME& right) const;
122 bool operator <(const FILETIME& right) const;
123 bool operator <=(const FILETIME& right) const;
124 bool operator ==(const FILETIME& right) const;
125 bool operator !=(const FILETIME& right) const;
127 bool operator >(const SYSTEMTIME& right) const;
128 bool operator >=(const SYSTEMTIME& right) const;
129 bool operator <(const SYSTEMTIME& right) const;
130 bool operator <=(const SYSTEMTIME& right) const;
131 bool operator ==(const SYSTEMTIME& right) const;
132 bool operator !=(const SYSTEMTIME& right) const;
134 bool operator >(const time_t& right) const;
135 bool operator >=(const time_t& right) const;
136 bool operator <(const time_t& right) const;
137 bool operator <=(const time_t& right) const;
138 bool operator ==(const time_t& right) const;
139 bool operator !=(const time_t& right) const;
141 bool operator >(const tm& right) const;
142 bool operator >=(const tm& right) const;
143 bool operator <(const tm& right) const;
144 bool operator <=(const tm& right) const;
145 bool operator ==(const tm& right) const;
146 bool operator !=(const tm& right) const;
148 CDateTime operator +(const CDateTimeSpan& right) const;
149 CDateTime operator -(const CDateTimeSpan& right) const;
151 const CDateTime& operator +=(const CDateTimeSpan& right);
152 const CDateTime& operator -=(const CDateTimeSpan& right);
154 CDateTimeSpan operator -(const CDateTime& right) const;
156 operator FILETIME() const;
158 virtual void Archive(CArchive& ar);
163 int GetMonth() const;
166 int GetMinute() const;
167 int GetSecond() const;
168 int GetDayOfWeek() const;
169 int GetMinuteOfDay() const;
171 bool SetDateTime(int year, int month, int day, int hour, int minute, int second);
172 bool SetDate(int year, int month, int day);
173 bool SetTime(int hour, int minute, int second);
174 bool SetFromDBDate(const std::string &date);
175 bool SetFromDBTime(const std::string &time);
176 bool SetFromW3CDate(const std::string &date);
177 bool SetFromW3CDateTime(const std::string &date, bool ignoreTimezone = false);
178 bool SetFromUTCDateTime(const CDateTime &dateTime);
179 bool SetFromUTCDateTime(const time_t &dateTime);
180 bool SetFromRFC1123DateTime(const std::string &dateTime);
182 /*! \brief set from a database datetime format YYYY-MM-DD HH:MM:SS
183 \sa GetAsDBDateTime()
185 bool SetFromDBDateTime(const std::string &dateTime);
187 void GetAsSystemTime(SYSTEMTIME& time) const;
188 void GetAsTime(time_t& time) const;
189 void GetAsTm(tm& time) const;
190 void GetAsTimeStamp(FILETIME& time) const;
192 CDateTime GetAsUTCDateTime() const;
193 std::string GetAsSaveString() const;
194 std::string GetAsDBDateTime() const;
195 std::string GetAsDBDate() const;
196 std::string GetAsLocalizedDate(bool longDate=false, bool withShortNames=true) const;
197 std::string GetAsLocalizedDate(const std::string &strFormat, bool withShortNames=true) const;
198 std::string GetAsLocalizedTime(const std::string &format, bool withSeconds=true) const;
199 std::string GetAsLocalizedDateTime(bool longDate=false, bool withSeconds=true) const;
200 std::string GetAsRFC1123DateTime() const;
201 std::string GetAsW3CDate() const;
202 std::string GetAsW3CDateTime(bool asUtc = false) const;
204 void SetValid(bool yesNo);
205 bool IsValid() const;
207 static void ResetTimezoneBias(void);
208 static CDateTimeSpan GetTimezoneBias(void);
211 bool ToFileTime(const SYSTEMTIME& time, FILETIME& fileTime) const;
212 bool ToFileTime(const time_t& time, FILETIME& fileTime) const;
213 bool ToFileTime(const tm& time, FILETIME& fileTime) const;
215 void ToULargeInt(ULARGE_INTEGER& time) const;
216 void FromULargeInt(const ULARGE_INTEGER& time);