Merge pull request #2435 from dezi/master
[vuplus_xbmc] / xbmc / XBDateTime.h
1 #pragma once
2
3 /*
4  *      Copyright (C) 2005-2013 Team XBMC
5  *      http://www.xbmc.org
6  *
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)
10  *  any later version.
11  *
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.
16  *
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/>.
20  *
21  */
22
23 #include "utils/Archive.h"
24
25 /*! \brief TIME_FORMAT enum/bitmask used for formatting time strings
26  Note the use of bitmasking, e.g.
27   TIME_FORMAT_HH_MM_SS = TIME_FORMAT_HH | TIME_FORMAT_MM | TIME_FORMAT_SS
28  \sa StringUtils::SecondsToTimeString
29  */
30 enum TIME_FORMAT { TIME_FORMAT_GUESS       =  0,
31                    TIME_FORMAT_SS          =  1,
32                    TIME_FORMAT_MM          =  2,
33                    TIME_FORMAT_MM_SS       =  3,
34                    TIME_FORMAT_HH          =  4,
35                    TIME_FORMAT_HH_SS       =  5, // not particularly useful
36                    TIME_FORMAT_HH_MM       =  6,
37                    TIME_FORMAT_HH_MM_SS    =  7,
38                    TIME_FORMAT_XX          =  8, // AM/PM
39                    TIME_FORMAT_HH_MM_XX    = 14,
40                    TIME_FORMAT_HH_MM_SS_XX = 15,
41                    TIME_FORMAT_H           = 16,
42                    TIME_FORMAT_H_MM_SS     = 19,
43                    TIME_FORMAT_H_MM_SS_XX  = 27};
44
45 class CDateTime;
46
47 class CDateTimeSpan
48 {
49 public:
50   CDateTimeSpan();
51   CDateTimeSpan(const CDateTimeSpan& span);
52   CDateTimeSpan(int day, int hour, int minute, int second);
53
54   bool operator >(const CDateTimeSpan& right) const;
55   bool operator >=(const CDateTimeSpan& right) const;
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
61   CDateTimeSpan operator +(const CDateTimeSpan& right) const;
62   CDateTimeSpan operator -(const CDateTimeSpan& right) const;
63
64   const CDateTimeSpan& operator +=(const CDateTimeSpan& right);
65   const CDateTimeSpan& operator -=(const CDateTimeSpan& right);
66
67   void SetDateTimeSpan(int day, int hour, int minute, int second);
68   void SetFromPeriod(const CStdString &period);
69   void SetFromTimeString(const CStdString& time);
70
71   int GetDays() const;
72   int GetHours() const;
73   int GetMinutes() const;
74   int GetSeconds() const;
75
76 private:
77   void ToULargeInt(ULARGE_INTEGER& time) const;
78   void FromULargeInt(const ULARGE_INTEGER& time);
79
80 private:
81   FILETIME m_timeSpan;
82
83   friend class CDateTime;
84 };
85
86 /// \brief DateTime class, which uses FILETIME as it's base.
87 class CDateTime : public IArchivable
88 {
89 public:
90   CDateTime();
91   CDateTime(const CDateTime& time);
92   CDateTime(const SYSTEMTIME& time);
93   CDateTime(const FILETIME& time);
94   CDateTime(const time_t& time);
95   CDateTime(const tm& time);
96   CDateTime(int year, int month, int day, int hour, int minute, int second);
97   virtual ~CDateTime() {}
98
99   void SetFromDateString(const CStdString &date);
100
101   static CDateTime GetCurrentDateTime();
102   static CDateTime GetUTCDateTime();
103   static int MonthStringToMonthNum(const CStdString& month);
104
105   const CDateTime& operator =(const SYSTEMTIME& right);
106   const CDateTime& operator =(const FILETIME& right);
107   const CDateTime& operator =(const time_t& right);
108   const CDateTime& operator =(const tm& right);
109
110   bool operator >(const CDateTime& right) const;
111   bool operator >=(const CDateTime& right) const;
112   bool operator <(const CDateTime& right) const;
113   bool operator <=(const CDateTime& right) const;
114   bool operator ==(const CDateTime& right) const;
115   bool operator !=(const CDateTime& right) const;
116
117   bool operator >(const FILETIME& right) const;
118   bool operator >=(const FILETIME& right) const;
119   bool operator <(const FILETIME& right) const;
120   bool operator <=(const FILETIME& right) const;
121   bool operator ==(const FILETIME& right) const;
122   bool operator !=(const FILETIME& right) const;
123
124   bool operator >(const SYSTEMTIME& right) const;
125   bool operator >=(const SYSTEMTIME& right) const;
126   bool operator <(const SYSTEMTIME& right) const;
127   bool operator <=(const SYSTEMTIME& right) const;
128   bool operator ==(const SYSTEMTIME& right) const;
129   bool operator !=(const SYSTEMTIME& right) const;
130
131   bool operator >(const time_t& right) const;
132   bool operator >=(const time_t& right) const;
133   bool operator <(const time_t& 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
138   bool operator >(const tm& right) const;
139   bool operator >=(const tm& right) const;
140   bool operator <(const tm& right) const;
141   bool operator <=(const tm& right) const;
142   bool operator ==(const tm& right) const;
143   bool operator !=(const tm& right) const;
144
145   CDateTime operator +(const CDateTimeSpan& right) const;
146   CDateTime operator -(const CDateTimeSpan& right) const;
147
148   const CDateTime& operator +=(const CDateTimeSpan& right);
149   const CDateTime& operator -=(const CDateTimeSpan& right);
150
151   CDateTimeSpan operator -(const CDateTime& right) const;
152
153   operator FILETIME() const;
154
155   virtual void Archive(CArchive& ar);
156
157   void Reset();
158
159   int GetDay() const;
160   int GetMonth() const;
161   int GetYear() const;
162   int GetHour() const;
163   int GetMinute() const;
164   int GetSecond() const;
165   int GetDayOfWeek() const;
166   int GetMinuteOfDay() const;
167
168   void SetDateTime(int year, int month, int day, int hour, int minute, int second);
169   void SetDate(int year, int month, int day);
170   void SetTime(int hour, int minute, int second);
171   void SetFromDBDate(const CStdString &date);
172   void SetFromDBTime(const CStdString &time);
173   void SetFromW3CDate(const CStdString &date);
174   void SetFromUTCDateTime(const CDateTime &dateTime);
175   void SetFromUTCDateTime(const time_t &dateTime);
176   void SetFromRFC1123DateTime(const CStdString &dateTime);
177
178   /*! \brief set from a database datetime format YYYY-MM-DD HH:MM:SS
179    \sa GetAsDBDateTime()
180    */
181   void SetFromDBDateTime(const CStdString &dateTime);
182
183   void GetAsSystemTime(SYSTEMTIME& time) const;
184   void GetAsTime(time_t& time) const;
185   void GetAsTm(tm& time) const;
186   void GetAsTimeStamp(FILETIME& time) const;
187
188   CDateTime GetAsUTCDateTime() const;
189   CStdString GetAsSaveString() const;
190   CStdString GetAsDBDateTime() const;
191   CStdString GetAsDBDate() const;
192   CStdString GetAsLocalizedDate(bool longDate=false, bool withShortNames=true) const;
193   CStdString GetAsLocalizedDate(const CStdString &strFormat, bool withShortNames=true) const;
194   CStdString GetAsLocalizedTime(const CStdString &format, bool withSeconds=true) const;
195   CStdString GetAsLocalizedDateTime(bool longDate=false, bool withSeconds=true) const;
196   CStdString GetAsRFC1123DateTime() const;
197
198   void SetValid(bool yesNo);
199   bool IsValid() const;
200
201   static void ResetTimezoneBias(void);
202   static CDateTimeSpan GetTimezoneBias(void);
203
204 private:
205   bool ToFileTime(const SYSTEMTIME& time, FILETIME& fileTime) const;
206   bool ToFileTime(const time_t& time, FILETIME& fileTime) const;
207   bool ToFileTime(const tm& time, FILETIME& fileTime) const;
208
209   void ToULargeInt(ULARGE_INTEGER& time) const;
210   void FromULargeInt(const ULARGE_INTEGER& time);
211
212 private:
213   FILETIME m_time;
214
215   typedef enum _STATE
216   {
217     invalid=0,
218     valid
219   } STATE;
220
221   STATE m_state;
222 };