libcruft-util/test/time/8601.cpp

52 lines
1.5 KiB
C++
Raw Normal View History

2018-01-01 15:47:41 +11:00
#include "tap.hpp"
#include "time/parse.hpp"
int
main (int, char**)
{
cruft::TAP::logger tap;
2018-01-01 15:47:41 +11:00
static const char* BAD[] = {
"",
"foo"
};
for (const auto &t: BAD) {
tap.expect_throw<std::invalid_argument> (
[t] () { cruft::time::iso8601::parse (t); },
2021-04-13 16:05:08 +10:00
"invalid timestamp '{:s}'", t
2018-01-01 15:47:41 +11:00
);
}
using namespace std::chrono_literals;
static const struct {
const char *string;
std::chrono::nanoseconds value;
const char *message;
} DECODE[] = {
{ "1970-01-01T00:00:00Z", 0s, "UNIX epoch" },
{ "1970-01-01T01:01:00+01:01", 0s, "UNIX epoch, +offset" },
{ "1985-04-12T23:20:50.52Z", 482196050520ms, "fractional UTC time" },
{ "2000-02-28T01:00:00Z", 951699600s, "leap year, pre-29th" },
{ "2000-02-29T01:00:00Z", 951786000s, "leap year, on-29th" },
{ "2000-03-01T01:00:00Z", 951872400s, "leap year, post-29th" },
{ "1990-12-31T23:59:60Z", 662688000s, "1990 leap second in UTC" },
{ "1990-12-31T15:59:60-08:00", 662688000s, "1990 leap second in PST" },
};
for (const auto &t: DECODE) {
auto val = cruft::time::iso8601::parse (t.string);
2021-04-13 16:05:08 +10:00
tap.expect_eq (val, t.value, "{:s}", t.message);
2018-01-01 15:47:41 +11:00
}
tap.expect_eq (
cruft::time::iso8601::parse ("1996-12-19T16:39:57-08:00"),
cruft::time::iso8601::parse ("1996-12-20T00:39:57Z"),
2018-01-01 15:47:41 +11:00
"timezone equivalence"
);
return tap.status ();
}