]> pilppa.org Git - lib1wire.git/blobdiff - src/Date.cc
Added support for cacheing monthly data.
[lib1wire.git] / src / Date.cc
index e82ee999edc8dad3c321c7de7cda3d54558ec974..e45b544fc6073d40ba431f2c6eeb0d1693cc4edf 100644 (file)
@@ -70,52 +70,162 @@ void Date::printout() {
 Date *Date::clone() {
        Date    *ret_val;
 
-       ret_val = new Date(this->year,
-                       this->month,
-                       this->day,
-                       this->hour,
-                       this->min,
-                       this->sec);
+       ret_val = new Date(year,
+                       month,
+                       day,
+                       hour,
+                       min,
+                       sec);
        return ret_val;
 }
 
 void Date::copy(Date *date) {
-       this->year      = date->year;
-       this->month     = date->month;
-       this->day       = date->day;
-       this->hour      = date->hour;
-       this->min       = date->min;
-       this->sec       = date->sec;
+       year    = date->year;
+       month   = date->month;
+       day     = date->day;
+       hour    = date->hour;
+       min     = date->min;
+       sec     = date->sec;
 }
 
-bool Date::before(Date date2) {
+bool Date::before(Date *date2) {
        bool    ret_val;
-       string s1 = this->to_string();
-       string s2 = date2.to_string();
+       string  s1;
+       string  s2;
 
        ret_val = false;
-       if (s1.compare(s2) < 0) {
-               ret_val = true;
+       if (date2 != NULL) {
+               s1      = this->to_sortable_string();
+               s2      = date2->to_sortable_string();
+               if (s1.compare(s2) < 0) {
+                       ret_val = true;
+               }
        }
        return ret_val;
 }
 
-bool Date::equals(Date date2) {
+bool Date::equals(Date *date2) {
        bool ret_val;
 
        ret_val = false;
-       if ((this->sec == date2.sec) &&
-           (this->min == date2.min) &&
-           (this->hour == date2.hour) &&
-           (this->day == date2.day) &&
-           (this->month == date2.month) &&
-           (this->year == date2.year)) {
+       if (date2 != NULL) {
+               if ((this->sec == date2->sec) &&
+                   (this->min == date2->min) &&
+                   (this->hour == date2->hour) &&
+                   (this->day == date2->day) &&
+                   (this->month == date2->month) &&
+                   (this->year == date2->year)) {
+                       ret_val = true;
+               }
+       }
+       return ret_val;
+}
+
+bool Date::before_or_equal_year(Date *date2) {
+       bool    ret_val;
+       string  s1;
+       string  s2;
+
+       ret_val = (this->year <= date2->year);
+       return ret_val;
+}
+
+bool Date::before_or_equal_month(Date *date2) {
+       bool    ret_val;
+       string  s1;
+       string  s2;
+
+       ret_val = false;
+       if (this->year < date2->year) {
                ret_val = true;
        }
+       else {
+               if ((this->year == date2->year) &&
+                   (this->month <= date2->month)) {
+                       ret_val = true;
+               }
+       }
        return ret_val;
 }
 
-void Date::tomorrow() {
+bool Date::before_or_equal_day(Date *date2) {
+       bool    ret_val;
+       string  s1;
+       string  s2;
+
+       ret_val = false;
+       if (date2 != NULL) {
+               s1      = this->to_sortable_day_string();
+               s2      = date2->to_sortable_day_string();
+               if (s1.compare(s2) <= 0) {
+                       ret_val = true;
+               }
+       }
+       return ret_val;
+}
+
+bool Date::before_or_equal_hour(Date *date2) {
+       bool    ret_val;
+       string  s1;
+       string  s2;
+
+       ret_val = false;
+       if (date2 != NULL) {
+               s1      = this->to_sortable_hour_string();
+               s2      = date2->to_sortable_hour_string();
+               if (s1.compare(s2) <= 0) {
+                       ret_val = true;
+               }
+       }
+       return ret_val;
+}
+
+bool Date::before_or_equal_min(Date *date2) {
+       bool    ret_val;
+       string  s1;
+       string  s2;
+
+       ret_val = false;
+       if (date2 != NULL) {
+               s1      = this->to_sortable_min_string();
+               s2      = date2->to_sortable_min_string();
+               if (s1.compare(s2) <= 0) {
+                       ret_val = true;
+               }
+       }
+       return ret_val;
+}
+
+bool Date::before_or_equal(Date *date2) {
+       bool    ret_val;
+       string  s1;
+       string  s2;
+
+       ret_val = false;
+       if (date2 != NULL) {
+               s1      = this->to_sortable_string();
+               s2      = date2->to_sortable_string();
+               if (s1.compare(s2) <= 0) {
+                       ret_val = true;
+               }
+       }
+       return ret_val;
+}
+
+void Date::next_hour() {
+       sec     = 0;
+       min     = 0;
+       hour++;
+       if (hour < 0) {
+               hour    = 0;
+       }
+       if (hour >= 24) {
+               hour    = 0;
+               next_day();
+       }
+}
+
+void Date::next_day() {
        if ((month > 0) &&
            (month <= 12)) {
                day++;
@@ -128,7 +238,7 @@ void Date::tomorrow() {
                        else {
                                day     = 1;
                                month++;
-                               if (month == 12) {
+                               if (month > 12) {
                                        year++;
                                        month   = 1;
                                }
@@ -137,17 +247,27 @@ void Date::tomorrow() {
        }
 }
 
-void Date::next_hour() {
-       if ((hour >= 0) &&
-           (hour <= 24)) {
-               hour++;
-               if (hour > 24) {
-                       hour    = 0;
-                       tomorrow();
-               }
+void Date::next_month() {
+       sec     = 0;
+       min     = 0;
+       hour    = 0;
+       day     = 1;
+       month++;
+       if (month > 12) {
+               month   = 1;
+               year++;
        }
 }
 
+void Date::next_year() {
+       sec     = 0;
+       min     = 0;
+       hour    = 0;
+       day     = 1;
+       month   = 1;
+       year++;
+}
+
 void Date::inc_minutes(int minutes) {
        int     day_c;
        int     hour_c;
@@ -158,13 +278,13 @@ void Date::inc_minutes(int minutes) {
        hour_c  = minutes / 60;
        minutes = minutes - hour_c * 60;
        for (ii = 0; ii < day_c; ii++) {
-               tomorrow();
+               next_day();
        }
        for (ii = 0; ii < hour_c; ii++) {
                hour++;
                if (hour > 24) {
                        hour    = 0;
-                       tomorrow();
+                       next_day();
                }
        }
        min     = min + minutes;
@@ -173,7 +293,7 @@ void Date::inc_minutes(int minutes) {
                hour++;
                if (hour > 24) {
                        hour    = 0;
-                       tomorrow();
+                       next_day();
                }
        }
 }
@@ -191,7 +311,36 @@ void Date::inc_seconds(int seconds) {
        }
 }
 
-string Date::to_string() {
+string Date::to_sortable_day_string() {
+       char    buffer[30];
+
+
+       string  ret_val;
+
+       sprintf(buffer, "%016d%02d%02d", year, month, day);
+       ret_val = buffer;
+       return ret_val;
+}
+
+string Date::to_sortable_hour_string() {
+       char    buffer[30];
+       string  ret_val;
+
+       sprintf(buffer, "%016d%02d%02d%02d", year, month, day, hour);
+       ret_val = buffer;
+       return ret_val;
+}
+
+string Date::to_sortable_min_string() {
+       char    buffer[30];
+       string  ret_val;
+
+       sprintf(buffer, "%016d%02d%02d%02d%02d", year, month, day, hour, min);
+       ret_val = buffer;
+       return ret_val;
+}
+
+string Date::to_sortable_string() {
        char    buffer[30];
        string  ret_val;
 
@@ -199,3 +348,12 @@ string Date::to_string() {
        ret_val = buffer;
        return ret_val;
 }
+
+string Date::to_string() {
+       char    buffer[30];
+       string  ret_val;
+
+       sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec);
+       ret_val = buffer;
+       return ret_val;
+}