]> pilppa.org Git - lib1wire.git/blob - src/Date.cc
Started adding support for caches when reading data. Cache files for
[lib1wire.git] / src / Date.cc
1 /*
2  * Date.cc
3  *
4  *  Created on: Dec 7, 2010
5  *      Author: lamikr
6  */
7
8 #include <iostream>
9
10 #include <stdio.h>
11 #include <time.h>
12 #include <malloc.h>
13
14 #include <plp/log.h>
15
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->month) {
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::tomorrow() {
216         if ((month > 0) &&
217             (month <= 12)) {
218                 day++;
219                 if (day > CONST__DAYS_PER_MONTH[month - 1]) {
220                         if ((month == 2) &&
221                             (is_leap_year() == true) &&
222                             (day == 29)) {
223                                 // ok
224                         }
225                         else {
226                                 day     = 1;
227                                 month++;
228                                 if (month > 12) {
229                                         year++;
230                                         month   = 1;
231                                 }
232                         }
233                 }
234         }
235 }
236
237 void Date::next_hour() {
238         if ((hour >= 0) &&
239             (hour <= 24)) {
240                 hour++;
241                 if (hour > 24) {
242                         hour    = 0;
243                         tomorrow();
244                 }
245         }
246 }
247
248 void Date::inc_minutes(int minutes) {
249         int     day_c;
250         int     hour_c;
251         int     ii;
252
253         day_c   = minutes / 1440;
254         minutes = minutes - day_c * 1440;
255         hour_c  = minutes / 60;
256         minutes = minutes - hour_c * 60;
257         for (ii = 0; ii < day_c; ii++) {
258                 tomorrow();
259         }
260         for (ii = 0; ii < hour_c; ii++) {
261                 hour++;
262                 if (hour > 24) {
263                         hour    = 0;
264                         tomorrow();
265                 }
266         }
267         min     = min + minutes;
268         if (min >= 60) {
269                 min     = min % 60;
270                 hour++;
271                 if (hour > 24) {
272                         hour    = 0;
273                         tomorrow();
274                 }
275         }
276 }
277
278 void Date::inc_seconds(int seconds) {
279         int     min_c;
280
281         min_c   = seconds / 60;
282         seconds = seconds - min_c * 60;
283         inc_minutes(min_c);
284         sec     = sec + seconds;
285         if (sec >= 60) {
286                 sec     = sec % 60;
287                 inc_minutes(1);
288         }
289 }
290
291 string Date::to_sortable_day_string() {
292         char    buffer[30];
293
294
295         string  ret_val;
296
297         sprintf(buffer, "%016d%02d%02d", year, month, day);
298         ret_val = buffer;
299         return ret_val;
300 }
301
302 string Date::to_sortable_hour_string() {
303         char    buffer[30];
304         string  ret_val;
305
306         sprintf(buffer, "%016d%02d%02d%02d", year, month, day, hour);
307         ret_val = buffer;
308         return ret_val;
309 }
310
311 string Date::to_sortable_min_string() {
312         char    buffer[30];
313         string  ret_val;
314
315         sprintf(buffer, "%016d%02d%02d%02d%02d", year, month, day, hour, min);
316         ret_val = buffer;
317         return ret_val;
318 }
319
320 string Date::to_sortable_string() {
321         char    buffer[30];
322         string  ret_val;
323
324         sprintf(buffer, "%016d%02d%02d%02d%02d%02d", year, month, day, hour, min, sec);
325         ret_val = buffer;
326         return ret_val;
327 }
328
329 string Date::to_string() {
330         char    buffer[30];
331         string  ret_val;
332
333         sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec);
334         ret_val = buffer;
335         return ret_val;
336 }