diff options
Diffstat (limited to 'src/utils/time.cpp')
-rw-r--r-- | src/utils/time.cpp | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/src/utils/time.cpp b/src/utils/time.cpp index 856b1c7be..97804813d 100644 --- a/src/utils/time.cpp +++ b/src/utils/time.cpp @@ -43,27 +43,33 @@ u64bit combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz) return res; } -} - -/** -* Get the system clock -*/ -u64bit system_time() +std::tm do_gmtime(time_t time_val) { - return static_cast<u64bit>(std::time(0)); + // Race condition: std::gmtime is not assured thread safe, + // and C++ does not include gmtime_r. Use a mutex here? + + std::tm* tm_p = std::gmtime(&time_val); + if (tm_p == 0) + throw Encoding_Error("calendar_value could not convert with gmtime"); + return *tm_p; } +} + /* -* Convert a time_t to a struct tm +* Convert a time_point to a calendar_point */ -std::tm time_t_to_tm(u64bit timer) +calendar_point calendar_value( + const std::chrono::system_clock::time_point& time_point) { - std::time_t time_val = static_cast<std::time_t>(timer); - - std::tm* tm_p = std::gmtime(&time_val); - if (tm_p == 0) - throw Encoding_Error("time_t_to_tm could not convert"); - return (*tm_p); + std::tm tm = do_gmtime(std::chrono::system_clock::to_time_t(time_point)); + + return calendar_point(tm.tm_year + 1900, + tm.tm_mon + 1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec); } u64bit get_nanoseconds_clock() |