1#define CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
3#include "../chrono/datetime.h"
4#include "../chrono/format.h"
5#include "../chrono/period.h"
6#include "../chrono/timespan.h"
7#include "../conversion/conversionexception.h"
8#include "../tests/testutils.h"
10#include <cppunit/TestFixture.h>
11#include <cppunit/extensions/HelperMacros.h>
21using namespace CPPUNIT_NS;
31static constexpr auto dateFromUnixEpoch(
33static_assert(dateFromUnixEpoch.dayOfWeek() == DayOfWeek::Thursday,
"dayOfWeek()");
34static_assert(dateFromUnixEpoch.hour() == 1,
"hour()");
35static_assert(dateFromUnixEpoch.minute() == 2,
"minute()");
36static_assert(dateFromUnixEpoch.second() == 3,
"second()");
37static_assert(dateFromUnixEpoch.millisecond() == 125,
"millisecond()");
38static_assert(dateFromUnixEpoch.microsecond() == 678,
"microsecond()");
39static_assert(dateFromUnixEpoch.nanosecond() == 900,
"nanosecond()");
78 CPPUNIT_TEST_SUITE_END();
108 CPPUNIT_ASSERT_EQUAL(2012, test1.year());
109 CPPUNIT_ASSERT_EQUAL(2, test1.month());
110 CPPUNIT_ASSERT_EQUAL(29, test1.day());
111 CPPUNIT_ASSERT_EQUAL(15, test1.hour());
112 CPPUNIT_ASSERT_EQUAL(34, test1.minute());
113 CPPUNIT_ASSERT_EQUAL(20, test1.second());
114 CPPUNIT_ASSERT_EQUAL(33, test1.millisecond());
115 CPPUNIT_ASSERT_EQUAL(DayOfWeek::Wednesday, test1.dayOfWeek());
116 CPPUNIT_ASSERT_EQUAL((31 + 29), test1.dayOfYear());
117 CPPUNIT_ASSERT(test1.isLeapYear());
120 CPPUNIT_ASSERT_EQUAL(
"Wed 2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
122 CPPUNIT_ASSERT_EQUAL(1, test2.year());
123 CPPUNIT_ASSERT_EQUAL(1, test2.month());
124 CPPUNIT_ASSERT_EQUAL(1, test2.day());
125 CPPUNIT_ASSERT_EQUAL(15, test2.hour());
126 CPPUNIT_ASSERT_EQUAL(34, test2.minute());
127 CPPUNIT_ASSERT_EQUAL(20, test2.second());
128 CPPUNIT_ASSERT_EQUAL(33, test2.millisecond());
131 const auto timeStamp =
static_cast<time_t
>(1453840331);
133 CPPUNIT_ASSERT_EQUAL(
"Tue 2016-01-26 20:32:11"s, fromTimeStampGmt.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
134 CPPUNIT_ASSERT(fabs((fromTimeStamp - fromTimeStampGmt).totalDays()) <= 1.0);
136 CPPUNIT_ASSERT_EQUAL(timeStamp, fromTimeStampGmt.toTimeStamp());
140 CPPUNIT_ASSERT_EQUAL(
"Tue 2016-01-26 20:32:11"s, fromChronoTimePointGmt.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
155 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"surplus parts ignored", test1,
DateTime::fromString(
"2012-02-29 15:34:20.033:12"));
156 CPPUNIT_ASSERT_EQUAL(
"2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateAndTime,
false));
159 CPPUNIT_ASSERT_EQUAL(
"2016-08-29T21:32:31.125+02:00"s, test3.first.toIsoString(test3.second));
163 CPPUNIT_ASSERT_EQUAL(2.5, test4.second.totalHours());
164 CPPUNIT_ASSERT_EQUAL(15, test4.first.second());
165 CPPUNIT_ASSERT_EQUAL(985, test4.first.millisecond());
166 CPPUNIT_ASSERT_EQUAL(77, test4.first.microsecond());
167 CPPUNIT_ASSERT_EQUAL(600, test4.first.nanosecond());
168 CPPUNIT_ASSERT_EQUAL(
"2017-08-23T19:40:15.9850776+02:30"s, test4.first.toIsoString(test4.second));
171 CPPUNIT_ASSERT_EQUAL(-2.5, test5.second.totalHours());
172 CPPUNIT_ASSERT_EQUAL(15, test5.first.second());
173 CPPUNIT_ASSERT_EQUAL(985, test5.first.millisecond());
174 CPPUNIT_ASSERT_EQUAL(77, test5.first.microsecond());
175 CPPUNIT_ASSERT_EQUAL(600, test5.first.nanosecond());
176 CPPUNIT_ASSERT_EQUAL(
"2017-08-23T19:40:15.9850776-02:30"s, test5.first.toIsoString(test5.second));
184 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"no seconds fraction (positive timezone offset, 1)",
DateTime::fromDateAndTime(1970, 1, 1, 1, 2, 3), test6.first);
185 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"no seconds fraction (positive timezone offset, 2)",
TimeSpan::fromHours(1.0), test6.second);
187 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"no seconds fraction (negative timezone offset, 1)",
DateTime::fromDateAndTime(2021, 5, 20, 23, 2, 45), test7.first);
188 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"no seconds fraction (negative timezone offset, 2)",
TimeSpan::fromHours(-4.0), test7.second);
190 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"no separators", test5.first - test5.second,
DateTime::fromIsoStringGmt(
"20170823T194015.985077682-0230"));
191 CPPUNIT_ASSERT_EQUAL_MESSAGE(
204 CPPUNIT_ASSERT_EQUAL(
"1234-05-06T07:08:09.0105005"s,
DateTime::fromDateAndTime(1234, 5, 6, 7, 8, 9, 10.5005).toString(DateTimeOutputFormat::Iso));
205 CPPUNIT_ASSERT_EQUAL(
"1234-05-06T07:08:09.0105005"s,
207 CPPUNIT_ASSERT_EQUAL(
"1234-05-06T07:08:09.010500"s,
209 CPPUNIT_ASSERT_EQUAL(
210 "1234-05-06T07:08:09.010"s,
DateTime::fromDateAndTime(1234, 5, 6, 7, 8, 9, 10).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
211 CPPUNIT_ASSERT_EQUAL(
212 "1234-05-06T07:08:09"s,
DateTime::fromDateAndTime(1234, 5, 6, 7, 8, 9).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
213 CPPUNIT_ASSERT_EQUAL(
214 "1234-05-06T07:08"s,
DateTime::fromDateAndTime(1234, 5, 6, 7, 8).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
215 CPPUNIT_ASSERT_EQUAL(
"1234-05-06T07"s,
DateTime::fromDateAndTime(1234, 5, 6, 7).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
216 CPPUNIT_ASSERT_EQUAL(
"1234-05-06"s,
DateTime::fromDateAndTime(1234, 5, 6).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
217 CPPUNIT_ASSERT_EQUAL(
"1234-05"s,
DateTime::fromDateAndTime(1234, 5).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
218 CPPUNIT_ASSERT_EQUAL(
"1234"s,
DateTime::fromDateAndTime(1234).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
219 CPPUNIT_ASSERT_EQUAL(
"0001"s,
DateTime().toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
222#if defined(PLATFORM_UNIX)
235 auto parts = DateTimeParts::Year;
236 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
237 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
238 CPPUNIT_ASSERT_EQUAL(parts, expr.parts);
239 CPPUNIT_ASSERT_EQUAL(
"0001"s, expr.toIsoString());
241 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
242 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
243 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Month, expr.parts);
244 CPPUNIT_ASSERT_EQUAL(
"0001-01"s, expr.toIsoString());
246 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
247 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
248 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Day, expr.parts);
249 CPPUNIT_ASSERT_EQUAL(
"0001-01-01"s, expr.toIsoString());
251 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
252 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
253 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Hour, expr.parts);
254 CPPUNIT_ASSERT_EQUAL(
"0001-01-01T00"s, expr.toIsoString());
256 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
257 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
258 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Minute, expr.parts);
259 CPPUNIT_ASSERT_EQUAL(
"0001-01-01T00:00"s, expr.toIsoString());
261 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
262 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
263 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Second, expr.parts);
264 CPPUNIT_ASSERT_EQUAL(
"0001-01-01T00:00:00"s, expr.toIsoString());
266 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
267 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
268 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::SubSecond, expr.parts);
269 CPPUNIT_ASSERT_EQUAL(
"0001-01-01T00:00:00.000"s, expr.toIsoString());
271 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
272 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
273 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::DeltaHour, expr.parts);
274 CPPUNIT_ASSERT_EQUAL(
"0001-01-01T00:00:00.000+00"s, expr.toIsoString());
276 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
277 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
278 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::DeltaMinute, expr.parts);
279 CPPUNIT_ASSERT_EQUAL(
"0001-01-01T00:00:00.000+00:00"s, expr.toIsoString());
286 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
287 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
288 CPPUNIT_ASSERT_EQUAL(parts = DateTimeParts::Year, expr.parts);
290 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
291 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
292 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Month, expr.parts);
294 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
295 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
296 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Day, expr.parts);
298 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
299 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
300 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Hour, expr.parts);
302 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
303 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
304 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Minute, expr.parts);
306 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
307 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
308 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Second, expr.parts);
310 CPPUNIT_ASSERT_EQUAL(
DateTime(), expr.value);
311 CPPUNIT_ASSERT_EQUAL(
TimeSpan(), expr.delta);
312 CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::SubSecond, expr.parts);
327 CPPUNIT_ASSERT_EQUAL(3, test1.days());
328 CPPUNIT_ASSERT_EQUAL(10, test1.hours());
329 CPPUNIT_ASSERT_EQUAL(53, test1.minutes());
330 CPPUNIT_ASSERT_EQUAL(2, test1.seconds());
331 CPPUNIT_ASSERT_EQUAL(500, test1.milliseconds());
332 CPPUNIT_ASSERT(test1.totalDays() > 3.0 && test1.totalDays() < 4.0);
333 CPPUNIT_ASSERT(test1.totalHours() > (2 * 24 + 34) && test1.totalHours() < (2 * 24 + 35));
334 CPPUNIT_ASSERT(test1.totalMinutes() > (2 * 24 * 60 + 34 * 60 + 53) && test1.totalHours() < (2 * 24 * 60 + 34 * 60 + 54));
336 CPPUNIT_ASSERT_EQUAL(
"3 d 10 h 53 min 2 s 500 ms"s, test1.toString(TimeSpanOutputFormat::WithMeasures,
false));
338 CPPUNIT_ASSERT_EQUAL(
"-5 s"s,
TimeSpan::fromSeconds(-5.0).toString(TimeSpanOutputFormat::WithMeasures,
false));
339 CPPUNIT_ASSERT_EQUAL(
"0 s"s,
TimeSpan().toString(TimeSpanOutputFormat::WithMeasures,
false));
343 CPPUNIT_ASSERT_EQUAL(15.9850776, test2.totalSeconds());
344 CPPUNIT_ASSERT_EQUAL(15, test2.seconds());
345 CPPUNIT_ASSERT_EQUAL(985, test2.milliseconds());
346 CPPUNIT_ASSERT_EQUAL(77, test2.microseconds());
347 CPPUNIT_ASSERT_EQUAL(600, test2.nanoseconds());
348 CPPUNIT_ASSERT_EQUAL(
"00:00:15.9850776"s, test2.toString());
349 CPPUNIT_ASSERT_EQUAL(
"15 s 985 ms 77 µs 600 ns"s, test2.toString(TimeSpanOutputFormat::WithMeasures));
350 CPPUNIT_ASSERT_EQUAL(
"15.9850776"s, test2.toString(TimeSpanOutputFormat::TotalSeconds));
367 CPPUNIT_ASSERT_EQUAL(2000, dateTime.year());
368 CPPUNIT_ASSERT_EQUAL(5, dateTime.day());
381 const auto begin(
DateTime::fromDateAndTime(1994, 7, 18, 15, 30, 21)), end(
DateTime::fromDateAndTime(2017, 12, 2, 15, 30, 21));
382 const Period period(begin, end);
383 CPPUNIT_ASSERT_EQUAL(23, period.
years());
384 CPPUNIT_ASSERT_EQUAL(4, period.
months());
385 CPPUNIT_ASSERT_EQUAL(14, period.
days());
386 CPPUNIT_ASSERT_EQUAL(end.toString(), (begin + period).toString());
389 const Period period2(begin, end2);
390 CPPUNIT_ASSERT_EQUAL(23, period2.
years());
391 CPPUNIT_ASSERT_EQUAL(5, period2.
months());
392 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"one more day, because December has 31 days", 15, period2.
days());
393 CPPUNIT_ASSERT_EQUAL(end2.toString(), (begin + period2).toString());
401 set<DateTime> dateTimes;
405 CPPUNIT_ASSERT_EQUAL(2_st, dateTimes.size());
407 set<TimeSpan> timeSpans;
411 CPPUNIT_ASSERT_EQUAL(2_st, timeSpans.size());
CPPUNIT_TEST_SUITE_REGISTRATION(ChronoTests)
The ChronoTests class tests classes and functions provided by the files inside the chrono directory.
void testTimeSpan()
Tests most important TimeSpan features.
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 testPeriod()
Tests Period.
void testDateTimeExpression()
Tests parsing a DateTimeExpression.
The ConversionException class is thrown by the various conversion functions of this library when a co...
Represents an instant in time, typically expressed as a date and time of day.
static constexpr DateTime fromTimeStampGmt(std::time_t timeStamp)
Constructs a new DateTime object with the GMT time from the specified UNIX timeStamp.
constexpr bool isNull() const
Returns true if the date represented by the current DateTime class is null.
static constexpr DateTime unixEpochStart()
Returns the DateTime object for the "1970-01-01T00:00:00Z".
static constexpr DateTime fromChronoTimePointGmt(TimePoint timePoint)
Constructs a new DateTime object with the GMT time from the specified std::chrono::time_point.
static std::pair< DateTime, TimeSpan > fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
static DateTime fromString(const std::string &str)
Parses the given std::string as DateTime.
static DateTime fromDateAndTime(int year=1, int month=1, int day=1, int hour=0, int minute=0, int second=0, double millisecond=0.0)
Constructs a DateTime to the specified year, month, day, hour, minute, second and millisecond.
static DateTime fromDate(int year=1, int month=1, int day=1)
Constructs a DateTime to the specified year, month, and day.
constexpr TickType totalTicks() const
Returns the number of ticks which represent the value of the current instance.
static DateTime gmtNow()
Returns a DateTime object that is set to the current date and time on this computer,...
static DateTime fromTimeStamp(std::time_t timeStamp)
Constructs a new DateTime object with the local time from the specified UNIX timeStamp.
constexpr bool isEternity() const
Returns whether the instance has the maximal number of ticks.
static DateTime fromIsoStringGmt(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
static int daysInMonth(int year, int month)
Returns the number of days in the specified month and year.
static constexpr DateTime eternity()
Constructs a new instance of the DateTime class with the maximal number of ticks.
Represents a period of time.
constexpr int days() const
Returns the days component of the period represented by the current instance.
constexpr int years() const
Returns the years component of the period represented by the current instance.
constexpr int months() const
Returns the months component of the period represented by the current instance.
Represents a time interval.
constexpr double totalHours() const
Returns the value of the current TimeSpan class expressed in whole and fractional hours.
constexpr double totalSeconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
constexpr bool isNull() const
Returns true if the time interval represented by the current TimeSpan class is null.
constexpr int seconds() const
Returns the seconds component of the time interval represented by the current TimeSpan class.
constexpr bool isNegativeInfinity() const
Returns whether the time interval represented by the current instance is the smallest representable T...
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class.
constexpr double totalMicroseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
constexpr TickType totalTicks() const
Returns the number of ticks that represent the value of the current TimeSpan class.
constexpr int days() const
Returns the days component of the time interval represented by the current TimeSpan class.
constexpr int milliseconds() const
Returns the milliseconds component of the time interval represented by the current TimeSpan class.
static constexpr TimeSpan fromDays(double days)
Constructs a new instance of the TimeSpan class with the specified number of days.
static constexpr TimeSpan fromHours(double hours)
Constructs a new instance of the TimeSpan class with the specified number of hours.
static constexpr TimeSpan fromMinutes(double minutes)
Constructs a new instance of the TimeSpan class with the specified number of minutes.
static constexpr TimeSpan infinity()
Constructs a new instance of the TimeSpan class with the maximal number of ticks.
static constexpr TimeSpan fromSeconds(double seconds)
Constructs a new instance of the TimeSpan class with the specified number of seconds.
constexpr bool isInfinity() const
Returns whether the time interval represented by the current instance is the longest representable Ti...
static constexpr TimeSpan negativeInfinity()
Constructs a new instance of the TimeSpan class with the minimal number of ticks.
constexpr int microseconds() const
Returns the microseconds component of the time interval represented by the current TimeSpan class.
constexpr double totalDays() const
Returns the value of the current TimeSpan class expressed in whole and fractional days.
static TimeSpan fromString(const std::string &str, char separator=':')
Parses the given std::string as TimeSpan.
constexpr double totalMilliseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
constexpr bool isNegative() const
Returns true if the time interval represented by the current TimeSpan class is negative.
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
constexpr double totalMinutes() const
Returns the value of the current TimeSpan class expressed in whole and fractional minutes.
constexpr int nanoseconds() const
Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
static constexpr TimeSpan fromMilliseconds(double milliseconds)
Constructs a new instance of the TimeSpan class with the specified number of milliseconds.
Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL.
Contains all utilities provides by the c++utilities library.
static DateTimeExpression fromString(const char *str)
Parses the given C-style string.
static DateTimeExpression fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.