From b16d51e7d108c50e960d4598cb7cd47854c76f3e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 19 Sep 2024 19:55:14 +0100 Subject: Add helper to quickly parse an ISO date/time --- lib/chronology.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 lib/chronology.cpp (limited to 'lib/chronology.cpp') diff --git a/lib/chronology.cpp b/lib/chronology.cpp new file mode 100644 index 0000000..8707bba --- /dev/null +++ b/lib/chronology.cpp @@ -0,0 +1,12 @@ +#include "chronology.h" + +time_t +operator""_time_t(const char * iso, size_t) +{ + struct tm tm {}; + + if (const auto end = strptime(iso, "%FT%T", &tm); !end || *end) { + throw std::invalid_argument("Invalid date"); + } + return mktime(&tm); +} -- cgit v1.2.3