]> pilppa.org Git - lib1wire.git/blobdiff - src/Date.cc
Refactoring and fixes.
[lib1wire.git] / src / Date.cc
index 8c03c5bd661e6ed7a5c371037ef4e0923b268af2..6e8fdb6250b3d7e101a2e760cf176349a7cc52eb 100644 (file)
@@ -27,7 +27,7 @@ Date::Date() {
        time(&wtime);
        ltime   = localtime(&wtime);
        year    = 1900 + ltime->tm_year;
-       month   = ltime->tm_mon;
+       month   = ltime->tm_mon + 1;    // ltime-month: values 0 - 11...
        day     = ltime->tm_mday;
        hour    = ltime->tm_hour;
        min     = ltime->tm_min;
@@ -70,29 +70,29 @@ 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    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;
@@ -137,7 +137,61 @@ 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;
 
@@ -145,3 +199,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;
+}