8 #include "xclox/time.hpp"
10 using namespace xclox;
11 using namespace std::chrono;
15 TEST_CASE(
"constructible")
28 SUBCASE(
"hour, minute, second")
32 CHECK(t.hour() == 13);
33 CHECK(t.minute() == 44);
34 CHECK(t.second() == 2);
35 CHECK(t.millisecond() == 0);
36 CHECK(t.microsecond() == 0);
37 CHECK(t.nanosecond() == 0);
39 SUBCASE(
"time values with a subsecond duration")
43 CHECK(t.hour() == 13);
44 CHECK(t.minute() == 44);
45 CHECK(t.second() == 2);
46 CHECK(t.millisecond() == 781);
47 CHECK(t.microsecond() == 781945);
48 CHECK(t.nanosecond() == 781945521);
50 SUBCASE(
"aliased time duration")
60 SUBCASE(
"system time duration")
62 const auto& duration = system_clock::now().time_since_epoch() % hours(24);
65 CHECK(t.hour() == duration_cast<hours>(duration).count());
66 CHECK(t.minute() == duration_cast<minutes>(duration % hours(1)).count());
67 CHECK(t.second() == duration_cast<seconds>(duration % minutes(1)).count());
68 CHECK(t.millisecond() == duration_cast<milliseconds>(duration % seconds(1)).count());
69 CHECK(t.microsecond() == duration_cast<microseconds>(duration % seconds(1)).count());
70 CHECK(t.nanosecond() == duration_cast<nanoseconds>(duration % seconds(1)).count());
72 SUBCASE(
"system time point")
74 const auto& now = system_clock::now();
77 CHECK(t.hour() == duration_cast<hours>(now.time_since_epoch() % hours(24)).count());
78 CHECK(t.minute() == duration_cast<minutes>(now.time_since_epoch() % hours(1)).count());
79 CHECK(t.second() == duration_cast<seconds>(now.time_since_epoch() % minutes(1)).count());
80 CHECK(t.millisecond() == duration_cast<milliseconds>(now.time_since_epoch() % seconds(1)).count());
81 CHECK(t.microsecond() == duration_cast<microseconds>(now.time_since_epoch() % seconds(1)).count());
82 CHECK(t.nanosecond() == duration_cast<nanoseconds>(now.time_since_epoch() % seconds(1)).count());
86 TEST_CASE(
"validatable")
88 CHECK(
Time().isValid() ==
false);
94 CHECK(
Time(0, 0, 0).isValid() ==
true);
95 CHECK(
Time(1, 2, 3).isValid() ==
true);
96 CHECK(
Time(-1, 0, 3).isValid() ==
false);
97 CHECK(
Time(30, 20, 10).isValid() ==
false);
98 CHECK(
Time(system_clock::now().time_since_epoch()).isValid() ==
false);
99 CHECK(
Time(system_clock::now()).isValid() ==
true);
104 TEST_CASE(
"comparable")
114 TEST_CASE(
"copyable")
120 SUBCASE(
"construction")
124 Time t6(std::move(t3));
129 SUBCASE(
"assignment")
133 Time t6 = std::move(t3);
141 TEST_CASE(
"current time")
143 CHECK(abs(duration_cast<milliseconds>(
Time::current() -
Time(system_clock::now())).count()) < 100);
146 TEST_CASE(
"midnight")
151 TEST_CASE(
"addition & subtraction")
168 SUBCASE(
"milliseconds")
173 SUBCASE(
"microseconds")
178 SUBCASE(
"nanoseconds")
201 TEST_CASE(
"formatting")
203 SUBCASE(
"empty format")
205 CHECK(
Time(1, 2, 3).toString(
"") ==
"");
207 SUBCASE(
"invalid time")
209 CHECK(
Time().toString(
"H:m:s") ==
"");
210 CHECK(
Time(0, 0, -1).toString(
"H:m:s") ==
"");
215 CHECK(
Time(23, 45, 2).toString(
"H:m:s") ==
"11:45:2");
216 CHECK(
Time(0, 45, 2).toString(
"H:m:s") ==
"12:45:2");
217 CHECK(
Time(3, 45, 2).toString(
"HH:m:s") ==
"03:45:2");
219 SUBCASE(
"meridiem label small letters")
221 CHECK(
Time(3, 45, 2).toString(
"HH:mm:ss a") ==
"03:45:02 am");
222 CHECK(
Time(13, 45, 2).toString(
"HH:mm:ss a") ==
"01:45:02 pm");
226 SUBCASE(
"meridiem label capital letters")
228 CHECK(
Time(3, 45, 2).toString(
"HH:mm:ss A") ==
"03:45:02 AM");
229 CHECK(
Time(13, 45, 2).toString(
"HH:mm:ss A") ==
"01:45:02 PM");
233 SUBCASE(
"reserve unparsable expressions")
235 CHECK(
Time(3, 45, 2).toString(
"hhmmss") ==
"034502");
236 CHECK(
Time(21, 52, 41).toString(
"hhmmss ieee") ==
"215241 ieee");
240 CHECK(
Time(0, 0, 0).toString(
"h:m:s") ==
"0:0:0");
241 CHECK(
Time(0, 0, 0).toString(
"hh:mm:ss") ==
"00:00:00");
242 CHECK(
Time(22, 45, 2).toString(
"h:m:s") ==
"22:45:2");
243 CHECK(
Time(3, 45, 2).toString(
"hh:m:s") ==
"03:45:2");
259 CHECK(t.toString(
"hh:mm:ss.f") ==
"07:09:02.6");
260 CHECK(t.toString(
"hh:mm:ss.ff") ==
"07:09:02.67");
261 CHECK(t.toString(
"hh:mm:ss.fff") ==
"07:09:02.675");
262 CHECK(t.toString(
"hh:mm:ss.ffff") ==
"07:09:02.6758");
263 CHECK(t.toString(
"hh:mm:ss.fffff") ==
"07:09:02.67586");
264 CHECK(t.toString(
"hh:mm:ss.ffffff") ==
"07:09:02.675869");
265 CHECK(t.toString(
"hh:mm:ss.fffffff") ==
"07:09:02.6758690");
266 CHECK(t.toString(
"hh:mm:ss.ffffffff") ==
"07:09:02.67586909");
267 CHECK(t.toString(
"hh:mm:ss.fffffffff") ==
"07:09:02.675869093");
269 SUBCASE(
"fraction - zero milliseconds")
273 CHECK(t.toString(
"hh:mm:ss.f") ==
"07:09:02.0");
274 CHECK(t.toString(
"hh:mm:ss.ff") ==
"07:09:02.00");
275 CHECK(t.toString(
"hh:mm:ss.fff") ==
"07:09:02.000");
276 CHECK(t.toString(
"hh:mm:ss.ffff") ==
"07:09:02.0008");
277 CHECK(t.toString(
"hh:mm:ss.fffff") ==
"07:09:02.00086");
278 CHECK(t.toString(
"hh:mm:ss.ffffff") ==
"07:09:02.000869");
279 CHECK(t.toString(
"hh:mm:ss.fffffff") ==
"07:09:02.0008690");
280 CHECK(t.toString(
"hh:mm:ss.ffffffff") ==
"07:09:02.00086909");
281 CHECK(t.toString(
"hh:mm:ss.fffffffff") ==
"07:09:02.000869093");
283 SUBCASE(
"fraction - zero microseconds")
287 CHECK(t.toString(
"h:m:s") ==
"7:9:2");
288 CHECK(t.toString(
"hh:mm:ss") ==
"07:09:02");
289 CHECK(t.toString(
"hh:mm:ss.f") ==
"07:09:02.6");
290 CHECK(t.toString(
"hh:mm:ss.ff") ==
"07:09:02.67");
291 CHECK(t.toString(
"hh:mm:ss.fff") ==
"07:09:02.675");
292 CHECK(t.toString(
"hh:mm:ss.ffff") ==
"07:09:02.6750");
293 CHECK(t.toString(
"hh:mm:ss.fffff") ==
"07:09:02.67500");
294 CHECK(t.toString(
"hh:mm:ss.ffffff") ==
"07:09:02.675000");
295 CHECK(t.toString(
"hh:mm:ss.fffffff") ==
"07:09:02.6750000");
296 CHECK(t.toString(
"hh:mm:ss.ffffffff") ==
"07:09:02.67500004");
297 CHECK(t.toString(
"hh:mm:ss.fffffffff") ==
"07:09:02.675000044");
299 SUBCASE(
"fraction - zero nanoseconds")
303 CHECK(t.toString(
"h:m:s") ==
"7:9:2");
304 CHECK(t.toString(
"hh:mm:ss") ==
"07:09:02");
305 CHECK(t.toString(
"hh:mm:ss.f") ==
"07:09:02.6");
306 CHECK(t.toString(
"hh:mm:ss.ff") ==
"07:09:02.67");
307 CHECK(t.toString(
"hh:mm:ss.fff") ==
"07:09:02.675");
308 CHECK(t.toString(
"hh:mm:ss.ffff") ==
"07:09:02.6758");
309 CHECK(t.toString(
"hh:mm:ss.fffff") ==
"07:09:02.67586");
310 CHECK(t.toString(
"hh:mm:ss.ffffff") ==
"07:09:02.675869");
311 CHECK(t.toString(
"hh:mm:ss.fffffff") ==
"07:09:02.6758690");
312 CHECK(t.toString(
"hh:mm:ss.ffffffff") ==
"07:09:02.67586900");
313 CHECK(t.toString(
"hh:mm:ss.fffffffff") ==
"07:09:02.675869000");
314 CHECK(t.toString(
"hh:mm:ss.fff fff fff") ==
"07:09:02.675 675 675");
345 TEST_CASE(
"conversion")
347 SUBCASE(
"nanoseconds")
349 CHECK(
Time(23, 56, 33,
Time::Nanoseconds(978432162)).toNanosecondsSinceMidnight() == 86193978432162);
351 SUBCASE(
"microseconds")
355 SUBCASE(
"milliseconds")
374 std::tm tmTime = t.toBrokenStdTime();
375 CHECK(tmTime.tm_hour == t.hour());
376 CHECK(tmTime.tm_min == t.minute());
377 CHECK(tmTime.tm_sec == t.second());
382 std::time_t tTime = t.toScalarStdTime();
383 CHECK(tTime == t.toSecondsSinceMidnight());
387 TEST_CASE(
"serialization & deserialization")
390 std::stringstream ss;
391 ss <<
Time(14, 32, 9);
393 CHECK(t ==
Time(14, 32, 9));
Time is an immutable time class representing a time without a time zone in the ISO-8601 calendar syst...
Definition: time.hpp:46
static long millisecondsBetween(const Time &from, const Time &to)
Returns the number of milliseconds between from and to.
Definition: time.hpp:599
static int minutesBetween(const Time &from, const Time &to)
Returns the number of minutes between from and to.
Definition: time.hpp:611
long nanosecond() const
Returns the nanosecond of second (0, 999999999).
Definition: time.hpp:263
int minute() const
Returns the minute of hour (0, 59).
Definition: time.hpp:287
int millisecond() const
Returns the millisecond of second (0, 999).
Definition: time.hpp:275
static Time current()
Returns a Time object set to the current time obtained from the system clock.
Definition: time.hpp:529
int hour() const
Returns the hour of day (0, 23).
Definition: time.hpp:293
std::chrono::milliseconds Milliseconds
Millisecond duration.
Definition: time.hpp:58
std::chrono::minutes Minutes
Minute duration.
Definition: time.hpp:60
static Time fromString(const std::string &time, const std::string &format)
Returns a Time object from the string time according to the format string format.
Definition: time.hpp:544
long microsecond() const
Returns the microsecond of second (0, 999999).
Definition: time.hpp:269
static long long microsecondsBetween(const Time &from, const Time &to)
Returns the number of microseconds between from and to.
Definition: time.hpp:593
static long long nanosecondsBetween(const Time &from, const Time &to)
Returns the number of nanoseconds between from and to.
Definition: time.hpp:587
std::chrono::microseconds Microseconds
Microsecond duration.
Definition: time.hpp:57
static int hoursBetween(const Time &from, const Time &to)
Returns the number of hours between from and to.
Definition: time.hpp:617
static long secondsBetween(const Time &from, const Time &to)
Returns the number of seconds between from and to.
Definition: time.hpp:605
std::chrono::nanoseconds Nanoseconds
Nanosecond duration.
Definition: time.hpp:56
std::chrono::seconds Seconds
Second duration.
Definition: time.hpp:59
std::chrono::hours Hours
Hour duration.
Definition: time.hpp:61
static Time midnight()
Returns a Time object set to midnight (i.e., "00:00:00").
Definition: time.hpp:535
int second() const
Returns the second of minute (0, 59).
Definition: time.hpp:281