Time: ISO 8601 stamps, civil dates and the captions
imsd renders the service centre's time stamp of a text as ISO 8601 with the centre's offset and adds the arrival epoch; the captions need the user's local day and clock. The parser is hand-rolled, with the calendar's own check of the date (a 30th of February is no date); the civil dates, the weekdays and the month names are std::chrono's calendar and its formatting; every caption takes the local offset of the instant as an argument, so nothing here depends on a time zone database and the tests inject the offsets. Tested: tests/Time, the KPN bench stamp in five spellings, rejected garbage and days the month does not have, the day and list captions around midnight and across a year.
This commit is contained in:
parent
692f550e31
commit
2a833b0bad
3 changed files with 215 additions and 0 deletions
|
|
@ -1,8 +1,146 @@
|
||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||||
|
|
||||||
|
/*
|
||||||
|
ImsdMessages:Time — the stamps of a text and the captions made of them.
|
||||||
|
|
||||||
|
imsd gives the service centre's stamp of a text in ISO 8601 with the
|
||||||
|
centre's offset ("2026-10-17T00:41:40+02:00") and adds the arrival as an
|
||||||
|
epoch. Captions need the user's local day and clock, which the caller
|
||||||
|
passes in as the local offset of the instant in question, so nothing here
|
||||||
|
depends on a time zone database: the tests inject the offsets they want.
|
||||||
|
*/
|
||||||
export module ImsdMessages:Time;
|
export module ImsdMessages:Time;
|
||||||
import std;
|
import std;
|
||||||
|
|
||||||
export namespace ImsdMessages {
|
export namespace ImsdMessages {
|
||||||
|
struct ParsedTime {
|
||||||
|
std::int64_t epoch = 0; // UTC seconds
|
||||||
|
std::int32_t offsetMinutes = 0; // the stamp's own offset from UTC
|
||||||
|
friend bool operator==(const ParsedTime&, const ParsedTime&) = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CivilDate {
|
||||||
|
std::int64_t year = 1970;
|
||||||
|
std::uint32_t month = 1;
|
||||||
|
std::uint32_t day = 1;
|
||||||
|
friend bool operator==(const CivilDate&, const CivilDate&) = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Days since 1970-01-01 of a proleptic Gregorian date, and the inverse:
|
||||||
|
// std::chrono's calendar.
|
||||||
|
constexpr std::int64_t DaysFromCivil(std::int64_t year, std::uint32_t month, std::uint32_t day) noexcept {
|
||||||
|
const std::chrono::year_month_day date{ std::chrono::year{ static_cast<std::int32_t>(year) }, std::chrono::month{ month }, std::chrono::day{ day } };
|
||||||
|
return std::chrono::sys_days{ date }.time_since_epoch().count();
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr CivilDate CivilFromDays(std::int64_t days) noexcept {
|
||||||
|
const std::chrono::year_month_day date{ std::chrono::sys_days{ std::chrono::days{ days } } };
|
||||||
|
return { static_cast<std::int32_t>(date.year()), static_cast<std::uint32_t>(date.month()), static_cast<std::uint32_t>(date.day()) };
|
||||||
|
}
|
||||||
|
|
||||||
|
// 0 = Sunday.
|
||||||
|
constexpr std::uint32_t WeekdayFromDays(std::int64_t days) noexcept {
|
||||||
|
return std::chrono::weekday{ std::chrono::sys_days{ std::chrono::days{ days } } }.c_encoding();
|
||||||
|
}
|
||||||
|
|
||||||
|
// The local calendar day of an instant, as days since 1970-01-01.
|
||||||
|
constexpr std::int64_t LocalDay(std::int64_t epoch, std::int32_t offsetMinutes) noexcept {
|
||||||
|
const std::int64_t local = epoch + static_cast<std::int64_t>(offsetMinutes) * 60;
|
||||||
|
return local >= 0 ? local / 86400 : (local - 86399) / 86400;
|
||||||
|
}
|
||||||
|
|
||||||
|
// "YYYY-MM-DDTHH:MM:SS[.fff][Z|±HH:MM|±HHMM]", read leniently (a space for
|
||||||
|
// the T, a comma for the point, a small z, an offset of hours alone); a
|
||||||
|
// stamp without an offset is read as UTC, and what does not parse is
|
||||||
|
// nullopt. Hand-rolled on purpose: the stamps come from the network's
|
||||||
|
// service centre, not from a locale.
|
||||||
|
std::optional<ParsedTime> ParseIso8601(std::string_view s) {
|
||||||
|
auto digits = [&](std::size_t at, std::size_t n, std::int64_t& out) {
|
||||||
|
if (at + n > s.size()) return false;
|
||||||
|
out = 0;
|
||||||
|
for (std::size_t i = at; i < at + n; ++i) {
|
||||||
|
const char c = s[i];
|
||||||
|
if (c < '0' || c > '9') return false;
|
||||||
|
out = out * 10 + (c - '0');
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
std::int64_t year = 0;
|
||||||
|
std::int64_t month = 0;
|
||||||
|
std::int64_t day = 0;
|
||||||
|
std::int64_t hour = 0;
|
||||||
|
std::int64_t minute = 0;
|
||||||
|
std::int64_t second = 0;
|
||||||
|
if (s.size() < 19 || !digits(0, 4, year) || s[4] != '-' || !digits(5, 2, month) || s[7] != '-' || !digits(8, 2, day)) return std::nullopt;
|
||||||
|
if ((s[10] != 'T' && s[10] != ' ') || !digits(11, 2, hour) || s[13] != ':' || !digits(14, 2, minute) || s[16] != ':' || !digits(17, 2, second)) return std::nullopt;
|
||||||
|
if (hour > 23 || minute > 59 || second > 60) return std::nullopt;
|
||||||
|
// The calendar's own check: a day the month does not have (a 30th of
|
||||||
|
// February) is no date, not the next month's.
|
||||||
|
if (!std::chrono::year_month_day{ std::chrono::year{ static_cast<std::int32_t>(year) }, std::chrono::month{ static_cast<std::uint32_t>(month) }, std::chrono::day{ static_cast<std::uint32_t>(day) } }.ok()) return std::nullopt;
|
||||||
|
std::size_t pos = 19;
|
||||||
|
if (pos < s.size() && (s[pos] == '.' || s[pos] == ',')) {
|
||||||
|
++pos;
|
||||||
|
while (pos < s.size() && s[pos] >= '0' && s[pos] <= '9') ++pos;
|
||||||
|
}
|
||||||
|
std::int32_t offset = 0;
|
||||||
|
if (pos < s.size()) {
|
||||||
|
if (s[pos] == 'Z' || s[pos] == 'z') {
|
||||||
|
++pos;
|
||||||
|
} else if (s[pos] == '+' || s[pos] == '-') {
|
||||||
|
const std::int32_t sign = s[pos] == '-' ? -1 : 1;
|
||||||
|
++pos;
|
||||||
|
std::int64_t hours = 0;
|
||||||
|
std::int64_t minutes = 0;
|
||||||
|
if (!digits(pos, 2, hours)) return std::nullopt;
|
||||||
|
pos += 2;
|
||||||
|
if (pos < s.size() && s[pos] == ':') ++pos;
|
||||||
|
if (pos < s.size()) {
|
||||||
|
if (!digits(pos, 2, minutes)) return std::nullopt;
|
||||||
|
pos += 2;
|
||||||
|
}
|
||||||
|
if (hours > 14 || minutes > 59) return std::nullopt;
|
||||||
|
offset = sign * static_cast<std::int32_t>(hours * 60 + minutes);
|
||||||
|
} else {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pos != s.size()) return std::nullopt;
|
||||||
|
const std::int64_t days = DaysFromCivil(year, static_cast<std::uint32_t>(month), static_cast<std::uint32_t>(day));
|
||||||
|
const std::int64_t local = days * 86400 + hour * 3600 + minute * 60 + second;
|
||||||
|
return ParsedTime{ local - static_cast<std::int64_t>(offset) * 60, offset };
|
||||||
|
}
|
||||||
|
|
||||||
|
// "14:05" in the local time the offset describes.
|
||||||
|
std::string ClockCaption(std::int64_t epoch, std::int32_t offsetMinutes) {
|
||||||
|
const std::int64_t local = epoch + static_cast<std::int64_t>(offsetMinutes) * 60;
|
||||||
|
const std::int64_t secondsOfDay = local - LocalDay(epoch, offsetMinutes) * 86400;
|
||||||
|
return std::format("{:02}:{:02}", secondsOfDay / 3600, secondsOfDay % 3600 / 60);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The day separator of a conversation: "Today", "Yesterday", "Fri 10 Oct"
|
||||||
|
// within the year, "17 October 2025" before it. `now` and its offset
|
||||||
|
// say what today is.
|
||||||
|
std::string DayCaption(std::int64_t epoch, std::int32_t offsetMinutes, std::int64_t now, std::int32_t nowOffsetMinutes) {
|
||||||
|
const std::int64_t day = LocalDay(epoch, offsetMinutes);
|
||||||
|
const std::int64_t today = LocalDay(now, nowOffsetMinutes);
|
||||||
|
if (day == today) return "Today";
|
||||||
|
if (day == today - 1) return "Yesterday";
|
||||||
|
const CivilDate date = CivilFromDays(day);
|
||||||
|
if (date.year == CivilFromDays(today).year) return std::format("{:%a} {} {:%b}", std::chrono::weekday{ WeekdayFromDays(day) }, date.day, std::chrono::month{ date.month });
|
||||||
|
return std::format("{} {:%B} {}", date.day, std::chrono::month{ date.month }, date.year);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The time column of the conversation list: the clock today, the weekday
|
||||||
|
// within the last six days, "10 Oct" within the year, "10 Oct 2025"
|
||||||
|
// before it.
|
||||||
|
std::string ListCaption(std::int64_t epoch, std::int32_t offsetMinutes, std::int64_t now, std::int32_t nowOffsetMinutes) {
|
||||||
|
const std::int64_t day = LocalDay(epoch, offsetMinutes);
|
||||||
|
const std::int64_t today = LocalDay(now, nowOffsetMinutes);
|
||||||
|
if (day == today) return ClockCaption(epoch, offsetMinutes);
|
||||||
|
if (day < today && today - day <= 6) return std::format("{:%a}", std::chrono::weekday{ WeekdayFromDays(day) });
|
||||||
|
const CivilDate date = CivilFromDays(day);
|
||||||
|
if (date.year == CivilFromDays(today).year) return std::format("{} {:%b}", date.day, std::chrono::month{ date.month });
|
||||||
|
return std::format("{} {:%b} {}", date.day, std::chrono::month{ date.month }, date.year);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -134,6 +134,7 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
|
||||||
ApplyGioFlags(cfg);
|
ApplyGioFlags(cfg);
|
||||||
|
|
||||||
cfg.AddTest("Number").Dependencies({ Core.get() });
|
cfg.AddTest("Number").Dependencies({ Core.get() });
|
||||||
|
cfg.AddTest("Time").Dependencies({ Core.get() });
|
||||||
|
|
||||||
LintRules::AddRules(cfg);
|
LintRules::AddRules(cfg);
|
||||||
return cfg;
|
return cfg;
|
||||||
|
|
|
||||||
76
tests/Time/main.cpp
Normal file
76
tests/Time/main.cpp
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue