1 #include "../chrono/datetime.h" 2 #include "../chrono/format.h" 3 #include "../chrono/period.h" 4 #include "../chrono/timespan.h" 5 #include "../conversion/conversionexception.h" 6 #include "../tests/testutils.h" 8 #include <cppunit/TestFixture.h> 9 #include <cppunit/extensions/HelperMacros.h> 29 CPPUNIT_TEST(testDateTime);
30 CPPUNIT_TEST(testTimeSpan);
31 CPPUNIT_TEST(testOperators);
32 CPPUNIT_TEST(testPeriod);
33 CPPUNIT_TEST(testHashing);
34 CPPUNIT_TEST_SUITE_END();
59 const auto test1 = DateTime::fromDateAndTime(2012, 2, 29, 15, 34, 20, 33.0);
60 CPPUNIT_ASSERT_EQUAL(2012, test1.year());
61 CPPUNIT_ASSERT_EQUAL(2, test1.month());
62 CPPUNIT_ASSERT_EQUAL(29, test1.day());
63 CPPUNIT_ASSERT_EQUAL(34, test1.minute());
64 CPPUNIT_ASSERT_EQUAL(20, test1.second());
65 CPPUNIT_ASSERT_EQUAL(33, test1.millisecond());
66 CPPUNIT_ASSERT(test1.dayOfWeek() == DayOfWeek::Wednesday);
67 CPPUNIT_ASSERT_EQUAL((31 + 29), test1.dayOfYear());
68 CPPUNIT_ASSERT(test1.isLeapYear());
69 CPPUNIT_ASSERT_EQUAL(
"Wed 2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
72 const auto fromTimeStampGmt = DateTime::fromTimeStampGmt(1453840331), fromTimeStamp = DateTime::fromTimeStamp(1453840331);
73 CPPUNIT_ASSERT_EQUAL(
"Tue 2016-01-26 20:32:11"s, fromTimeStampGmt.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
74 CPPUNIT_ASSERT(fabs((fromTimeStamp - fromTimeStampGmt).totalDays()) <= 1.0);
75 CPPUNIT_ASSERT_EQUAL(
DateTime(), DateTime::fromTimeStamp(0));
80 CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(0, 2, 29, 15, 34, 20, 33),
ConversionException);
81 CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2013, 2, 29, 15, 34, 20, 33),
ConversionException);
82 CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 2, 29, 15, 61, 20, 33),
ConversionException);
83 CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 4, 31, 15, 0, 20, 33),
ConversionException);
84 CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 3, 31, 15, 0, 61, 33),
ConversionException);
86 CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 1, 1, 15, 2, 1, 2000.0),
ConversionException);
89 CPPUNIT_ASSERT_EQUAL(test1, DateTime::fromString(
"2012-02-29 15:34:20.033"));
90 CPPUNIT_ASSERT_EQUAL(
"2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateAndTime,
false));
91 CPPUNIT_ASSERT_THROW(TimeSpan::fromString(
"2012-02-29 15:34:34:20.033"),
ConversionException);
92 const auto test3 = DateTime::fromIsoString(
"2016-08-29T21:32:31.125+02:00");
93 CPPUNIT_ASSERT_EQUAL(
"2016-08-29T21:32:31.125+02:00"s, test3.first.toIsoString(test3.second));
96 const auto test4 = DateTime::fromIsoString(
"2017-08-23T19:40:15.985077682+02:00");
97 CPPUNIT_ASSERT_EQUAL(15, test4.first.second());
98 CPPUNIT_ASSERT_EQUAL(985, test4.first.millisecond());
99 CPPUNIT_ASSERT_EQUAL(77, test4.first.microsecond());
100 CPPUNIT_ASSERT_EQUAL(600, test4.first.nanosecond());
101 CPPUNIT_ASSERT_EQUAL(
"2017-08-23T19:40:15.9850776+02:00"s, test4.first.toIsoString(test4.second));
104 #if defined(PLATFORM_UNIX) 105 const auto delta = DateTime::gmtNow() - DateTime::exactGmtNow();
106 CPPUNIT_ASSERT(delta < TimeSpan::fromSeconds(2) && delta > TimeSpan::fromSeconds(-2));
116 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), TimeSpan::fromString(
string()));
117 CPPUNIT_ASSERT_EQUAL(TimeSpan::fromSeconds(5.0), TimeSpan::fromString(
"5.0"));
118 CPPUNIT_ASSERT_EQUAL(TimeSpan::fromMinutes(5.5), TimeSpan::fromString(
"5:30"));
119 CPPUNIT_ASSERT_EQUAL(TimeSpan::fromHours(7) + TimeSpan::fromMinutes(5.5), TimeSpan::fromString(
"7:5:30"));
120 const auto test1 = TimeSpan::fromString(
"2:34:53:2.5");
122 CPPUNIT_ASSERT_EQUAL(3, test1.days());
123 CPPUNIT_ASSERT_EQUAL(10, test1.hours());
124 CPPUNIT_ASSERT_EQUAL(53, test1.minutes());
125 CPPUNIT_ASSERT_EQUAL(2, test1.seconds());
126 CPPUNIT_ASSERT_EQUAL(500, test1.milliseconds());
127 CPPUNIT_ASSERT(test1.totalDays() > 3.0 && test1.totalDays() < 4.0);
128 CPPUNIT_ASSERT(test1.totalHours() > (2 * 24 + 34) && test1.totalHours() < (2 * 24 + 35));
129 CPPUNIT_ASSERT(test1.totalMinutes() > (2 * 24 * 60 + 34 * 60 + 53) && test1.totalHours() < (2 * 24 * 60 + 34 * 60 + 54));
131 CPPUNIT_ASSERT_EQUAL(
"3 d 10 h 53 min 2 s 500 ms"s, test1.toString(TimeSpanOutputFormat::WithMeasures,
false));
132 CPPUNIT_ASSERT_EQUAL(
"07:05:30"s, (TimeSpan::fromHours(7) + TimeSpan::fromMinutes(5.5)).toString());
133 CPPUNIT_ASSERT_EQUAL(
"-5 s"s, TimeSpan::fromSeconds(-5.0).toString(TimeSpanOutputFormat::WithMeasures,
false));
134 CPPUNIT_ASSERT_EQUAL(
"0 s"s,
TimeSpan().toString(TimeSpanOutputFormat::WithMeasures,
false));
135 CPPUNIT_ASSERT_EQUAL(
"5e+02 µs"s, TimeSpan::fromMilliseconds(0.5).toString(TimeSpanOutputFormat::WithMeasures,
false));
137 const auto test2 = TimeSpan::fromString(
"15.985077682");
138 CPPUNIT_ASSERT_EQUAL(15.9850776, test2.totalSeconds());
139 CPPUNIT_ASSERT_EQUAL(15, test2.seconds());
140 CPPUNIT_ASSERT_EQUAL(985, test2.milliseconds());
141 CPPUNIT_ASSERT_EQUAL(77, test2.microseconds());
142 CPPUNIT_ASSERT_EQUAL(600, test2.nanoseconds());
143 CPPUNIT_ASSERT_EQUAL(
"00:00:15.9850776"s, test2.toString());
144 CPPUNIT_ASSERT_EQUAL(
"15 s 985 ms 77 µs 600 ns"s, test2.toString(TimeSpanOutputFormat::WithMeasures));
145 CPPUNIT_ASSERT_EQUAL(
"15.9850776"s, test2.toString(TimeSpanOutputFormat::TotalSeconds));
156 auto dateTime = DateTime::fromDateAndTime(1999, 1, 5, 4, 16);
157 CPPUNIT_ASSERT_EQUAL(7, (dateTime + TimeSpan::fromDays(2)).day());
158 CPPUNIT_ASSERT_EQUAL(6, (dateTime + TimeSpan::fromHours(24)).day());
159 CPPUNIT_ASSERT_EQUAL(3, (dateTime + TimeSpan::fromHours(24) + TimeSpan::fromHours(-1)).hour());
160 CPPUNIT_ASSERT_EQUAL(17, (dateTime + TimeSpan::fromHours(24) - TimeSpan::fromMinutes(-1)).minute());
161 dateTime += TimeSpan::fromDays(365);
162 CPPUNIT_ASSERT_EQUAL(2000, dateTime.year());
163 CPPUNIT_ASSERT_EQUAL(5, dateTime.day());
171 const auto begin(DateTime::fromDateAndTime(1994, 7, 18, 15, 30, 21)), end(DateTime::fromDateAndTime(2017, 12, 2, 15, 30, 21));
172 const Period period(begin, end);
173 CPPUNIT_ASSERT_EQUAL(23, period.
years());
174 CPPUNIT_ASSERT_EQUAL(4, period.
months());
175 CPPUNIT_ASSERT_EQUAL(14, period.
days());
176 CPPUNIT_ASSERT_EQUAL(end.toString(), (begin + period).toString());
178 const auto end2(DateTime::fromDateAndTime(2018, 1, 2, 15, 30, 21));
179 const Period period2(begin, end2);
180 CPPUNIT_ASSERT_EQUAL(23, period2.
years());
181 CPPUNIT_ASSERT_EQUAL(5, period2.
months());
182 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"one more day, because December has 31 days", 15, period2.
days());
183 CPPUNIT_ASSERT_EQUAL(end2.toString(), (begin + period2).toString());
191 set<DateTime> dateTimes;
192 dateTimes.emplace(DateTime::fromDate(2500, 2, 1));
193 dateTimes.emplace(DateTime::fromDate(2500, 2, 2));
194 dateTimes.emplace(DateTime::fromDate(2500, 2, 1));
195 CPPUNIT_ASSERT_EQUAL(2_st, dateTimes.size());
197 set<TimeSpan> timeSpans;
198 timeSpans.emplace(TimeSpan::fromDays(5));
199 timeSpans.emplace(TimeSpan::fromDays(10));
200 timeSpans.emplace(TimeSpan::fromDays(5));
201 CPPUNIT_ASSERT_EQUAL(2_st, timeSpans.size());
CPPUNIT_TEST_SUITE_REGISTRATION(ChronoTests)
void testPeriod()
Tests Period.
Represents an instant in time, typically expressed as a date and time of day.
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...
Represents a time interval.
Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL.
void testHashing()
Tests hashing DateTime / TimeSpan by using in a set.
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.
int days() const
Returns the days component of the period represented by the current instance.
int years() const
Returns the years component of the period represented by the current instance.
int months() const
Returns the months component of the period represented by the current instance.