docs

You are reading the docs for v0.1.2. Nabi 0.1.2 is no longer supported. Latest release: v0.1.3 →

std::time

On this page

the wall clock (SystemTime), the boot clock (BootTime), and the civil calendar: Date, Time, DateTime, Offset, TimeZone and Zoned, with ISO 8601 / RFC 3339 text and strftime-style format / parse_with. Duration / Instant are core::time. Every span is an exact signed Duration; the arithmetic goes through Duration's, which panics on overflow and saturates only with overflow checks off.

Structs

DateTimeSource

pub struct DateTime

A date and a time of day with no time zone ("naive" or "local" time, as on a wall calendar). DateTime + Duration counts exact time; attach a zone with in_zone to get an instant. Display is ISO 8601 (2024-03-10T02:30:00).

  • pub const fn new(year: i32, month: i32, day: i32, hour: i32, minute: i32, second: i32) throws (ValueError) -> DateTime§

    Source

    year-month-day hour:minute:second; ValueError when it is not a valid date and time.

  • pub const fn of(date: Date, time: Time) -> DateTime§

    Source

    date at time.

  • pub const fn from_unix(secs: i64, nano: u32) -> DateTime§

    Source

    The civil time secs seconds and nano nanoseconds after 1970-01-01T00:00:00, counting every day as 86400 seconds (UTC). Panics outside the Date range.

  • pub const fn unix_secs(self) -> i64§

    Source

    Seconds from 1970-01-01T00:00:00 to this civil time, reading it as UTC: from_unix(dt.unix_secs(), dt.nanosecond() as u32) == dt.

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

    Source

    The date.

  • pub const fn time(self) -> Time§

    Source

    The time of day.

  • pub const fn year(self) -> i32§

    Source

    The year.

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

    Source

    The month, 1..=12.

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

    Source

    The day of the month.

  • pub const fn hour(self) -> i32§

    Source

    The hour, 0..=23.

  • pub const fn minute(self) -> i32§

    Source

    The minute.

  • pub const fn second(self) -> i32§

    Source

    The second.

  • pub const fn nanosecond(self) -> i32§

    Source

    The nanosecond within the second.

  • pub const fn add_days(self, n: i64) -> DateTime§

    Source

    n days later, same time of day.

  • pub const fn add_months(self, n: i64) -> DateTime§

    Source

    n months later, same time of day; the day clamps as Date::add_months.

  • pub const fn add_years(self, n: i64) -> DateTime§

    Source

    n years later, same time of day.

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

    Source

    self - earlier as exact time, negative when earlier is later. Panics past the Duration range (about 292 years).

  • pub fn in_zone(self, tz: TimeZone) -> Zoned§

    Source

    This civil time in tz. A time the clocks skip (a gap) or show twice (an overlap) resolves as Disambiguation::Compatible.

  • pub fn in_zone_with(self, tz: TimeZone, choice: Disambiguation) throws (ValueError) -> Zoned§

    Source

    This civil time in tz, with choice for a gap or an overlap; Disambiguation::Reject throws there.

  • pub const fn format(self, pattern: str) -> str§

    Source

    The date and time written by strftime-style pattern (%Y-%m-%d %H:%M). Panics on a bad pattern or an offset directive.

  • pub fn parse_with(text: str, pattern: str) throws (ParseError) -> DateTime§

    Source

    The date and time in text, read by pattern; missing time fields are 0.

Trait implementations 6

TimeSource

pub struct Time

A time of day with no date or zone: hour (0..=23), minute, second (0..=59) and nanosecond. No leap second: 23:59:60 is not a Time. Display is ISO 8601 (09:05:00, 09:05:00.25).

  • pub const MIDNIGHT: Time = Time { hour: 0, minute: 0, second: 0, nano: 0 }§

    Source

    00:00:00.

  • pub const fn new(hour: i32, minute: i32, second: i32) throws (ValueError) -> Time§

    Source

    hour:minute:second; ValueError outside 00:00:00..=23:59:59.

  • pub const fn with_nanosecond(self, nano: i32) throws (ValueError) -> Time§

    Source

    The same time with nanosecond nano (0..=999999999).

  • pub const fn from_nanos_of_day(ns: i64) -> Time§

    Source

    The time ns nanoseconds after midnight. Panics outside one day.

  • pub const fn hour(self) -> i32§

    Source

    The hour, 0..=23.

  • pub const fn minute(self) -> i32§

    Source

    The minute, 0..=59.

  • pub const fn second(self) -> i32§

    Source

    The second, 0..=59.

  • pub const fn nanosecond(self) -> i32§

    Source

    The nanosecond within the second, 0..=999999999.

  • pub const fn secs_of_day(self) -> i64§

    Source

    Whole seconds since midnight.

  • pub const fn nanos_of_day(self) -> i64§

    Source

    Nanoseconds since midnight.

  • pub const fn format(self, pattern: str) -> str§

    Source

    The time written by strftime-style pattern (%H:%M). Panics on a bad pattern or a directive that needs a date.

  • pub fn parse_with(text: str, pattern: str) throws (ParseError) -> Time§

    Source

    The time in text, read by pattern; missing fields are 0.

