imsd-messages/tests/Time/main.cpp

76 lines
4.1 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Time: the service centre's ISO 8601 stamp, civil date arithmetic, and the
// captions the window shows — with the local offsets injected, so the test
// needs no time zone database.
import std;
import ImsdMessages;
using namespace ImsdMessages;
namespace {
std::int32_t failures = 0;
void Check(bool ok, std::string_view what) {
std::println("{} {}", ok ? "PASS" : "FAIL", what);
if (!ok) ++failures;
}
void CheckEqual(std::string_view actual, std::string_view expected, std::string_view what) {
const bool ok = actual == expected;
if (ok) std::println("PASS {}", what);
else std::println("FAIL {} — expected \"{}\", got \"{}\"", what, expected, actual);
if (!ok) ++failures;
}
}
int main() {
constexpr std::int64_t e1 = 1792190500; // 2026-10-17T00:41:40+02:00
constexpr std::int64_t day = 86400;
Check(DaysFromCivil(1970, 1, 1) == 0, "1970-01-01 is day 0");
Check(DaysFromCivil(2000, 3, 1) == 11017, "2000-03-01 is day 11017");
Check(CivilFromDays(0) == CivilDate{ 1970, 1, 1 }, "day 0 is 1970-01-01");
Check(CivilFromDays(DaysFromCivil(2026, 10, 17)) == CivilDate{ 2026, 10, 17 }, "civil round trip");
Check(CivilFromDays(DaysFromCivil(2024, 2, 29)) == CivilDate{ 2024, 2, 29 }, "leap day round trip");
Check(WeekdayFromDays(0) == 4, "1970-01-01 was a Thursday");
Check(WeekdayFromDays(DaysFromCivil(2026, 10, 17)) == 6, "2026-10-17 is a Saturday");
const std::optional<ParsedTime> a = ParseIso8601("2026-10-17T00:41:40+02:00");
Check(a && a->epoch == e1 && a->offsetMinutes == 120, "the SC stamp with +02:00");
const std::optional<ParsedTime> b = ParseIso8601("2026-10-16T22:41:40Z");
Check(b && b->epoch == e1 && b->offsetMinutes == 0, "the same instant in Z");
const std::optional<ParsedTime> c = ParseIso8601("2026-10-16T22:41:40");
Check(c && c->epoch == e1 && c->offsetMinutes == 0, "no offset reads as UTC");
const std::optional<ParsedTime> d = ParseIso8601("2026-10-17T00:41:40.123+0200");
Check(d && d->epoch == e1 && d->offsetMinutes == 120, "fractional seconds and a colon-less offset");
const std::optional<ParsedTime> e = ParseIso8601("2026-10-16 22:41:40Z");
Check(e && e->epoch == e1, "a space instead of T");
const std::optional<ParsedTime> f = ParseIso8601("2026-10-17T00:41:40-05:30");
Check(f && f->epoch == e1 + 27000 && f->offsetMinutes == -330, "a negative half-hour offset");
for (std::string_view bad : { "yesterday", "2026-13-01T00:00:00Z", "2026-02-30T00:00:00Z", "2025-02-29T00:00:00Z", "2026-10-17T00:41:40+99:00", "2026-10-17T00:41:40+02:00x", "", "2026-10-17T00:41", "2026-10-17T00:41:40+2" }) {
Check(!ParseIso8601(bad), std::format("rejects \"{}\"", bad));
}
Check(LocalDay(e1, 120) == DaysFromCivil(2026, 10, 17), "local day at +02:00");
Check(LocalDay(e1, 0) == DaysFromCivil(2026, 10, 16), "local day in UTC");
CheckEqual(ClockCaption(e1, 120), "00:41", "clock at +02:00");
CheckEqual(ClockCaption(e1, 0), "22:41", "clock in UTC");
CheckEqual(ClockCaption(e1, -330), "17:11", "clock at -05:30");
CheckEqual(DayCaption(e1, 120, e1 + 3600, 120), "Today", "day caption today");
CheckEqual(DayCaption(e1, 120, e1 + day, 120), "Yesterday", "day caption yesterday");
CheckEqual(DayCaption(e1, 120, e1 + 10 * day, 120), "Sat 17 Oct", "day caption within the year");
CheckEqual(DayCaption(e1, 120, e1 + 100 * day, 120), "17 October 2026", "day caption in another year");
CheckEqual(DayCaption(e1 - 4300, 120, e1, 120), "Yesterday", "23:30 last night is yesterday at +02:00");
CheckEqual(DayCaption(e1 - 4300, 0, e1, 0), "Today", "the same two instants are one day in UTC: the viewer's offset decides");
CheckEqual(ListCaption(e1, 120, e1 + 3600, 120), "00:41", "list caption today");
CheckEqual(ListCaption(e1, 120, e1 + 3 * day, 120), "Sat", "list caption within six days");
CheckEqual(ListCaption(e1, 120, e1 + 10 * day, 120), "17 Oct", "list caption within the year");
CheckEqual(ListCaption(e1, 120, e1 + 100 * day, 120), "17 Oct 2026", "list caption in another year");
return failures == 0 ? 0 : 1;
}