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#

silk
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#

silk
pub struct PlainYearMonth

A 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#

silk
impl Copy for PlainYearMonth

Implementation PlainYearMonth#

silk
impl PlainYearMonth

Associated function make#

silk
pub fn make(year: i32, month: u8) -> PlainYearMonth

Returns 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#

silk
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#

silk
pub fn fromDate(date: PlainDate) -> PlainYearMonth

Returns the month that date is in.

Method year#

silk
pub fn year(self: Self) -> i32

Returns the year. Year 0 is the year before year 1.

Method month#

silk
pub fn month(self: Self) -> u8

Returns the month, from 1 for January to 12 for December.

Method daysInMonth#

silk
pub fn daysInMonth(self: Self) -> u8

Returns the number of days in the month, from 28 to 31.

Method daysInYear#

silk
pub fn daysInYear(self: Self) -> u16

Returns the number of days in the year, 365 or 366.

Method inLeapYear#

silk
pub fn inLeapYear(self: Self) -> bool

Returns true if the year is a leap year.

Method atDay#

silk
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#

silk
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#

silk
pub fn addMonths(self: Self, months: i64) -> PlainYearMonth

Returns 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#

silk
pub fn monthsUntil(self: Self, other: PlainYearMonth) -> i64

Returns the number of months from this month to other. It is negative when other is earlier.

Associated function parse#

silk
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.UnexpectedEnd or UnexpectedCharacter if the text does not have that form.

  • TimeParseReason.OutOfRange if the month is not from 1 to 12 or is outside the range.

  • TimeParseReason.UtcNotAllowed if the text has the UTC designator Z.

  • TimeParseReason.UnsupportedCalendar if a calendar annotation is not [u-ca=iso8601].

  • TimeParseReason.UnknownCriticalAnnotation if an annotation with an unknown key starts with !.

Associated function parseBytes#

silk
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#

Implementation Equal for PlainYearMonth#

silk
impl Equal for PlainYearMonth

Operation equals#

silk
equals: PlainYearMonth.same

Implementation Order for PlainYearMonth#

silk
impl Order for PlainYearMonth

Operation compare#

silk
compare: PlainYearMonth.compare

Implementation Display for PlainYearMonth#

silk
impl Display for PlainYearMonth

Writes 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#

silk
effect fn display(self: &Self, formatter: &mut Formatter) -> ()
    ! WriterError
    ? &mut Writer

Implementation Debug for PlainYearMonth#

silk
impl Debug for PlainYearMonth

Writes the same text as Display: the month as YYYY-MM.

Operation debug#

silk
effect fn debug(self: &Self, formatter: &mut Formatter) -> ()
    ! WriterError
    ? &mut Writer

Category: 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.

View the source of this page