openMSX
Date_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2#include "Date.hh"
3#include "ranges.hh"
4#include <cstring>
5
6using namespace openmsx;
7
8static void test(time_t t, const char* s)
9{
10 REQUIRE(strlen(s) >= 24); // precondition
11 CHECK(Date::fromString(std::span<const char, 24>{s, 24}) == t);
12 CHECK(Date::toString(t) == s);
13}
14
15TEST_CASE("Date")
16{
17 putenv(const_cast<char*>("TZ=UTC")); tzset();
18 test( 0, "Thu Jan 01 00:00:00 1970");
19 test( 1, "Thu Jan 01 00:00:01 1970");
20 test( 60, "Thu Jan 01 00:01:00 1970");
21 test( 3600, "Thu Jan 01 01:00:00 1970");
22 test(1318850077, "Mon Oct 17 11:14:37 2011");
23 test(1403092862, "Wed Jun 18 12:01:02 2014");
24
25 // Check invalid formats
26 // - invalid separator characters
27 CHECK(Date::fromString(subspan<24>("WedXJun 18 12:01:02 2014")) == Date::INVALID_TIME_T);
28 CHECK(Date::fromString(subspan<24>("Wed JunX18 12:01:02 2014")) == Date::INVALID_TIME_T);
29 CHECK(Date::fromString(subspan<24>("Wed Jun 18X12:01:02 2014")) == Date::INVALID_TIME_T);
30 CHECK(Date::fromString(subspan<24>("Wed Jun 18 12X01:02 2014")) == Date::INVALID_TIME_T);
31 CHECK(Date::fromString(subspan<24>("Wed Jun 18 12:01X02 2014")) == Date::INVALID_TIME_T);
32 CHECK(Date::fromString(subspan<24>("Wed Jun 18 12:01:02X2014")) == Date::INVALID_TIME_T);
33 // - weekday is not verified
34 // - invalid month (must also have correct case)
35 CHECK(Date::fromString(subspan<24>("Wed Foo 18 12:01:02 2014")) == Date::INVALID_TIME_T);
36 CHECK(Date::fromString(subspan<24>("Wed jun 18 12:01:02 2014")) == Date::INVALID_TIME_T);
37 // - invalid day
38 CHECK(Date::fromString(subspan<24>("Wed Jun 00 12:01:02 2014")) == Date::INVALID_TIME_T);
39 CHECK(Date::fromString(subspan<24>("Wed Jun 32 12:01:02 2014")) == Date::INVALID_TIME_T);
40 // - invalid hour
41 CHECK(Date::fromString(subspan<24>("Wed Jun 18 24:01:02 2014")) == Date::INVALID_TIME_T);
42 CHECK(Date::fromString(subspan<24>("Wed Jun 18 xx:01:02 2014")) == Date::INVALID_TIME_T);
43 // - invalid minute
44 CHECK(Date::fromString(subspan<24>("Wed Jun 18 12:60:02 2014")) == Date::INVALID_TIME_T);
45 CHECK(Date::fromString(subspan<24>("Wed Jun 18 12:-1:02 2014")) == Date::INVALID_TIME_T);
46 // - invalid second
47 CHECK(Date::fromString(subspan<24>("Wed Jun 18 12:01:60 2014")) == Date::INVALID_TIME_T);
48 CHECK(Date::fromString(subspan<24>("Wed Jun 18 12:01:0 2014 ")) == Date::INVALID_TIME_T);
49 // - invalid year
50 CHECK(Date::fromString(subspan<24>("Wed Jun 18 12:01:02 1800")) == Date::INVALID_TIME_T);
51 CHECK(Date::fromString(subspan<24>("Wed Jun 18 12:01:02 X800")) == Date::INVALID_TIME_T);
52
53 // extra characters at the end are ignored, even digits
54 CHECK(Date::fromString(subspan<24>("Wed Jun 18 12:01:02 2014x")) == 1403092862);
55 CHECK(Date::fromString(subspan<24>("Wed Jun 18 12:01:02 20140")) == 1403092862);
56}
TEST_CASE("Date")
Definition Date_test.cc:15
TclObject t
CHECK(m3==m3)
std::string toString(time_t time)
Definition Date.cc:153
time_t fromString(std::span< const char, 24 > s)
Definition Date.cc:33
constexpr time_t INVALID_TIME_T
Definition Date.hh:11
This file implemented 3 utility functions:
Definition Autofire.cc:11