]> pilppa.org Git - lib1wire.git/blobdiff - src/Date.cc
Refactoring and fixes.
[lib1wire.git] / src / Date.cc
index 8327d1513924a17afd33fd1a038d57b2aefbc73a..6e8fdb6250b3d7e101a2e760cf176349a7cc52eb 100644 (file)
@@ -11,6 +11,8 @@
 #include <time.h>
 #include <malloc.h>
 
+#include <plp/log.h>
+
 #include "Date.hh"
 
 using namespace std;
@@ -24,8 +26,8 @@ Date::Date() {
 
        time(&wtime);
        ltime   = localtime(&wtime);
-       year    = ltime->tm_year;
-       month   = ltime->tm_mon;
+       year    = 1900 + ltime->tm_year;
+       month   = ltime->tm_mon + 1;    // ltime-month: values 0 - 11...
        day     = ltime->tm_mday;
        hour    = ltime->tm_hour;
        min     = ltime->tm_min;
@@ -55,42 +57,42 @@ bool Date::is_leap_year() {
 
        ret_val = false;
        if ((year % 4 == 0) &&
-           (year % 400 == 0) || (year % 100 != 0)) {
+           ((year % 400 == 0) || (year % 100 != 0))) {
                ret_val = true;
        }
        return ret_val;
 }
 
 void Date::printout() {
-       cout << "date: " << year << " " << month << " " << day << " " << hour << " " << min << " " << sec << endl;
+       log_debug("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, min, sec);
 }
 
 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    ret_val;
-       string s1 = this->to_string();
-       string s2 = date2.to_string();
 
+       string s1 = this->to_sortable_string();
+       string s2 = date2.to_sortable_string();
        ret_val = false;
        if (s1.compare(s2) < 0) {
                ret_val = true;
@@ -135,12 +137,74 @@ void Date::tomorrow() {
        }
 }
 
-string Date::to_string() {
+void Date::next_hour() {
+       if ((hour >= 0) &&
+           (hour <= 24)) {
+               hour++;
+               if (hour > 24) {
+                       hour    = 0;
+                       tomorrow();
+               }
+       }
+}
+
+void Date::inc_minutes(int minutes) {
+       int     day_c;
+       int     hour_c;
+       int     ii;
+
+       day_c   = minutes / 1440;
+       minutes = minutes - day_c * 1440;
+       hour_c  = minutes / 60;
+       minutes = minutes - hour_c * 60;
+       for (ii = 0; ii < day_c; ii++) {
+               tomorrow();
+       }
+       for (ii = 0; ii < hour_c; ii++) {
+               hour++;
+               if (hour > 24) {
+                       hour    = 0;
+                       tomorrow();
+               }
+       }
+       min     = min + minutes;
+       if (min >= 60) {
+               min     = min % 60;
+               hour++;
+               if (hour > 24) {
+                       hour    = 0;
+                       tomorrow();
+               }
+       }
+}
+
+void Date::inc_seconds(int seconds) {
+       int     min_c;
+
+       min_c   = seconds / 60;
+       seconds = seconds - min_c * 60;
+       inc_minutes(min_c);
+       sec     = sec + seconds;
+       if (sec >= 60) {
+               sec     = sec % 60;
+               inc_minutes(1);
+       }
+}
+
+string Date::to_sortable_string() {
        char    buffer[30];
        string  ret_val;
 
-       int n, a=5, b=3;
        sprintf(buffer, "%016d%02d%02d%02d%02d%02d", year, month, day, hour, min, sec);
        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;
+}