year() - > int- Module
- std.
date - Receiver
- Date
- Minimum arity
- 0
- Maximum arity
- 0
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· 1107 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 60 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1107 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 461 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 289
Receiver
Date
Parameter constraints
Date is an immutable proleptic Gregorian calendar date from 0001-01-01 through 9999-12-31. It contains no time of day, time zone, or locale. The same calendar rules apply before and after 1582.
Date.fromYmd accepts years from 1 through 9999 and months from 1 through 12. The day must be a real day of the selected month and year. All range endpoints are included.
A year divisible by 4 is a leap year except a century year that is not divisible by 400. February has 29 days in a leap year and 28 otherwise. April, June, September, and November have 30 days. The other months have 31.
Valid components return Ok containing that date. Year zero, a year outside the supported range, an invalid month, and an invalid day return Err containing a descriptive nonempty string. Components are not normalized into another date.
The error string describes the failed construction and has no stable machine readable format. Callers branch on Ok or Err.
Date.year, Date.month, and Date.day are calls with no arguments. They return the validated components of the receiver. A valid Date makes these observations total and leaves the receiver unchanged.
Arguments are evaluated once in source order before validation. An earlier fault prevents later evaluation. Constructors and observations do not read a host clock or perform external effects.
Related rule: Construct valid Gregorian civil datesResult
The declared result type is int.
Effects
Failures
Result and effects
LinkThis call returns the receiver year and leaves the date unchanged.
Resolved contracts
LinkRelated entries
LinkConstruct valid Gregorian civil dates
LinkConstruct valid Gregorian civil dates
Date is an immutable proleptic Gregorian calendar date from 0001-01-01 through 9999-12-31. It contains no time of day, time zone, or locale. The same calendar rules apply before and after 1582.
Date.fromYmd accepts years from 1 through 9999 and months from 1 through 12. The day must be a real day of the selected month and year. All range endpoints are included.
A year divisible by 4 is a leap year except a century year that is not divisible by 400. February has 29 days in a leap year and 28 otherwise. April, June, September, and November have 30 days. The other months have 31.
Valid components return Ok containing that date. Year zero, a year outside the supported range, an invalid month, and an invalid day return Err containing a descriptive nonempty string. Components are not normalized into another date.
The error string describes the failed construction and has no stable machine readable format. Callers branch on Ok or Err.
Date.year, Date.month, and Date.day are calls with no arguments. They return the validated components of the receiver. A valid Date makes these observations total and leaves the receiver unchanged.
Arguments are evaluated once in source order before validation. An earlier fault prevents later evaluation. Constructors and observations do not read a host clock or perform external effects.
Why
Validating civil dates at construction gives every date observation a definite value. A shared Gregorian calendar and year range make dates independent of locale, time zones, and historical calendar cutovers.
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· 164 -
topaz.stdlib.contract.date-calendaratworking-tree - topaz.decision.date-civil-range.1 at
topaz.decisi
Read leap day and both supported endpoints
Linkimport std.date { Date }import std.testimport std.iolet leap = test.assertOk(Date.fromYmd(2000, 2, 29))io.writeLine("{leap.year()} {leap.month()} {leap.day()}")io.writeLine(leap.toIso())io.writeLine(test.assertOk(Date.fromYmd(1, 1, 1)).toIso())io.writeLine(test.assertOk(Date.fromYmd(9999, 12, 31)).toIso())
2000 2 29 2000-02-29 0001-01-01 9999-12-31
Expected result
Expected result
Execution completes. Exit code 0.
2000 2 292000-02-290001-01-019999-12-31
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· 164 -
topaz.stdlib.contract.date-calendaratworking-tree - topaz.decision.date-civil-range.1 at
topaz.decisi
Reject invalid years, months, and days without normalization
Linkimport std.date { Date }import std.iofunction reject(year: int, month: int, day: int) -> () {match (Date.fromYmd(year, month, day)) {case Err(_) => io.writeLine("rejected")case Ok(_) => io.writeLine("accepted")}}reject(1900, 2, 29)reject(2023, 2, 29)reject(2024, 0, 1)reject(2024, 13, 1)reject(2024, 4, 31)reject(2024, 1, 0)reject(2024, 1, 32)reject(-1, 1, 1)reject(10000, 1, 1)reject(9223372036854775807, 1, 1)
rejected rejected rejected rejected rejected rejected rejected rejected rejected rejected
Expected result
Expected result
Execution completes. Exit code 0.
rejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejectedrejected
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· 164 -
topaz.stdlib.contract.date-calendaratworking-tree - topaz.decision.date-civil-range.1 at
topaz.decisi
Reject year zero during component construction
Linkimport std.date { Date }import std.iomatch (Date.fromYmd(0, 1, 1)) {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· 164 -
topaz.stdlib.contract.date-calendaratworking-tree - topaz.decision.date-civil-range.1 at
topaz.decisi
Evaluate all components before date validation
Linkimport std.date { Date }import std.iofunction observed(label: string, value: int) -> int {io.writeLine(label)return value}match (Date.fromYmd(observed("year", 2024), observed("month", 13), observed("day", 1))) {case Err(_) => io.writeLine("rejected")case Ok(_) => io.writeLine("accepted")}
year month day rejected
Expected result
Expected result
Execution completes. Exit code 0.
yearmonthdayrejected
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· 164 -
topaz.stdlib.contract.date-calendaratworking-tree - topaz.decision.date-civil-range.1 at
topaz.decisi