8 #ifndef INCL_evo_systime_h 9 #define INCL_evo_systime_h 13 #if !defined(_WIN32) && !(defined(_POSIX_TIMERS) && defined(CLOCK_PROCESS_CPUTIME_ID)) 14 #include <sys/resource.h> 38 const ulongl NSEC100_PER_SEC = 10000000;
39 const ulongl UNIX_OFFSET = 11644473600ULL;
41 if (::SystemTimeToFileTime(&ts, &ft) == 0)
43 const ulongl ft_nsec100 = ((ulongl)ft.dwHighDateTime << 32) | (ulongl)ft.dwLowDateTime;
44 const int64 sec = (int64)(ft_nsec100 / NSEC100_PER_SEC);
45 if (sec < UNIX_OFFSET)
47 return sec - UNIX_OFFSET;
51 return ts.wMilliseconds;
55 const long NSEC_PER_MSEC = 1000000L;
56 return (
long)ts.wMilliseconds / NSEC_PER_MSEC;
61 dt.date.year = ts.wYear;
62 dt.date.month = ts.wMonth;
63 dt.date.day = ts.wDay;
64 dt.time.hour = ts.wHour;
65 dt.time.minute = ts.wMinute;
66 dt.time.second = ts.wSecond;
67 dt.time.msecond = ts.wMilliseconds;
73 TIME_ZONE_INFORMATION info;
74 TIME_ZONE_INFORMATION* p_info;
75 if (::GetTimeZoneInformation(&info) == TIME_ZONE_ID_INVALID) {
80 dt.tz.minutes = (int)-info.Bias;
84 BOOL result = ::SystemTimeToTzSpecificLocalTime(p_info, &ts, &local);
85 assert( result != 0 );
87 dt.date.year = local.wYear;
88 dt.date.month = local.wMonth;
89 dt.date.day = local.wDay;
90 dt.time.hour = local.wHour;
91 dt.time.minute = local.wMinute;
92 dt.time.second = local.wSecond;
93 dt.time.msecond = local.wMilliseconds;
101 BOOL result = ::SystemTimeToTzSpecificLocalTime(NULL, &ts, &local);
102 assert( result != 0 );
104 dt.date.year = local.wYear;
105 dt.date.month = local.wMonth;
106 dt.date.day = local.wDay;
107 dt.time.hour = local.wHour;
108 dt.time.minute = local.wMinute;
109 dt.time.second = local.wSecond;
110 dt.time.msecond = local.wMilliseconds;
118 #if defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME) && !defined(EVO_USE_GETTIMEOFDAY) 119 typedef struct timespec Fields;
123 #if defined(CLOCK_REALTIME_COARSE) 124 ::clock_gettime(CLOCK_REALTIME_COARSE, &ts);
126 ::clock_gettime(CLOCK_REALTIME, &ts);
128 assert( ts.tv_sec > 0 );
132 return (int64)ts.tv_sec;
136 const long NSEC_PER_MSEC = 1000000L;
137 return (
int)(ts.tv_nsec / NSEC_PER_MSEC);
145 typedef struct timeval Fields;
150 ::gettimeofday(&ts, NULL);
151 assert( ts.tv_sec > 0 );
160 return (int64)ts.tv_sec;
167 const long USEC_PER_MSEC = 1000L;
168 return (
int)((long)ts.tv_usec / USEC_PER_MSEC);
175 const long NSEC_PER_USEC = 1000L;
176 return (
long)ts.tv_usec * NSEC_PER_USEC;
189 ::gmtime_r(&ts.tv_sec, &tm);
190 dt.date.year = 1900 + tm.tm_year;
191 dt.date.month = tm.tm_mon + 1;
192 dt.date.day = tm.tm_mday;
193 dt.time.hour = tm.tm_hour;
194 dt.time.minute = tm.tm_min;
195 dt.time.second = tm.tm_sec;
207 ::localtime_r(&ts.tv_sec, &tm);
208 #if defined(__FreeBSD__) 213 dt.date.year = 1900 + tm.tm_year;
214 dt.date.month = tm.tm_mon + 1;
215 dt.date.day = tm.tm_mday;
216 dt.time.hour = tm.tm_hour;
217 dt.time.minute = tm.tm_min;
218 dt.time.second = tm.tm_sec;
229 ::localtime_r(&ts.tv_sec, &tm);
230 dt.date.year = 1900 + tm.tm_year;
231 dt.date.month = tm.tm_mon + 1;
232 dt.date.day = tm.tm_mday;
233 dt.time.hour = tm.tm_hour;
234 dt.time.minute = tm.tm_min;
235 dt.time.second = tm.tm_sec;
261 { ::memset(&ts, 0,
sizeof(Fields)); }
270 static const ulong NSEC_PER_SEC = 1000000000UL;
271 static const ulong USEC_PER_SEC = 1000000UL;
272 static const ulong MSEC_PER_SEC = 1000;
273 static const ulong NSEC_PER_MSEC = 1000000UL;
274 static const ulong NSEC_PER_USEC = 1000;
278 static const int EPOCH_YEAR = 1601;
280 static const int EPOCH_YEAR = 1970;
313 void set_win32_ft(
const FILETIME& ft) {
314 const ulongl NSEC100_PER_SEC = 10000000;
315 const ulong NSEC_PER_NSEC100 = 100;
317 const ulongl ft_nsec100 = ((ulongl)ft.dwHighDateTime << 32) | (ulongl)ft.dwLowDateTime;
318 sec = ft_nsec100 / NSEC100_PER_SEC;
319 nsec = (ulong)(ft_nsec100 - (sec * NSEC100_PER_SEC)) * NSEC_PER_NSEC100;
322 void add_win32_ft(
const FILETIME& ft) {
323 const ulongl NSEC100_PER_SEC = 10000000;
324 const ulong NSEC_PER_NSEC100 = 100;
326 const ulongl ft_nsec100 = ((ulongl)ft.dwHighDateTime << 32) | (ulongl)ft.dwLowDateTime;
327 sec += ft_nsec100 / NSEC100_PER_SEC;
328 nsec += (ulong)(ft_nsec100 - (sec * NSEC100_PER_SEC)) * NSEC_PER_NSEC100;
330 const ulong add_sec = (nsec / NSEC_PER_SEC);
332 nsec -= (add_sec * NSEC_PER_SEC);
345 ::SystemTimeToFileTime(&src.ts, &ft);
348 sec = (ulongl)src.ts.tv_sec;
349 nsec = (ulong)src.get_nsec();
362 ::GetSystemTime(&stm);
363 ::SystemTimeToFileTime(&stm, &ft);
365 #elif defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME) && !defined(EVO_USE_GETTIMEOFDAY) 367 #if defined(CLOCK_REALTIME_COARSE) 368 ::clock_gettime(CLOCK_REALTIME_COARSE, &ts);
370 ::clock_gettime(CLOCK_REALTIME, &ts);
372 assert( ts.tv_sec > 0 );
373 assert( ts.tv_nsec >= 0 );
375 sec = (ulongl)ts.tv_sec;
376 nsec = (ulong)ts.tv_nsec;
379 ::gettimeofday(&tv, NULL);
380 assert( tv.tv_sec > 0 );
381 assert( tv.tv_usec >= 0 );
383 sec = (ulongl)tv.tv_sec;
384 nsec = (ulong)tv.tv_usec * NSEC_PER_USEC;
394 #if defined(EVO_WIN32_NO_QPC) 397 ::GetSystemTimeAsFileTime(&ft);
401 static const ulongl FREQ = (ulongl)qpc_get_freq();
403 ::QueryPerformanceCounter(&qpc);
404 assert( qpc.QuadPart > 0 );
406 sec = qpc.QuadPart / FREQ;
407 nsec = (ulong)( ((ulongl)qpc.QuadPart - (sec * FREQ)) * NSEC_PER_SEC / FREQ );
409 #elif defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME) && !defined(EVO_USE_GETTIMEOFDAY) 411 #if defined(CLOCK_MONOTONIC_RAW) 412 ::clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
413 #elif defined(CLOCK_MONOTONIC) 414 ::clock_gettime(CLOCK_MONOTONIC, &ts);
416 ::clock_gettime(CLOCK_REALTIME, &ts);
418 assert( ts.tv_sec > 0 );
419 assert( ts.tv_nsec >= 0 );
421 sec = (ulongl)ts.tv_sec;
422 nsec = (ulong)ts.tv_nsec;
425 ::gettimeofday(&tv, NULL);
426 assert( tv.tv_sec > 0 );
427 assert( tv.tv_usec >= 0 );
429 sec = (ulongl)tv.tv_sec;
430 nsec = (ulong)tv.tv_usec * NSEC_PER_USEC;
437 FILETIME create, exit, kernel, user;
438 ::GetProcessTimes(::GetCurrentProcess(), &create, &exit, &kernel, &user);
439 set_win32_ft(kernel);
441 #elif defined(_POSIX_TIMERS) && defined(CLOCK_PROCESS_CPUTIME_ID) 443 ::clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
444 assert( ts.tv_nsec >= 0 );
446 sec = (ulongl)ts.tv_sec;
447 nsec = (ulong)ts.tv_nsec;
450 ::getrusage(RUSAGE_SELF, &ru);
452 sec = (ulongl)ru.ru_stime.tv_sec + (ulongl)ru.ru_utime.tv_sec;
453 nsec = ((ulong)ru.ru_stime.tv_usec + (ulong)ru.ru_stime.tv_usec) * NSEC_PER_USEC;
455 if (nsec >= NSEC_PER_SEC) {
457 nsec -= NSEC_PER_SEC;
469 ulong new_sec = new_msec / MSEC_PER_SEC;
471 nsec += (ulong)(new_msec - (new_sec * MSEC_PER_SEC)) * NSEC_PER_MSEC;
473 new_sec = nsec / NSEC_PER_SEC;
475 nsec -= (ulong)(new_sec * NSEC_PER_SEC);
485 ulongl new_sec = new_nsec / NSEC_PER_SEC;
487 nsec += (ulong)(new_nsec - (new_sec * NSEC_PER_SEC));
489 new_sec = nsec / NSEC_PER_SEC;
491 nsec -= (ulong)(new_sec * NSEC_PER_SEC);
501 else if (sec > oth.
sec)
503 else if (nsec < oth.
nsec)
505 else if (nsec > oth.
nsec)
515 assert( start.
sec <= sec );
516 assert( start.
sec < sec || start.
nsec <= nsec );
517 return ((sec - start.
sec) * MSEC_PER_SEC) + (nsec / NSEC_PER_MSEC) - (start.
nsec / NSEC_PER_MSEC);
525 assert( start.
sec <= sec );
526 assert( start.
sec < sec || start.
nsec <= nsec );
527 return ((sec - start.
sec) * USEC_PER_SEC) + (nsec / NSEC_PER_USEC) - (start.
nsec / NSEC_PER_USEC);
535 assert( start.
sec <= sec );
536 assert( start.
sec < sec || start.
nsec <= nsec );
537 return ((sec - start.
sec) * NSEC_PER_SEC) + nsec - start.
nsec;
555 ::GetSystemTime(&stm);
560 minute = stm.wMinute;
561 second = stm.wSecond;
562 msecond = stm.wMilliseconds;
568 const time_t sec = (time_t)ts.
sec;
569 ::gmtime_r(&sec, &tm);
570 msecond = (int)(ts.
nsec / NSEC_PER_MSEC);
572 year = 1900 + tm.tm_year;
573 month = tm.tm_mon + 1;
596 ::GetLocalTime(&stm);
601 minute = stm.wMinute;
602 second = stm.wSecond;
603 msecond = stm.wMilliseconds;
609 const time_t sec = (time_t)ts.
sec;
610 ::localtime_r(&sec, &tm);
611 msecond = (int)(ts.
nsec / NSEC_PER_MSEC);
613 year = 1900 + tm.tm_year;
614 month = tm.tm_mon + 1;
638 ::GetLocalTime(&stm);
643 minute = stm.wMinute;
644 second = stm.wSecond;
645 msecond = stm.wMilliseconds;
646 tz_offset = tz_get_offset();
652 const time_t sec = (time_t)ts.
sec;
654 ::localtime_r(&sec, &tm);
655 msecond = (int)(ts.
nsec / NSEC_PER_MSEC);
656 #if defined(__FreeBSD__) 662 year = 1900 + tm.tm_year;
663 month = tm.tm_mon + 1;
677 #if defined(__linux__) 679 const char* FILE_STR =
":/etc/localtime";
680 if (::getenv(
"TZ") == NULL && ::access(FILE_STR + 1, R_OK) == 0)
681 ::setenv(
"TZ", FILE_STR, 1);
693 TIME_ZONE_INFORMATION info;
694 if (::GetTimeZoneInformation(&info) == TIME_ZONE_ID_INVALID)
696 return (
int)-info.Bias;
699 #if defined(__FreeBSD__) 702 if (localtime_r(&dummy, &tm) == NULL)
712 #if defined(_WIN32) && !defined(EVO_WIN32_NO_QPC) 713 static longl qpc_get_freq() {
715 ::QueryPerformanceFrequency(&freq);
716 assert( freq.QuadPart > 0 );
717 return freq.QuadPart;
Holds a system timestamp as native (platform specific) fields.
Definition: systime.h:27
void clear()
Clear and reset as 0.
Definition: systime.h:307
int compare(const SysTimestamp &oth) const
Compare to another timestamp.
Definition: systime.h:498
void set_cpu()
Set as current CPU (process) time for use by timers.
Definition: systime.h:435
void set_utc()
Set to current date/time (UTC).
Definition: systime.h:149
SysNativeTimeStamp & operator=(const SysNativeTimeStamp &src)
Assignment operator.
Definition: systime.h:256
static const int SEC_PER_MIN
Number of seconds per minute.
Definition: systime.h:180
void set_wall_timer()
Set as current real (wall clock) time for use by timers.
Definition: systime.h:392
Evo implementation detail for system portability – this is included by most Evo headers, include this via: include <evo/type.h>.
SysTimestamp()
Constructor.
Definition: systime.h:287
void add_msec(ulong new_msec)
Add milliseconds to current time.
Definition: systime.h:467
ulongl diff_msec(const SysTimestamp &start) const
Use this as an end-time and get the difference from start time in milliseconds.
Definition: systime.h:514
SysTimestamp(const SysTimestamp &src)
Copy constructor.
Definition: systime.h:293
ulongl diff_nsec(const SysTimestamp &start) const
Use this as an end-time and get the difference from start time in nanoseconds.
Definition: systime.h:534
struct timeval Fields
Definition: systime.h:145
void convert_utc_dt(DT &dt) const
Convert current date/time to DateTime holding UTC.
Definition: systime.h:187
ulongl diff_usec(const SysTimestamp &start) const
Use this as an end-time and get the difference from start time in microseconds.
Definition: systime.h:524
struct timeval ts
Definition: systime.h:146
ulongl sec
Number of seconds since Jan 1, EPOCH_YEAR.
Definition: systime.h:283
static void get_wall_datetime_fields_local(int &year, int &month, int &day, int &hour, int &minute, int &second, int &msecond, int &tz_offset)
Get fields for current real (wall clock) time for calendar date/time use (Local Time).
Definition: systime.h:635
void set_wall_datetime()
Set as current real (wall clock) time for calendar date/time use.
Definition: systime.h:358
Evo C++ Library namespace.
Definition: alg.h:11
void convert_local_dt(DT &dt) const
Convert current date/time to DateTime holding Local Time, including timezone offset.
Definition: systime.h:204
long get_nsec() const
Get current fractional second value in nanoseconds.
Definition: systime.h:174
void convert_local_dt_notz(DT &dt) const
Convert current date/time to DateTime holding Local Time, without storing timezone offset...
Definition: systime.h:227
static void get_wall_datetime_fields_utc(int &year, int &month, int &day, int &hour, int &minute, int &second, int &msecond)
Get fields for current real (wall clock) time for calendar date/time use (UTC).
Definition: systime.h:552
static void tz_init()
Initializater that optimizes timezone (local time) conversion in some cases.
Definition: systime.h:676
int64 get_unix_timestamp() const
Get current date/time as Unix timestamp.
Definition: systime.h:159
static void get_wall_datetime_fields_local(int &year, int &month, int &day, int &hour, int &minute, int &second, int &msecond)
Get fields for current real (wall clock) time for calendar date/time use (Local Time).
Definition: systime.h:593
SysTimestamp & operator=(const SysTimestamp &src)
Assignment operator.
Definition: systime.h:300
SysNativeTimeStamp(const SysNativeTimeStamp &src)
Copy constructor.
Definition: systime.h:249
SysNativeTimeStamp()
Constructor.
Definition: systime.h:243
Holds a system timestamp for storing date/time and measuring elapsed time.
Definition: systime.h:269
static int tz_get_offset()
Get current time zone (local time) offset from UTC in minutes.
Definition: systime.h:691
int get_msec() const
Get current fractional second value in milliseconds.
Definition: systime.h:166
ulong nsec
Number of nanoseconds from seconds.
Definition: systime.h:284
void add_nsec(ulongl new_nsec)
Add nanoseconds to current time.
Definition: systime.h:483