1 /**
2 *  Copyright Mikael H�gdahl - triyana@users.sourceforge.net
3 *
4 *  This source is distributed under the terms of the Q Public License version 1.0,
5 *  created by Trolltech (www.trolltech.com).
6 */
7 
8 #ifndef MHDate_h
9 #define MHDate_h
10 
11 #include <time.h>
12 #include "MHUtil.h"
13 #include "MHVector.h"
14 #include "MHString.h"
15 
16 
17 
18 /**
19 *  A class for handling date/time operations.
20 *  It can format date and times into many different
21 *  format. CreateTimeSerie can be called to create a
22 *  vector with date strings. Some of the operations will fail
23 *  if date is before 1970.
24 */
25 class MHDate : public MH {
26 public:
27     enum DAYS {
28                             MONDAY = 1,
29                             TUESDAY,
30                             WENDSDAY,
31                             THURSDAY,
32                             FRIDAY,
33                             SATURDAY,
34                             SUNDAY,
35                             WEEKDAY,
36                             WEEKEND,
37                             WEEK,
38                             MONTH,
39                             ALL,
40     };
41 
42     enum MONTHS {
43                             JANUARY = 1,
44                             FEBRUARY,
45                             MARS,
46                             APRIL,
47                             MAY,
48                             JUNE,
49                             JULY,
50                             AUGUST,
51                             SEPTEMBER,
52                             OCTOBER,
53                             NOVEMBER,
54                             DECEMBER,
55     };
56 
57     enum FORMAT {
58                             DATE = 1,
59                             DATE_,
60                             FULL_DATE,
61                             LONG_DATE,
62                             LONG_DATE_,
63                             SHORT_DATE,
64                             SHORT_DATE_,
65                             SHORT_DATE_NO_YEAR,
66                             SHORT_DATE_NO_YEAR_,
67                             TIME,
68                             SHORT_TIME,
69                             YEAR,
70                             WEEK_IN_YEAR,
71                             DAY_IN_YEAR,
72                             LOCAL_DATE_AND_TIME,
73                             LOCAL_DATE,
74                             LOCAL_TIME,
75                             NAME_OF_DAY,
76                             ABBR_OF_DAY,
77                             NAME_OF_MONTH,
78                             ABBR_OF_MONTH,
79     };
80 
81     enum SIZE {
82                             SECS_HOUR      = 3600,
83                             SECSDAY        = SECS_HOUR * 24,
84                             SECSWEEK       = SECSDAY * 7,
85                             SECSYEAR       = SECSDAY * 365,
86                             MAX_STRING_LEN = 20,
87     };
88 
89                             MHDate ();
90                             MHDate (MHDate& Date);
91                             MHDate (const char* s);
92                             MHDate (int year, int month, int date, int hour = 0, int min = 0, int sec = 0);
~MHDate()93     virtual                 ~MHDate () {;}
94 
95     MHDate&                 operator=(const MHDate&);
96     bool                    operator==(MHDate& x) {return GetSecs() == x.GetSecs();}
97     bool                    operator!=(MHDate& x) {return GetSecs() != x.GetSecs();}
98     bool                    operator<(MHDate& x) {return GetSecs() < x.GetSecs();}
99     bool                    operator<=(MHDate& x) {return GetSecs() <= x.GetSecs();}
100     bool                    operator>(MHDate& x) {return GetSecs() > x.GetSecs();}
101     bool                    operator>=(MHDate& x) {return GetSecs() >= x.GetSecs();}
102 
103     void                    AddDays (int nDays);
Class()104     const char*             Class () {return "MHDate";}
Compare(MH * o,int type)105     int                     Compare (MH* o, int type) {MHDate* d = (MHDate*)o; if (aSecs < d->aSecs) return -1; else if (aSecs == d->aSecs) return 0; else return 1;}
106     static void             ConvertSecsToTime (int Secs, MHString& s);
107     static void             CreateTimeSerie (const char* pStartDate, const char* pStopDate, int TimeType, MHVector* in, MHVector* out, int StringType = LONG_DATE);
DiffDays(MHDate * d1,MHDate * d2)108     static int              DiffDays (MHDate* d1, MHDate* d2) {return (d1->GetSecs() - d2->GetSecs()) / SECSDAY;}
109     const char*             Get (FORMAT nDateType);
110     void                    Get (FORMAT nDateType, MHString& s);
111     int                     GetDate ();
112     int                     GetLongYear ();
113     int                     GetMonth ();
114     int                     GetSecs ();
115     int                     GetSecsToday ();
116     int                     GetWeekday ();
117     int                     GetYear ();
118     void                    GotoNextWeekday (int nWeekday);
119     void                    GotoWeekday (int nWeekday);
120     bool                    IsLeapYear ();
121     static bool             IsLeapYear (int year);
122     static void             LongToVeryLong (const char* in, char* out);
123     static double           NoYears (const char* d1, const char* d2);
124     static MHString*        ParseString (MHString* dateString);
125     static long             Reference ();
126     void                    Reset ();
127     void                    Set (int nSecs = 0);
128     void                    Set (const char* s);
129     void                    Set (int year, int month, int date, int hour = 0, int min = 0, int sec = 0);
130     static bool             ValidDate (const char* date);
131 
132 private:
133     MHString                aDate;
134     struct tm               aTm;
135     time_t                  aSecs;
136 };
137 
138 
139 
140 /**
141 *  Construct a new date object with date and time set to now
142 */
MHDate()143 inline MHDate::MHDate () : MH(){
144     aDate = 0;
145     Set(0);
146 }
147 
148 
149 
150 /**
151 *  Construct new object with date set to string.
152 *  @param const char* - Date in the form of "YYYYMMDD"
153 */
MHDate(const char * s)154 inline MHDate::MHDate (const char* s) : MH(){
155     aDate = 0;
156     Set (s);
157 }
158 
159 
160 
161 /**
162 *  Construct new object.
163 *  @param int - Year
164 *  @param int - Month
165 *  @param int - Day in month
166 *  @param int - Hour, can be omitted
167 *  @param int - Min, can be omitted
168 *  @param int - Sec, can be omitted
169 */
MHDate(int year,int month,int date,int hour,int min,int sec)170 inline MHDate::MHDate (int year, int month, int date, int hour, int min, int sec) : MH() {
171     aDate = 0;
172     Set (year, month, date, hour, min, sec);
173 }
174 
175 
176 
177 /**
178 *  Add days to current date.
179 *  @param int - Number of days to add
180 */
AddDays(int nDays)181 inline void MHDate::AddDays (int nDays) {
182     Set ((int)aSecs + (nDays * SECSDAY));
183 }
184 
185 
186 
187 /**
188 *  Count day in month.
189 *  @return int - Day number
190 */
GetDate()191 inline int MHDate::GetDate () {
192     return aTm.tm_mday;
193 }
194 
195 
196 
197 /**
198 *  Return current year.
199 *  @return int - Year
200 */
GetLongYear()201 inline int MHDate::GetLongYear () {
202     return aTm.tm_year + 1900;
203 }
204 
205 
206 
207 /**
208 *  Get current month number, one of JANUARY - DECEMBER
209 *  @return int - Month number
210 */
GetMonth()211 inline int MHDate::GetMonth () {
212     return (aTm.tm_mon + 1);
213 }
214 
215 
216 
217 /**
218 *  Count number of seconds since 1970.
219 *  @return int - Seconds
220 */
GetSecs()221 inline int MHDate::GetSecs () {
222     return (int) aSecs;
223 }
224 
225 
226 
227 /**
228 *  Count number of seconds since midnight.
229 *  @return int - Seconds
230 */
GetSecsToday()231 inline int MHDate::GetSecsToday () {
232     return ((aTm.tm_hour * 3600) + (aTm.tm_min * 60) + aTm.tm_sec);
233 }
234 
235 
236 
237 /**
238 *  Return current year.
239 *  @return int - Year
240 */
GetYear()241 inline int MHDate::GetYear () {
242     return aTm.tm_year;
243 }
244 
245 
246 
247 /**
248 *  Check if year is leap year.
249 *  @return bool - true if leap year
250 */
IsLeapYear()251 inline bool MHDate::IsLeapYear () {
252     return MHDate::IsLeapYear (GetYear());
253 }
254 
255 
256 
257 /**
258 *  Set date/time set to now.
259 */
Reset()260 inline void MHDate::Reset () {
261     Set (0);
262 }
263 
264 #endif
265