Skip to content

Bounds datetimes to years 0000 through 9999 - #255

Merged
johnnyt merged 1 commit into
mainfrom
px-dd1b-datetime-range
Nov 20, 2026
Merged

johnnyt merged 1 commit into
mainfrom
px-dd1b-datetime-range

Conversation

@johnnyt

@johnnyt johnnyt commented Nov 20, 2026

Copy link
Copy Markdown
Member

What

A datetime is an instant from 0000-01-01T00:00:00Z through 9999-12-31T23:59:59.999999Z in UTC, and nothing outside that range is a datetime (ruled by the operator, 2026-11-20). An offset can carry a string or a literal that names a time inside those years to an instant outside them. This PR enforces the range at the two places a string becomes a datetime.

  • Cast. A string cast to ::datetime whose UTC instant is outside the range answers :undefined, the answer an unreadable string already gets (Predicator.Cast.cast/2). Before, past the top the cast raised an ArgumentError, and below the bottom it answered a year -0001 value.
  • Literal. A datetime literal whose UTC instant is outside the range is refused by the lexer with the error a malformed datetime literal already gets: the same Invalid datetime format: ... message and a span over the whole literal (parse_date_content/1 in lib/predicator/lexer.ex). Before, a literal past the top lexed to a year-10000 value, and its decompiled text had a five-digit year that the lexer refused, so the text round trip broke.
  • Spec. docs/isa.md section 3 states the range in one sentence.

No other function changes its answer.

ISA

The ISA does not move: no opcode, operand or encoding changes, and isa_version stays 6.

Corpus

conformance/manifest.json and conformance/corpus/tier-7.json moved because conformance/cases/casts.json gained four new cases. mix corpus.generate wrote both files. No existing case changed. The new ids:

  • casts/string-to-datetime-last-in-range-instant
  • casts/string-to-datetime-offset-past-the-top-is-undefined
  • casts/string-to-datetime-offset-below-the-bottom-is-undefined
  • casts/string-to-datetime-offset-at-the-bottom-is-in-range

The corpus hash moves. conformance/examples/registry.example.json's corpus_hash follows the manifest, as the ratchet registry test requires. That example has no tier-7 entries, so nothing else in it changed.

The corpus has no shape for a source that fails to compile (conformance/README.md, "Not a parse-error test"). So the lexer refusal is pinned by unit tests rather than a corpus case, and the follow-up for a corpus shape is tracked separately.

Tests

  • test/predicator/cast_test.exs tests both bounds with positive and negative offsets. A sweep over offsets at both bounds turns any raise into a test failure.
  • test/predicator/lexer_literals_test.exs tests the refusal at each end with its message and span, and acceptance at each bound.
  • test/predicator/visitors/string_visitor_datetime_range_test.exs is the round-trip property. It is enumerated rather than randomised, because the repo carries no property-testing dependency and this PR adds none. It applies every UTC offset from -23:59 to +23:59, minute by minute, to several local times at each bound. Each candidate's expected verdict is computed without the lexer. It asserts that a literal is accepted exactly when its instant is in range, that a refused literal gets the malformed-literal error, and that every accepted literal decompiles to text that parses to the same value.

Each new range guard was removed in turn, and each removal turned the new tests red on assertions.

Gate

The full mix quality gate is green on this commit. Doctor, Gettext, Sobelow and the opt-in Diataxis stage report skipped, as they do on main.

Changelog

changelog.d/px-dd1b.md, under Fixed.

A datetime is an instant from 0000-01-01T00:00:00Z through
9999-12-31T23:59:59.999999Z in UTC, and nothing outside that range is
one (ruled by the operator, 2026-11-20). An offset can carry a string
or a literal naming a time inside those years to an instant outside
them.

A string cast to ::datetime naming such an instant now answers
:undefined, the answer an unreadable string gets: past the top it
raised out of the cast, below the bottom it answered a year -0001
value. A datetime literal naming one is refused by the lexer with the
same malformed-literal error and span a bad datetime literal gets;
before, the top side lexed to a value whose decompiled text carried a
five-digit year the lexer refused, so the text round trip broke.

docs/isa.md section 3 states the range. conformance/cases/casts.json
gains four new cases at both bounds; mix corpus.generate rewrote
tier-7 and the manifest, so the corpus hash moves and the ISA version
does not. The ratchet example's corpus_hash follows the manifest. The
corpus has no shape for a compile refusal, so the lexer refusal is
pinned by unit tests and an enumerated round-trip test instead.

Refs: px-dd1b
@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
lib/predicator/cast.ex 100.00% <100.00%> (ø)
lib/predicator/lexer.ex 97.87% <100.00%> (+<0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@johnnyt
johnnyt merged commit dc721a7 into main Nov 20, 2026
2 checks passed
@johnnyt
johnnyt deleted the px-dd1b-datetime-range branch November 20, 2026 20:44
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.

1 participant