time/ISO8601: support omitting field separators
Closes https://github.com/MusicPlayerDaemon/MPD/issues/685
This commit is contained in:
parent
b7744be208
commit
15ce8eb487
4
NEWS
4
NEWS
@ -1,7 +1,7 @@
|
|||||||
ver 0.21.17 (not yet released)
|
ver 0.21.17 (not yet released)
|
||||||
* protocol
|
* protocol
|
||||||
- relax the ISO 8601 parser: allow omitting the time of day and the "Z"
|
- relax the ISO 8601 parser: allow omitting field separators, the
|
||||||
suffix
|
time of day and the "Z" suffix
|
||||||
* archive
|
* archive
|
||||||
- zzip: improve error reporting
|
- zzip: improve error reporting
|
||||||
* outputs
|
* outputs
|
||||||
|
@ -123,8 +123,12 @@ ParseISO8601(const char *s)
|
|||||||
|
|
||||||
/* parse the date */
|
/* parse the date */
|
||||||
const char *end = strptime(s, "%F", &tm);
|
const char *end = strptime(s, "%F", &tm);
|
||||||
|
if (end == nullptr) {
|
||||||
|
/* try without field separators */
|
||||||
|
end = strptime(s, "%Y%m%d", &tm);
|
||||||
if (end == nullptr)
|
if (end == nullptr)
|
||||||
throw std::runtime_error("Failed to parse date");
|
throw std::runtime_error("Failed to parse date");
|
||||||
|
}
|
||||||
|
|
||||||
s = end;
|
s = end;
|
||||||
|
|
||||||
@ -136,6 +140,12 @@ ParseISO8601(const char *s)
|
|||||||
|
|
||||||
if ((end = strptime(s, "%T", &tm)) != nullptr)
|
if ((end = strptime(s, "%T", &tm)) != nullptr)
|
||||||
precision = std::chrono::seconds(1);
|
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)
|
else if ((end = strptime(s, "%H:%M", &tm)) != nullptr)
|
||||||
precision = std::chrono::minutes(1);
|
precision = std::chrono::minutes(1);
|
||||||
else if ((end = strptime(s, "%H", &tm)) != nullptr)
|
else if ((end = strptime(s, "%H", &tm)) != nullptr)
|
||||||
|
@ -71,6 +71,15 @@ static constexpr struct {
|
|||||||
{ "2019-02-04T16:46:41+0200", 1549291601, std::chrono::seconds(1) },
|
{ "2019-02-04T16:46:41+0200", 1549291601, std::chrono::seconds(1) },
|
||||||
{ "2019-02-04T16:46:41+02:00", 1549291601, std::chrono::seconds(1) },
|
{ "2019-02-04T16:46:41+02:00", 1549291601, std::chrono::seconds(1) },
|
||||||
{ "2019-02-04T16:46:41-0200", 1549306001, std::chrono::seconds(1) },
|
{ "2019-02-04T16:46:41-0200", 1549306001, std::chrono::seconds(1) },
|
||||||
|
|
||||||
|
/* without field separators */
|
||||||
|
{ "19700101T000000Z", 0, std::chrono::seconds(1) },
|
||||||
|
{ "19700101T000001Z", 1, std::chrono::seconds(1) },
|
||||||
|
{ "20190204T164641Z", 1549298801, std::chrono::seconds(1) },
|
||||||
|
{ "19700101", 0, std::chrono::hours(24) },
|
||||||
|
{ "20190204", 1549238400, std::chrono::hours(24) },
|
||||||
|
{ "20190204T1646", 1549298760, std::chrono::minutes(1) },
|
||||||
|
{ "20190204T16", 1549296000, std::chrono::hours(1) },
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST(ISO8601, Parse)
|
TEST(ISO8601, Parse)
|
||||||
|
Loading…
Reference in New Issue
Block a user