From 98edb5a67c4463b2befd5d0eafcdcb8d332be79b Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 1 Nov 2018 20:20:32 +0100 Subject: [PATCH] Allow omitting second fraction in DateTime::fromIsoString --- chrono/datetime.cpp | 7 ++++--- tests/chronotests.cpp | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/chrono/datetime.cpp b/chrono/datetime.cpp index 9f9430d..f2b9913 100644 --- a/chrono/datetime.cpp +++ b/chrono/datetime.cpp @@ -171,10 +171,11 @@ std::pair DateTime::fromIsoString(const char *str) } else { ++valueIndex; } - } else if ((c == '+') && (++valueIndex == deltaHourIndex)) { + } else if ((c == '+') && (++valueIndex >= secondsIndex)) { + valueIndex = deltaHourIndex; deltaNegative = false; - } else if ((c == 'Z') && (++valueIndex == deltaHourIndex)) { - valueIndex += 2; + } else if ((c == 'Z') && (++valueIndex >= secondsIndex)) { + valueIndex = deltaHourIndex + 2; } else if (c == '\0') { break; } else { diff --git a/tests/chronotests.cpp b/tests/chronotests.cpp index 138007c..727ff09 100644 --- a/tests/chronotests.cpp +++ b/tests/chronotests.cpp @@ -159,6 +159,9 @@ void ChronoTests::testDateTime() // test further variants 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); // 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);