Trait implementations 2

DateSource

pub struct Date

A calendar date with no time zone: year, month (1..=12) and day, in the proleptic Gregorian calendar, years MIN_YEAR..=MAX_YEAR (year 0 is 1 BC). Ordered by time. Display is ISO 8601 (2024-02-29).

  • pub const MIN: Date = Date { year: -999999, month: 1, day: 1 }§

    Source

    The first date: -999999-01-01.

  • pub const MAX: Date = Date { year: 999999, month: 12, day: 31 }§

    Source

    The last date: +999999-12-31.

  • pub const fn new(year: i32, month: i32, day: i32) throws (ValueError) -> Date§

    Source

    year-month-day; ValueError when it is not a date (Date::new(2023, 2, 29)).

  • pub const fn from_ordinal(year: i32, ordinal: i32) throws (ValueError) -> Date§

    Source

    Day ordinal (1..=365, or 366 in a leap year) of year.

  • pub const fn from_iso_week(year: i32, week: i32, weekday: Weekday) throws (ValueError) -> Date§

    Source

    The weekday of ISO week week (1..=52 or 53) of ISO year year.

  • pub const fn from_unix_days(days: i64) -> Date§

    Source

    The date days days after 1970-01-01 (before it when negative). Panics outside Date::MIN..=Date::MAX.

  • pub const fn unix_days(self) -> i64§

    Source

    Days from 1970-01-01, negative before it.

  • pub const fn year(self) -> i32§

    Source

    The year (0 is 1 BC, -1 is 2 BC).

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

    Source

    The month, 1..=12.

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

    Source

    The day of the month, 1..=31.

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

    Source

    The day of the week.

  • pub const fn ordinal(self) -> i32§

    Source

    The day of the year, 1..=366.

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

    Source

    The ISO 8601 week: (year, week). Week 1 holds the year's first Thursday, so 2021-01-03 is in week 53 of 2020.

  • pub const fn is_leap_year(self) -> bool§

    Source

    true in a leap year.

  • pub const fn days_in_month(self) -> i32§

    Source

    Days in this date's month.

  • pub const fn days_in_year(self) -> i32§

    Source

    Days in this date's year: 365 or 366.

  • pub const fn with_day(self, day: i32) throws (ValueError) -> Date§

    Source

    The same month and year on day day.

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

    Source

    The first day of this month.

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

    Source

    The last day of this month.

  • pub const fn checked_add_days(self, n: i64) -> Date | none§

    Source

    n days later (earlier when negative); none outside the range.

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

    Source

    n days later (earlier when negative). Panics outside the range.

  • pub const fn checked_add_months(self, n: i64) -> Date | none§

    Source

    n months later (earlier when negative); a day past the end of the new month becomes its last day (2024-01-31 + 1 month is 2024-02-29). none outside the range.

  • pub const fn add_months(self, n: i64) -> Date§

    Source

    n months later, clamping the day as checked_add_months. Panics outside the range.

  • pub const fn add_years(self, n: i64) -> Date§

    Source

    n years later; February 29 becomes February 28 in a common year. Panics outside the range.

  • pub const fn days_since(self, earlier: Date) -> i64§

    Source

    Days from earlier to self, negative when earlier is later.

  • pub const fn until(self, later: Date) -> Period§

    Source

    The calendar span from self to later: whole years, then whole months, then days, all negative when later is earlier. self + self.until(later) == later.

  • pub const fn and_time(self, time: Time) -> DateTime§

    Source

    This date at time.

  • pub const fn format(self, pattern: str) -> str§

    Source

    The date written by strftime-style pattern (%d %B %Y); the directives are listed under std::time. Panics on a bad pattern or a directive that needs a time.

  • pub fn parse_with(text: str, pattern: str) throws (ParseError) -> Date§

    Source

    The date in text, read by pattern (as format writes it).

