Expand description
Facilities for dealing with inexact dates and times.
§Overview
The essential types in this module are:
Dateis a specific day in the proleptic Gregorian calendar.Timeis a specific wall clock time.DateTimeis a combination of a day and a time.
Moreover, the date and time free functions can be used
to conveniently create values of any of three types above:
use jiff::civil::{date, time};
assert_eq!(date(2024, 7, 31).to_string(), "2024-07-31");
assert_eq!(time(15, 20, 0, 123).to_string(), "15:20:00.000000123");
assert_eq!(
date(2024, 7, 31).at(15, 20, 0, 123).to_string(),
"2024-07-31T15:20:00.000000123",
);
assert_eq!(
time(15, 20, 0, 123).on(2024, 7, 31).to_string(),
"2024-07-31T15:20:00.000000123",
);§What is “civil” time?
A civil datetime is a calendar date and a clock time. It also goes by the names
“naive,” “local” or “plain.” The most important thing to understand about civil
time is that it does not correspond to a precise instant in time. This is in
contrast to types like Timestamp and
ZonedDateTime, which do correspond to a precise
instant in time (to nanosecond precision).
Because a civil datetime never has a time zone associated with it, and
because some time zones have transitions that skip or repeat clock times, it
follows that not all civil datetimes precisely map to a single instant in time.
For example, 2024-03-10 02:30 never existed on a clock in America/New_York
because the 2 o’clock hour was skipped when the clocks were “moved forward”
for daylight saving time. Conversely, 2024-11-03 01:30 occurred twice in
America/New_York because the 1 o’clock hour was repeated when clocks were
“moved backward” for daylight saving time. (When time is skipped, it’s called a
“gap.” When time is repeated, it’s called a “fold.”)
In contrast, an instant in time (that is, Timestamp or ZonedDateTime) can
always be converted to a civil datetime. And, when a civil datetime is
combined with its time zone identifier and its offset, the resulting machine
readable string is unambiguous 100% of the time:
use jiff::{civil::date, tz::TimeZone};
let tz = TimeZone::get("America/New_York")?;
let dt = date(2024, 11, 3).at(1, 30, 0, 0);
// It's ambiguous, so asking for an unambiguous instant presents an error!
assert!(tz.to_ambiguous_zoned_datetime(dt).unambiguous().is_err());
// Gives you the earlier time in a fold, i.e., before DST ends:
assert_eq!(
tz.to_ambiguous_zoned_datetime(dt).earlier()?.to_string(),
"2024-11-03T01:30:00-04:00[America/New_York]",
);
// Gives you the later time in a fold, i.e., after DST ends.
// Notice the offset change from the previous example!
assert_eq!(
tz.to_ambiguous_zoned_datetime(dt).later()?.to_string(),
"2024-11-03T01:30:00-05:00[America/New_York]",
);
// "Just give me something reasonable"
assert_eq!(
tz.to_ambiguous_zoned_datetime(dt).compatible()?.to_string(),
"2024-11-03T01:30:00-04:00[America/New_York]",
);
§When should I use civil time?
Here is a likely non-exhaustive list of reasons why you might want to use civil time:
- When you want or need to deal with calendar and clock units as an
intermediate step before and/or after associating it with a time zone. For
example, perhaps you need to parse strings like
2000-01-01T00:00:00from a CSV file that have no time zone or offset information, but the time zone is implied through some out-of-band mechanism. - When time zone is actually irrelevant. For example, a fitness tracking app that reminds you to work-out at 6am local time, regardless of which time zone you’re in.
- When you need to perform arithmetic that deliberately ignores daylight saving time.
- When interacting with legacy systems or systems that specifically do not support time zones.
§What about dates preceding the introduction of the Gregorian calendar?
Jiff uses what is called the proleptic Gregorian calendar. This means that Jiff behaves as if the Gregorian calendar has been used for all of time.
In reality, the Gregorian calendar wasn’t introduced until October 1582. However, not all places in the world adopted the Gregorian calendar at that time. For example, Russia didn’t convert from the Julian calendar to the Gregorian calendar until 1917.
By using the proleptic calendar—where by we pretend the Gregorian calendar has always been in use—we are sacrificing contextually and historically accurate dates in favor of a massive simplification in datetime handling. In general, the process of determining the correct calendar used by a given locale and at a given time is a research task that is outside the scope of what Jiff offers.
Structs§
- Date
- A representation of a civil date in the Gregorian calendar.
- Date
Arithmetic - Options for
Date::checked_addandDate::checked_sub. - Date
Difference - Options for
Date::sinceandDate::until. - Date
Series - An iterator over periodic dates, created by
Date::series. - Date
Time - A representation of a civil datetime in the Gregorian calendar.
- Date
Time Arithmetic - Options for
DateTime::checked_addandDateTime::checked_sub. - Date
Time Difference - Options for
DateTime::sinceandDateTime::until. - Date
Time Round - Options for
DateTime::round. - Date
Time Series - An iterator over periodic datetimes, created by
DateTime::series. - Date
Time With - A builder for setting the fields on a
DateTime. - Date
With - A builder for setting the fields on a
Date. - ISOWeek
Date - A type representing an ISO 8601 week date.
- Time
- A representation of civil “wall clock” time.
- Time
Arithmetic - Options for
Time::checked_addandTime::checked_sub. - Time
Difference - Options for
Time::sinceandTime::until. - Time
Round - Options for
Time::round. - Time
Series - An iterator over periodic times, created by
Time::series. - Time
With - A builder for setting the fields on a
Time. - Weekdays
Forward - An unending iterator of the days of the week.
- Weekdays
Reverse - An unending iterator of the days of the week in reverse.