From 9076a13c78cf64a6afafe98817aed31feda568b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 1 Mar 2020 21:54:33 +0100 Subject: Date and time record item type --- src/storage.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src') diff --git a/src/storage.cpp b/src/storage.cpp index e0819d4..525d83d 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -536,6 +536,34 @@ const Storage & Ref::storage() const } +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(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()); +} + + UUID::UUID(string str) { if (uuid_parse(str.c_str(), uuid) != 0) @@ -590,6 +618,14 @@ optional> RecordT::Item::asBinary() const return nullopt; } +template +optional RecordT::Item::asDate() const +{ + if (holds_alternative(value)) + return std::get(value); + return nullopt; +} + template optional RecordT::Item::asUUID() const { @@ -655,6 +691,8 @@ optional> RecordT::decode(const S & st, items->emplace_back(name, value); else if (type == "b") items->emplace_back(name, base64::decode(value)); + else if (type == "d") + items->emplace_back(name, ZonedTime(value)); else if (type == "u") items->emplace_back(name, UUID(value)); else if (type == "r.b2") { @@ -736,6 +774,9 @@ vector RecordT::encodeInner() const } else if (auto x = item.asBinary()) { type = "b"; value = base64::encode(*x); + } else if (auto x = item.asDate()) { + type = "d"; + value = string(*x); } else if (auto x = item.asUUID()) { type = "u"; value = string(*x); -- cgit v1.2.3