Date structv0.1.0

Source
A calendar date that exists.
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

NameTypeDescription
yearint32The year, astronomically numbered: year 0 exists and is 1 BCE.
monthuint8The month, 1 through 12.
dayuint8The 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.

NameTypeDescription
yearint32the year, as a signed count from year zero
monthuint8the month, 1 through 12
dayuint8the day of the month, counting from one

Returns: the date, or None when the components do not name one

Source

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.

NameTypeDescription
daysint64how many days

Returns: the date that many days after 1970-01-01

Source

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

Source

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

Source

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

Source

Equals

pub func Equals(self: &Date, other: Date) -> bool

Whether two dates are the same day.

NameTypeDescription
otherDatethe value to compare against

Returns: whether the two are the same

Source

Compare

pub func Compare(self: &Date, other: Date) -> Ordering

How this date orders against other.

NameTypeDescription
otherDatethe value to compare against

Returns: how this one orders against other

Source

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.

NameTypeDescription
daysint64how many days

Returns: the date that many days later, or None when it leaves the range

Source

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.

NameTypeDescription
monthsint64how many months

Returns: the date that many months later, clamped to the month's last day, or None when it leaves the range

Source

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.

NameTypeDescription
yearsint64how many years

Returns: the date that many years later, clamped for 29 February, or None when it leaves the range

Source

DaysSince

pub func DaysSince(self: &Date, earlier: Date) -> int64

The whole days from earlier to this date, negative when it is the later one.

NameTypeDescription
earlierDatethe earlier reading

Returns: how many days earlier is before this date, negative if it is after

Source

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.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole date was accepted

Source

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.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole date was accepted

Source

IsLeapYear

pub func IsLeapYear(year: int32) -> bool

Whether year is a leap year: every fourth, except every hundredth, except every four-hundredth.

Source

Parameters

NameTypeDescription
yearint32the 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.

Source

Parameters

NameTypeDescription
yearint32the year, as a signed count from year zero
monthuint8the 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.

Source

Parameters

NameTypeDescription
textchar8[..]the text to read

Returns: the date, or why the text is not one