Trait implementations 4

PeriodSource

pub struct Period

A calendar span: years, months and days, each counted on its own (one month is not a fixed number of days). Display is ISO 8601 (P1Y2M10D).

  • pub const ZERO: Period = Period { years: 0, months: 0, days: 0 }§

    Source

    No span.

  • pub const fn new(years: i32, months: i32, days: i32) -> Period§

    Source

    years, months and days, each kept as given.

  • pub const fn of_days(n: i32) -> Period§

    Source

    n days.

  • pub const fn of_months(n: i32) -> Period§

    Source

    n months.

  • pub const fn of_years(n: i32) -> Period§

    Source

    n years.

  • pub const fn years(self) -> i32§

    Source

    The years part.

  • pub const fn months(self) -> i32§

    Source

    The months part.

  • pub const fn days(self) -> i32§

    Source

    The days part.

  • pub const fn total_months(self) -> i64§

    Source

    Years and months as months.

  • pub const fn is_zero(self) -> bool§

    Source

    true when every part is zero.

Trait implementations 5

OffsetSource

pub struct Offset

A fixed offset from UTC in seconds, east positive (+09:00 is 32400), within +/-25:59:59. Display is +09:00 (+05:30:15 when it has seconds).

  • pub const UTC: Offset = Offset { secs: 0 }§

    Source

    UTC itself, +00:00.

  • pub const fn new(hours: i32, minutes: i32) throws (ValueError) -> Offset§

    Source

    hours and minutes east of UTC; both negative (or hours 0) for west: Offset::new(-3, -30) is -03:30.

  • pub const fn from_secs(secs: i32) throws (ValueError) -> Offset§

    Source

    secs seconds east of UTC; ValueError past +/-25:59:59.

  • pub const fn secs(self) -> i32§

    Source

    Seconds east of UTC.

  • pub const fn is_utc(self) -> bool§

    Source

    true for +00:00.

Trait implementations 2

TimeZoneSource

pub struct TimeZone

A time zone: the offset from UTC at each instant, with its abbreviation (KST, EDT). One of UTC, a fixed offset, a POSIX TZ rule, or a zone of the system tz database (TimeZone::get). Copies share the loaded table. Display is the name.

  • pub const fn utc() -> TimeZone§

    Source

    UTC.

  • pub const fn fixed(offset: Offset) -> TimeZone§

    Source

    The zone that is offset from UTC at every instant, named like the offset (+09:00).

  • pub fn get(name: str) throws (ValueError) -> TimeZone§

    Source

    The zone name from the tz database (Asia/Seoul, America/New_York), read from $TZDIR or /usr/share/zoneinfo. UTC and fixed offsets (+09:00) work without a database (Windows and the browser have none); any other name then throws.

  • pub fn from_tzif<B: AsBytes>(name: str, data: B) throws (ValueError) -> TimeZone§

    Source

    The zone data (a TZif file, as in the tz database) describes, named name.

  • pub fn from_posix(rule: str) throws (ValueError) -> TimeZone§

    Source

    The zone of a POSIX TZ rule: JST-9, EST5EDT,M3.2.0,M11.1.0. Its offsets are west positive, as POSIX writes them.

  • pub fn local() -> TimeZone§

    Source

    The system's local zone: $TZ (a database name, a path or a POSIX rule), else /etc/localtime, else UTC. On Windows, which has no tz database, only a POSIX rule in $TZ works; otherwise UTC. Reads files: keep the result rather than calling it per use.

  • pub const fn name(self) -> str§

    Source

    The name: Asia/Seoul, UTC, +09:00, or the TZ rule.

  • pub const fn is_fixed(self) -> bool§

    Source

    true when the offset never changes.

  • pub fn offset_at(self, t: SystemTime) -> Offset§

    Source

    The offset from UTC at t.

  • pub fn abbreviation_at(self, t: SystemTime) -> str§

    Source

    The abbreviation at t (EST, EDT; the offset for a fixed zone).

  • pub fn is_dst_at(self, t: SystemTime) -> bool§

    Source

    true when t falls in daylight saving time.

