25#include "common/defs.h"
26#include "common/lang/string.h"
55 SECONDS_PER_DAY = 86400,
56 SECONDS_PER_HOUR = 3600,
58 MINUTES_PER_HOUR = 60,
60 MILLIS_PER_DAY = 86400000,
61 MILLIS_PER_HOUR = 3600000,
62 MILLIS_PER_MIN = 60000,
63 MILLIS_PER_SEC = 1000,
66 JULIAN_19700101 = 2440588
88 DateTime(
int date,
int time) : m_date(date), m_time(time) {}
91 DateTime(
int year,
int month,
int day,
int hour,
int minute,
int second,
int millis)
93 m_date = julian_date(year, month, day);
94 m_time = make_hms(hour, minute, second, millis);
101 static bool is_valid_xml_datetime(
const string &str);
105 inline void get_ymd(
int &year,
int &month,
int &day)
const { get_ymd(m_date, year, month, day); }
109 inline void get_hms(
int &hour,
int &minute,
int &second,
int &millis)
const
111 int ticks = m_time / MILLIS_PER_SEC;
112 hour = ticks / SECONDS_PER_HOUR;
113 minute = (ticks / SECONDS_PER_MIN) % MINUTES_PER_HOUR;
114 second = ticks % SECONDS_PER_MIN;
115 millis = m_time % MILLIS_PER_SEC;
120 inline time_t to_time_t()
const {
return (SECONDS_PER_DAY * (m_date - JULIAN_19700101) + m_time / MILLIS_PER_SEC); }
125 int year, month, day;
126 int hour, minute, second, millis;
129 get_ymd(year, month, day);
130 get_hms(hour, minute, second, millis);
132 result.tm_year = year - 1900;
133 result.tm_mon = month - 1;
134 result.tm_mday = day;
135 result.tm_hour = hour;
136 result.tm_min = minute;
137 result.tm_sec = second;
138 result.tm_isdst = -1;
144 void set_ymd(
int year,
int month,
int day) { m_date = julian_date(year, month, day); }
147 void set_hms(
int hour,
int minute,
int second,
int millis) { m_time = make_hms(hour, minute, second, millis); }
150 void clear_date() { m_date = 0; }
153 void clear_time() { m_time = 0; }
156 void set(
int date,
int time)
165 m_date = other.m_date;
166 m_time = other.m_time;
170 void operator+=(
int seconds)
172 int d = seconds / SECONDS_PER_DAY;
173 int s = seconds % SECONDS_PER_DAY;
176 m_time += s * MILLIS_PER_SEC;
178 if (m_time > MILLIS_PER_DAY) {
180 m_time %= MILLIS_PER_DAY;
181 }
else if (m_time < 0) {
183 m_time += MILLIS_PER_DAY;
191 time_t str_to_time_t(
string &xml_str);
194 string time_t_to_xml_str(time_t timet);
197 string time_t_to_str(
int timet);
200 string str_to_time_t_str(
string &xml_str);
204 static int make_hms(
int hour,
int minute,
int second,
int millis)
206 return MILLIS_PER_SEC * (SECONDS_PER_HOUR * hour + SECONDS_PER_MIN * minute + second) + millis;
216 static DateTime from_time_t(time_t t,
int millis = 0)
219 tm *tm = gmtime_r(&t, &tmbuf);
220 return from_tm(*tm, millis);
225 static DateTime from_tm(
const tm &tm,
int millis = 0)
228 julian_date(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday), make_hms(tm.tm_hour, tm.tm_min, tm.tm_sec, millis));
232 static int julian_date(
int year,
int month,
int day)
234 int a = (14 - month) / 12;
235 int y = year + 4800 - a;
236 int m = month + 12 * a - 3;
237 return (day +
int((153 * m + 2) / 5) + y * 365 + int(y / 4) - int(y / 100) + int(y / 400) - 32045);
241 static void get_ymd(
int jday,
int &year,
int &month,
int &day)
243 int a = jday + 32044;
244 int b = (4 * a + 3) / 146097;
245 int c = a - int((b * 146097) / 4);
246 int d = (4 * c + 3) / 1461;
247 int e = c - int((1461 * d) / 4);
248 int m = (5 * e + 2) / 153;
249 day = e - int((153 * m + 2) / 5) + 1;
250 month = m + 3 - 12 * int(m / 10);
251 year = b * 100 + d - 4800 + int(m / 10);
256 string to_string_local()
258 const time_t tt = to_time_t();
262 asctime_r(localtime_r(&tt, &tm), &(buffer[0]));
269 string to_string_utc()
271 const time_t tt = to_time_t();
275 asctime_r(gmtime_r(&tt, &tm), &(buffer[0]));
281 time_t add_duration(
string xml_dur);
284 void add_duration_date_time(
string xml_dur);
287 int max_day_in_month_for(
int year,
int month);
290 void parse_duration(
string dur_str,
struct tm &tm_t);
295 return lhs.m_date == rhs.m_date && lhs.m_time == rhs.m_time;
298inline bool operator!=(
const DateTime &lhs,
const DateTime &rhs) {
return !(lhs == rhs); }
300inline bool operator<(
const DateTime &lhs,
const DateTime &rhs)
302 if (lhs.m_date < rhs.m_date)
304 else if (lhs.m_date > rhs.m_date)
306 else if (lhs.m_time < rhs.m_time)
311inline bool operator>(
const DateTime &lhs,
const DateTime &rhs) {
return !(lhs == rhs || lhs < rhs); }
313inline bool operator<=(
const DateTime &lhs,
const DateTime &rhs) {
return lhs == rhs || lhs < rhs; }
315inline bool operator>=(
const DateTime &lhs,
const DateTime &rhs) {
return lhs == rhs || lhs > rhs; }
319inline int operator-(
const DateTime &lhs,
const DateTime &rhs)
321 return (DateTime::SECONDS_PER_DAY * (lhs.m_date - rhs.m_date) +
323 lhs.m_time / 1000 - rhs.m_time / 1000);
334 TimeStamp(
int hour,
int minute,
int second,
int millisecond = 0) :
DateTime(DateTime::now())
336 set_hms(hour, minute, second, millisecond);
339 TimeStamp(
int hour,
int minute,
int second,
int date,
int month,
int year)
340 :
DateTime(year, month, date, hour, minute, second, 0)
343 TimeStamp(
int hour,
int minute,
int second,
int millisecond,
int date,
int month,
int year)
344 :
DateTime(year, month, date, hour, minute, second, millisecond)
347 TimeStamp(time_t time,
int millisecond = 0) :
DateTime(from_time_t(time, millisecond)) {}
349 TimeStamp(
const tm *time,
int millisecond = 0) :
DateTime(from_tm(*time, millisecond)) {}
351 void set_current() { set(DateTime::now()); }
359 Time() { set_current(); }
363 Time(
int hour,
int minute,
int second,
int millisecond = 0) { set_hms(hour, minute, second, millisecond); }
365 Time(time_t time,
int millisecond = 0) :
DateTime(from_time_t(time, millisecond)) { clear_date(); }
367 Time(
const tm *time,
int millisecond = 0) :
DateTime(from_tm(*time, millisecond)) { clear_date(); }
382 Date() { set_current(); }
386 Date(
int date,
int month,
int year) :
DateTime(year, month, date, 0, 0, 0, 0) {}
388 Date(
long sec) :
DateTime(sec / DateTime::SECONDS_PER_DAY, 0) {}
390 Date(
const tm *time) :
DateTime(from_tm(*time)) { clear_time(); }
403 static inline int64_t sec()
406 gettimeofday(&tv, 0);
407 time_t sec = tv.tv_sec;
409 if (tv.tv_usec > 500 * 1000)
414 static inline int64_t usec()
417 gettimeofday(&tv, 0);
418 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
421 static inline int64_t msec()
424 gettimeofday(&tv, 0);
425 int64_t msec = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
426 if (tv.tv_usec % 1000 >= 500)
431 static string unique();
Definition: datetime.h:379
Definition: datetime.h:401
Definition: datetime.h:328
Definition: datetime.h:356
Definition: datetime.h:49
string to_xml_date_time()
Return date and time as a string in Xml Schema date-time format
Definition: datetime.cpp:125