]> pilppa.org Git - lib1wire.git/blob - src/Date.cc
started adding support for reading average and sum data...
[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 "Date.hh"
15
16 using namespace std;
17 using namespace plp;
18
19 static const int CONST__DAYS_PER_MONTH[]        = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
20
21 Date::Date() {
22         time_t          wtime;
23         struct tm       *ltime;
24
25         time(&wtime);
26         ltime   = localtime(&wtime);
27         year    = ltime->tm_year;
28         month   = ltime->tm_mon;
29         day     = ltime->tm_mday;
30         hour    = ltime->tm_hour;
31         min     = ltime->tm_min;
32         sec     = ltime->tm_sec;
33 }
34
35 Date::Date(int year_param,
36         int month_param,
37         int day_param,
38         int hour_param,
39         int min_param,
40         int sec_param) {
41         year    = year_param;
42         month   = month_param;
43         day     = day_param;
44         hour    = hour_param;
45         min     = min_param;
46         sec     = sec_param;
47 }
48
49 Date::~Date() {
50         // TODO Auto-generated destructor stub
51 }
52
53 bool Date::is_leap_year() {
54         bool    ret_val;
55
56         ret_val = false;
57         if ((year % 4 == 0) &&
58             (year % 400 == 0) || (year % 100 != 0)) {
59                 ret_val = true;
60         }
61         return ret_val;
62 }
63
64 void Date::printout() {
65         cout << "date: " << year << " " << month << " " << day << " " << hour << " " << min << " " << sec << endl;
66 }
67
68 Date *Date::clone() {
69         Date    *ret_val;
70
71         ret_val = new Date(this->year,
72                         this->month,
73                         this->day,
74                         this->hour,
75                         this->min,
76                         this->sec);
77         return ret_val;
78 }
79
80 void Date::copy(Date *date) {
81         this->year      = date->year;
82         this->month     = date->month;
83         this->day       = date->day;
84         this->hour      = date->hour;
85         this->min       = date->min;
86         this->sec       = date->sec;
87 }
88
89 bool Date::before(Date date2) {
90         bool    ret_val;
91         string s1 = this->to_string();
92         string s2 = date2.to_string();
93
94         ret_val = false;
95         if (s1.compare(s2) < 0) {
96                 ret_val = true;
97         }
98         return ret_val;
99 }
100
101 bool Date::equals(Date date2) {
102         bool ret_val;
103
104         ret_val = false;
105         if ((this->sec == date2.sec) &&
106             (this->min == date2.min) &&
107             (this->hour == date2.hour) &&
108             (this->day == date2.day) &&
109             (this->month == date2.month) &&
110             (this->year == date2.year)) {
111                 ret_val = true;
112         }
113         return ret_val;
114 }
115
116 void Date::tomorrow() {
117         if ((month > 0) &&
118             (month <= 12)) {
119                 day++;
120                 if (day > CONST__DAYS_PER_MONTH[month - 1]) {
121                         if ((month == 2) &&
122                             (is_leap_year() == true) &&
123                             (day == 29)) {
124                                 // ok
125                         }
126                         else {
127                                 day     = 1;
128                                 month++;
129                                 if (month == 12) {
130                                         year++;
131                                         month   = 1;
132                                 }
133                         }
134                 }
135         }
136 }
137
138 string Date::to_string() {
139         char    buffer[30];
140         string  ret_val;
141
142         int n, a=5, b=3;
143         sprintf(buffer, "%016d%02d%02d%02d%02d%02d", year, month, day, hour, min, sec);
144         ret_val = buffer;
145         return ret_val;
146 }