From ab5d2a5a88b9ddea73c6d341c77dac74b0d8b7eb Mon Sep 17 00:00:00 2001 From: Sergey Magidovich Date: Fri, 20 Nov 2015 14:45:18 +0300 Subject: [PATCH] Add time_t to w3c time format converter and from time_t to w3c time converter as well. --- base/base_tests/base_tests.pro | 1 + base/base_tests/w3ctime_test.cpp | 17 ++++++++++++ base/w3ctime.hpp | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 base/base_tests/w3ctime_test.cpp create mode 100644 base/w3ctime.hpp diff --git a/base/base_tests/base_tests.pro b/base/base_tests/base_tests.pro index a827fb1513..fa45f82abd 100644 --- a/base/base_tests/base_tests.pro +++ b/base/base_tests/base_tests.pro @@ -39,5 +39,6 @@ SOURCES += \ threads_test.cpp \ timer_test.cpp \ worker_thread_test.cpp \ + w3ctime_test.cpp \ HEADERS += diff --git a/base/base_tests/w3ctime_test.cpp b/base/base_tests/w3ctime_test.cpp new file mode 100644 index 0000000000..1689734aa9 --- /dev/null +++ b/base/base_tests/w3ctime_test.cpp @@ -0,0 +1,17 @@ +#include "base/w3ctime.hpp" +#include "testing/testing.hpp" + +using namespace w3ctime; + +UNIT_TEST(ParseTime) +{ + TEST_EQUAL(ParseTime(""), kNotATime, ()); + TEST_EQUAL(ParseTime("2015-10-11 23:21"), kNotATime, ()); + TEST_EQUAL(ParseTime("2015-10-11T23:21Z"), 1444605660, ()); +} + +UNIT_TEST(TimeToString) +{ + TEST_EQUAL(TimeToString(0), "1970-01-01T00:00Z", ()); + TEST_EQUAL(TimeToString(1444605660), "2015-10-11T23:21Z", ()); +} diff --git a/base/w3ctime.hpp b/base/w3ctime.hpp new file mode 100644 index 0000000000..5bc8ae7621 --- /dev/null +++ b/base/w3ctime.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include "std/array.hpp" +#include "std/ctime.hpp" +#include "std/string.hpp" + +// It would be better to use c++ standard means of +// time parsing and unparsing like +// time_get/time_put () or get_time/put_time (). +// But at the moment of writing they was not widely supporded +// and/or worked inkorekd. +// +// All covertions are made in UTC + +namespace w3ctime +{ +char constexpr kW3CTimeFormat[] = "%Y-%m-%dT%H:%MZ"; + +// Two more digits for year and one for \n +size_t constexpr kBufSize = sizeof(kW3CTimeFormat) + 2 + 1; + +time_t constexpr kNotATime = -1; + +inline time_t ParseTime(std::string const & w3ctime) noexcept +{ + std::tm tm{}; + if (strptime(w3ctime.data(), kW3CTimeFormat, &tm) == nullptr) + return kNotATime; + + return timegm(&tm); +} + +inline std::string TimeToString(time_t const timestamp) noexcept +{ + std::tm tm{}; + array buff{}; + + gmtime_r(×tamp, &tm); + strftime(buff.data(), kBufSize, kW3CTimeFormat, &tm); + + return buff.data(); +} + +} // namespace w3ctime