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.
146 lines
7.6 KiB
C++
146 lines
7.6 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
// 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;
|
|
import std;
|
|
|
|
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);
|
|
}
|
|
}
|