time/ISO8601: implement with strptime(), without ParseTimePoint()

Prepare for adding more flexible parsing.
This commit is contained in:
Max Kellermann 2019-08-19 22:26:43 +02:00
parent 06dac4783f
commit 48b122f2ed
1 changed files with 18 additions and 2 deletions

View File

@ -32,9 +32,12 @@
#include "ISO8601.hxx" #include "ISO8601.hxx"
#include "Convert.hxx" #include "Convert.hxx"
#include "Parser.hxx"
#include "util/StringBuffer.hxx" #include "util/StringBuffer.hxx"
#include <stdexcept>
#include <assert.h>
StringBuffer<64> StringBuffer<64>
FormatISO8601(const struct tm &tm) noexcept FormatISO8601(const struct tm &tm) noexcept
{ {
@ -58,5 +61,18 @@ FormatISO8601(std::chrono::system_clock::time_point tp)
std::chrono::system_clock::time_point std::chrono::system_clock::time_point
ParseISO8601(const char *s) ParseISO8601(const char *s)
{ {
return ParseTimePoint(s, "%FT%TZ"); assert(s != nullptr);
#ifdef _WIN32
/* TODO: emulate strptime()? */
(void)s;
throw std::runtime_error("Time parsing not implemented on Windows");
#else
struct tm tm{};
const char *end = strptime(s, "%FT%TZ", &tm);
if (end == nullptr || *end != 0)
throw std::runtime_error("Failed to parse time stamp");
return TimeGm(tm);
#endif /* !_WIN32 */
} }