Fix parsing ISO timestamp with negative timezone offset and no seconds fraction

See https://github.com/Martchus/syncthingtray/issues/93
This commit is contained in:
Martchus 2021-05-21 19:40:12 +02:00
parent 63955ffcc5
commit 546b1fecb7
2 changed files with 7 additions and 3 deletions

View File

@ -146,7 +146,8 @@ std::pair<DateTime, TimeSpan> DateTime::fromIsoString(const char *str)
} else if (c == '-') {
if (valueIndex < dayIndex) {
++valueIndex;
} else if (++valueIndex == deltaHourIndex) {
} else if (++valueIndex >= secondsIndex) {
valueIndex = deltaHourIndex;
deltaNegative = true;
} else {
throw ConversionException("unexpected \"-\" after day");

View File

@ -177,8 +177,11 @@ void ChronoTests::testDateTime()
CPPUNIT_ASSERT_EQUAL_MESSAGE("Zulu time", TimeSpan(), DateTime::fromIsoString("2017-08-23T19:40:15.985077682Z").second);
CPPUNIT_ASSERT_EQUAL_MESSAGE("no minutes", TimeSpan::fromHours(3), DateTime::fromIsoString("2017-08-23T19:40:15.985077682+03").second);
const auto test6 = DateTime::fromIsoString("1970-01-01T01:02:03+01:00");
CPPUNIT_ASSERT_EQUAL_MESSAGE("no seconds fraction", DateTime::fromDateAndTime(1970, 1, 1, 1, 2, 3), test6.first);
CPPUNIT_ASSERT_EQUAL_MESSAGE("no seconds fraction", TimeSpan::fromHours(1.0), test6.second);
CPPUNIT_ASSERT_EQUAL_MESSAGE("no seconds fraction (positive timezone offset, 1)", DateTime::fromDateAndTime(1970, 1, 1, 1, 2, 3), test6.first);
CPPUNIT_ASSERT_EQUAL_MESSAGE("no seconds fraction (positive timezone offset, 2)", TimeSpan::fromHours(1.0), test6.second);
const auto test7 = DateTime::fromIsoString("2021-05-20T23:02:45-04:00");
CPPUNIT_ASSERT_EQUAL_MESSAGE("no seconds fraction (negative timezone offset, 1)", DateTime::fromDateAndTime(2021, 5, 20, 23, 2, 45), test7.first);
CPPUNIT_ASSERT_EQUAL_MESSAGE("no seconds fraction (negative timezone offset, 2)", TimeSpan::fromHours(-4.0), test7.second);
// test invalid characters
CPPUNIT_ASSERT_THROW_MESSAGE("digits after Z", DateTime::fromIsoString("2017-O8-23T19:40:15.985077682Z02:00"), ConversionException);
CPPUNIT_ASSERT_THROW_MESSAGE("invalid letter", DateTime::fromIsoString("2017-O8-23T19:40:15.985077682:+02:00"), ConversionException);