summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--louloulibs/utils/time.cpp11
-rw-r--r--louloulibs/utils/time.hpp1
-rw-r--r--tests/utils.cpp8
3 files changed, 20 insertions, 0 deletions
diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp
index b85d764..305b2ad 100644
--- a/louloulibs/utils/time.cpp
+++ b/louloulibs/utils/time.cpp
@@ -1,4 +1,5 @@
#include <utils/time.hpp>
+#include <time.h>
namespace utils
{
@@ -10,4 +11,14 @@ std::string to_string(const std::time_t& timestamp)
return "";
return {std::begin(date_buf), std::end(date_buf) - 1};
}
+
+std::time_t parse_datetime(const std::string& stamp)
+{
+ struct tm tm;
+ if (!::strptime(stamp.data(), "%FT%T%z", &tm))
+ return -1;
+ auto res = ::timegm(&tm);
+ return res;
+}
+
} \ No newline at end of file
diff --git a/louloulibs/utils/time.hpp b/louloulibs/utils/time.hpp
index dff1250..c71cd9c 100644
--- a/louloulibs/utils/time.hpp
+++ b/louloulibs/utils/time.hpp
@@ -6,4 +6,5 @@
namespace utils
{
std::string to_string(const std::time_t& timestamp);
+std::time_t parse_datetime(const std::string& stamp);
} \ No newline at end of file
diff --git a/tests/utils.cpp b/tests/utils.cpp
index abe7ef7..084a048 100644
--- a/tests/utils.cpp
+++ b/tests/utils.cpp
@@ -121,3 +121,11 @@ TEST_CASE("time_to_string")
CHECK(utils::to_string(stamp).size() == result.size());
CHECK(utils::to_string(0) == "1970-01-01T00:00:00Z");
}
+
+TEST_CASE("parse_datetime")
+{
+ const auto time = utils::parse_datetime("1970-01-01T00:00:00Z");
+ CHECK(time == 0);
+ CHECK(utils::parse_datetime("2016-08-29T14:29:28Z") == 1472480968);
+ CHECK(utils::parse_datetime("blah") == -1);
+} \ No newline at end of file