From ce4ceb6e9dc5cfff939b920ae7a5cd1e2e337ef2 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Tue, 7 Jun 2016 20:03:40 -0400 Subject: [PATCH] Add tai_clock and gps_clock. --- tz.h | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/tz.h b/tz.h index 09c7a0f..5501d12 100644 --- a/tz.h +++ b/tz.h @@ -1115,6 +1115,176 @@ operator<<(std::basic_ostream& os, const utc_time& t) return os << tp; } +// tai_clock + +class tai_clock +{ +public: + using duration = std::chrono::system_clock::duration; + using rep = duration::rep; + using period = duration::period; + using time_point = std::chrono::time_point; + static const bool is_steady = true; + + static time_point now() noexcept; + + template + static + std::chrono::time_point::type> + utc_to_tai(utc_time t); + + template + static + utc_time::type> + tai_to_utc(std::chrono::time_point t); +}; + +template + using tai_time = std::chrono::time_point; + +using tai_seconds = tai_time; + +template +tai_time::type> +tai_clock::utc_to_tai(utc_time t) +{ + using namespace std::chrono; + using duration = typename std::common_type::type; + return tai_time{t.time_since_epoch()} + + (sys_days{1970_y/jan/1} - sys_days{1958_y/jan/1} + 10s); +} + +template +utc_time::type> +tai_clock::tai_to_utc(tai_time t) +{ + using namespace std::chrono; + using duration = typename std::common_type::type; + return utc_time{t.time_since_epoch()} - + (sys_days{1970_y/jan/1} - sys_days{1958_y/jan/1} + 10s); +} + +template +inline +utc_time::type> +to_utc_time(tai_time t) +{ + return tai_clock::tai_to_utc(t); +} + +template +inline +tai_time::type> +to_tai_time(utc_time t) +{ + return tai_clock::utc_to_tai(t); +} + +inline +tai_clock::time_point +tai_clock::now() noexcept +{ + using namespace std::chrono; + return to_tai_time(utc_clock::now()); +} + +template +std::basic_ostream& +operator<<(std::basic_ostream& os, const tai_time& t) +{ + using namespace std::chrono; + using duration = typename std::common_type::type; + auto tp = sys_time{t.time_since_epoch()} - + (sys_days{1970_y/jan/1} - sys_days{1958_y/jan/1}); + return os << tp; +} + +// gps_clock + +class gps_clock +{ +public: + using duration = std::chrono::system_clock::duration; + using rep = duration::rep; + using period = duration::period; + using time_point = std::chrono::time_point; + static const bool is_steady = true; + + static time_point now() noexcept; + + template + static + std::chrono::time_point::type> + utc_to_gps(utc_time t); + + template + static + utc_time::type> + gps_to_utc(std::chrono::time_point t); +}; + +template + using gps_time = std::chrono::time_point; + +using gps_seconds = gps_time; + +template +gps_time::type> +gps_clock::utc_to_gps(utc_time t) +{ + using namespace std::chrono; + using duration = typename std::common_type::type; + return gps_time{t.time_since_epoch()} - + (sys_days{1980_y/jan/6} - sys_days{1970_y/jan/1} + 9s); +} + +template +utc_time::type> +gps_clock::gps_to_utc(gps_time t) +{ + using namespace std::chrono; + using duration = typename std::common_type::type; + return utc_time{t.time_since_epoch()} + + (sys_days{1980_y/jan/6} - sys_days{1970_y/jan/1} + 9s); +} + +template +inline +utc_time::type> +to_utc_time(gps_time t) +{ + return gps_clock::gps_to_utc(t); +} + +template +inline +gps_time::type> +to_gps_time(utc_time t) +{ + return gps_clock::utc_to_gps(t); +} + +inline +gps_clock::time_point +gps_clock::now() noexcept +{ + using namespace std::chrono; + return to_gps_time(utc_clock::now()); +} + +template +std::basic_ostream& +operator<<(std::basic_ostream& os, const gps_time& t) +{ + using namespace std::chrono; + using duration = typename std::common_type::type; + auto tp = sys_time{t.time_since_epoch()} + + (sys_days{1980_y/jan/6} - sys_days{1970_y/jan/1}); + return os << tp; +} + // format namespace detail