diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2020-03-18 22:53:40 +0100 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2020-03-23 21:37:30 +0100 |
commit | 627d135bf5108f514161e1d37acf6b97c4b3c4a3 (patch) | |
tree | 601b2560bb484aedbe51f25a6da3beeae62a78fc /src/time.cpp | |
parent | 29ade9784fe65ecd686b5e8e18d84e6acc30b37a (diff) |
Move UUID and time definitions to separate modules
Diffstat (limited to 'src/time.cpp')
-rw-r--r-- | src/time.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/time.cpp b/src/time.cpp new file mode 100644 index 0000000..631e0f8 --- /dev/null +++ b/src/time.cpp @@ -0,0 +1,35 @@ +#include <erebos/time.h> + +#include <stdexcept> + +using namespace erebos; + +using std::runtime_error; +using std::string; + +ZonedTime::ZonedTime(string str) +{ + intmax_t t; + unsigned int h, m; + char sign[2]; + if (sscanf(str.c_str(), "%jd %1[+-]%2u%2u", &t, sign, &h, &m) != 4) + throw runtime_error("invalid zoned time"); + + time = std::chrono::system_clock::time_point(std::chrono::seconds(t)); + zone = std::chrono::minutes((sign[0] == '-' ? -1 : 1) * (60 * h + m)); +} + +ZonedTime::operator string() const +{ + char buf[32]; + unsigned int az = std::chrono::abs(zone).count(); + snprintf(buf, sizeof(buf), "%jd %c%02u%02u", + (intmax_t) std::chrono::duration_cast<std::chrono::seconds>(time.time_since_epoch()).count(), + zone < decltype(zone)::zero() ? '-' : '+', az / 60, az % 60); + return string(buf); +} + +ZonedTime ZonedTime::now() +{ + return ZonedTime(std::chrono::system_clock::now()); +} |