parse Iso(text: string) - > Result<Date, string>- Module
- std.
date - Receiver
- Not applicable
- Minimum arity
- 1
- Maximum arity
- 1
Declaration and parameters
LinkAvailability
Included by default: Pending
Import std.date in language mode 5.21.
The current compiler selects std/date.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1877 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 60 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1877 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 239 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1973
Receiver
Parameter constraints
Date.parseIso accepts exactly ten ASCII characters in the form YYYY-MM-DD. The year has four digits and the month and day each have two digits. Hyphens occur after the year and month.
The components must form a valid Gregorian date in the same range as Date.fromYmd. Valid text returns Ok containing that date. Invalid grammar or an invalid calendar date returns Err containing a descriptive nonempty string.
Parsing does not trim whitespace or normalize another spelling. Signs, omitted zero padding, alternate separators, non ASCII digits, timestamps, time zones, and extra characters are rejected.
Date.toIso returns exactly the same ten character form using ASCII digits and zero padding. It has no locale or time zone parameter and leaves the receiver unchanged.
Parsing the formatted text recovers the original date. Formatting accepted input returns that same text. Date.fromYmd and Date.parseIso establish the same component and range invariants.
The receiver or argument is evaluated once before the operation. These operations do not change the input or perform external effects. Error strings have no stable machine readable format.
Related rule: Exchange dates in one fixed ISO formResult
The declared result type is Result<Date, string>.
Effects
Failures
Result and effects
LinkFixed width ASCII text and the shared Gregorian range are required. Malformed text or an invalid date returns Err.
Resolved contracts
LinkRelated entries
LinkExchange dates in one fixed ISO form
LinkExchange dates in one fixed ISO form
Date.parseIso accepts exactly ten ASCII characters in the form YYYY-MM-DD. The year has four digits and the month and day each have two digits. Hyphens occur after the year and month.
The components must form a valid Gregorian date in the same range as Date.fromYmd. Valid text returns Ok containing that date. Invalid grammar or an invalid calendar date returns Err containing a descriptive nonempty string.
Parsing does not trim whitespace or normalize another spelling. Signs, omitted zero padding, alternate separators, non ASCII digits, timestamps, time zones, and extra characters are rejected.
Date.toIso returns exactly the same ten character form using ASCII digits and zero padding. It has no locale or time zone parameter and leaves the receiver unchanged.
Parsing the formatted text recovers the original date. Formatting accepted input returns that same text. Date.fromYmd and Date.parseIso establish the same component and range invariants.
The receiver or argument is evaluated once before the operation. These operations do not change the input or perform external effects. Error strings have no stable machine readable format.
Why
One fixed calendar format makes date text predictable to store, compare lexically, and exchange. Sharing validation with component construction prevents two constructors from assigning different meanings to the same date.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 13 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 34 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 76 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 110 - compiler/crates/topaz_product_runtime/src/lib.rs at
912a5e86e1f9· 17 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 130 -
topaz.stdlib.contract.date-isoatworking-tree - topaz.decision.date-civil-range.1 at
topaz.decisi
Parse canonical date text and preserve its components
Linkimport std.date { Date }import std.testimport std.iofor text in (["0001-01-01", "1582-10-10", "2000-02-29", "9999-12-31"]) {let date = test.assertOk(Date.parseIso(text))io.writeLine(date.toIso())}let leap = test.assertOk(Date.parseIso("2000-02-29"))io.writeLine("{leap.year()} {leap.month()} {leap.day()}")
0001-01-01 1582-10-10 2000-02-29 9999-12-31 2000 2 29
Expected result
Expected result
Execution completes. Exit code 0.
0001-01-011582-10-102000-02-299999-12-312000 2 29
Source evidence
- compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 13 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 34 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 76 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 110 - compiler/crates/topaz_product_runtime/src/lib.rs at
912a5e86e1f9· 17 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 130 -
topaz.stdlib.contract.date-isoatworking-tree - topaz.decision.date-civil-range.1 at
topaz.decisi
Reject alternate spellings and invalid calendar dates
Linkimport std.date { Date }import std.iofor text in (["", "2024-2-09", "2024-02-9", "2024/02/09", " 2024-02-09", "2024-02-09 ", "2024-02-09T00:00:00Z", "+2024-02-09", "2024-02-09", "2024-02-30", "1900-02-29", "10000-01-01"]) {match (Date.parseIso(text)) {case Err(_) => io.writeLine("rejected")case Ok(_) => io.writeLine("accepted")}}
rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected
Expected result
Expected result
Execution completes. Exit code 0.
rejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejected
Source evidence
- compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 13 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 34 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 76 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 110 - compiler/crates/topaz_product_runtime/src/lib.rs at
912a5e86e1f9· 17 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 130 -
topaz.stdlib.contract.date-isoatworking-tree - topaz.decision.date-civil-range.1 at
topaz.decisi
Reject year zero in ISO text
Linkimport std.date { Date }import std.iomatch (Date.parseIso("0000-01-01")) {case Err(_) => io.writeLine("year zero rejected")case Ok(_) => io.writeLine("accepted")}
year zero rejected
Expected result
Expected result
Execution completes. Exit code 0.
year zero rejected
Source evidence
- compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 13 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 34 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 76 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 110 - compiler/crates/topaz_product_runtime/src/lib.rs at
912a5e86e1f9· 17 - compiler/crates/topaz_value/src/value/builtins/date.rs at
912a5e86e1f9· 130 -
topaz.stdlib.contract.date-isoatworking-tree - topaz.decision.date-civil-range.1 at
topaz.decisi