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