136 lines
4.3 KiB
C++
136 lines
4.3 KiB
C++
#pragma once
|
|
|
|
#include <Void/Scheduler/Types.hpp>
|
|
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Scheduler
|
|
{
|
|
struct CivilMinute {
|
|
std::int32_t year{1970};
|
|
std::uint32_t month{1}; // 1..12
|
|
std::uint32_t day{1}; // 1..31
|
|
std::uint32_t hour{0}; // 0..23
|
|
std::uint32_t minute{0}; // 0..59
|
|
std::uint32_t weekday{4}; // 0=Sunday .. 6=Saturday
|
|
};
|
|
|
|
/// Convert civil UTC components to minutes since 1970-01-01T00:00Z.
|
|
[[nodiscard]] std::int64_t
|
|
civil_to_epoch_minute(std::int32_t year, std::uint32_t month,
|
|
std::uint32_t day, std::uint32_t hour,
|
|
std::uint32_t minute) noexcept;
|
|
|
|
/// Convert minutes since 1970-01-01T00:00Z to civil components + weekday.
|
|
[[nodiscard]] CivilMinute
|
|
epoch_minute_to_civil(std::int64_t epoch_minute) noexcept;
|
|
|
|
/// Explicit DST transition rule for deterministic timezone resolution
|
|
/// without relying on ambient OS tzdata behavior.
|
|
struct DstTransitionRule {
|
|
std::string name{};
|
|
std::int32_t standard_offset_minutes{0};
|
|
std::int32_t dst_offset_minutes{0};
|
|
/// Spring-forward gap window in local wall minutes `[start, end)`.
|
|
std::optional<std::int64_t> gap_start_local_min{};
|
|
std::optional<std::int64_t> gap_end_local_min{};
|
|
/// Fall-back fold window in local wall minutes `[start, end)`.
|
|
/// Minutes in `[start, end)` happen twice: fold 0 at `dst_offset`,
|
|
/// fold 1 at `standard_offset`.
|
|
std::optional<std::int64_t> fold_start_local_min{};
|
|
std::optional<std::int64_t> fold_end_local_min{};
|
|
};
|
|
|
|
struct ResolvedSlot {
|
|
std::int64_t utc_ms{0};
|
|
std::uint8_t fold_index{0};
|
|
};
|
|
|
|
/// Explicit timezone registry. Recognizes `"UTC"`, `"Z"`, fixed numeric
|
|
/// offsets (`+HH:MM`, `-HH:MM`, `UTC+HH:MM`, `UTC-HH:MM`), and explicitly
|
|
/// registered `DstTransitionRule` zones. Unknown zones fail with
|
|
/// `UnsupportedTimezone` (never silent UTC fallback).
|
|
class TimezoneRegistry final
|
|
{
|
|
public:
|
|
|
|
TimezoneRegistry() = default;
|
|
|
|
void register_dst_zone(DstTransitionRule rule);
|
|
|
|
[[nodiscard]] Result<void>
|
|
validate_timezone(std::string_view tz) const;
|
|
|
|
[[nodiscard]] Result<std::vector<ResolvedSlot>>
|
|
resolve_local_minute(std::string_view tz,
|
|
std::int64_t local_epoch_minute,
|
|
DstGapPolicy gap_policy,
|
|
DstFoldPolicy fold_policy) const;
|
|
|
|
/// Approximate local epoch minute range covering `[utc_min_lo,
|
|
/// utc_min_hi]` for `tz`.
|
|
[[nodiscard]] Result<std::pair<std::int64_t, std::int64_t>>
|
|
local_minute_bounds(std::string_view tz,
|
|
std::int64_t utc_min_lo,
|
|
std::int64_t utc_min_hi) const;
|
|
|
|
private:
|
|
|
|
std::map<std::string, DstTransitionRule, std::less<>> m_dst_zones{};
|
|
};
|
|
|
|
struct MaterializedSlots {
|
|
std::vector<ResolvedSlot> slots{};
|
|
std::int64_t next_cursor_utc_ms{0};
|
|
bool more_pending{false};
|
|
};
|
|
|
|
/// 5-field cron-like calendar expression (`minute hour day month weekday`)
|
|
/// supporting `*`, lists, ranges, and steps. Both day-of-month and weekday
|
|
/// constraints apply.
|
|
class CalendarExpression final
|
|
{
|
|
public:
|
|
|
|
[[nodiscard]] static Result<CalendarExpression>
|
|
parse(std::string_view expression);
|
|
|
|
[[nodiscard]] bool matches(const CivilMinute& cm) const noexcept;
|
|
|
|
/// Next matching local epoch minute strictly >= `from_local_min`,
|
|
/// capped by `max_local_min`.
|
|
[[nodiscard]] std::optional<std::int64_t>
|
|
next_matching_minute(std::int64_t from_local_min,
|
|
std::int64_t max_local_min) const noexcept;
|
|
|
|
/// Latest matching local epoch minute <= `from_local_min` and
|
|
/// >= `min_local_min`. Allows O(1) coalescing across millions of
|
|
/// missed minutes.
|
|
[[nodiscard]] std::optional<std::int64_t>
|
|
prev_matching_minute(std::int64_t from_local_min,
|
|
std::int64_t min_local_min) const noexcept;
|
|
|
|
[[nodiscard]] Result<MaterializedSlots>
|
|
materialize_range(const CalendarSpec& spec,
|
|
const TimezoneRegistry& tz_registry,
|
|
std::int64_t after_utc_ms,
|
|
std::int64_t up_to_utc_ms,
|
|
MisfirePolicy misfire,
|
|
std::size_t max_catch_up) const;
|
|
|
|
private:
|
|
|
|
std::uint64_t m_minutes{0}; // bits 0..59
|
|
std::uint32_t m_hours{0}; // bits 0..23
|
|
std::uint32_t m_days{0}; // bits 1..31
|
|
std::uint32_t m_months{0}; // bits 1..12
|
|
std::uint32_t m_weekdays{0}; // bits 0..6 (0 = Sunday)
|
|
};
|
|
|
|
} // namespace Void::Scheduler
|