]> pilppa.org Git - libplp.git/blob - src/Date.cc
368fafa2c12814e03089af08771ca21a06310793
[libplp.git] / src / Date.cc
1 /*
2  * Date.cc
3  *
4  *  Created on: Dec 7, 2010
5  *      Author: lamikr
6  */
7
8 #include <iostream>
9 #include <sstream>
10
11 #include <stdio.h>
12 #include <time.h>
13 #include <malloc.h>
14
15 #include "log.h"
16 #include "Date.hh"
17
18 using namespace std;
19 using namespace plp;
20
21 static const int CONST__DAYS_PER_MONTH[]        = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
22
23 Date::Date() {
24         time_t          wtime;
25         struct tm       *ltime;
26
27         time(&wtime);
28         ltime   = localtime(&wtime);
29         year    = 1900 + ltime->tm_year;
30         month   = ltime->tm_mon + 1;    // ltime-month: values 0 - 11...
31         day     = ltime->tm_mday;
32         hour    = ltime->tm_hour;
33         min     = ltime->tm_min;
34         sec     = ltime->tm_sec;
35 }
36
37 Date::Date(int year_param,
38         int month_param,
39         int day_param,
40         int hour_param,
41         int min_param,
42         int sec_param) {
43         year    = year_param;
44         month   = month_param;
45         day     = day_param;
46         hour    = hour_param;
47         min     = min_param;
48         sec     = sec_param;
49 }
50
51 Date::~Date() {
52         // TODO Auto-generated destructor stub
53 }
54
55 bool Date::is_leap_year() {
56         bool    ret_val;
57
58         ret_val = false;
59         if ((year % 4 == 0) &&
60             ((year % 400 == 0) || (year % 100 != 0))) {
61                 ret_val = true;
62         }
63         return ret_val;
64 }
65
66 void Date::printout() {
67         log_debug("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, min, sec);
68 }
69
70 Date *Date::clone() {
71         Date    *ret_val;
72
73         ret_val = new Date(year,
74                         month,
75                         day,
76                         hour,
77                         min,
78                         sec);
79         return ret_val;
80 }
81
82 void Date::copy(Date *date) {
83         year    = date->year;
84         month   = date->month;
85         day     = date->day;
86         hour    = date->hour;
87         min     = date->min;
88         sec     = date->sec;
89 }
90
91 bool Date::before(Date *date2) {
92         bool    ret_val;
93         string  s1;
94         string  s2;
95
96         ret_val = false;
97         if (date2 != NULL) {
98                 s1      = this->to_sortable_string();
99                 s2      = date2->to_sortable_string();
100                 if (s1.compare(s2) < 0) {
101                         ret_val = true;
102                 }
103         }
104         return ret_val;
105 }
106
107 bool Date::equals(Date *date2) {
108         bool ret_val;
109
110         ret_val = false;
111         if (date2 != NULL) {
112                 if ((this->sec == date2->sec) &&
113                     (this->min == date2->min) &&
114                     (this->hour == date2->hour) &&
115                     (this->day == date2->day) &&
116                     (this->month == date2->month) &&
117                     (this->year == date2->year)) {
118                         ret_val = true;
119                 }
120         }
121         return ret_val;
122 }
123
124 bool Date::before_or_equal_year(Date *date2) {
125         bool    ret_val;
126         string  s1;
127         string  s2;
128
129         ret_val = (this->year <= date2->year);
130         return ret_val;
131 }
132
133 bool Date::before_or_equal_month(Date *date2) {
134         bool    ret_val;
135         string  s1;
136         string  s2;
137
138         ret_val = false;
139         if (this->year < date2->year) {
140                 ret_val = true;
141         }
142         else {
143                 if ((this->year == date2->year) &&
144                     (this->month <= date2->month)) {
145                         ret_val = true;
146                 }
147         }
148         return ret_val;
149 }
150
151 bool Date::before_or_equal_day(Date *date2) {
152         bool    ret_val;
153         string  s1;
154         string  s2;
155
156         ret_val = false;
157         if (date2 != NULL) {
158                 s1      = this->to_sortable_day_string();
159                 s2      = date2->to_sortable_day_string();
160                 if (s1.compare(s2) <= 0) {
161                         ret_val = true;
162                 }
163         }
164         return ret_val;
165 }
166
167 bool Date::before_or_equal_hour(Date *date2) {
168         bool    ret_val;
169         string  s1;
170         string  s2;
171
172         ret_val = false;
173         if (date2 != NULL) {
174                 s1      = this->to_sortable_hour_string();
175                 s2      = date2->to_sortable_hour_string();
176                 if (s1.compare(s2) <= 0) {
177                         ret_val = true;
178                 }
179         }
180         return ret_val;
181 }
182
183 bool Date::before_or_equal_min(Date *date2) {
184         bool    ret_val;
185         string  s1;
186         string  s2;
187
188         ret_val = false;
189         if (date2 != NULL) {
190                 s1      = this->to_sortable_min_string();
191                 s2      = date2->to_sortable_min_string();
192                 if (s1.compare(s2) <= 0) {
193                         ret_val = true;
194                 }
195         }
196         return ret_val;
197 }
198
199 bool Date::before_or_equal(Date *date2) {
200         bool    ret_val;
201         string  s1;
202         string  s2;
203
204         ret_val = false;
205         if (date2 != NULL) {
206                 s1      = this->to_sortable_string();
207                 s2      = date2->to_sortable_string();
208                 if (s1.compare(s2) <= 0) {
209                         ret_val = true;
210                 }
211         }
212         return ret_val;
213 }
214
215 void Date::next_second() {
216         if (sec < 59) {
217                 sec++;
218         }
219         else {
220                 next_min();
221         }
222 }
223
224 void Date::next_min() {
225         if (min < 59) {
226                 sec     = 0;
227                 min++;
228         }
229         else {
230                 next_hour();
231         }
232 }
233
234 void Date::next_hour() {
235         hour++;
236         if (hour < 23) {
237                 sec     = 0;
238                 min     = 0;
239                 hour++;
240         }
241         else {
242                 next_day();
243         }
244 }
245
246 void Date::next_day() {
247         if ((month > 0) &&
248             (month <= 12)) {
249                 if ((day < CONST__DAYS_PER_MONTH[month - 1]) ||
250                     ((month == 2) &&
251                      (is_leap_year() == true) &&
252                      (day == 28))) {
253                         sec     = 0;
254                         min     = 0;
255                         hour    = 0;
256                         day++;
257                 }
258                 else {
259                         next_month();
260                 }
261         }
262 }
263
264 void Date::next_month() {
265         if (month < 12) {
266                 sec     = 0;
267                 min     = 0;
268                 hour    = 0;
269                 day     = 1;
270                 month++;
271         }
272         else {
273                 next_year();
274         }
275 }
276
277 void Date::next_year() {
278         sec     = 0;
279         min     = 0;
280         hour    = 0;
281         day     = 1;
282         month   = 1;
283         year++;
284 }
285
286 void Date::inc_minutes(int minutes) {
287         int     day_c;
288         int     hour_c;
289         int     ii;
290
291         day_c   = minutes / 1440;
292         minutes = minutes - day_c * 1440;
293         hour_c  = minutes / 60;
294         minutes = minutes - hour_c * 60;
295         for (ii = 0; ii < day_c; ii++) {
296                 next_day();
297         }
298         for (ii = 0; ii < hour_c; ii++) {
299                 hour++;
300                 if (hour > 24) {
301                         hour    = 0;
302                         next_day();
303                 }
304         }
305         min     = min + minutes;
306         if (min >= 60) {
307                 min     = min % 60;
308                 hour++;
309                 if (hour > 24) {
310                         hour    = 0;
311                         next_day();
312                 }
313         }
314 }
315
316 void Date::inc_seconds(int seconds) {
317         int     min_c;
318
319         min_c   = seconds / 60;
320         seconds = seconds - min_c * 60;
321         inc_minutes(min_c);
322         sec     = sec + seconds;
323         if (sec >= 60) {
324                 sec     = sec % 60;
325                 inc_minutes(1);
326         }
327 }
328
329 string Date::to_sortable_day_string() {
330         char    buffer[30];
331
332
333         string  ret_val;
334
335         sprintf(buffer, "%016d%02d%02d", year, month, day);
336         ret_val = buffer;
337         return ret_val;
338 }
339
340 string Date::to_sortable_hour_string() {
341         char    buffer[30];
342         string  ret_val;
343
344         sprintf(buffer, "%016d%02d%02d%02d", year, month, day, hour);
345         ret_val = buffer;
346         return ret_val;
347 }
348
349 string Date::to_sortable_min_string() {
350         char    buffer[30];
351         string  ret_val;
352
353         sprintf(buffer, "%016d%02d%02d%02d%02d", year, month, day, hour, min);
354         ret_val = buffer;
355         return ret_val;
356 }
357
358 string Date::to_sortable_string() {
359         char    buffer[30];
360         string  ret_val;
361
362         sprintf(buffer, "%016d%02d%02d%02d%02d%02d", year, month, day, hour, min, sec);
363         ret_val = buffer;
364         return ret_val;
365 }
366
367 string Date::to_string() {
368         string  ret_val;
369         char    buffer[40];
370
371         sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec);
372         ret_val = buffer;
373         return ret_val;
374 }
375
376 Date Date::parse_date_str(string date_str) {
377         char            c;
378         stringstream    ss(date_str);
379         Date            ret_val;
380
381         ss >>ret_val.year >>c >>ret_val.month >>c >>ret_val.day >>ret_val.hour >>c >>ret_val.min >>c >>ret_val.sec;
382         return ret_val;
383 }