packages

table::date

On this page

Calendar dates and timestamps in the proleptic Gregorian calendar, UTC, with no leap seconds. Day arithmetic follows Howard Hinnant's days_from_civil / civil_from_days, exact over the whole range.

Structs

DateSource

pub struct Date

A calendar date: days since 1970-01-01 (negative before it).

nabi
let d = table::Date::ymd(2024, 2, 29) ?? table::Date::from_days(0);
assert(f"{d}" == "2024-02-29");
assert(d.weekday() == 3); // Thursday, Monday is 0
  • pub const fn from_days(days: i32) -> Date§

    Source

    The date days after 1970-01-01.

    nabi
    assert(f"{table::Date::from_days(19723)}" == "2024-01-01");
    
  • pub const fn ymd(year: i32, month: u32, day: u32) -> Date | none§

    Source

    year-month-day, or none when there is no such day.

    nabi
    assert(table::Date::ymd(2023, 2, 29) is none);
    
  • pub const fn parse(s: str) -> Date | none§

    Source

    An ISO 8601 date, YYYY-MM-DD; none for anything else.

    nabi
    assert(table::Date::parse("2024-13-01") is none);
    
  • pub const fn days(self) -> i32§

    Source

    Days since 1970-01-01.

  • pub const fn parts(self) -> (i32, u32, u32)§

    Source

    (year, month, day).

    nabi
    let d = table::Date::from_days(0);
    assert(d.parts() == (1970, 1u32, 1u32));
    
  • pub const fn year(self) -> i32§

    Source

    The year (proleptic Gregorian, year 0 is 1 BC).

  • pub const fn month(self) -> u32§

    Source

    The month, 1 to 12.

  • pub const fn day(self) -> u32§

    Source

    The day of the month, 1 to 31.

  • pub const fn weekday(self) -> u32§

    Source

    Day of the week, Monday 0 to Sunday 6.

  • pub const fn add_days(self, n: i32) -> Date§

    Source

    n days later (earlier for a negative n).

    nabi
    assert(f"{table::Date::from_days(0).add_days(-1)}" == "1969-12-31");
    
  • pub const fn at_midnight(self) -> DateTime§

    Source

    Midnight UTC of this day.

Trait implementations 2
  • impl Display for Date
  • impl Debug for Date

DateTimeSource

pub struct DateTime

An instant: nanoseconds since 1970-01-01T00:00:00Z, which covers the years 1677 to 2262.

nabi
let t = table::DateTime::parse("2024-01-15T10:30:00Z") ?? table::DateTime::from_ns(0);
assert(t.date() == (table::Date::ymd(2024, 1, 15) ?? table::Date::from_days(0)));
assert(f"{t}" == "2024-01-15T10:30:00");
  • pub const fn from_ns(ns: i64) -> DateTime§

    Source

    ns nanoseconds after 1970-01-01T00:00:00Z.

  • pub const fn from_secs(secs: i64) -> DateTime§

    Source

    Whole seconds after the epoch.

  • pub fn from_system(t: SystemTime) -> DateTime§

    Source

    A std::time::SystemTime, to the nanosecond.

  • pub const fn parse(s: str) -> DateTime | none§

    Source

    ISO 8601 YYYY-MM-DD, then optionally T or a space and HH:MM, :SS, a fraction of up to 9 digits and a final Z; none for anything else. A bare date is midnight.

    nabi
    let t = table::DateTime::parse("2024-01-15 10:30:00.25") ?? table::DateTime::from_ns(0);
    assert(t.ns() % 1000000000 == 250000000);
    
  • pub const fn ns(self) -> i64§

    Source

    Nanoseconds since the epoch.

  • pub const fn date(self) -> Date§

    Source

    The calendar day (UTC).

  • pub const fn time(self) -> (u32, u32, u32, u32)§

    Source

    (hour, minute, second, nanosecond) within the day.

  • pub const fn add(self, d: Duration) -> DateTime§

    Source

    d later.

  • pub const fn since(self, earlier: DateTime) -> Duration§

    Source

    The time from earlier to this one.

Trait implementations 2
  • impl Display for DateTime
  • impl Debug for DateTime

Functions

date_literalSource

pub const fn date_literal(s: str) -> Date

date"2024-01-15": a Date, checked when the program compiles.

nabi
let d = date"2024-01-15";
assert(d.month() == 1u32);

datetime_literalSource

pub const fn datetime_literal(s: str) -> DateTime

datetime"2024-01-15T10:30:00": a DateTime, checked when the program compiles.

nabi
let t = datetime"2024-01-15T10:30:00";
assert(t.time().0 == 10u32);