+++ /dev/null
-/*
- * Date.cc
- *
- * Created on: Dec 7, 2010
- * Author: lamikr
- */
-
-#include <iostream>
-
-#include <stdio.h>
-#include <time.h>
-#include <malloc.h>
-
-#include <plp/log.h>
-
-#include "Date.hh"
-
-using namespace std;
-using namespace plp;
-
-static const int CONST__DAYS_PER_MONTH[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
-
-Date::Date() {
- time_t wtime;
- struct tm *ltime;
-
- time(&wtime);
- ltime = localtime(&wtime);
- 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;
- sec = ltime->tm_sec;
-}
-
-Date::Date(int year_param,
- int month_param,
- int day_param,
- int hour_param,
- int min_param,
- int sec_param) {
- year = year_param;
- month = month_param;
- day = day_param;
- hour = hour_param;
- min = min_param;
- sec = sec_param;
-}
-
-Date::~Date() {
- // TODO Auto-generated destructor stub
-}
-
-bool Date::is_leap_year() {
- bool ret_val;
-
- ret_val = false;
- if ((year % 4 == 0) &&
- ((year % 400 == 0) || (year % 100 != 0))) {
- ret_val = true;
- }
- return ret_val;
-}
-
-void Date::printout() {
- 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(year,
- month,
- day,
- hour,
- min,
- sec);
- return ret_val;
-}
-
-void Date::copy(Date *date) {
- 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;
- 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;
-}
-
-bool Date::equals(Date *date2) {
- bool ret_val;
-
- ret_val = false;
- 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;
-}
-
-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_second() {
- if (sec < 59) {
- sec++;
- }
- else {
- next_min();
- }
-}
-
-void Date::next_min() {
- if (min < 59) {
- sec = 0;
- min++;
- }
- else {
- next_hour();
- }
-}
-
-void Date::next_hour() {
- hour++;
- if (hour < 23) {
- sec = 0;
- min = 0;
- hour++;
- }
- else {
- next_day();
- }
-}
-
-void Date::next_day() {
- if ((month > 0) &&
- (month <= 12)) {
- if ((day < CONST__DAYS_PER_MONTH[month - 1]) ||
- ((month == 2) &&
- (is_leap_year() == true) &&
- (day == 28))) {
- sec = 0;
- min = 0;
- hour = 0;
- day++;
- }
- else {
- next_month();
- }
- }
-}
-
-void Date::next_month() {
- if (month < 12) {
- sec = 0;
- min = 0;
- hour = 0;
- day = 1;
- month++;
- }
- else {
- next_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;
- 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++) {
- next_day();
- }
- for (ii = 0; ii < hour_c; ii++) {
- hour++;
- if (hour > 24) {
- hour = 0;
- next_day();
- }
- }
- min = min + minutes;
- if (min >= 60) {
- min = min % 60;
- hour++;
- if (hour > 24) {
- hour = 0;
- next_day();
- }
- }
-}
-
-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_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;
-
- 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;
-}