1 #include "../chrono/datetime.h" 2 #include "../chrono/timespan.h" 3 #include "../chrono/period.h" 4 #include "../chrono/format.h" 5 #include "../conversion/conversionexception.h" 7 #include <cppunit/extensions/HelperMacros.h> 8 #include <cppunit/TestFixture.h> 24 CPPUNIT_TEST(testDateTime);
25 CPPUNIT_TEST(testTimeSpan);
26 CPPUNIT_TEST(testOperators);
27 CPPUNIT_TEST_SUITE_END();
46 const auto test1 = DateTime::fromDateAndTime(2012, 2, 29, 15, 34, 20, 33.0);
47 CPPUNIT_ASSERT_EQUAL(2012, test1.year());
48 CPPUNIT_ASSERT_EQUAL(2, test1.month());
49 CPPUNIT_ASSERT_EQUAL(29, test1.day());
50 CPPUNIT_ASSERT_EQUAL(34, test1.minute());
51 CPPUNIT_ASSERT_EQUAL(20, test1.second());
52 CPPUNIT_ASSERT_EQUAL(33, test1.millisecond());
53 CPPUNIT_ASSERT(test1.dayOfWeek() == DayOfWeek::Wednesday);
54 CPPUNIT_ASSERT_EQUAL((31 + 29), test1.dayOfYear());
55 CPPUNIT_ASSERT(test1.isLeapYear());
56 CPPUNIT_ASSERT_EQUAL(
"Wed 2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
59 const auto test2 = DateTime::fromTimeStampGmt(1453840331);
60 CPPUNIT_ASSERT_EQUAL(
"Tue 2016-01-26 20:32:11"s, test2.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
63 CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2013, 2, 29, 15, 34, 20, 33),
ConversionException);
64 CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 2, 29, 15, 61, 20, 33),
ConversionException);
65 CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 4, 31, 15, 0, 20, 33),
ConversionException);
66 CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 3, 31, 15, 0, 61, 33),
ConversionException);
69 CPPUNIT_ASSERT_EQUAL(test1, DateTime::fromString(
"2012-02-29 15:34:20.033"));
70 CPPUNIT_ASSERT_EQUAL(
"2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateAndTime,
false));
71 CPPUNIT_ASSERT_THROW(TimeSpan::fromString(
"2012-02-29 15:34:34:20.033"),
ConversionException);
72 const auto test3 = DateTime::fromIsoString(
"2016-08-29T21:32:31.125+02:00");
73 CPPUNIT_ASSERT_EQUAL(
"2016-08-29T21:32:31.125+02:00"s, test3.first.toIsoString(test3.second));
82 auto test1 = TimeSpan::fromString(
"2:34:53:2.5");
84 CPPUNIT_ASSERT_EQUAL(3, test1.days());
85 CPPUNIT_ASSERT_EQUAL(10, test1.hours());
86 CPPUNIT_ASSERT_EQUAL(53, test1.minutes());
87 CPPUNIT_ASSERT_EQUAL(2, test1.seconds());
88 CPPUNIT_ASSERT_EQUAL(500, test1.milliseconds());
89 CPPUNIT_ASSERT(test1.totalDays() > 3.0 && test1.totalDays() < 4.0);
90 CPPUNIT_ASSERT(test1.totalHours() > (2 * 24 + 34) && test1.totalHours() < (2 * 24 + 35));
91 CPPUNIT_ASSERT(test1.totalMinutes() > (2 * 24 * 60 + 34 * 60 + 53) && test1.totalHours() < (2 * 24 * 60 + 34 * 60 + 54));
92 CPPUNIT_ASSERT_EQUAL(
"3 d 10 h 53 min 2 s 500 ms"s, test1.toString(TimeSpanOutputFormat::WithMeasures,
false));
103 auto dateTime = DateTime::fromDateAndTime(1999, 1, 5, 4, 16);
104 CPPUNIT_ASSERT_EQUAL(7, (dateTime + TimeSpan::fromDays(2)).day());
105 CPPUNIT_ASSERT_EQUAL(6, (dateTime + TimeSpan::fromHours(24)).day());
106 CPPUNIT_ASSERT_EQUAL(3, (dateTime + TimeSpan::fromHours(24) + TimeSpan::fromHours(-1)).hour());
107 CPPUNIT_ASSERT_EQUAL(17, (dateTime + TimeSpan::fromHours(24) - TimeSpan::fromMinutes(-1)).minute());
108 dateTime += TimeSpan::fromDays(365);
109 CPPUNIT_ASSERT_EQUAL(2000, dateTime.year());
110 CPPUNIT_ASSERT_EQUAL(5, dateTime.day());
111 CPPUNIT_ASSERT_EQUAL(2,
Period(dateTime, dateTime + TimeSpan::fromDays(62)).months());
CPPUNIT_TEST_SUITE_REGISTRATION(ChronoTests)
Contains classes providing a means for handling date and time information.
The ConversionException class is thrown by the various conversion functions of this library when a co...
void testOperators()
Tests operators of DateTime / TimeSpan.
void testDateTime()
Tests most important DateTime features.
void testTimeSpan()
Tests most important TimeSpan features.
Represents a period of time.
The ChronoTests class tests classes and methods of the ChronoUtilities namespace. ...
Contains several functions providing conversions between different data types.