diff options
author | Max Kellermann <max@musicpd.org> | 2019-12-23 17:06:15 +0100 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2019-12-24 10:15:01 +0100 |
commit | 2bc127bb4336d1de047cca57b07865ed1e53f967 (patch) | |
tree | bea1845602e2d68dac7170bdcb3914d50bb113e8 /src | |
parent | 7770298a65e4c08eea1645abb121731867414488 (diff) |
time/ISO8601: move code to ParseTimeOfDay()
Diffstat (limited to 'src')
-rw-r--r-- | src/time/ISO8601.cxx | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/src/time/ISO8601.cxx b/src/time/ISO8601.cxx index 5cb4c486c..42bba021f 100644 --- a/src/time/ISO8601.cxx +++ b/src/time/ISO8601.cxx @@ -108,6 +108,30 @@ ParseTimeZoneOffset(const char *&s) return d; } +static const char * +ParseTimeOfDay(const char *s, struct tm &tm, + std::chrono::system_clock::duration &precision) noexcept +{ + const char *end; + + if ((end = strptime(s, "%T", &tm)) != nullptr) + precision = std::chrono::seconds(1); + else if ((end = strptime(s, "%H%M%S", &tm)) != nullptr) + /* no field separators */ + precision = std::chrono::seconds(1); + else if ((end = strptime(s, "%H%M", &tm)) != nullptr) + /* no field separators */ + precision = std::chrono::minutes(1); + else if ((end = strptime(s, "%H:%M", &tm)) != nullptr) + precision = std::chrono::minutes(1); + else if ((end = strptime(s, "%H", &tm)) != nullptr) + precision = std::chrono::hours(1); + else + return nullptr; + + return end; +} + std::pair<std::chrono::system_clock::time_point, std::chrono::system_clock::duration> ParseISO8601(const char *s) @@ -138,22 +162,9 @@ ParseISO8601(const char *s) if (*s == 'T') { ++s; - if ((end = strptime(s, "%T", &tm)) != nullptr) - precision = std::chrono::seconds(1); - else if ((end = strptime(s, "%H%M%S", &tm)) != nullptr) - /* no field separators */ - precision = std::chrono::seconds(1); - else if ((end = strptime(s, "%H%M", &tm)) != nullptr) - /* no field separators */ - precision = std::chrono::minutes(1); - else if ((end = strptime(s, "%H:%M", &tm)) != nullptr) - precision = std::chrono::minutes(1); - else if ((end = strptime(s, "%H", &tm)) != nullptr) - precision = std::chrono::hours(1); - else + s = ParseTimeOfDay(s, tm, precision); + if (s == nullptr) throw std::runtime_error("Failed to parse time of day"); - - s = end; } auto tp = TimeGm(tm); |