Trait implementations 4

ZonedSource

pub struct Zoned

An instant, the time zone it is shown in, and the civil date, time and offset there: 2024-03-10T03:30:00-04:00 in America/New_York. + / - a Duration is exact time; add_days and friends move the civil date and keep the time of day. Comparison and hashing see the instant only. Display is RFC 3339 (Z for a zero offset); Debug adds the zone (2024-03-10T03:30:00-04:00[America/New_York]).

  • pub fn now(tz: TimeZone) -> Zoned§

    Source

    Now, in tz.

  • pub fn from_system_time(t: SystemTime, tz: TimeZone) -> Zoned§

    Source

    The instant t, in tz.

  • pub fn from_unix(secs: i64, nano: u32, tz: TimeZone) -> Zoned§

    Source

    The instant secs seconds and nano nanoseconds after the Unix epoch, in tz. Panics outside the Date range.

  • pub const fn unix_secs(self) -> i64§

    Source

    Whole seconds since the Unix epoch, rounded down.

  • pub const fn unix_nanos(self) -> i64§

    Source

    Nanoseconds since the Unix epoch. Panics past the i64 range (years 1678..2262).

  • pub const fn to_system_time(self) -> SystemTime§

    Source

    The instant as a SystemTime. Panics outside its range (years 1678..2262).

  • pub const fn datetime(self) -> DateTime§

    Source

    The civil date and time in the zone.

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

    Source

    The civil date in the zone.

  • pub const fn time(self) -> Time§

    Source

    The time of day in the zone.

  • pub const fn offset(self) -> Offset§

    Source

    The offset from UTC at this instant.

  • pub const fn zone(self) -> TimeZone§

    Source

    The zone.

  • pub const fn abbreviation(self) -> str§

    Source

    The zone's abbreviation at this instant (EDT).

  • pub const fn is_dst(self) -> bool§

    Source

    true when the zone is on daylight saving time at this instant.

  • pub const fn year(self) -> i32§

    Source

    The year.

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

    Source

    The month, 1..=12.

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

    Source

    The day of the month.

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

    Source

    The day of the week.

  • pub const fn hour(self) -> i32§

    Source

    The hour, 0..=23.

  • pub const fn minute(self) -> i32§

    Source

    The minute.

  • pub const fn second(self) -> i32§

    Source

    The second.

  • pub const fn nanosecond(self) -> i32§

    Source

    The nanosecond within the second.

  • pub fn in_zone(self, tz: TimeZone) -> Zoned§

    Source

    The same instant in tz.

  • pub fn to_utc(self) -> Zoned§

    Source

    The same instant in UTC.

  • pub fn add_days(self, n: i64) -> Zoned§

    Source

    n days later on the zone's calendar at the same time of day, resolved as Disambiguation::Compatible when that time falls in a gap or an overlap.

  • pub fn add_months(self, n: i64) -> Zoned§

    Source

    n months later, as add_days; the day clamps as Date::add_months.

  • pub fn add_years(self, n: i64) -> Zoned§

    Source

    n years later, as add_days.

  • pub const fn duration_since(self, earlier: Zoned) -> Duration§

    Source

    self - earlier as exact time, negative when earlier is later.

  • pub const fn format(self, pattern: str) -> str§

    Source

    The time written by strftime-style pattern (%Y-%m-%d %H:%M:%S %Z). Panics on a bad pattern.

  • pub fn parse_with(text: str, pattern: str) throws (ParseError) -> Zoned§

    Source

    The time in text, read by pattern. It needs %z (or %Z as UTC or a zone name such as Europe/Paris), or %s.

  • pub const fn to_rfc2822(self) -> str§

    Source

    RFC 2822 / RFC 5322 (email): Thu, 14 Nov 2026 09:30:00 +0900.

  • pub fn parse_rfc2822(text: str) throws (ParseError) -> Zoned§

    Source

    An RFC 2822 / RFC 5322 date: the weekday is optional, seconds too, and the zone is +hhmm or an old name (GMT, EST, PDT).

Trait implementations 12

SystemTimeSource

pub struct SystemTime

A wall-clock (UTC) point: i64 nanoseconds from UNIX_EPOCH (1678..2262). It can step backwards (NTP, the user).

