[win32] cmake: make use of ADDONS_TO_BUILD in make-addons.bat
[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/IArchivable.h"
24 #include "system.h"
25 #include <string>
26
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
31  */
32 enum TIME_FORMAT { TIME_FORMAT_GUESS       =  0,
33                    TIME_FORMAT_SS          =  1,
34                    TIME_FORMAT_MM          =  2,
35                    TIME_FORMAT_MM_SS       =  3,
36                    TIME_FORMAT_HH          =  4,
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,
43                    TIME_FORMAT_H           = 16,
44                    TIME_FORMAT_H_MM_SS     = 19,
45                    TIME_FORMAT_H_MM_SS_XX  = 27};
46
47 class CDateTime;
48
49 class CDateTimeSpan
50 {
51 public:
52   CDateTimeSpan();
53   CDateTimeSpan(const CDateTimeSpan& span);
54   CDateTimeSpan(int day, int hour, int minute, int second);
55
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;
62
63   CDateTimeSpan operator +(const CDateTimeSpan& right) const;
64   CDateTimeSpan operator -(const CDateTimeSpan& right) const;
65
66   const CDateTimeSpan& operator +=(const CDateTimeSpan& right);
67   const CDateTimeSpan& operator -=(const CDateTimeSpan& right);
68
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);
72
73   int GetDays() const;
74   int GetHours() const;
75   int GetMinutes() const;
76   int GetSeconds() const;
77   int GetSecondsTotal() const;
78
79 private:
80   void ToULargeInt(ULARGE_INTEGER& time) const;
81   void FromULargeInt(const ULARGE_INTEGER& time);
82
83 private:
84   FILETIME m_timeSpan;
85
86   friend class CDateTime;
87 };
88
89 /// \brief DateTime class, which uses FILETIME as it's base.
90 class CDateTime : public IArchivable
91 {
92 public:
93   CDateTime();
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() {}
101
102   bool SetFromDateString(const std::string &date);
103
104   static CDateTime GetCurrentDateTime();
105   static CDateTime GetUTCDateTime();
106   static int MonthStringToMonthNum(const std::string& month);
107
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);
112
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;
119
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;
126
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;
133
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;
140
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;
147
148   CDateTime operator +(const CDateTimeSpan& right) const;
149   CDateTime operator -(const CDateTimeSpan& right) const;
150
151   const CDateTime& operator +=(const CDateTimeSpan& right);
152   const CDateTime& operator -=(const CDateTimeSpan& right);
153
154   CDateTimeSpan operator -(const CDateTime& right) const;
155
156   operator FILETIME() const;
157
158   virtual void Archive(CArchive& ar);
159
160   void Reset();
161
162   int GetDay() const;
163   int GetMonth() const;
164   int GetYear() const;
165   int GetHour() const;
166   int GetMinute() const;
167   int GetSecond() const;
168   int GetDayOfWeek() const;
169   int GetMinuteOfDay() const;
170
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);
181
182   /*! \brief set from a database datetime format YYYY-MM-DD HH:MM:SS
183    \sa GetAsDBDateTime()
184    */
185   bool SetFromDBDateTime(const std::string &dateTime);
186
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;
191
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;
203
204   void SetValid(bool yesNo);
205   bool IsValid() const;
206
207   static void ResetTimezoneBias(void);
208   static CDateTimeSpan GetTimezoneBias(void);
209
210 private:
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;
214
215   void ToULargeInt(ULARGE_INTEGER& time) const;
216   void FromULargeInt(const ULARGE_INTEGER& time);
217
218 private:
219   FILETIME m_time;
220
221   typedef enum _STATE
222   {
223     invalid=0,
224     valid
225   } STATE;
226
227   STATE m_state;
228 };