Date::Date(time_t t) { struct tm tm; safe_localtime(&tm, t); year_ = tm.tm_year + 1900; month_ = tm.tm_mon + 1; day_ = tm.tm_mday; }
Time::Time(time_t t) { struct tm tm; safe_localtime(&tm, t); hour_ = tm.tm_hour; minute_ = tm.tm_min; second_ = tm.tm_sec; }
Time::operator time_t() const { struct tm tm; safe_localtime(&tm, time(0)); tm.tm_sec = second_; tm.tm_min = minute_; tm.tm_hour = hour_; tm.tm_isdst = -1; return mktime(&tm); }
Date::operator time_t() const { struct tm tm; safe_localtime(&tm, time(0)); tm.tm_mday = day_; tm.tm_mon = month_ - 1; tm.tm_year = year_ - 1900; tm.tm_isdst = -1; return mktime(&tm); }
DateTime::DateTime(time_t t) { struct tm tm; safe_localtime(&tm, t); year_ = tm.tm_year + 1900; month_ = tm.tm_mon + 1; day_ = tm.tm_mday; hour_ = tm.tm_hour; minute_ = tm.tm_min; second_ = tm.tm_sec; now_ = false; }
static char * time_str (void) { static char formatted_time [9]; time_t time_secs; struct tm *time_struct; time_secs = time (NULL); time_struct = safe_localtime (&time_secs); snprintf (formatted_time, sizeof (formatted_time), "%02d:%02d:%02d", time_struct-> tm_hour, time_struct-> tm_min, time_struct-> tm_sec); return (formatted_time); }
static char * date_str (void) { static char formatted_date [11]; time_t time_secs; struct tm *time_struct; time_secs = time (NULL); time_struct = safe_localtime (&time_secs); snprintf (formatted_date, sizeof (formatted_date), "%4d/%02d/%02d", time_struct-> tm_year + 1900, time_struct-> tm_mon + 1, time_struct-> tm_mday); return (formatted_date); }
static void print_time_str (FILE *out) { time_t time_secs; struct tm *time_struct; ASSERT (out); /* Expect handle for output, fail nicely */ if (!out) return; time_secs = time (NULL); time_struct = safe_localtime (&time_secs); fprintf (out, "%2d/%02d/%02d %2d:%02d:%02d", time_struct-> tm_year + 1900, time_struct-> tm_mon + 1, time_struct-> tm_mday, time_struct-> tm_hour, time_struct-> tm_min, time_struct-> tm_sec); }