now() also takes a monotonic reading, which elapsed() uses, so a clock step cannot change it. Comparison and hashing see the wall reading only.

  • pub fn now() -> SystemTime§

    Source

    The wall clock now.

  • pub const fn duration_since(self, earlier: SystemTime) -> Duration§

    Source

    self - earlier on the wall clock, negative when earlier is later.

  • pub fn elapsed(self) -> Duration§

    Source

    Time since self on the monotonic clock (the Instant one) when self came from now(), else on the wall clock.

  • pub const fn checked_add(self, dur: Duration) -> SystemTime | none§

    Source

    self + dur; none when it overflows i64 nanoseconds.

  • pub const fn checked_sub(self, dur: Duration) -> SystemTime | none§

    Source

    self - dur; none when it overflows i64 nanoseconds.

  • pub const fn from_unix(secs: i64, nanos: u32) -> SystemTime§

    Source

    The time secs seconds and nanos nanoseconds after the Unix epoch.

  • pub const fn unix_secs(self) -> i64§

    Source

    Whole seconds since the epoch, rounded down (negative before it): from_unix(t.unix_secs(), ..) gives t back.

  • pub const fn unix_nanos(self) -> i64§

    Source

    Nanoseconds since the epoch, negative before it.

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

    Source

    The nanoseconds past unix_secs(), 0..=999999999.

  • pub const fn to_http_date(self) -> str§

    Source

    The HTTP date (RFC 9110 IMF-fixdate): Sun, 06 Nov 1994 08:49:37 GMT.

  • pub fn parse_http_date(text: str) throws (ParseError) -> SystemTime§

    Source

    An HTTP date: IMF-fixdate, or the obsolete RFC 850 (Sunday, 06-Nov-94 08:49:37 GMT) and asctime (Sun Nov 6 08:49:37 1994) forms.

Trait implementations 10

BootTimeSource

pub struct BootTime

Time since boot, counting suspend: CLOCK_BOOTTIME on Linux; on macOS CLOCK_MONOTONIC, which counts sleep there.

  • pub fn now() -> BootTime§

    Source

    The boot clock now.

  • pub fn elapsed(self) -> Duration§

    Source

    Time from self to now.

  • pub const fn duration_since(self, earlier: BootTime) -> Duration§

    Source

    self - earlier, negative when earlier is later.

Enums

WeekdaySource

pub enum Weekday

A day of the week, Monday first (ISO 8601).

  • Monday§

    Source
  • Tuesday§

    Source
  • Wednesday§

    Source
  • Thursday§

    Source
  • Friday§

    Source
  • Saturday§

    Source
  • Sunday§

    Source
  • pub const fn number(self) -> i32§

    Source

    1 for Monday .. 7 for Sunday (ISO 8601).

  • pub const fn from_number(number: i32) -> Weekday | none§

    Source

    The weekday number names (1 = Monday .. 7 = Sunday); none outside.

  • pub const fn name(self) -> str§

    Source

    The English name: "Monday".

  • pub const fn next(self) -> Weekday§

    Source

    The day after.

  • pub const fn prev(self) -> Weekday§

    Source

    The day before.

Trait implementations 1

DisambiguationSource

pub enum Disambiguation

What to do with a local time the clocks skip (a gap, when they move forward) or show twice (an overlap, when they move back).

  • Compatible§

    Source

    Earlier in an overlap, Later in a gap: a skipped time moves forward by the gap (02:30 in a 02:00 to 03:00 gap is 03:30).

  • Earlier§

    Source

    The earlier instant: the first 01:30 of an overlap; in a gap, the time read with the offset after it (02:30 becomes 01:30).

  • The later instant: the second 01:30 of an overlap; in a gap, the time read with the offset before it (02:30 becomes 03:30).

  • Reject§

    Source

    Throw a ValueError naming the gap or the overlap.

Functions

days_in_monthSource

pub const fn days_in_month(year: i32, month: i32) -> i32

Days in month (1..=12) of year; 0 for another month.

is_leap_yearSource

pub const fn is_leap_year(year: i32) -> bool

true when year has a February 29.

Constants

MAX_YEARSource

pub const MAX_YEAR: i32 = 999999

Highest year a Date holds.

MIN_YEARSource

pub const MIN_YEAR: i32 = -999999

Lowest year a Date holds.

UNIX_EPOCHSource

pub const UNIX_EPOCH: SystemTime = SystemTime { ns: 0, mono: NO_MONO }

1970-01-01 00:00:00 UTC.