]> pilppa.org Git - libplp.git/blob - src/Date.cc
Data read optimizations
[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 bool Date::is_last_day_of_month() {
247         bool ret_val;
248         int days_in_month;
249
250         days_in_month = -1;
251         if ((month > 0) &&
252             (month <= 12)) {
253                 days_in_month = CONST__DAYS_PER_MONTH[month - 1];
254                 if ((month == 2) &&
255                     (is_leap_year() == true)) {
256                         days_in_month = 29;
257                 }
258         }
259         ret_val = (day == days_in_month);
260         //log_debug("ret_val: %d", ret_val);
261         return ret_val;
262 }
263
264 void Date::next_day() {
265         if ((month > 0) &&
266             (month <= 12)) {
267                 if ((day < CONST__DAYS_PER_MONTH[month - 1]) ||
268                     ((month == 2) &&
269                      (is_leap_year() == true) &&
270                      (day == 28))) {
271                         sec     = 0;
272                         min     = 0;
273                         hour    = 0;
274                         day++;
275                 }
276                 else {
277                         next_month();
278                 }
279         }
280 }
281
282 void Date::next_month() {
283         if (month < 12) {
284                 sec     = 0;
285                 min     = 0;
286                 hour    = 0;
287                 day     = 1;
288                 month++;
289         }
290         else {
291                 next_year();
292         }
293 }
294
295 void Date::next_year() {
296         sec     = 0;
297         min     = 0;
298         hour    = 0;
299         day     = 1;
300         month   = 1;
301         year++;
302 }
303
304 void Date::inc_minutes(int minutes) {
305         int     day_c;
306         int     hour_c;
307         int     ii;
308
309         day_c   = minutes / 1440;
310         minutes = minutes - day_c * 1440;
311         hour_c  = minutes / 60;
312         minutes = minutes - hour_c * 60;
313         for (ii = 0; ii < day_c; ii++) {
314                 next_day();
315         }
316         for (ii = 0; ii < hour_c; ii++) {
317                 hour++;
318                 if (hour > 24) {
319                         hour    = 0;
320                         next_day();
321                 }
322         }
323         min     = min + minutes;
324         if (min >= 60) {
325                 min     = min % 60;
326                 hour++;
327                 if (hour > 24) {
328                         hour    = 0;
329                         next_day();
330                 }
331         }
332 }
333
334 void Date::inc_seconds(int seconds) {
335         int     min_c;
336
337         min_c   = seconds / 60;
338         seconds = seconds - min_c * 60;
339         inc_minutes(min_c);
340         sec     = sec + seconds;
341         if (sec >= 60) {
342                 sec     = sec % 60;
343                 inc_minutes(1);
344         }
345 }
346
347 string Date::to_sortable_day_string() {
348         char    buffer[30];
349
350
351         string  ret_val;
352
353         sprintf(buffer, "%016d%02d%02d", year, month, day);
354         ret_val = buffer;
355         return ret_val;
356 }
357
358 string Date::to_sortable_hour_string() {
359         char    buffer[30];
360         string  ret_val;
361
362         sprintf(buffer, "%016d%02d%02d%02d", year, month, day, hour);
363         ret_val = buffer;
364         return ret_val;
365 }
366
367 string Date::to_sortable_min_string() {
368         char    buffer[30];
369         string  ret_val;
370
371         sprintf(buffer, "%016d%02d%02d%02d%02d", year, month, day, hour, min);
372         ret_val = buffer;
373         return ret_val;
374 }
375
376 string Date::to_sortable_string() {
377         char    buffer[30];
378         string  ret_val;
379
380         sprintf(buffer, "%016d%02d%02d%02d%02d%02d", year, month, day, hour, min, sec);
381         ret_val = buffer;
382         return ret_val;
383 }
384
385 string Date::to_string() {
386         string  ret_val;
387         char    buffer[40];
388
389         sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec);
390         ret_val = buffer;
391         return ret_val;
392 }
393
394 Date Date::parse_date_str(string date_str) {
395         char            c;
396         stringstream    ss(date_str);
397         Date            ret_val;
398
399         ss >>ret_val.year >>c >>ret_val.month >>c >>ret_val.day >>ret_val.hour >>c >>ret_val.min >>c >>ret_val.sec;
400         return ret_val;
401 }