1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
#define CHRONO_UTILITIES_TIMESPAN_INTEGER_SCALE_OVERLOADS
#include "../chrono/datetime.h"
#include "../chrono/format.h"
#include "../chrono/period.h"
#include "../chrono/timespan.h"
#include "../conversion/conversionexception.h"
#include "../tests/testutils.h"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <chrono>
#include <cmath>
#include <iostream>
using namespace std;
using namespace CppUtilities;
using namespace CppUtilities::Literals;
using namespace CPPUNIT_NS;
// compile-time checks for DateTime class
static_assert(DateTime().isNull(), "isNull()");
static_assert(DateTime(1).totalTicks() == 1, "construction with ticks");
static_assert(DateTime(2) == DateTime(2), "operator ==");
static_assert(DateTime(2) != DateTime(3), "operator !=");
static_assert(DateTime(2) < DateTime(3), "operator <");
static_assert(DateTime(3) > DateTime(2), "operator >");
static_assert(DateTime::eternity().isEternity() && !DateTime().isEternity(), "isEternity()");
static constexpr auto dateFromUnixEpoch(
DateTime::unixEpochStart() + TimeSpan::fromHours(1.0) + TimeSpan::fromMinutes(2.0) + TimeSpan::fromSeconds(3.1256789));
static_assert(dateFromUnixEpoch.dayOfWeek() == DayOfWeek::Thursday, "dayOfWeek()");
static_assert(dateFromUnixEpoch.hour() == 1, "hour()");
static_assert(dateFromUnixEpoch.minute() == 2, "minute()");
static_assert(dateFromUnixEpoch.second() == 3, "second()");
static_assert(dateFromUnixEpoch.millisecond() == 125, "millisecond()");
static_assert(dateFromUnixEpoch.microsecond() == 678, "microsecond()");
static_assert(dateFromUnixEpoch.nanosecond() == 900, "nanosecond()");
static_assert(dateFromUnixEpoch.isSameDay(DateTime::unixEpochStart()), "isSameDay()");
static_assert(!dateFromUnixEpoch.isSameDay(DateTime::unixEpochStart() + TimeSpan::fromHours(24.0)), "!isSameDay()");
// compile-time checks for TimeSpan class
static_assert(TimeSpan().isNull(), "isNull()");
static_assert(TimeSpan(1).totalTicks() == 1, "construction with ticks");
static_assert(TimeSpan(-1).isNegative() && !TimeSpan(1).isNegative(), "isNegative()");
static_assert(TimeSpan::infinity().isInfinity() && !TimeSpan().isInfinity(), "isInfinity()");
static_assert(TimeSpan::negativeInfinity().isNegativeInfinity() && !TimeSpan().isNegativeInfinity(), "isNegativeInfinity()");
static_assert(TimeSpan::fromMilliseconds(1.0125).nanoseconds() == 500, "fromMilliseconds()/nanoseconds()");
static_assert(TimeSpan::fromMilliseconds(1.0125).microseconds() == 12, "fromMilliseconds()/microseconds()");
static_assert(TimeSpan::fromMilliseconds(1.0125).milliseconds() == 1, "fromMilliseconds()/milliseconds()");
static_assert(TimeSpan::fromSeconds(TimeSpan::TickType(61)).seconds() == 1, "fromSeconds()/seconds()");
static_assert(TimeSpan::fromSeconds(TimeSpan::TickType(61)).minutes() == 1, "fromSeconds()/minutes()");
static_assert(TimeSpan::fromMinutes(TimeSpan::TickType(61)).minutes() == 1, "fromMinutes()/minutes()");
static_assert(TimeSpan::fromHours(TimeSpan::TickType(25)).hours() == 1, "fromMinutes()/hours()");
static_assert(TimeSpan::fromDays(20.5).days() == 20, "fromDays()/days()");
static_assert(TimeSpan::fromMinutes(1.5).totalMicroseconds() == 90e6, "totalMicroseconds()");
static_assert(TimeSpan::fromMinutes(1.5).totalMilliseconds() == 90e3, "totalMilliseconds()");
static_assert(TimeSpan::fromMinutes(1.5).totalSeconds() == 90.0, "totalSeconds()");
static_assert(TimeSpan::fromHours(1.5).totalMinutes() == 90.0, "totalMinutes()");
static_assert(TimeSpan::fromDays(1.5).totalHours() == 36.0, "totalHours()");
static_assert(TimeSpan::fromDays(20.5).totalDays() == 20.5, "totalDays()");
/*!
* \brief The ChronoTests class tests classes and functions provided by the files inside the chrono directory.
* \remarks Before committing any changes to this test, run with different timezones to prevent
* mistakes like timezone-dependent checks. (Eg. set environment variable TZ to different
* values like 'UTC' or 'America/Los_Angeles'.)
*/
class ChronoTests : public TestFixture {
CPPUNIT_TEST_SUITE(ChronoTests);
CPPUNIT_TEST(testDateTime);
CPPUNIT_TEST(testDateTimeExpression);
CPPUNIT_TEST(testTimeSpan);
CPPUNIT_TEST(testOperators);
CPPUNIT_TEST(testPeriod);
CPPUNIT_TEST(testHashing);
CPPUNIT_TEST_SUITE_END();
public:
void setUp()
{
}
void tearDown()
{
}
void testDateTime();
void testDateTimeExpression();
void testTimeSpan();
void testOperators();
void testPeriod();
void testHashing();
};
CPPUNIT_TEST_SUITE_REGISTRATION(ChronoTests);
/*!
* \brief Tests most important DateTime features.
*/
void ChronoTests::testDateTime()
{
// test year(), month(), ...
CPPUNIT_ASSERT_EQUAL(DateTime::daysInMonth(2000, 2), 29);
CPPUNIT_ASSERT_EQUAL(DateTime::daysInMonth(2001, 2), 28);
CPPUNIT_ASSERT_EQUAL(DateTime::daysInMonth(2100, 2), 28);
const auto test1 = DateTime::fromDateAndTime(2012, 2, 29, 15, 34, 20, 33.0);
CPPUNIT_ASSERT_EQUAL(2012, test1.year());
CPPUNIT_ASSERT_EQUAL(2, test1.month());
CPPUNIT_ASSERT_EQUAL(29, test1.day());
CPPUNIT_ASSERT_EQUAL(15, test1.hour());
CPPUNIT_ASSERT_EQUAL(34, test1.minute());
CPPUNIT_ASSERT_EQUAL(20, test1.second());
CPPUNIT_ASSERT_EQUAL(33, test1.millisecond());
CPPUNIT_ASSERT_EQUAL(DayOfWeek::Wednesday, test1.dayOfWeek());
CPPUNIT_ASSERT_EQUAL((31 + 29), test1.dayOfYear());
CPPUNIT_ASSERT(test1.isLeapYear());
CPPUNIT_ASSERT(test1.isSameDay(test1 + TimeSpan::fromHours(8.0)));
CPPUNIT_ASSERT(!test1.isSameDay(test1 + TimeSpan::fromHours(9.0)));
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<time_t>(1453840331);
const auto fromTimeStampGmt = DateTime::fromTimeStampGmt(timeStamp), fromTimeStamp = DateTime::fromTimeStamp(timeStamp);
CPPUNIT_ASSERT_EQUAL("Tue 2016-01-26 20:32:11"s, fromTimeStampGmt.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
CPPUNIT_ASSERT(fabs((fromTimeStamp - fromTimeStampGmt).totalDays()) <= 1.0);
CPPUNIT_ASSERT_EQUAL(DateTime(), DateTime::fromTimeStamp(0));
CPPUNIT_ASSERT_EQUAL(timeStamp, fromTimeStampGmt.toTimeStamp());
// test fromChronoTimePointGmt()
const auto fromChronoTimePointGmt = DateTime::fromChronoTimePointGmt(chrono::system_clock::from_time_t(timeStamp));
CPPUNIT_ASSERT_EQUAL("Tue 2016-01-26 20:32:11"s, fromChronoTimePointGmt.toString(DateTimeOutputFormat::DateTimeAndShortWeekday));
// test whether ConversionException() is thrown when invalid values are specified
CPPUNIT_ASSERT_THROW(DateTime::fromDate(0, 1, 1), ConversionException);
CPPUNIT_ASSERT_THROW(DateTime::fromDate(2012, 15, 1), ConversionException);
CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(0, 2, 29, 15, 34, 20, 33), ConversionException);
CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2013, 2, 29, 15, 34, 20, 33), ConversionException);
CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 2, 29, 15, 61, 20, 33), ConversionException);
CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 4, 31, 15, 0, 20, 33), ConversionException);
CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 3, 31, 15, 0, 61, 33), ConversionException);
CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 1, 1, 61, 2, 1), ConversionException);
CPPUNIT_ASSERT_THROW(DateTime::fromDateAndTime(2012, 1, 1, 15, 2, 1, 2000.0), ConversionException);
// test fromString()/toString()
CPPUNIT_ASSERT_EQUAL(test1, DateTime::fromString("2012-02-29 15:34:20.033"));
CPPUNIT_ASSERT_EQUAL_MESSAGE("surplus parts ignored", test1, DateTime::fromString("2012-02-29 15:34:20.033:12"));
CPPUNIT_ASSERT_EQUAL("2012-02-29 15:34:20.033"s, test1.toString(DateTimeOutputFormat::DateAndTime, false));
CPPUNIT_ASSERT_THROW(TimeSpan::fromString("2012-02-29 15:34:34:20.033"), ConversionException);
const auto test3 = DateTime::fromIsoString("2016-08-29T21:32:31.125+02:00");
CPPUNIT_ASSERT_EQUAL("2016-08-29T21:32:31.125+02:00"s, test3.first.toIsoString(test3.second));
CPPUNIT_ASSERT_THROW(DateTime::fromString("#"), ConversionException);
// test accuracy (of 100 nanoseconds)
const auto test4 = DateTime::fromIsoString("2017-08-23T19:40:15.985077682+02:30");
CPPUNIT_ASSERT_EQUAL(2.5, test4.second.totalHours());
CPPUNIT_ASSERT_EQUAL(15, test4.first.second());
CPPUNIT_ASSERT_EQUAL(985, test4.first.millisecond());
CPPUNIT_ASSERT_EQUAL(77, test4.first.microsecond());
CPPUNIT_ASSERT_EQUAL(600, test4.first.nanosecond());
CPPUNIT_ASSERT_EQUAL("2017-08-23T19:40:15.9850776+02:30"s, test4.first.toIsoString(test4.second));
// test negative delta
const auto test5 = DateTime::fromIsoString("2017-08-23T19:40:15.985077682-02:30");
CPPUNIT_ASSERT_EQUAL(-2.5, test5.second.totalHours());
CPPUNIT_ASSERT_EQUAL(15, test5.first.second());
CPPUNIT_ASSERT_EQUAL(985, test5.first.millisecond());
CPPUNIT_ASSERT_EQUAL(77, test5.first.microsecond());
CPPUNIT_ASSERT_EQUAL(600, test5.first.nanosecond());
CPPUNIT_ASSERT_EQUAL("2017-08-23T19:40:15.9850776-02:30"s, test5.first.toIsoString(test5.second));
// test further variants
CPPUNIT_ASSERT_EQUAL_MESSAGE("only year", DateTime::fromDate(2008), DateTime::fromIsoStringGmt("2008"));
CPPUNIT_ASSERT_EQUAL_MESSAGE("only year and month", DateTime::fromDate(2008, 12), DateTime::fromIsoStringGmt("2008-12"));
CPPUNIT_ASSERT_EQUAL_MESSAGE("only date", DateTime::fromDate(2008, 12, 5), DateTime::fromIsoStringGmt("2008-12-05"));
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.0), 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 (positive timezone offset, 1)", DateTime::fromDateAndTime(1970, 1, 1, 1, 2, 3), test6.first);
CPPUNIT_ASSERT_EQUAL_MESSAGE("no seconds fraction (positive timezone offset, 2)", TimeSpan::fromHours(1.0), test6.second);
const auto test7 = DateTime::fromIsoString("2021-05-20T23:02:45-04:00");
CPPUNIT_ASSERT_EQUAL_MESSAGE("no seconds fraction (negative timezone offset, 1)", DateTime::fromDateAndTime(2021, 5, 20, 23, 2, 45), test7.first);
CPPUNIT_ASSERT_EQUAL_MESSAGE("no seconds fraction (negative timezone offset, 2)", TimeSpan::fromHours(-4.0), test7.second);
// implied separators / too many digits
CPPUNIT_ASSERT_EQUAL_MESSAGE("no separators", test5.first - test5.second, DateTime::fromIsoStringGmt("20170823T194015.985077682-0230"));
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"not even T separator", DateTime::fromDateAndTime(2017, 8, 23, 19, 40, 15), DateTime::fromIsoStringGmt("20170823194015"));
CPPUNIT_ASSERT_THROW_MESSAGE("too many digits after seconds", DateTime::fromIsoString("2017082319401516"), ConversionException);
CPPUNIT_ASSERT_THROW_MESSAGE("too many digits after timezone offset", DateTime::fromIsoString("20170823194015.16+02300"), ConversionException);
// 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);
CPPUNIT_ASSERT_THROW_MESSAGE("invalid T", DateTime::fromIsoString("2017-08-23T19:T40:15.985077682+02:00"), ConversionException);
CPPUNIT_ASSERT_THROW_MESSAGE("invalid -", DateTime::fromIsoString("2017-08-23T19:40-15.985077682+02:00"), ConversionException);
CPPUNIT_ASSERT_THROW_MESSAGE("invalid .", DateTime::fromIsoString("2017-08.5-23T19:40:15.985077682+02:00"), ConversionException);
CPPUNIT_ASSERT_THROW_MESSAGE("invalid :", DateTime::fromIsoString("2017:08-23T19:40:15.985077682+02:00"), ConversionException);
CPPUNIT_ASSERT_THROW_MESSAGE("invalid :", DateTime::fromIsoString("2017-08-23T19:40:15:985077682+02:00"), ConversionException);
// ISO string via toString() format option
CPPUNIT_ASSERT_EQUAL("1234-05-06T07:08:09.0105005"s, DateTime::fromDateAndTime(1234, 5, 6, 7, 8, 9, 10.5005).toString(DateTimeOutputFormat::Iso));
CPPUNIT_ASSERT_EQUAL("1234-05-06T07:08:09.0105005"s,
DateTime::fromDateAndTime(1234, 5, 6, 7, 8, 9, 10.5005).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
CPPUNIT_ASSERT_EQUAL("1234-05-06T07:08:09.010500"s,
DateTime::fromDateAndTime(1234, 5, 6, 7, 8, 9, 10.500).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
CPPUNIT_ASSERT_EQUAL(
"1234-05-06T07:08:09.010"s, DateTime::fromDateAndTime(1234, 5, 6, 7, 8, 9, 10).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
CPPUNIT_ASSERT_EQUAL(
"1234-05-06T07:08:09"s, DateTime::fromDateAndTime(1234, 5, 6, 7, 8, 9).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
CPPUNIT_ASSERT_EQUAL(
"1234-05-06T07:08"s, DateTime::fromDateAndTime(1234, 5, 6, 7, 8).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
CPPUNIT_ASSERT_EQUAL("1234-05-06T07"s, DateTime::fromDateAndTime(1234, 5, 6, 7).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
CPPUNIT_ASSERT_EQUAL("1234-05-06"s, DateTime::fromDateAndTime(1234, 5, 6).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
CPPUNIT_ASSERT_EQUAL("1234-05"s, DateTime::fromDateAndTime(1234, 5).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
CPPUNIT_ASSERT_EQUAL("1234"s, DateTime::fromDateAndTime(1234).toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
CPPUNIT_ASSERT_EQUAL("0001"s, DateTime().toString(DateTimeOutputFormat::IsoOmittingDefaultComponents));
// test now() and exactNow() (or at least whether both behave the same)
#if defined(PLATFORM_UNIX)
const auto delta = DateTime::gmtNow() - DateTime::exactGmtNow();
CPPUNIT_ASSERT(delta < TimeSpan::fromSeconds(2.0) && delta > TimeSpan::fromSeconds(-2.0));
#endif
}
/*!
* \brief Tests parsing a DateTimeExpression. Checks for the parts in particular.
*/
void ChronoTests::testDateTimeExpression()
{
// check adding ISO timestamp parts one-by-one and serialization back to string
auto expr = DateTimeExpression::fromIsoString("1");
auto parts = DateTimeParts::Year;
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts, expr.parts);
CPPUNIT_ASSERT_EQUAL("0001"s, expr.toIsoString());
expr = DateTimeExpression::fromIsoString("1-1");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Month, expr.parts);
CPPUNIT_ASSERT_EQUAL("0001-01"s, expr.toIsoString());
expr = DateTimeExpression::fromIsoString("1-1-1");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Day, expr.parts);
CPPUNIT_ASSERT_EQUAL("0001-01-01"s, expr.toIsoString());
expr = DateTimeExpression::fromIsoString("1-1-1T0");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Hour, expr.parts);
CPPUNIT_ASSERT_EQUAL("0001-01-01T00"s, expr.toIsoString());
expr = DateTimeExpression::fromIsoString("1-1-1T0:0");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Minute, expr.parts);
CPPUNIT_ASSERT_EQUAL("0001-01-01T00:00"s, expr.toIsoString());
expr = DateTimeExpression::fromIsoString("1-1-1T0:0:0");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Second, expr.parts);
CPPUNIT_ASSERT_EQUAL("0001-01-01T00:00:00"s, expr.toIsoString());
expr = DateTimeExpression::fromIsoString("1-1-1T0:0:0.0");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::SubSecond, expr.parts);
CPPUNIT_ASSERT_EQUAL("0001-01-01T00:00:00.000"s, expr.toIsoString());
expr = DateTimeExpression::fromIsoString("1-1-1T0:0:0.0+0");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::DeltaHour, expr.parts);
CPPUNIT_ASSERT_EQUAL("0001-01-01T00:00:00.000+00"s, expr.toIsoString());
expr = DateTimeExpression::fromIsoString("1-1-1T0:0:0.0-0:0");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::DeltaMinute, expr.parts);
CPPUNIT_ASSERT_EQUAL("0001-01-01T00:00:00.000+00:00"s, expr.toIsoString());
// check that omitting parts in the middle is not possible anyways
CPPUNIT_ASSERT_THROW(DateTimeExpression::fromIsoString("1-1T0"), ConversionException);
// check ::fromString()
expr = DateTimeExpression::fromString("1");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts = DateTimeParts::Year, expr.parts);
expr = DateTimeExpression::fromString("1/1");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Month, expr.parts);
expr = DateTimeExpression::fromString("1/1/1");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Day, expr.parts);
expr = DateTimeExpression::fromString("1/1/1 0");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Hour, expr.parts);
expr = DateTimeExpression::fromString("1/1/1 0:0");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Minute, expr.parts);
expr = DateTimeExpression::fromString("1/1/1 0:0:0");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::Second, expr.parts);
expr = DateTimeExpression::fromString("1/1/1 0:0:0.0");
CPPUNIT_ASSERT_EQUAL(DateTime(), expr.value);
CPPUNIT_ASSERT_EQUAL(TimeSpan(), expr.delta);
CPPUNIT_ASSERT_EQUAL(parts |= DateTimeParts::SubSecond, expr.parts);
}
/*!
* \brief Tests most important TimeSpan features.
*/
void ChronoTests::testTimeSpan()
{
// test various usages of fromString(...), all other from...() functions and the plus operator
CPPUNIT_ASSERT_EQUAL(TimeSpan(), TimeSpan::fromString(string()));
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromSeconds(5.0), TimeSpan::fromString("5.0"));
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromMinutes(5.5), TimeSpan::fromString("5:30"));
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromHours(7.0) + TimeSpan::fromMinutes(5.5), TimeSpan::fromString("7:5:30"));
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromDays(14.0), TimeSpan::fromString("14:::"));
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromDays(14.0), TimeSpan::fromString("14d"));
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromDays(14.0) + TimeSpan::fromHours(5.0), TimeSpan::fromString("14d 5h"));
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromDays(14.0) + TimeSpan::fromMinutes(5.0), TimeSpan::fromString(" 14d 5m"));
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromDays(14.0) + TimeSpan::fromMinutes(5.0) + TimeSpan::fromSeconds(24.5), TimeSpan::fromString("2 w 24.5s 5m "));
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromDays(14.0) + TimeSpan::fromSeconds(24.5), TimeSpan::fromString("2 w 24.5"));
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromDays(14.0) + TimeSpan::fromString("1:2:3:4"), TimeSpan::fromString("2 w 1:2:3:4"));
CPPUNIT_ASSERT_THROW(TimeSpan::fromString("2:34a:53:32.5"), ConversionException);
CPPUNIT_ASSERT_THROW(TimeSpan::fromString("1:2:3:4:5"), ConversionException);
// test fromString(...) again and days(), hours(), ...
const auto test1 = TimeSpan::fromString("2:34:53:2.5");
CPPUNIT_ASSERT_EQUAL(3, test1.days());
CPPUNIT_ASSERT_EQUAL(10, test1.hours());
CPPUNIT_ASSERT_EQUAL(53, test1.minutes());
CPPUNIT_ASSERT_EQUAL(2, test1.seconds());
CPPUNIT_ASSERT_EQUAL(500, test1.milliseconds());
CPPUNIT_ASSERT(test1.totalDays() > 3.0 && test1.totalDays() < 4.0);
CPPUNIT_ASSERT(test1.totalHours() > (2 * 24 + 34) && test1.totalHours() < (2 * 24 + 35));
CPPUNIT_ASSERT(test1.totalMinutes() > (2 * 24 * 60 + 34 * 60 + 53) && test1.totalHours() < (2 * 24 * 60 + 34 * 60 + 54));
// test toString(...)
CPPUNIT_ASSERT_EQUAL("3 d 10 h 53 min 2 s 500 ms"s, test1.toString(TimeSpanOutputFormat::WithMeasures, false));
CPPUNIT_ASSERT_EQUAL("07:05:30"s, (TimeSpan::fromHours(7.0) + TimeSpan::fromMinutes(5.5)).toString());
CPPUNIT_ASSERT_EQUAL("-5 s"s, TimeSpan::fromSeconds(-5.0).toString(TimeSpanOutputFormat::WithMeasures, false));
CPPUNIT_ASSERT_EQUAL("0 s"s, TimeSpan().toString(TimeSpanOutputFormat::WithMeasures, false));
CPPUNIT_ASSERT_EQUAL("5e+02 µs"s, TimeSpan::fromMilliseconds(0.5).toString(TimeSpanOutputFormat::WithMeasures, false));
// test accuracy (of 100 nanoseconds)
const auto test2 = TimeSpan::fromString("15.985077682");
CPPUNIT_ASSERT_EQUAL(15.9850776, test2.totalSeconds());
CPPUNIT_ASSERT_EQUAL(15, test2.seconds());
CPPUNIT_ASSERT_EQUAL(985, test2.milliseconds());
CPPUNIT_ASSERT_EQUAL(77, test2.microseconds());
CPPUNIT_ASSERT_EQUAL(600, test2.nanoseconds());
CPPUNIT_ASSERT_EQUAL("00:00:15.9850776"s, test2.toString());
CPPUNIT_ASSERT_EQUAL("15 s 985 ms 77 µs 600 ns"s, test2.toString(TimeSpanOutputFormat::WithMeasures));
CPPUNIT_ASSERT_EQUAL("15.9850776"s, test2.toString(TimeSpanOutputFormat::TotalSeconds));
}
/*!
* \brief Tests operators of DateTime / TimeSpan.
*/
void ChronoTests::testOperators()
{
auto dateTime = DateTime::fromDateAndTime(1999, 1, 5, 4, 16);
CPPUNIT_ASSERT_EQUAL(7, (dateTime + TimeSpan::fromDays(2.0)).day());
CPPUNIT_ASSERT_EQUAL(6, (dateTime + TimeSpan::fromHours(24.0)).day());
CPPUNIT_ASSERT_EQUAL(3, (dateTime + TimeSpan::fromHours(24.0) + TimeSpan::fromHours(-1.0)).hour());
CPPUNIT_ASSERT_EQUAL(17, (dateTime + TimeSpan::fromHours(24.0) - TimeSpan::fromMinutes(-1.0)).minute());
dateTime += TimeSpan::fromDays(365.0);
CPPUNIT_ASSERT_EQUAL(2000, dateTime.year());
CPPUNIT_ASSERT_EQUAL(5, dateTime.day());
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromDays(1.0), TimeSpan::fromHours(12.0) * 2.0);
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromHours(12.0), TimeSpan::fromDays(1.0) / 2.0);
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromDays(1.0), TimeSpan::fromHours(12.0) * TimeSpan::TickType(2));
CPPUNIT_ASSERT_EQUAL(TimeSpan::fromHours(12.0), TimeSpan::fromDays(1.0) / TimeSpan::TickType(2));
CPPUNIT_ASSERT_EQUAL(2.0, TimeSpan::fromDays(1.0) / TimeSpan::fromHours(12.0));
}
/*!
* \brief Tests Period.
*/
void ChronoTests::testPeriod()
{
const auto begin(DateTime::fromDateAndTime(1994, 7, 18, 15, 30, 21)), end(DateTime::fromDateAndTime(2017, 12, 2, 15, 30, 21));
const Period period(begin, end);
CPPUNIT_ASSERT_EQUAL(23, period.years());
CPPUNIT_ASSERT_EQUAL(4, period.months());
CPPUNIT_ASSERT_EQUAL(14, period.days());
CPPUNIT_ASSERT_EQUAL(end.toString(), (begin + period).toString());
const auto end2(DateTime::fromDateAndTime(2018, 1, 2, 15, 30, 21));
const Period period2(begin, end2);
CPPUNIT_ASSERT_EQUAL(23, period2.years());
CPPUNIT_ASSERT_EQUAL(5, period2.months());
CPPUNIT_ASSERT_EQUAL_MESSAGE("one more day, because December has 31 days", 15, period2.days());
CPPUNIT_ASSERT_EQUAL(end2.toString(), (begin + period2).toString());
}
/*!
* \brief Tests hashing DateTime / TimeSpan by using in a set.
*/
void ChronoTests::testHashing()
{
set<DateTime> dateTimes;
dateTimes.emplace(DateTime::fromDate(2500, 2, 1));
dateTimes.emplace(DateTime::fromDate(2500, 2, 2));
dateTimes.emplace(DateTime::fromDate(2500, 2, 1));
CPPUNIT_ASSERT_EQUAL(2_st, dateTimes.size());
set<TimeSpan> timeSpans;
timeSpans.emplace(TimeSpan::fromDays(5.0));
timeSpans.emplace(TimeSpan::fromDays(10.0));
timeSpans.emplace(TimeSpan::fromDays(5.0));
CPPUNIT_ASSERT_EQUAL(2_st, timeSpans.size());
}
|