time/ISO8601: allow omitting the time of day

This commit is contained in:
Max Kellermann
2019-08-19 22:30:31 +02:00
parent bbe403f141
commit ba4cd47fd8
3 changed files with 30 additions and 4 deletions

2
NEWS
View File

@@ -1,4 +1,6 @@
ver 0.21.17 (not yet released) ver 0.21.17 (not yet released)
* protocol
- relax the ISO 8601 parser: allow omitting the time of day
* archive * archive
- zzip: improve error reporting - zzip: improve error reporting
* outputs * outputs

View File

@@ -70,14 +70,32 @@ ParseISO8601(const char *s)
throw std::runtime_error("Time parsing not implemented on Windows"); throw std::runtime_error("Time parsing not implemented on Windows");
#else #else
struct tm tm{}; 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");
std::chrono::system_clock::duration precision = std::chrono::seconds(1); /* parse the date */
const char *end = strptime(s, "%F", &tm);
if (end == nullptr)
throw std::runtime_error("Failed to parse date");
s = end;
std::chrono::system_clock::duration precision = std::chrono::hours(24);
/* parse the time of day */
if (*s == 'T') {
++s;
end = strptime(s, "%TZ", &tm);
if (end == nullptr)
throw std::runtime_error("Failed to parse time of day");
s = end;
precision = std::chrono::seconds(1);
}
auto tp = TimeGm(tm); auto tp = TimeGm(tm);
if (*s != 0)
throw std::runtime_error("Garbage at end of time stamp");
return std::make_pair(tp, precision); return std::make_pair(tp, precision);
#endif /* !_WIN32 */ #endif /* !_WIN32 */
} }

View File

@@ -45,6 +45,12 @@ static constexpr struct {
{ "2019-02-04T16:46:41Z", 1549298801, std::chrono::seconds(1) }, { "2019-02-04T16:46:41Z", 1549298801, std::chrono::seconds(1) },
{ "2018-12-31T23:59:59Z", 1546300799, std::chrono::seconds(1) }, { "2018-12-31T23:59:59Z", 1546300799, std::chrono::seconds(1) },
{ "2019-01-01T00:00:00Z", 1546300800, std::chrono::seconds(1) }, { "2019-01-01T00:00:00Z", 1546300800, std::chrono::seconds(1) },
/* only date */
{ "1970-01-01", 0, std::chrono::hours(24) },
{ "2019-02-04", 1549238400, std::chrono::hours(24) },
{ "2018-12-31", 1546214400, std::chrono::hours(24) },
{ "2019-01-01", 1546300800, std::chrono::hours(24) },
}; };
TEST(ISO8601, Parse) TEST(ISO8601, Parse)