std::time
이 페이지의 내용
API 참조는 영어로만 있습니다. 표준 라이브러리 소스의 영어 문서 주석에서 만듭니다.
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 DateTimeA 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).
Sourcepub const fn new(§year: i32, month: i32, day: i32, hour: i32, minute: i32, second: i32) throws (ValueError) -> DateTime year-month-day hour:minute:second;ValueErrorwhen it is not a valid date and time.dateattime.The civil time
secsseconds andnanonanoseconds after 1970-01-01T00:00:00, counting every day as 86400 seconds (UTC). Panics outside theDaterange.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.The date.
The time of day.
The year.
The month, 1..=12.
The day of the month.
The hour, 0..=23.
The minute.
The second.
The nanosecond within the second.
ndays later, same time of day.nmonths later, same time of day; the day clamps asDate::add_months.nyears later, same time of day.self - earlieras exact time, negative whenearlieris later. Panics past theDurationrange (about 292 years).This civil time in
tz. A time the clocks skip (a gap) or show twice (an overlap) resolves asDisambiguation::Compatible.
Sourcepub fn in_zone_with(§self, tz: TimeZone, choice: Disambiguation) throws (ValueError) -> Zoned This civil time in
tz, withchoicefor a gap or an overlap;Disambiguation::Rejectthrows there.The date and time written by strftime-style
pattern(%Y-%m-%d %H:%M). Panics on a bad pattern or an offset directive.
Sourcepub fn parse_with(§text: str, pattern: str) throws (ParseError) -> DateTime The date and time in
text, read bypattern; missing time fields are 0.
TimeSource
pub struct TimeA 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).
00:00:00.
Sourcepub const fn new(§hour: i32, minute: i32, second: i32) throws (ValueError) -> Time hour:minute:second;ValueErroroutside 00:00:00..=23:59:59.
Sourcepub const fn with_nanosecond(§self, nano: i32) throws (ValueError) -> Time The same time with nanosecond
nano(0..=999999999).The time
nsnanoseconds after midnight. Panics outside one day.The hour, 0..=23.
The minute, 0..=59.
The second, 0..=59.
The nanosecond within the second, 0..=999999999.
Whole seconds since midnight.
Nanoseconds since midnight.
The time written by strftime-style
pattern(%H:%M). Panics on a bad pattern or a directive that needs a date.
Sourcepub fn parse_with(§text: str, pattern: str) throws (ParseError) -> Time The time in
text, read bypattern; missing fields are 0.
DateSource
pub struct DateA 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).
The first date:
-999999-01-01.The last date:
+999999-12-31.
Sourcepub const fn new(§year: i32, month: i32, day: i32) throws (ValueError) -> Date year-month-day;ValueErrorwhen it is not a date (Date::new(2023, 2, 29)).
Sourcepub const fn from_ordinal(§year: i32, ordinal: i32) throws (ValueError) -> Date Day
ordinal(1..=365, or 366 in a leap year) ofyear.
Sourcepub const fn from_iso_week(§year: i32, week: i32, weekday: Weekday) throws (ValueError) -> Date The
weekdayof ISO weekweek(1..=52 or 53) of ISO yearyear.The date
daysdays after 1970-01-01 (before it when negative). Panics outsideDate::MIN..=Date::MAX.Days from 1970-01-01, negative before it.
The year (0 is 1 BC, -1 is 2 BC).
The month, 1..=12.
The day of the month, 1..=31.
The day of the week.
The day of the year, 1..=366.
The ISO 8601 week:
(year, week). Week 1 holds the year's first Thursday, so 2021-01-03 is in week 53 of 2020.truein a leap year.Days in this date's month.
Days in this date's year: 365 or 366.
Sourcepub const fn with_day(§self, day: i32) throws (ValueError) -> Date The same month and year on day
day.The first day of this month.
The last day of this month.
ndays later (earlier when negative);noneoutside the range.ndays later (earlier when negative). Panics outside the range.nmonths 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).noneoutside the range.nmonths later, clamping the day aschecked_add_months. Panics outside the range.nyears later; February 29 becomes February 28 in a common year. Panics outside the range.Days from
earliertoself, negative whenearlieris later.The calendar span from
selftolater: whole years, then whole months, then days, all negative whenlateris earlier.self + self.until(later) == later.This date at
time.The date written by strftime-style
pattern(%d %B %Y); the directives are listed understd::time. Panics on a bad pattern or a directive that needs a time.
Sourcepub fn parse_with(§text: str, pattern: str) throws (ParseError) -> Date The date in
text, read bypattern(asformatwrites it).
PeriodSource
pub struct PeriodA 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).
No span.
years,monthsanddays, each kept as given.ndays.nmonths.nyears.The years part.
The months part.
The days part.
Years and months as months.
truewhen every part is zero.
OffsetSource
pub struct OffsetA 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).
UTC itself,
+00:00.
Sourcepub const fn new(§hours: i32, minutes: i32) throws (ValueError) -> Offset hoursandminuteseast of UTC; both negative (orhours0) for west:Offset::new(-3, -30)is-03:30.
Sourcepub const fn from_secs(§secs: i32) throws (ValueError) -> Offset secsseconds east of UTC;ValueErrorpast +/-25:59:59.Seconds east of UTC.
truefor+00:00.
TimeZoneSource
pub struct TimeZoneA 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.
UTC.
The zone that is
offsetfrom UTC at every instant, named like the offset (+09:00).
Sourcepub fn get(§name: str) throws (ValueError) -> TimeZone The zone
namefrom the tz database (Asia/Seoul,America/New_York), read from$TZDIRor/usr/share/zoneinfo.UTCand fixed offsets (+09:00) work without a database (Windows and the browser have none); any other name then throws.
Sourcepub fn from_tzif<B: AsBytes>(§name: str, data: B) throws (ValueError) -> TimeZone The zone
data(a TZif file, as in the tz database) describes, namedname.
Sourcepub fn from_posix(§rule: str) throws (ValueError) -> TimeZone The zone of a POSIX
TZrule:JST-9,EST5EDT,M3.2.0,M11.1.0. Its offsets are west positive, as POSIX writes them.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$TZworks; otherwise UTC. Reads files: keep the result rather than calling it per use.The name:
Asia/Seoul,UTC,+09:00, or theTZrule.truewhen the offset never changes.
Sourcepub fn offset_at(§self, t: SystemTime) -> Offset The offset from UTC at
t.
Sourcepub fn abbreviation_at(§self, t: SystemTime) -> str The abbreviation at
t(EST,EDT; the offset for a fixed zone).
Sourcepub fn is_dst_at(§self, t: SystemTime) -> bool truewhentfalls in daylight saving time.
ZonedSource
pub struct ZonedAn 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]).
Now, in
tz.
Sourcepub fn from_system_time(§t: SystemTime, tz: TimeZone) -> Zoned The instant
t, intz.The instant
secsseconds andnanonanoseconds after the Unix epoch, intz. Panics outside theDaterange.Whole seconds since the Unix epoch, rounded down.
Nanoseconds since the Unix epoch. Panics past the
i64range (years 1678..2262).
Sourcepub const fn to_system_time(§self) -> SystemTime The instant as a
SystemTime. Panics outside its range (years 1678..2262).The civil date and time in the zone.
The civil date in the zone.
The time of day in the zone.
The offset from UTC at this instant.
The zone.
The zone's abbreviation at this instant (
EDT).truewhen the zone is on daylight saving time at this instant.The year.
The month, 1..=12.
The day of the month.
The day of the week.
The hour, 0..=23.
The minute.
The second.
The nanosecond within the second.
The same instant in
tz.The same instant in UTC.
ndays later on the zone's calendar at the same time of day, resolved asDisambiguation::Compatiblewhen that time falls in a gap or an overlap.nmonths later, asadd_days; the day clamps asDate::add_months.nyears later, asadd_days.self - earlieras exact time, negative whenearlieris later.The time written by strftime-style
pattern(%Y-%m-%d %H:%M:%S %Z). Panics on a bad pattern.
Sourcepub fn parse_with(§text: str, pattern: str) throws (ParseError) -> Zoned The time in
text, read bypattern. It needs%z(or%ZasUTCor a zone name such asEurope/Paris), or%s.RFC 2822 / RFC 5322 (email):
Thu, 14 Nov 2026 09:30:00 +0900.
Sourcepub fn parse_rfc2822(§text: str) throws (ParseError) -> Zoned An RFC 2822 / RFC 5322 date: the weekday is optional, seconds too, and the zone is
+hhmmor an old name (GMT,EST,PDT).
SystemTimeSource
pub struct SystemTimeA 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.
The wall clock now.
self - earlieron the wall clock, negative whenearlieris later.Time since
selfon the monotonic clock (theInstantone) whenselfcame fromnow(), else on the wall clock.self + dur;nonewhen it overflowsi64nanoseconds.self - dur;nonewhen it overflowsi64nanoseconds.The time
secsseconds andnanosnanoseconds after the Unix epoch.Whole seconds since the epoch, rounded down (negative before it):
from_unix(t.unix_secs(), ..)givestback.Nanoseconds since the epoch, negative before it.
The nanoseconds past
unix_secs(), 0..=999999999.The HTTP date (RFC 9110 IMF-fixdate):
Sun, 06 Nov 1994 08:49:37 GMT.
Sourcepub fn parse_http_date(§text: str) throws (ParseError) -> SystemTime 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.
BootTimeSource
pub struct BootTimeTime since boot, counting suspend: CLOCK_BOOTTIME on Linux;
on macOS CLOCK_MONOTONIC, which counts sleep there.
The boot clock now.
Time from
selfto now.self - earlier, negative whenearlieris later.
Enums
WeekdaySource
pub enum WeekdayA day of the week, Monday first (ISO 8601).
1 for Monday .. 7 for Sunday (ISO 8601).
The weekday
numbernames (1 = Monday .. 7 = Sunday);noneoutside.The English name:
"Monday".The day after.
The day before.
Trait implementations 1
impl Display for Weekday
DisambiguationSource
pub enum DisambiguationWhat to do with a local time the clocks skip (a gap, when they move forward) or show twice (an overlap, when they move back).
Earlierin an overlap,Laterin a gap: a skipped time moves forward by the gap (02:30 in a 02:00 to 03:00 gap is 03:30).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).
Throw a
ValueErrornaming 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 = 999999Highest year a Date holds.
MIN_YEARSource
pub const MIN_YEAR: i32 = -999999Lowest year a Date holds.
UNIX_EPOCHSource
pub const UNIX_EPOCH: SystemTime = SystemTime { ns: 0, mono: NO_MONO }1970-01-01 00:00:00 UTC.