]> pilppa.org Git - lib1wire.git/blob - src/Date.cc
Several data read and store fixes.
[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(this->year,
74                         this->month,
75                         this->day,
76                         this->hour,
77                         this->min,
78                         this->sec);
79         return ret_val;
80 }
81
82 void Date::copy(Date *date) {
83         this->year      = date->year;
84         this->month     = date->month;
85         this->day       = date->day;
86         this->hour      = date->hour;
87         this->min       = date->min;
88         this->sec       = date->sec;
89 }
90
91 bool Date::before(Date date2) {
92         bool    ret_val;
93         string s1 = this->to_string();
94         string s2 = date2.to_string();
95
96         ret_val = false;
97         if (s1.compare(s2) < 0) {
98                 ret_val = true;
99         }
100         return ret_val;
101 }
102
103 bool Date::equals(Date date2) {
104         bool ret_val;
105
106         ret_val = false;
107         if ((this->sec == date2.sec) &&
108             (this->min == date2.min) &&
109             (this->hour == date2.hour) &&
110             (this->day == date2.day) &&
111             (this->month == date2.month) &&
112             (this->year == date2.year)) {
113                 ret_val = true;
114         }
115         return ret_val;
116 }
117
118 void Date::tomorrow() {
119         if ((month > 0) &&
120             (month <= 12)) {
121                 day++;
122                 if (day > CONST__DAYS_PER_MONTH[month - 1]) {
123                         if ((month == 2) &&
124                             (is_leap_year() == true) &&
125                             (day == 29)) {
126                                 // ok
127                         }
128                         else {
129                                 day     = 1;
130                                 month++;
131                                 if (month == 12) {
132                                         year++;
133                                         month   = 1;
134                                 }
135                         }
136                 }
137         }
138 }
139
140 void Date::next_hour() {
141         if ((hour >= 0) &&
142             (hour <= 24)) {
143                 hour++;
144                 if (hour > 24) {
145                         hour    = 0;
146                         tomorrow();
147                 }
148         }
149 }
150
151 void Date::inc_minutes(int minutes) {
152         int     day_c;
153         int     hour_c;
154         int     ii;
155
156         day_c   = minutes / 1440;
157         minutes = minutes - day_c * 1440;
158         hour_c  = minutes / 60;
159         minutes = minutes - hour_c * 60;
160         for (ii = 0; ii < day_c; ii++) {
161                 tomorrow();
162         }
163         for (ii = 0; ii < hour_c; ii++) {
164                 hour++;
165                 if (hour > 24) {
166                         hour    = 0;
167                         tomorrow();
168                 }
169         }
170         min     = min + minutes;
171         if (min >= 60) {
172                 min     = min % 60;
173                 hour++;
174                 if (hour > 24) {
175                         hour    = 0;
176                         tomorrow();
177                 }
178         }
179 }
180
181 void Date::inc_seconds(int seconds) {
182         int     min_c;
183
184         min_c   = seconds / 60;
185         seconds = seconds - min_c * 60;
186         inc_minutes(min_c);
187         sec     = sec + seconds;
188         if (sec >= 60) {
189                 sec     = sec % 60;
190                 inc_minutes(1);
191         }
192 }
193
194 string Date::to_string() {
195         char    buffer[30];
196         string  ret_val;
197
198         sprintf(buffer, "%016d%02d%02d%02d%02d%02d", year, month, day, hour, min, sec);
199         ret_val = buffer;
200         return ret_val;
201 }