From 73470d0b65e505533b2191f0c6459b8c022d5bb9 Mon Sep 17 00:00:00 2001 From: Martchus Date: Fri, 24 Apr 2020 21:55:45 +0200 Subject: [PATCH] Fix DateTime::fromDateAndTime() for the day 0001-01-01 --- chrono/datetime.h | 5 +---- tests/chronotests.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/chrono/datetime.h b/chrono/datetime.h index 09e8dcb..319f8e1 100644 --- a/chrono/datetime.h +++ b/chrono/datetime.h @@ -174,10 +174,7 @@ inline DateTime DateTime::fromTime(int hour, int minute, int second, double mill */ inline DateTime DateTime::fromDateAndTime(int year, int month, int day, int hour, int minute, int second, double millisecond) { - if (std::uint64_t ticks = dateToTicks(year, month, day)) { - return DateTime(ticks + timeToTicks(hour, minute, second, millisecond)); - } - return DateTime(); + return DateTime(dateToTicks(year, month, day) + timeToTicks(hour, minute, second, millisecond)); } /*! diff --git a/tests/chronotests.cpp b/tests/chronotests.cpp index 528d2ea..df09521 100644 --- a/tests/chronotests.cpp +++ b/tests/chronotests.cpp @@ -114,6 +114,14 @@ void ChronoTests::testDateTime() CPPUNIT_ASSERT(test1.isSameDay(test1 + TimeSpan::fromHours(8))); CPPUNIT_ASSERT(!test1.isSameDay(test1 + TimeSpan::fromHours(9))); CPPUNIT_ASSERT_EQUAL("Wed 2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateTimeAndShortWeekday)); + const auto test2 = DateTime::fromDateAndTime(1, 1, 1, 15, 34, 20, 33.0); + CPPUNIT_ASSERT_EQUAL(1, test2.year()); + CPPUNIT_ASSERT_EQUAL(1, test2.month()); + CPPUNIT_ASSERT_EQUAL(1, test2.day()); + CPPUNIT_ASSERT_EQUAL(15, test2.hour()); + CPPUNIT_ASSERT_EQUAL(34, test2.minute()); + CPPUNIT_ASSERT_EQUAL(20, test2.second()); + CPPUNIT_ASSERT_EQUAL(33, test2.millisecond()); // test fromTimeStamp()/toTimeStamp() const auto timeStamp = static_cast(1453840331);