silk.time.plain_year_month.
Months of specific years: the PlainYearMonth type, such as November 2026.
When to use#
Use PlainYearMonth for a value that names a whole month: the month of a bill, the expiry
of a card, a page of a calendar. For one day, use PlainDate of silk.time.plain_date.
Details#
Months are in the ISO 8601 calendar. The range is -271821-04 to +275760-09, the months
in which PlainDate has dates. The text form is YYYY-MM.
Examples#
Go through the months of a subscription#
import silk.core.option { Option }
import silk.time.plain_date { PlainDate }
import silk.time.plain_year_month { PlainYearMonth }
pub fn main() -> i32 {
let start = PlainYearMonth.fromDate(PlainDate.make(2026, 11, 19))
let end = start.addMonths(3)
if end.year() != 2027 || end.month() != 2 || end.daysInMonth() != 28 {
return 1
}
if start.monthsUntil(end) != 3 || !(&start < &end) {
return 2
}
match end.atDay(28) {
Option<PlainDate>.Some { value } => {
if value.dayOfYear() != 59 {
return 3
}
}
Option<PlainDate>.None => {
return 4
}
}
return 0
}Import as PlainYearMonth with import silk.time.plain_year_month { PlainYearMonth }.
Public declarations: 1.
PlainYearMonth#
pub struct PlainYearMonthA month of one year, in the ISO 8601 calendar.
Details#
==, !=, <, <=, >, and >= compare in time order. Display writes YYYY-MM, with
a sign and six digits for a year outside 0 to 9999.
Implementation Copy for PlainYearMonth#
impl Copy for PlainYearMonthImplementation PlainYearMonth#
impl PlainYearMonthAssociated function make#
pub fn make(year: i32, month: u8) -> PlainYearMonthReturns the month month, from 1 to 12, of the year year.
Gotchas#
A month that does not exist or is outside the range traps. PlainYearMonth.checkedMake
returns None instead.
Associated function checkedMake#
pub fn checkedMake(year: i32, month: u8) -> Option<PlainYearMonth>Returns the month month, from 1 to 12, of the year year, or None when the month does
not exist or is outside -271821-04 to +275760-09.
Associated function fromDate#
pub fn fromDate(date: PlainDate) -> PlainYearMonthReturns the month that date is in.
Method year#
pub fn year(self: Self) -> i32Returns the year. Year 0 is the year before year 1.
Method month#
pub fn month(self: Self) -> u8Returns the month, from 1 for January to 12 for December.
Method daysInMonth#
pub fn daysInMonth(self: Self) -> u8Returns the number of days in the month, from 28 to 31.
Method daysInYear#
pub fn daysInYear(self: Self) -> u16Returns the number of days in the year, 365 or 366.
Method inLeapYear#
pub fn inLeapYear(self: Self) -> boolReturns true if the year is a leap year.
Method atDay#
pub fn atDay(self: Self, day: u8) -> Option<PlainDate>Returns the date of day day of this month, or None when the month has no such day or
the date is outside the range of PlainDate.
Method checkedAddMonths#
pub fn checkedAddMonths(self: Self, months: i64) -> Option<PlainYearMonth>Returns the month months months later, or earlier for a negative count, or None when
it is outside the range.
Method addMonths#
pub fn addMonths(self: Self, months: i64) -> PlainYearMonthReturns the month months months later, or earlier for a negative count.
Gotchas#
A result outside the range traps. PlainYearMonth.checkedAddMonths returns None
instead.
Method monthsUntil#
pub fn monthsUntil(self: Self, other: PlainYearMonth) -> i64Returns the number of months from this month to other. It is negative when other is
earlier.
Associated function parse#
pub fn parse(text: string) -> Result<PlainYearMonth, TimeParseError>Reads a month from ISO 8601 text such as 2026-11.
Details#
The text is YYYY-MM or YYYYMM; a year outside 0 to 9999 has a sign and six digits.
Bracketed annotations can follow. The text can also be a whole date, with or without a
time, in the form that PlainDate.parse reads; the day is checked and then ignored.
Errors#
TimeParseReason.UnexpectedEndorUnexpectedCharacterif the text does not have that form.TimeParseReason.OutOfRangeif the month is not from 1 to 12 or is outside the range.TimeParseReason.UtcNotAllowedif the text has the UTC designatorZ.TimeParseReason.UnsupportedCalendarif a calendar annotation is not[u-ca=iso8601].TimeParseReason.UnknownCriticalAnnotationif an annotation with an unknown key starts with!.
Associated function parseBytes#
pub fn parseBytes(bytes: &[u8]) -> Result<PlainYearMonth, TimeParseError>Reads a month from the bytes of ISO 8601 text such as 2026-11.
Details#
The form is the one of PlainYearMonth.parse.
Errors#
The errors of
PlainYearMonth.parse.
Implementation Equal for PlainYearMonth#
impl Equal for PlainYearMonthOperation equals#
equals: PlainYearMonth.sameImplementation Order for PlainYearMonth#
impl Order for PlainYearMonthOperation compare#
compare: PlainYearMonth.compareImplementation Display for PlainYearMonth#
impl Display for PlainYearMonthWrites the month as YYYY-MM, with a sign and six digits for a year outside 0 to 9999, as in
2026-11 or -000044-03.
Operation display#
effect fn display(self: &Self, formatter: &mut Formatter) -> ()
! WriterError
? &mut WriterImplementation Debug for PlainYearMonth#
impl Debug for PlainYearMonthWrites the same text as Display: the month as YYYY-MM.
Operation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> ()
! WriterError
? &mut WriterCategory: Time.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.