diff options
author | Max Kellermann <mk@cm4all.com> | 2020-11-23 19:36:07 +0100 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2020-11-30 21:55:12 +0100 |
commit | 0c28d8dcbe6fb227f5cb59c75171e9525ac8efa7 (patch) | |
tree | 77990abcaf97f8f0b67d3bc83bf4f474938b623f /src | |
parent | 764eaadd25fb5b29b3cc3af64730f6d91912bd10 (diff) |
time/ISO8601: support YYYY-MM (without day of month)
Diffstat (limited to 'src')
-rw-r--r-- | src/time/ISO8601.cxx | 19 | ||||
-rw-r--r-- | src/time/ISO8601.hxx | 2 |
2 files changed, 19 insertions, 2 deletions
diff --git a/src/time/ISO8601.cxx b/src/time/ISO8601.cxx index 067e4c4ee..07ff3de81 100644 --- a/src/time/ISO8601.cxx +++ b/src/time/ISO8601.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2007-2019 Content Management AG + * Copyright 2007-2020 CM4all GmbH * All rights reserved. * * author: Max Kellermann <mk@cm4all.com> @@ -32,6 +32,7 @@ #include "ISO8601.hxx" #include "Convert.hxx" +#include "Math.hxx" #include "util/StringBuffer.hxx" #include <cassert> @@ -169,6 +170,13 @@ ParseTimeOfDay(const char *s, struct tm &tm, return end; } +static bool +StrptimeFull(const char *s, const char *fmt, struct tm *tm) noexcept +{ + const char *end = strptime(s, fmt, tm); + return end != nullptr && *end == 0; +} + #endif std::pair<std::chrono::system_clock::time_point, @@ -184,6 +192,15 @@ ParseISO8601(const char *s) #else struct tm tm{}; + if (StrptimeFull(s, "%Y-%m", &tm)) { + /* full month */ + tm.tm_mday = 1; + const auto start = TimeGm(tm); + EndOfMonth(tm); + const auto end = TimeGm(tm); + return {start, end - start}; + } + /* parse the date */ const char *end = strptime(s, "%F", &tm); if (end == nullptr) { diff --git a/src/time/ISO8601.hxx b/src/time/ISO8601.hxx index ceb97bfee..94633386d 100644 --- a/src/time/ISO8601.hxx +++ b/src/time/ISO8601.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2007-2019 Content Management AG + * Copyright 2007-2020 CM4all GmbH * All rights reserved. * * author: Max Kellermann <mk@cm4all.com> |