Skip to content

feat(core): time zone rules with a replaceable source (#580, part 1) - #641

Merged
ajianaz merged 1 commit into
developfrom
feat/cron-timezones
Nov 12, 2026
Merged

ajianaz merged 1 commit into
developfrom
feat/cron-timezones

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What

Part 1 of #580: the time-zone layer in tole-core (tz), with no embedded zone data and no new dependency. Schedules do not use it yet. Part 2 wires per-job time zones into the schedules, the ledger (each fire records its zone and DST case), the CLI (--timezone, cron.timezone, cron.tz_source, list in local and UTC) and the docs.

Why

Jobs must fire at the owner's wall-clock time (09:00 Asia/Jakarta) without silently assuming UTC and without forcing a tz database into every binary (#580, design §4). The full change is about 1,900 lines, so it is split at the cleanest seam: the self-contained zone layer here, then its use.

Changes

How a zone is resolved

  • tz::Zone is one of:

    • UTC;
    • a fixed offset +HH:MM / -HH:MM (at most 14 hours);
    • rules from TZif bytes (RFC 8536, versions 1 to 4);
    • rules from a POSIX TZ string (EST5EDT,M3.2.0,M11.1.0).
  • TZif: the listed transitions are used, then the POSIX TZ footer for every later instant. That matters: slim zoneinfo files, the default since tzdata 2020b, describe all future DST changes only through the footer. The footer supports Mm.w.d, Jn and n dates, signed times up to 167 hours (TZif v3) and southern-hemisphere rules (DST across the new year).

  • Zone::from_local(wall_clock) returns one of:

    • Single(t): exactly one instant;
    • Gap(t): the time does not exist, and t is the first instant after the gap. It is found by binary search on the offset, so no next-transition logic is needed;
    • Ambiguous(first, second): the time happens twice.

    Part 2's schedule fires a gap time at t and an ambiguous time only at first, which gives at-most-once.

  • ZoneSource is the replaceable source, a trait:

    • OffsetOnly (default, no data) refuses every IANA name with the remedy: use UTC or +07:00, or set cron.tz_source = "system".
    • ZoneinfoDir reads /usr/share/zoneinfo (or any directory) at run time, so rules follow OS tzdata updates at zero binary cost. It follows the directory's own alias links (US/Eastern), but never a link that leads out of it. It requires a regular file of at most 512 KiB and opens with O_NONBLOCK, so a FIFO is refused instead of waited on.
    • An embedder plugs in its own source (bundled data, a database) without touching the scheduler.
  • tz::resolve(source, name) handles UTC and offsets itself. It refuses an invalid name before any disk access: 1 to 64 bytes, /-separated components of [A-Za-z0-9_+-], none empty or starting with .. Only then does it ask the source. Every error names the zone, says what is wrong and gives a remedy.

  • Robustness: TZif parsing is bounds-checked with checked arithmetic. Counts, type indices, transition order and offsets (±26 h) are validated, and the footer must parse. Leap-second (right/) zones are refused. Bad data is an error, never a panic, and evaluation saturates instead of overflowing.

Decisions to check

  • bundled is not built. The issue lists a tz-bundled cargo feature. A bundled database is just another ZoneSource, so it can come later as a separate feature or crate. Nothing is embedded in core now, and there is no binary-size delta to report. The issue's measurement belongs with that feature.
  • No new dependency. No Cargo.toml or Cargo.lock change. Unique crates from cargo tree -e normal --prefix none | sort -u: tole-core --no-default-features stays at 50 and tole-cli at 207, so the delta is 0. libc was already a dependency; it is used for O_NONBLOCK under cfg(unix) only.
  • Fixed offsets are at most ±14:00 and must be written +HH:MM. +7, +0700 and 07:00 are refused with the expected form.
  • Default DST rule: when a POSIX string has a DST name but no rule, the US rule (M3.2.0,M11.1.0) is used, as glibc does. Real zoneinfo footers always carry a rule.

Docs

  • docs/automation-design.md §4: an "as implemented, part 1" note.
  • CHANGELOG [Unreleased] / Added.

Testing

New crates/tole-core/tests/tz_rules.rs (8 tests):

  • offsets around both US changes, and a southern-hemisphere rule;
  • from_local for a normal time, a gap (02:30 on 2026-03-08 → 07:00Z) and an overlap (01:30 on 2026-11-01 → 05:30Z and 06:30Z);
  • a hand-built TZif v2 file: before the first transition, a listed transition, then the footer rule;
  • broken TZif: every truncation plus 2,000 seeded random mutations never panic, and five specific defects are errors (not TZif, type index, offset, order, footer);
  • names and offsets: valid forms, invalid forms with the +07:00 remedy, offset-only naming its source and the fix, path-like names refused before disk access, and a missing zone naming the directory;
  • a zoneinfo dir: an alias link inside it resolves, a link leading outside is refused, and a FIFO is refused within 5 s;
  • the system zoneinfo, when present, agrees with the POSIX rule on 400 instants of 2025 (it ran here: the full America/New_York file);
  • a custom ZoneSource serves names, its errors pass through, and offsets and validation stay the same.

The tests were written before the code; they could not compile until the API existed, so each rule's proof is a deliberate break, restored after each:

Deliberate break Failing test
Zone names not validated names_and_offsets_are_validated_with_a_remedy
Transitions out of order accepted broken_tzif_data_is_an_error_never_a_panic
Offsets out of range accepted broken_tzif_data_is_an_error_never_a_panic
TZif footer rule ignored tzif_transitions_then_the_footer_rule
Fixed offsets beyond 14 hours accepted names_and_offsets_are_validated_with_a_remedy
Zoneinfo dir follows a link out of itself a_zoneinfo_dir_stays_inside_itself_and_never_blocks
Zoneinfo dir opened without O_NONBLOCK (FIFO blocks) a_zoneinfo_dir_stays_inside_itself_and_never_blocks
Southern-hemisphere DST inverted offsets_follow_the_rule_around_both_changes
A gap maps to its start, not its end local_times_map_to_one_instant_a_gap_or_two_instants
An overlap reports one instant local_times_map_to_one_instant_a_gap_or_two_instants

The schedule-level DST tests (wall-clock 0 9 * * * across both changes, a skipped 02:30 firing once at 03:00, a repeated 01:30 firing once, */30 across the gap and the overlap, starting inside the repeated hour) and the ledger tests are written and pass on the part 2 work, with their own deliberate breaks. They come with part 2.

Gates:

  • cargo fmt --all
  • cargo clippy --workspace --all-targets --all-features -- -D warnings
  • cargo clippy -p tole-core --no-default-features --all-targets -- -D warnings
  • cargo test --workspace: 1627 passed
  • cargo test -p tole-core --no-default-features: 574 passed
  • cora review --no-llm --staged: no issues

The zone layer for per-job time zones, with no embedded data and no new
dependency. Schedules do not use it yet (part 2).

- tz::Zone: UTC, a fixed offset (at most 14 hours) or rules from TZif
  bytes (RFC 8536 versions 1 to 4, including the POSIX TZ footer that
  slim zoneinfo files use for every future date) or a POSIX TZ string.
- Zone::from_local maps a wall-clock time to one instant, the first
  instant after a DST gap, or both instants of an overlap.
- ZoneSource is a trait. OffsetOnly takes no data and refuses IANA
  names with the remedy; ZoneinfoDir reads the host's zoneinfo at run
  time, follows alias links only inside the directory, requires a
  regular file of at most 512 KiB and never waits on a FIFO.
- Invalid names, offsets and TZif data are errors with a remedy, never
  a panic; leap-second zones are refused.
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit 1f56ef2 into develop Nov 12, 2026
22 checks passed
@ajianaz
ajianaz deleted the feat/cron-timezones branch November 20, 2026 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants