silk.time.civil.

Calendar arithmetic of the ISO 8601 calendar on plain integers: leap years, month lengths, and the conversion between a date and a day count.

When to use#

Most programs use silk.time.plain_date, which checks its values. Use these functions when a parser or a formatter has the year, month, and day as numbers and a PlainDate is not needed.

Details#

The calendar is the proleptic Gregorian calendar: the leap year rule of today applies to every year, also before 1582. Year 0 exists and is the year before year 1. Day counts are days since 1970-01-01, which is day 0.

Gotchas#

The functions do not check their arguments. A month outside 1 to 12 gives a result without meaning.

Examples#

Convert a date to a day count and back#

silk
import silk.time.civil as Civil

pub fn main() -> i32 {
  let days = Civil.daysFromCivil(2000, 3, 1)
  if days != 11017 {
    return 1
  }
  let date = Civil.civilFromDays(days)
  if date.year != 2000 || date.month != 3 || date.day != 1 {
    return 2
  }
  return 0
}

Import as civil with import silk.time.civil as civil.

Public declarations: 8.

CivilDate#

silk
pub struct CivilDate

A year, a month from 1 to 12, and a day of the month from 1, as plain numbers.

Details#

The fields are not checked, so a value can name a day that does not exist. PlainDate in silk.time.plain_date is the checked type.

Field year#

silk
pub year: i32

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

Field month#

silk
pub month: u8

The month, from 1 for January to 12 for December.

Field day#

silk
pub day: u8

The day of the month, from 1.

Implementation Copy for CivilDate#

silk
impl Copy for CivilDate

isLeapYear#

silk
pub fn isLeapYear(year: i32) -> bool

Returns true if year has 366 days: it divides by 4, and not by 100 unless also by 400.

daysInYear#

silk
pub fn daysInYear(year: i32) -> u16

Returns the number of days in year: 366 for a leap year, 365 otherwise.

daysInMonth#

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

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

Gotchas#

month must be from 1 to 12. Another value gives a result without meaning.

daysFromCivil#

silk
pub fn daysFromCivil(year: i32, month: u8, day: u8) -> i64

Returns the number of days from 1970-01-01 to the given date. It is negative for an earlier date.

Gotchas#

The date is not checked. month must be from 1 to 12; a day past the end of the month counts on into the next month.

civilFromDays#

silk
pub fn civilFromDays(days: i64) -> CivilDate

Returns the date that is days days after 1970-01-01. days can be negative.

Gotchas#

The year must fit an i32, so days must be within about 784 billion of zero. A count outside that range traps.

weekdayFromDays#

silk
pub fn weekdayFromDays(days: i64) -> u8

Returns the ISO 8601 day of the week of the day days days after 1970-01-01: 1 for Monday to 7 for Sunday.

dayOfYear#

silk
pub fn dayOfYear(year: i32, month: u8, day: u8) -> u16

Returns the day of the year of the given date: 1 for January 1, up to 366.

Gotchas#

The date is not checked. month must be from 1 to 12.

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