Date structv0.1.0
pub struct Date {
pub year: int32;
pub month: uint8;
pub day: uint8;
}
Copy. Only constructible through the fallible New factory, which lets everything downstream assume validity.
Fields
| Name | Type | Description |
|---|---|---|
year | int32 | The year, astronomically numbered: year 0 exists and is 1 BCE. |
month | uint8 | The month, 1 through 12. |
day | uint8 | The day, 1 through the month's length. |
Associated functions
New
pub func New(year: int32, month: uint8, day: uint8) -> Date?
The date, when it exists.
| Name | Type | Description |
|---|---|---|
year | int32 | the year, as a signed count from year zero |
month | uint8 | the month, 1 through 12 |
day | uint8 | the day of the month, counting from one |
Returns: the date, or None when the components do not name one
FromEpochDays
pub func FromEpochDays(days: int64) -> Date
The date days after 1970-01-01, negative before it.
The inverse of DaysFromEpoch, exactly: the pair round-trips every day of every representable year.
| Name | Type | Description |
|---|---|---|
days | int64 | how many days |
Returns: the date that many days after 1970-01-01
Methods
DaysFromEpoch
pub func DaysFromEpoch(self: &Date) -> int64
Days since 1970-01-01, negative before it. The civil-calendar formula: the year is shifted so March opens it, which puts the leap day at the end where it cannot disturb the month arithmetic.
Returns: days since 1970-01-01, negative before it
DayOfWeek
pub func DayOfWeek(self: &Date) -> uint8
The day of the week, 0 for Sunday through 6 for Saturday.
Returns: the day of the week, with Monday as one
DayOfYear
pub func DayOfYear(self: &Date) -> uint16
The day of the year, 1 through 365 or 366.
Returns: the day of the year, counting from one
Equals
pub func Equals(self: &Date, other: Date) -> bool
Whether two dates are the same day.
| Name | Type | Description |
|---|---|---|
other | Date | the value to compare against |
Returns: whether the two are the same
Compare
pub func Compare(self: &Date, other: Date) -> Ordering
How this date orders against other.
| Name | Type | Description |
|---|---|---|
other | Date | the value to compare against |
Returns: how this one orders against other
PlusDays
pub func PlusDays(self: &Date, days: int64) -> Date?
The date days later, negative for earlier, or None outside the years the calendar spells.
Exact: a day is a day, and the round trip through the epoch count neither loses nor invents one.
| Name | Type | Description |
|---|---|---|
days | int64 | how many days |
Returns: the date that many days later, or None when it leaves the range
PlusMonths
pub func PlusMonths(self: &Date, months: int64) -> Date?
The date months later, clamped to the month's end where the day is missing.
January 31st plus one month is the last day of February; the clamp is the policy the module header states.
| Name | Type | Description |
|---|---|---|
months | int64 | how many months |
Returns: the date that many months later, clamped to the month's last day, or None when it leaves the range
PlusYears
pub func PlusYears(self: &Date, years: int64) -> Date?
The date years later, with February 29th clamping to the 28th where the year is not leap.
| Name | Type | Description |
|---|---|---|
years | int64 | how many years |
Returns: the date that many years later, clamped for 29 February, or None when it leaves the range
DaysSince
pub func DaysSince(self: &Date, earlier: Date) -> int64
The whole days from earlier to this date, negative when it is the later one.
| Name | Type | Description |
|---|---|---|
earlier | Date | the earlier reading |
Returns: how many days earlier is before this date, negative if it is after
Implements Display
Date conforms to Display.
WriteDisplay
pub func WriteDisplay(
self: &Date,
writer: &var TextWriter,
spec: FormatSpec
) -> ! FormatError
Writes the date as YYYY-MM-DD.
A year outside 0 through 9999 carries a sign and as many digits as it needs, which is what ISO 8601 requires
for one: four digits is the agreed width, and anything wider is ambiguous without a sign to mark it.
Reports whatever the writer reported, and UnsupportedRequest for a style other than iso8601 or
rfc3339, or for a precision, which a date has nothing to apply one to.
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
spec | FormatSpec | what the placeholder asked for |
Returns: success when the whole date was accepted
Implements Debug
Date conforms to Debug.
WriteDebug
pub func WriteDebug(
self: &Date,
writer: &var TextWriter,
spec: FormatSpec
) -> ! FormatError
The same text as Display: a date has one reading, and quoting it would say nothing extra.
The failures WriteDisplay reports.
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
spec | FormatSpec | what the placeholder asked for |
Returns: success when the whole date was accepted
Related
IsLeapYear
pub func IsLeapYear(year: int32) -> bool
Whether year is a leap year: every fourth, except every hundredth, except every four-hundredth.
Parameters
| Name | Type | Description |
|---|---|---|
year | int32 | the year, as a signed count from year zero |
Returns: whether that year has 366 days under the proleptic Gregorian calendar
DaysInMonth
pub func DaysInMonth(year: int32, month: uint8) -> uint8
How many days month holds in year.
Zero for a month that does not exist, which is what lets Date::New use it as the validator.
Parameters
| Name | Type | Description |
|---|---|---|
year | int32 | the year, as a signed count from year zero |
month | uint8 | the month, 1 through 12 |
Returns: how many days that month has in that year
ParseDate
pub func ParseDate(text: char8[..]) -> Date ! TimeParseError
Reads YYYY-MM-DD.
Strict: four year digits, two month digits, two day digits, dashes between, and nothing after.
Reports InvalidSyntax for a shape that does not match, InvalidMonth for a month outside 1 through 12, and
NonexistentDay for a day the month does not have. Each carries the byte offset where it was found.
Parameters
| Name | Type | Description |
|---|---|---|
text | char8[..] | the text to read |
Returns: the date, or why the text is not one