chrono
Date and time types unconcerned with timezones.
They are primarily building blocks for other types
(e.g. TimeZone),
but can be also used for the simpler date and time handling.
Fully qualified path: chrono
Modules
| format | — |
| date | ISO 8601 calendar date without timezone. The implementation is optimized for determining year, month, day and day of week. Format of NaiveDate : YYYY_YYYY_YYYY_YYYY_YYYO_OOOO_OOOO_LWWW Y : Year… |
| datetime | — |
| days | — |
| internals | Internal helper types for working with dates. |
| isoweek | ISO 8601 week. |
| months | — |
| time | ISO 8601 time without timezone. |
| time_delta | Temporal quantification |
| traits | — |
| utils | — |
| week | — |
| weekday | — |
| prelude | A convenience module appropriate for glob imports ( use chrono::prelude::*; ). |
Modules
Modules
| format | — |
| date | ISO 8601 calendar date without timezone. The implementation is optimized for determining year, month, day and day of week. Format of NaiveDate : YYYY_YYYY_YYYY_YYYY_YYYO_OOOO_OOOO_LWWW Y : Year… |
| datetime | — |
| days | — |
| internals | Internal helper types for working with dates. |
| isoweek | ISO 8601 week. |
| months | — |
| time | ISO 8601 time without timezone. |
| time_delta | Temporal quantification |
| traits | — |
| utils | — |
| week | — |
| weekday | — |
| prelude | A convenience module appropriate for glob imports ( use chrono::prelude::*; ). |
format
Fully qualified path: chrono::format
Modules
Modules
Modules
formatting
Fully qualified path: chrono::format::formatting
date
ISO 8601 calendar date without timezone.
The implementation is optimized for determining year, month, day and day of week.
Format of NaiveDate:
YYYY_YYYY_YYYY_YYYY_YYYO_OOOO_OOOO_LWWW
Y: Year
O: Ordinal
L: leap year flag (1 = common year, 0 is leap year)
W: weekday before the first day of the year
LWWW: will also be referred to as the year flags (F)
Fully qualified path: chrono::date
Constants
| MAX_YEAR | MAX_YEAR is one year less than the type is capable of representing. Internally we may sometimes use the headroom, notably to handle cases where the offset of a DateTime constructed with… |
| MIN_YEAR | MIN_YEAR is one year more than the type is capable of representing. Internally we may sometimes use the headroom, notably to handle cases where the offset of a DateTime constructed with… |
Structs
| Date | ISO 8601 calendar date without timezone. Allows for every proleptic Gregorian date from Jan 1, 262145 BCE to Dec 31, 262143 CE. Also supports the conversion from ISO 8601 ordinal and week date…. |
Traits
Impls
Constants
Constants
| MAX_YEAR | MAX_YEAR is one year less than the type is capable of representing. Internally we may sometimes use the headroom, notably to handle cases where the offset of a DateTime constructed with… |
| MIN_YEAR | MIN_YEAR is one year more than the type is capable of representing. Internally we may sometimes use the headroom, notably to handle cases where the offset of a DateTime constructed with… |
MAX_YEAR
MAX_YEAR is one year less than the type is capable of representing. Internally we may sometimes
use the headroom, notably to handle cases where the offset of a DateTime constructed with
NaiveDate::MAX pushes it beyond the valid, representable range.
Fully qualified path: chrono::date::MAX_YEAR
pub const MAX_YEAR: u32 = 262142;
MIN_YEAR
MIN_YEAR is one year more than the type is capable of representing. Internally we may sometimes
use the headroom, notably to handle cases where the offset of a DateTime constructed with
NaiveDate::MIN pushes it beyond the valid, representable range.
Fully qualified path: chrono::date::MIN_YEAR
pub const MIN_YEAR: u32 = 0;
Structs
Structs
| Date | ISO 8601 calendar date without timezone. Allows for every proleptic Gregorian date from Jan 1, 262145 BCE to Dec 31, 262143 CE. Also supports the conversion from ISO 8601 ordinal and week date…. |
Date
ISO 8601 calendar date without timezone. Allows for every proleptic Gregorian date from Jan 1, 262145 BCE to Dec 31, 262143 CE. Also supports the conversion from ISO 8601 ordinal and week date.
Calendar Date
The ISO 8601 calendar date follows the proleptic Gregorian calendar. It is like a normal civil calendar but note some slight differences:
- Dates before the Gregorian calendar’s inception in 1582 are defined via the extrapolation. Be careful, as historical dates are often noted in the Julian calendar and others and the transition to Gregorian may differ across countries (as late as early 20C).
(Some example: Both Shakespeare from Britain and Cervantes from Spain seemingly died on the same calendar date—April 23, 1616—but in the different calendar. Britain used the Julian calendar at that time, so Shakespeare’s death is later.)
- ISO 8601 calendars have the year 0, which is 1 BCE (a year before 1 CE).
If you need a typical BCE/BC and CE/AD notation for year numbers,
use the
Datelike::year_cemethod.
Week Date
The ISO 8601 week date is a triple of year number, week number and day of the week with the following rules:
-
A week consists of Monday through Sunday, and is always numbered within some year. The week number ranges from 1 to 52 or 53 depending on the year.
-
The week 1 of given year is defined as the first week containing January 4 of that year, or equivalently, the first week containing four or more days in that year.
-
The year number in the week date may not correspond to the actual Gregorian year. For example, January 3, 2016 (Sunday) was on the last (53rd) week of 2015.
Chrono’s date types default to the ISO 8601 calendar date, but
Datelike::iso_week and Datelike::weekday methods can be used to get the corresponding
week date.
Ordinal Date
The ISO 8601 ordinal date is a pair of year number and day of the year (“ordinal”). The ordinal number ranges from 1 to 365 or 366 depending on the year. The year number is the same as that of the calendar date.
This is currently the internal format of Chrono’s date types.
Fully qualified path: chrono::date::Date
[derive(Clone, Copy, PartialEq, Drop, Serde, starknet::Store)]
pub struct Date { /* private fields */ }
Traits
Traits
DateTrait
Fully qualified path: chrono::date::DateTrait
pub trait DateTrait
Trait constants
MIN
The minimum possible NaiveDate (January 1, 262144 BCE).
(MIN_YEAR << 13) | (1 << 4) | 0o12 /* D */
Fully qualified path: chrono::date::DateTrait::MIN
const MIN: Date;
MAX
The maximum possible NaiveDate (December 31, 262142 CE).
(MAX_YEAR << 13) | (365 << 4) | 0o16 /* G */
Fully qualified path: chrono::date::DateTrait::MAX
const MAX: Date;
AFTER_MAX
One day before the minimum possible NaiveDate (December 31, 262145 BCE).
One day after the maximum possible NaiveDate (January 1, 262143 CE).
Fully qualified path: chrono::date::DateTrait::AFTER_MAX
const AFTER_MAX: Date;
Trait functions
weeks_from
Fully qualified path: chrono::date::DateTrait::weeks_from
fn weeks_from(self: @Date, day: Weekday) -> i32
from_ordinal_and_flags
Makes a new NaiveDate from year, ordinal and flags.
Does not check whether the flags are correct for the provided year.
Fully qualified path: chrono::date::DateTrait::from_ordinal_and_flags
fn from_ordinal_and_flags(year: u32, ordinal: u32, flags: YearFlags) -> Option<Date>
from_mdf
Makes a new NaiveDate from year and packed month-day-flags.
Does not check whether the flags are correct for the provided year.
Fully qualified path: chrono::date::DateTrait::from_mdf
fn from_mdf(year: u32, mdf: Mdf) -> Option<Date>
from_ymd_opt
Makes a new NaiveDate from the calendar date
(year, month and day).
Errors
Returns None if:
- The specified calendar day does not exist (for example 2023-04-31).
- The value for
monthordayis invalid. yearis out of range forNaiveDate.
Example
use chrono::NaiveDate;
let from_ymd_opt = NaiveDate::from_ymd_opt;
assert!(from_ymd_opt(2015, 3, 14).is_some());
assert!(from_ymd_opt(2015, 0, 14).is_none());
assert!(from_ymd_opt(2015, 2, 29).is_none());
assert!(from_ymd_opt(-4, 2, 29).is_some()); // 5 BCE is a leap year
assert!(from_ymd_opt(400000, 1, 1).is_none());
assert!(from_ymd_opt(-400000, 1, 1).is_none());
Fully qualified path: chrono::date::DateTrait::from_ymd_opt
fn from_ymd_opt(year: u32, month: u32, day: u32) -> Option<Date>
from_yo_opt
Makes a new NaiveDate from the ordinal date
(year and day of the year).
Errors
Returns None if:
- The specified ordinal day does not exist (for example 2023-366).
- The value for
ordinalis invalid (for example:0,400). yearis out of range forNaiveDate.
Example
use chrono::NaiveDate;
let from_yo_opt = NaiveDate::from_yo_opt;
assert!(from_yo_opt(2015, 100).is_some());
assert!(from_yo_opt(2015, 0).is_none());
assert!(from_yo_opt(2015, 365).is_some());
assert!(from_yo_opt(2015, 366).is_none());
assert!(from_yo_opt(-4, 366).is_some()); // 5 BCE is a leap year
assert!(from_yo_opt(400000, 1).is_none());
assert!(from_yo_opt(-400000, 1).is_none());
Fully qualified path: chrono::date::DateTrait::from_yo_opt
fn from_yo_opt(year: u32, ordinal: u32) -> Option<Date>
from_isoywd_opt
Makes a new NaiveDate from the ISO week date
(year, week number and day of the week).
The resulting NaiveDate may have a different year from the input year.
Errors
Returns None if:
- The specified week does not exist in that year (for example 2023 week 53).
- The value for
weekis invalid (for example:0,60). - If the resulting date is out of range for
NaiveDate.
Example
use chrono::{NaiveDate, Weekday};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let from_isoywd_opt = NaiveDate::from_isoywd_opt;
assert_eq!(from_isoywd_opt(2015, 0, Weekday::Sun), None);
assert_eq!(from_isoywd_opt(2015, 10, Weekday::Sun), Some(from_ymd(2015, 3, 8)));
assert_eq!(from_isoywd_opt(2015, 30, Weekday::Mon), Some(from_ymd(2015, 7, 20)));
assert_eq!(from_isoywd_opt(2015, 60, Weekday::Mon), None);
assert_eq!(from_isoywd_opt(400000, 10, Weekday::Fri), None);
assert_eq!(from_isoywd_opt(-400000, 10, Weekday::Sat), None);
The year number of ISO week date may differ from that of the calendar date.
use chrono::{NaiveDate, Weekday};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let from_isoywd_opt = NaiveDate::from_isoywd_opt;
// Mo Tu We Th Fr Sa Su
// 2014-W52 22 23 24 25 26 27 28 has 4+ days of new year,
// 2015-W01 29 30 31 1 2 3 4 <- so this is the first week
assert_eq!(from_isoywd_opt(2014, 52, Weekday::Sun), Some(from_ymd(2014, 12, 28)));
assert_eq!(from_isoywd_opt(2014, 53, Weekday::Mon), None);
assert_eq!(from_isoywd_opt(2015, 1, Weekday::Mon), Some(from_ymd(2014, 12, 29)));
// 2015-W52 21 22 23 24 25 26 27 has 4+ days of old year,
// 2015-W53 28 29 30 31 1 2 3 <- so this is the last week
// 2016-W01 4 5 6 7 8 9 10
assert_eq!(from_isoywd_opt(2015, 52, Weekday::Sun), Some(from_ymd(2015, 12, 27)));
assert_eq!(from_isoywd_opt(2015, 53, Weekday::Sun), Some(from_ymd(2016, 1, 3)));
assert_eq!(from_isoywd_opt(2015, 54, Weekday::Mon), None);
assert_eq!(from_isoywd_opt(2016, 1, Weekday::Mon), Some(from_ymd(2016, 1, 4)));
Fully qualified path: chrono::date::DateTrait::from_isoywd_opt
fn from_isoywd_opt(year: u32, week: u32, weekday: Weekday) -> Option<Date>
from_num_days_from_ce_opt
Makes a new NaiveDate from a day’s number in the proleptic Gregorian calendar, with
January 1, 1 being day 1.
Errors
Returns None if the date is out of range.
Example
use chrono::NaiveDate;
let from_ndays_opt = NaiveDate::from_num_days_from_ce_opt;
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
assert_eq!(from_ndays_opt(730_000), Some(from_ymd(1999, 9, 3)));
assert_eq!(from_ndays_opt(1), Some(from_ymd(1, 1, 1)));
assert_eq!(from_ndays_opt(0), Some(from_ymd(0, 12, 31)));
assert_eq!(from_ndays_opt(-1), Some(from_ymd(0, 12, 30)));
assert_eq!(from_ndays_opt(100_000_000), None);
assert_eq!(from_ndays_opt(-100_000_000), None);
Fully qualified path: chrono::date::DateTrait::from_num_days_from_ce_opt
fn from_num_days_from_ce_opt(days: i32) -> Option<Date>
from_weekday_of_month_opt
Makes a new NaiveDate by counting the number of occurrences of a particular day-of-week
since the beginning of the given month. For instance, if you want the 2nd Friday of March
2017, you would use NaiveDate::from_weekday_of_month(2017, 3, Weekday::Fri, 2).
n is 1-indexed.
Errors
Returns None if:
- The specified day does not exist in that month (for example the 5th Monday of Apr. 2023).
- The value for
monthornis invalid. yearis out of range forNaiveDate.
Example
use chrono::{NaiveDate, Weekday};
assert_eq!(
NaiveDate::from_weekday_of_month_opt(2017, 3, Weekday::Fri, 2),
NaiveDate::from_ymd_opt(2017, 3, 10)
)
Fully qualified path: chrono::date::DateTrait::from_weekday_of_month_opt
fn from_weekday_of_month_opt(year: u32, month: u32, weekday: Weekday, n: u8) -> Option<Date>
checked_add_months
Add a duration in Months to the date
Uses the last day of the month if the day does not exist in the resulting month.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, Months};
assert_eq!(
NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_add_months(Months::new(6)),
Some(NaiveDate::from_ymd_opt(2022, 8, 20).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2022, 7, 31).unwrap().checked_add_months(Months::new(2)),
Some(NaiveDate::from_ymd_opt(2022, 9, 30).unwrap())
);
Fully qualified path: chrono::date::DateTrait::checked_add_months
fn checked_add_months(self: @Date, months: Months) -> Option<Date>
checked_sub_months
Subtract a duration in Months from the date
Uses the last day of the month if the day does not exist in the resulting month.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, Months};
assert_eq!(
NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_sub_months(Months::new(6)),
Some(NaiveDate::from_ymd_opt(2021, 8, 20).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1)
.unwrap()
.checked_sub_months(Months::new(core::i32::MAX as u32 + 1)),
None
);
Fully qualified path: chrono::date::DateTrait::checked_sub_months
fn checked_sub_months(self: @Date, months: Months) -> Option<Date>
diff_months
Fully qualified path: chrono::date::DateTrait::diff_months
fn diff_months(self: @Date, months: i32) -> Option<Date>
checked_add_days
Add a duration in Days to the date
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, Days};
assert_eq!(
NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_add_days(Days::new(9)),
Some(NaiveDate::from_ymd_opt(2022, 3, 1).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2022, 7, 31).unwrap().checked_add_days(Days::new(2)),
Some(NaiveDate::from_ymd_opt(2022, 8, 2).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2022, 7,
31).unwrap().checked_add_days(Days::new(1000000000000)), None
);
Fully qualified path: chrono::date::DateTrait::checked_add_days
fn checked_add_days(self: @Date, days: Days) -> Option<Date>
checked_sub_days
Subtract a duration in Days from the date
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, Days};
assert_eq!(
NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_sub_days(Days::new(6)),
Some(NaiveDate::from_ymd_opt(2022, 2, 14).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2022, 2,
20).unwrap().checked_sub_days(Days::new(1000000000000)), None
);
Fully qualified path: chrono::date::DateTrait::checked_sub_days
fn checked_sub_days(self: @Date, days: Days) -> Option<Date>
add_days
Add a duration of i32 days to the date.
Fully qualified path: chrono::date::DateTrait::add_days
fn add_days(self: @Date, days: i32) -> Option<Date>
and_time
Makes a new NaiveDateTime from the current date and given NaiveTime.
Example
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
let t = NaiveTime::from_hms_milli_opt(12, 34, 56, 789).unwrap();
let dt: NaiveDateTime = d.and_time(t);
assert_eq!(dt.date(), d);
assert_eq!(dt.time(), t);
Fully qualified path: chrono::date::DateTrait::and_time
fn and_time(self: @Date, time: Time) -> DateTime
and_hms_opt
Makes a new NaiveDateTime from the current date, hour, minute and second.
No leap second is allowed here;
use NaiveDate::and_hms_*_opt methods with a subsecond parameter instead.
Errors
Returns None on invalid hour, minute and/or second.
Example
use chrono::NaiveDate;
let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
assert!(d.and_hms_opt(12, 34, 56).is_some());
assert!(d.and_hms_opt(12, 34, 60).is_none()); // use `and_hms_milli_opt` instead
assert!(d.and_hms_opt(12, 60, 56).is_none());
assert!(d.and_hms_opt(24, 34, 56).is_none());
Fully qualified path: chrono::date::DateTrait::and_hms_opt
fn and_hms_opt(self: @Date, hour: u32, min: u32, sec: u32) -> Option<DateTime>
mdf
Returns the packed month-day-flags.
Fully qualified path: chrono::date::DateTrait::mdf
fn mdf(self: @Date) -> Mdf
with_mdf
Makes a new NaiveDate with the packed month-day-flags changed.
Returns None when the resulting NaiveDate would be invalid.
Fully qualified path: chrono::date::DateTrait::with_mdf
fn with_mdf(self: @Date, mdf: Mdf) -> Option<Date>
succ_opt
Makes a new NaiveDate for the next calendar date.
Errors
Returns None when self is the last representable date.
Example
use chrono::NaiveDate;
assert_eq!(
NaiveDate::from_ymd_opt(2015, 6, 3).unwrap().succ_opt(),
Some(NaiveDate::from_ymd_opt(2015, 6, 4).unwrap())
);
assert_eq!(NaiveDate::MAX.succ_opt(), None);
Fully qualified path: chrono::date::DateTrait::succ_opt
fn succ_opt(self: @Date) -> Option<Date>
pred_opt
Makes a new NaiveDate for the previous calendar date.
Errors
Returns None when self is the first representable date.
Example
use chrono::NaiveDate;
assert_eq!(
NaiveDate::from_ymd_opt(2015, 6, 3).unwrap().pred_opt(),
Some(NaiveDate::from_ymd_opt(2015, 6, 2).unwrap())
);
assert_eq!(NaiveDate::MIN.pred_opt(), None);
Fully qualified path: chrono::date::DateTrait::pred_opt
fn pred_opt(self: @Date) -> Option<Date>
checked_add_signed
Adds the number of whole days in the given TimeDelta to the current date.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, TimeDelta};
let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
assert_eq!(
d.checked_add_signed(TimeDelta::try_days(40).unwrap()),
Some(NaiveDate::from_ymd_opt(2015, 10, 15).unwrap())
);
assert_eq!(
d.checked_add_signed(TimeDelta::try_days(-40).unwrap()),
Some(NaiveDate::from_ymd_opt(2015, 7, 27).unwrap())
);
assert_eq!(d.checked_add_signed(TimeDelta::try_days(1_000_000_000).unwrap()), None);
assert_eq!(d.checked_add_signed(TimeDelta::try_days(-1_000_000_000).unwrap()), None);
assert_eq!(NaiveDate::MAX.checked_add_signed(TimeDelta::try_days(1).unwrap()), None);
Fully qualified path: chrono::date::DateTrait::checked_add_signed
fn checked_add_signed(self: @Date, rhs: TimeDelta) -> Option<Date>
checked_sub_signed
Subtracts the number of whole days in the given TimeDelta from the current date.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, TimeDelta};
let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
assert_eq!(
d.checked_sub_signed(TimeDelta::try_days(40).unwrap()),
Some(NaiveDate::from_ymd_opt(2015, 7, 27).unwrap())
);
assert_eq!(
d.checked_sub_signed(TimeDelta::try_days(-40).unwrap()),
Some(NaiveDate::from_ymd_opt(2015, 10, 15).unwrap())
);
assert_eq!(d.checked_sub_signed(TimeDelta::try_days(1_000_000_000).unwrap()), None);
assert_eq!(d.checked_sub_signed(TimeDelta::try_days(-1_000_000_000).unwrap()), None);
assert_eq!(NaiveDate::MIN.checked_sub_signed(TimeDelta::try_days(1).unwrap()), None);
Fully qualified path: chrono::date::DateTrait::checked_sub_signed
fn checked_sub_signed(self: @Date, rhs: TimeDelta) -> Option<Date>
signed_duration_since
Subtracts another NaiveDate from the current date.
Returns a TimeDelta of integral numbers.
This does not overflow or underflow at all,
as all possible output fits in the range of TimeDelta.
Example
use chrono::{NaiveDate, TimeDelta};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let since = NaiveDate::signed_duration_since;
assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 1)), TimeDelta::zero());
assert_eq!(
since(from_ymd(2014, 1, 1), from_ymd(2013, 12, 31)),
TimeDelta::try_days(1).unwrap()
);
assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 2)),
TimeDelta::try_days(-1).unwrap());
assert_eq!(
since(from_ymd(2014, 1, 1), from_ymd(2013, 9, 23)),
TimeDelta::try_days(100).unwrap()
);
assert_eq!(
since(from_ymd(2014, 1, 1), from_ymd(2013, 1, 1)),
TimeDelta::try_days(365).unwrap()
);
assert_eq!(
since(from_ymd(2014, 1, 1), from_ymd(2010, 1, 1)),
TimeDelta::try_days(365 * 4 + 1).unwrap()
);
assert_eq!(
since(from_ymd(2014, 1, 1), from_ymd(1614, 1, 1)),
TimeDelta::try_days(365 * 400 + 97).unwrap()
);
Fully qualified path: chrono::date::DateTrait::signed_duration_since
fn signed_duration_since(self: @Date, rhs: Date) -> TimeDelta
years_since
Returns the number of whole years from the given base until self.
Errors
Returns None if base > self.
Fully qualified path: chrono::date::DateTrait::years_since
fn years_since(self: @Date, base: Date) -> Option<u32>
week
Returns the NaiveWeek that the date belongs to, starting with the Weekday
specified.
Fully qualified path: chrono::date::DateTrait::week
fn week(self: @Date, start: Weekday) -> Week
leap_year
Returns true if this is a leap year.
use chrono::NaiveDate;
assert_eq!(NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().leap_year(), true);
assert_eq!(NaiveDate::from_ymd_opt(2001, 1, 1).unwrap().leap_year(), false);
assert_eq!(NaiveDate::from_ymd_opt(2002, 1, 1).unwrap().leap_year(), false);
assert_eq!(NaiveDate::from_ymd_opt(2003, 1, 1).unwrap().leap_year(), false);
assert_eq!(NaiveDate::from_ymd_opt(2004, 1, 1).unwrap().leap_year(), true);
assert_eq!(NaiveDate::from_ymd_opt(2100, 1, 1).unwrap().leap_year(), false);
Fully qualified path: chrono::date::DateTrait::leap_year
fn leap_year(self: @Date) -> bool
year_flags
Fully qualified path: chrono::date::DateTrait::year_flags
fn year_flags(self: @Date) -> YearFlags
from_yof
Create a new NaiveDate from a raw year-ordinal-flags i32.
In a valid value an ordinal is never 0, and neither are the year flags. This method
doesn’t do any validation in release builds.
Fully qualified path: chrono::date::DateTrait::from_yof
fn from_yof(yof: u32) -> Date
yof
Get the raw year-ordinal-flags i32.
Fully qualified path: chrono::date::DateTrait::yof
fn yof(self: @Date) -> u32
Impls
Impls
DateImpl
Fully qualified path: chrono::date::DateImpl
pub impl DateImpl of DateTrait;
Impl constants
MIN
The minimum possible NaiveDate (January 1, 262144 BCE).
(MIN_YEAR << 13) | (1 << 4) | 0o12 /* D */
Fully qualified path: chrono::date::DateImpl::MIN
const MIN: Date = Self::from_yof((MIN_YEAR * 2_u32.pow(13)) | (1 * 2_u32.pow(4)) | 0o4);
MAX
The maximum possible NaiveDate (December 31, 262142 CE).
(MAX_YEAR << 13) | (365 << 4) | 0o16 /* G */
Fully qualified path: chrono::date::DateImpl::MAX
const MAX: Date = Self::from_yof((MAX_YEAR * 2_u32.pow(13)) | (365 * 2_u32.pow(4)) | 0o16);
AFTER_MAX
One day before the minimum possible NaiveDate (December 31, 262145 BCE).
One day after the maximum possible NaiveDate (January 1, 262143 CE).
Fully qualified path: chrono::date::DateImpl::AFTER_MAX
const AFTER_MAX: Date = Self::from_yof(
((MAX_YEAR + 1) * 2_u32.pow(13)) | (1 * 2_u32.pow(4)) | 0o17,
);
Impl functions
weeks_from
Fully qualified path: chrono::date::DateImpl::weeks_from
fn weeks_from(self: @Date, day: Weekday) -> i32
from_ordinal_and_flags
Makes a new NaiveDate from year, ordinal and flags.
Does not check whether the flags are correct for the provided year.
Fully qualified path: chrono::date::DateImpl::from_ordinal_and_flags
fn from_ordinal_and_flags(year: u32, ordinal: u32, flags: YearFlags) -> Option<Date>
from_mdf
Makes a new NaiveDate from year and packed month-day-flags.
Does not check whether the flags are correct for the provided year.
Fully qualified path: chrono::date::DateImpl::from_mdf
fn from_mdf(year: u32, mdf: Mdf) -> Option<Date>
from_ymd_opt
Makes a new NaiveDate from the calendar date
(year, month and day).
Errors
Returns None if:
- The specified calendar day does not exist (for example 2023-04-31).
- The value for
monthordayis invalid. yearis out of range forNaiveDate.
Example
use chrono::NaiveDate;
let from_ymd_opt = NaiveDate::from_ymd_opt;
assert!(from_ymd_opt(2015, 3, 14).is_some());
assert!(from_ymd_opt(2015, 0, 14).is_none());
assert!(from_ymd_opt(2015, 2, 29).is_none());
assert!(from_ymd_opt(-4, 2, 29).is_some()); // 5 BCE is a leap year
assert!(from_ymd_opt(400000, 1, 1).is_none());
assert!(from_ymd_opt(-400000, 1, 1).is_none());
Fully qualified path: chrono::date::DateImpl::from_ymd_opt
fn from_ymd_opt(year: u32, month: u32, day: u32) -> Option<Date>
from_yo_opt
Makes a new NaiveDate from the ordinal date
(year and day of the year).
Errors
Returns None if:
- The specified ordinal day does not exist (for example 2023-366).
- The value for
ordinalis invalid (for example:0,400). yearis out of range forNaiveDate.
Example
use chrono::NaiveDate;
let from_yo_opt = NaiveDate::from_yo_opt;
assert!(from_yo_opt(2015, 100).is_some());
assert!(from_yo_opt(2015, 0).is_none());
assert!(from_yo_opt(2015, 365).is_some());
assert!(from_yo_opt(2015, 366).is_none());
assert!(from_yo_opt(-4, 366).is_some()); // 5 BCE is a leap year
assert!(from_yo_opt(400000, 1).is_none());
assert!(from_yo_opt(-400000, 1).is_none());
Fully qualified path: chrono::date::DateImpl::from_yo_opt
fn from_yo_opt(year: u32, ordinal: u32) -> Option<Date>
from_isoywd_opt
Makes a new NaiveDate from the ISO week date
(year, week number and day of the week).
The resulting NaiveDate may have a different year from the input year.
Errors
Returns None if:
- The specified week does not exist in that year (for example 2023 week 53).
- The value for
weekis invalid (for example:0,60). - If the resulting date is out of range for
NaiveDate.
Example
use chrono::{NaiveDate, Weekday};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let from_isoywd_opt = NaiveDate::from_isoywd_opt;
assert_eq!(from_isoywd_opt(2015, 0, Weekday::Sun), None);
assert_eq!(from_isoywd_opt(2015, 10, Weekday::Sun), Some(from_ymd(2015, 3, 8)));
assert_eq!(from_isoywd_opt(2015, 30, Weekday::Mon), Some(from_ymd(2015, 7, 20)));
assert_eq!(from_isoywd_opt(2015, 60, Weekday::Mon), None);
assert_eq!(from_isoywd_opt(400000, 10, Weekday::Fri), None);
assert_eq!(from_isoywd_opt(-400000, 10, Weekday::Sat), None);
The year number of ISO week date may differ from that of the calendar date.
use chrono::{NaiveDate, Weekday};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let from_isoywd_opt = NaiveDate::from_isoywd_opt;
// Mo Tu We Th Fr Sa Su
// 2014-W52 22 23 24 25 26 27 28 has 4+ days of new year,
// 2015-W01 29 30 31 1 2 3 4 <- so this is the first week
assert_eq!(from_isoywd_opt(2014, 52, Weekday::Sun), Some(from_ymd(2014, 12, 28)));
assert_eq!(from_isoywd_opt(2014, 53, Weekday::Mon), None);
assert_eq!(from_isoywd_opt(2015, 1, Weekday::Mon), Some(from_ymd(2014, 12, 29)));
// 2015-W52 21 22 23 24 25 26 27 has 4+ days of old year,
// 2015-W53 28 29 30 31 1 2 3 <- so this is the last week
// 2016-W01 4 5 6 7 8 9 10
assert_eq!(from_isoywd_opt(2015, 52, Weekday::Sun), Some(from_ymd(2015, 12, 27)));
assert_eq!(from_isoywd_opt(2015, 53, Weekday::Sun), Some(from_ymd(2016, 1, 3)));
assert_eq!(from_isoywd_opt(2015, 54, Weekday::Mon), None);
assert_eq!(from_isoywd_opt(2016, 1, Weekday::Mon), Some(from_ymd(2016, 1, 4)));
Fully qualified path: chrono::date::DateImpl::from_isoywd_opt
fn from_isoywd_opt(year: u32, week: u32, weekday: Weekday) -> Option<Date>
from_num_days_from_ce_opt
Makes a new NaiveDate from a day’s number in the proleptic Gregorian calendar, with
January 1, 1 being day 1.
Errors
Returns None if the date is out of range.
Example
use chrono::NaiveDate;
let from_ndays_opt = NaiveDate::from_num_days_from_ce_opt;
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
assert_eq!(from_ndays_opt(730_000), Some(from_ymd(1999, 9, 3)));
assert_eq!(from_ndays_opt(1), Some(from_ymd(1, 1, 1)));
assert_eq!(from_ndays_opt(0), Some(from_ymd(0, 12, 31)));
assert_eq!(from_ndays_opt(-1), Some(from_ymd(0, 12, 30)));
assert_eq!(from_ndays_opt(100_000_000), None);
assert_eq!(from_ndays_opt(-100_000_000), None);
Fully qualified path: chrono::date::DateImpl::from_num_days_from_ce_opt
fn from_num_days_from_ce_opt(days: i32) -> Option<Date>
from_weekday_of_month_opt
Makes a new NaiveDate by counting the number of occurrences of a particular day-of-week
since the beginning of the given month. For instance, if you want the 2nd Friday of March
2017, you would use NaiveDate::from_weekday_of_month(2017, 3, Weekday::Fri, 2).
n is 1-indexed.
Errors
Returns None if:
- The specified day does not exist in that month (for example the 5th Monday of Apr. 2023).
- The value for
monthornis invalid. yearis out of range forNaiveDate.
Example
use chrono::{NaiveDate, Weekday};
assert_eq!(
NaiveDate::from_weekday_of_month_opt(2017, 3, Weekday::Fri, 2),
NaiveDate::from_ymd_opt(2017, 3, 10)
)
Fully qualified path: chrono::date::DateImpl::from_weekday_of_month_opt
fn from_weekday_of_month_opt(year: u32, month: u32, weekday: Weekday, n: u8) -> Option<Date>
checked_add_months
Add a duration in Months to the date
Uses the last day of the month if the day does not exist in the resulting month.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, Months};
assert_eq!(
NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_add_months(Months::new(6)),
Some(NaiveDate::from_ymd_opt(2022, 8, 20).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2022, 7, 31).unwrap().checked_add_months(Months::new(2)),
Some(NaiveDate::from_ymd_opt(2022, 9, 30).unwrap())
);
Fully qualified path: chrono::date::DateImpl::checked_add_months
fn checked_add_months(self: @Date, months: Months) -> Option<Date>
checked_sub_months
Subtract a duration in Months from the date
Uses the last day of the month if the day does not exist in the resulting month.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, Months};
assert_eq!(
NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_sub_months(Months::new(6)),
Some(NaiveDate::from_ymd_opt(2021, 8, 20).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1)
.unwrap()
.checked_sub_months(Months::new(core::i32::MAX as u32 + 1)),
None
);
Fully qualified path: chrono::date::DateImpl::checked_sub_months
fn checked_sub_months(self: @Date, months: Months) -> Option<Date>
diff_months
Fully qualified path: chrono::date::DateImpl::diff_months
fn diff_months(self: @Date, months: i32) -> Option<Date>
checked_add_days
Add a duration in Days to the date
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, Days};
assert_eq!(
NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_add_days(Days::new(9)),
Some(NaiveDate::from_ymd_opt(2022, 3, 1).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2022, 7, 31).unwrap().checked_add_days(Days::new(2)),
Some(NaiveDate::from_ymd_opt(2022, 8, 2).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2022, 7,
31).unwrap().checked_add_days(Days::new(1000000000000)), None
);
Fully qualified path: chrono::date::DateImpl::checked_add_days
fn checked_add_days(self: @Date, days: Days) -> Option<Date>
checked_sub_days
Subtract a duration in Days from the date
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, Days};
assert_eq!(
NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_sub_days(Days::new(6)),
Some(NaiveDate::from_ymd_opt(2022, 2, 14).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2022, 2,
20).unwrap().checked_sub_days(Days::new(1000000000000)), None
);
Fully qualified path: chrono::date::DateImpl::checked_sub_days
fn checked_sub_days(self: @Date, days: Days) -> Option<Date>
add_days
Add a duration of i32 days to the date.
Fully qualified path: chrono::date::DateImpl::add_days
fn add_days(self: @Date, days: i32) -> Option<Date>
and_time
Makes a new NaiveDateTime from the current date and given NaiveTime.
Example
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
let t = NaiveTime::from_hms_milli_opt(12, 34, 56, 789).unwrap();
let dt: NaiveDateTime = d.and_time(t);
assert_eq!(dt.date(), d);
assert_eq!(dt.time(), t);
Fully qualified path: chrono::date::DateImpl::and_time
fn and_time(self: @Date, time: Time) -> DateTime
and_hms_opt
Makes a new NaiveDateTime from the current date, hour, minute and second.
No leap second is allowed here;
use NaiveDate::and_hms_*_opt methods with a subsecond parameter instead.
Errors
Returns None on invalid hour, minute and/or second.
Example
use chrono::NaiveDate;
let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
assert!(d.and_hms_opt(12, 34, 56).is_some());
assert!(d.and_hms_opt(12, 34, 60).is_none()); // use `and_hms_milli_opt` instead
assert!(d.and_hms_opt(12, 60, 56).is_none());
assert!(d.and_hms_opt(24, 34, 56).is_none());
Fully qualified path: chrono::date::DateImpl::and_hms_opt
fn and_hms_opt(self: @Date, hour: u32, min: u32, sec: u32) -> Option<DateTime>
mdf
Returns the packed month-day-flags.
Fully qualified path: chrono::date::DateImpl::mdf
fn mdf(self: @Date) -> Mdf
with_mdf
Makes a new NaiveDate with the packed month-day-flags changed.
Returns None when the resulting NaiveDate would be invalid.
Fully qualified path: chrono::date::DateImpl::with_mdf
fn with_mdf(self: @Date, mdf: Mdf) -> Option<Date>
succ_opt
Makes a new NaiveDate for the next calendar date.
Errors
Returns None when self is the last representable date.
Example
use chrono::NaiveDate;
assert_eq!(
NaiveDate::from_ymd_opt(2015, 6, 3).unwrap().succ_opt(),
Some(NaiveDate::from_ymd_opt(2015, 6, 4).unwrap())
);
assert_eq!(NaiveDate::MAX.succ_opt(), None);
Fully qualified path: chrono::date::DateImpl::succ_opt
fn succ_opt(self: @Date) -> Option<Date>
pred_opt
Makes a new NaiveDate for the previous calendar date.
Errors
Returns None when self is the first representable date.
Example
use chrono::NaiveDate;
assert_eq!(
NaiveDate::from_ymd_opt(2015, 6, 3).unwrap().pred_opt(),
Some(NaiveDate::from_ymd_opt(2015, 6, 2).unwrap())
);
assert_eq!(NaiveDate::MIN.pred_opt(), None);
Fully qualified path: chrono::date::DateImpl::pred_opt
fn pred_opt(self: @Date) -> Option<Date>
checked_add_signed
Adds the number of whole days in the given TimeDelta to the current date.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, TimeDelta};
let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
assert_eq!(
d.checked_add_signed(TimeDelta::try_days(40).unwrap()),
Some(NaiveDate::from_ymd_opt(2015, 10, 15).unwrap())
);
assert_eq!(
d.checked_add_signed(TimeDelta::try_days(-40).unwrap()),
Some(NaiveDate::from_ymd_opt(2015, 7, 27).unwrap())
);
assert_eq!(d.checked_add_signed(TimeDelta::try_days(1_000_000_000).unwrap()), None);
assert_eq!(d.checked_add_signed(TimeDelta::try_days(-1_000_000_000).unwrap()), None);
assert_eq!(NaiveDate::MAX.checked_add_signed(TimeDelta::try_days(1).unwrap()), None);
Fully qualified path: chrono::date::DateImpl::checked_add_signed
fn checked_add_signed(self: @Date, rhs: TimeDelta) -> Option<Date>
checked_sub_signed
Subtracts the number of whole days in the given TimeDelta from the current date.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, TimeDelta};
let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
assert_eq!(
d.checked_sub_signed(TimeDelta::try_days(40).unwrap()),
Some(NaiveDate::from_ymd_opt(2015, 7, 27).unwrap())
);
assert_eq!(
d.checked_sub_signed(TimeDelta::try_days(-40).unwrap()),
Some(NaiveDate::from_ymd_opt(2015, 10, 15).unwrap())
);
assert_eq!(d.checked_sub_signed(TimeDelta::try_days(1_000_000_000).unwrap()), None);
assert_eq!(d.checked_sub_signed(TimeDelta::try_days(-1_000_000_000).unwrap()), None);
assert_eq!(NaiveDate::MIN.checked_sub_signed(TimeDelta::try_days(1).unwrap()), None);
Fully qualified path: chrono::date::DateImpl::checked_sub_signed
fn checked_sub_signed(self: @Date, rhs: TimeDelta) -> Option<Date>
signed_duration_since
Subtracts another NaiveDate from the current date.
Returns a TimeDelta of integral numbers.
This does not overflow or underflow at all,
as all possible output fits in the range of TimeDelta.
Example
use chrono::{NaiveDate, TimeDelta};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let since = NaiveDate::signed_duration_since;
assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 1)), TimeDelta::zero());
assert_eq!(
since(from_ymd(2014, 1, 1), from_ymd(2013, 12, 31)),
TimeDelta::try_days(1).unwrap()
);
assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 2)),
TimeDelta::try_days(-1).unwrap());
assert_eq!(
since(from_ymd(2014, 1, 1), from_ymd(2013, 9, 23)),
TimeDelta::try_days(100).unwrap()
);
assert_eq!(
since(from_ymd(2014, 1, 1), from_ymd(2013, 1, 1)),
TimeDelta::try_days(365).unwrap()
);
assert_eq!(
since(from_ymd(2014, 1, 1), from_ymd(2010, 1, 1)),
TimeDelta::try_days(365 * 4 + 1).unwrap()
);
assert_eq!(
since(from_ymd(2014, 1, 1), from_ymd(1614, 1, 1)),
TimeDelta::try_days(365 * 400 + 97).unwrap()
);
Fully qualified path: chrono::date::DateImpl::signed_duration_since
fn signed_duration_since(self: @Date, rhs: Date) -> TimeDelta
years_since
Returns the number of whole years from the given base until self.
Errors
Returns None if base > self.
Fully qualified path: chrono::date::DateImpl::years_since
fn years_since(self: @Date, base: Date) -> Option<u32>
week
Returns the NaiveWeek that the date belongs to, starting with the Weekday
specified.
Fully qualified path: chrono::date::DateImpl::week
fn week(self: @Date, start: Weekday) -> Week
leap_year
Returns true if this is a leap year.
use chrono::NaiveDate;
assert_eq!(NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().leap_year(), true);
assert_eq!(NaiveDate::from_ymd_opt(2001, 1, 1).unwrap().leap_year(), false);
assert_eq!(NaiveDate::from_ymd_opt(2002, 1, 1).unwrap().leap_year(), false);
assert_eq!(NaiveDate::from_ymd_opt(2003, 1, 1).unwrap().leap_year(), false);
assert_eq!(NaiveDate::from_ymd_opt(2004, 1, 1).unwrap().leap_year(), true);
assert_eq!(NaiveDate::from_ymd_opt(2100, 1, 1).unwrap().leap_year(), false);
Fully qualified path: chrono::date::DateImpl::leap_year
fn leap_year(self: @Date) -> bool
year_flags
Fully qualified path: chrono::date::DateImpl::year_flags
fn year_flags(self: @Date) -> YearFlags
from_yof
Create a new NaiveDate from a raw year-ordinal-flags i32.
In a valid value an ordinal is never 0, and neither are the year flags. This method
doesn’t do any validation in release builds.
Fully qualified path: chrono::date::DateImpl::from_yof
fn from_yof(yof: u32) -> Date
yof
Get the raw year-ordinal-flags i32.
Fully qualified path: chrono::date::DateImpl::yof
fn yof(self: @Date) -> u32
datetime
Fully qualified path: chrono::datetime
Structs
| DateTime | ISO 8601 combined date and time without timezone…. |
Traits
Impls
Structs
Structs
| DateTime | ISO 8601 combined date and time without timezone…. |
DateTime
ISO 8601 combined date and time without timezone.
Example
NaiveDateTime is commonly created from NaiveDate.
use chrono::{NaiveDate, NaiveDateTime};
let dt: NaiveDateTime =
NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(9, 10, 11).unwrap();
let _ = dt;
You can use typical date-like and time-like methods, provided that relevant traits are in the scope.
use chrono::{NaiveDate, NaiveDateTime};
let dt: NaiveDateTime = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(9, 10,
11).unwrap();
use chrono::{Datelike, Timelike, Weekday};
assert_eq!(dt.weekday(), Weekday::Fri);
assert_eq!(dt.num_seconds_from_midnight(), 33011);
Fully qualified path: chrono::datetime::DateTime
#[derive(Copy, PartialEq, Drop, Serde, starknet::Store)]
pub struct DateTime {
pub date: Date,
pub time: Time,
}
Members
date
Fully qualified path: chrono::datetime::DateTime::date
pub date: Date
time
Fully qualified path: chrono::datetime::DateTime::time
pub time: Time
Traits
Traits
DateTimeTrait
Fully qualified path: chrono::datetime::DateTimeTrait
pub trait DateTimeTrait
Trait constants
MIN
The minimum possible NaiveDateTime.
Fully qualified path: chrono::datetime::DateTimeTrait::MIN
const MIN: DateTime;
MAX
The maximum possible NaiveDateTime.
Fully qualified path: chrono::datetime::DateTimeTrait::MAX
const MAX: DateTime;
UNIX_EPOCH
The datetime of the Unix Epoch, 1970-01-01 00:00:00.
Note that while this may look like the UNIX epoch, it is missing the
time zone. The actual UNIX epoch cannot be expressed by this type,
however it is available as DateTime::UNIX_EPOCH.
Fully qualified path: chrono::datetime::DateTimeTrait::UNIX_EPOCH
const UNIX_EPOCH: DateTime;
Trait functions
new
Makes a new NaiveDateTime from date and time components.
Equivalent to date.and_time(time)
and many other helper constructors on NaiveDate.
Example
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
let t = NaiveTime::from_hms_milli_opt(12, 34, 56, 789).unwrap();
let dt = NaiveDateTime::new(d, t);
assert_eq!(dt.date(), d);
assert_eq!(dt.time(), t);
Fully qualified path: chrono::datetime::DateTimeTrait::new
fn new(date: Date, time: Time) -> DateTime
from_timestamp
Makes a new NaiveDateTime corresponding to a UTC date and time,
from the number of non-leap seconds
since the midnight UTC on January 1, 1970 (aka “UNIX timestamp”)
and the number of nanoseconds since the last whole non-leap second.
For a non-naive version of this function see TimeZone::timestamp.
The nanosecond part can exceed 1,000,000,000 in order to represent a
leap second, but only when secs % 60 == 59.
(The true “UNIX timestamp” cannot represent a leap second unambiguously.)
Panics
Panics if the number of seconds would be out of range for a NaiveDateTime (more than
ca. 262,000 years away from common era), and panics on an invalid nanosecond (2 seconds or
more).
Fully qualified path: chrono::datetime::DateTimeTrait::from_timestamp
fn from_timestamp(secs: i64) -> Option<DateTime>
from_block_timestamp
Fully qualified path: chrono::datetime::DateTimeTrait::from_block_timestamp
fn from_block_timestamp(block_timestamp: u64) -> Option<DateTime>
date
Retrieves a date component.
Example
use chrono::NaiveDate;
let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(9, 10, 11).unwrap();
assert_eq!(dt.date(), NaiveDate::from_ymd_opt(2016, 7, 8).unwrap());
Fully qualified path: chrono::datetime::DateTimeTrait::date
fn date(self: @DateTime) -> Date
time
Retrieves a time component.
Example
use chrono::{NaiveDate, NaiveTime};
let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(9, 10, 11).unwrap();
assert_eq!(dt.time(), NaiveTime::from_hms_opt(9, 10, 11).unwrap());
Fully qualified path: chrono::datetime::DateTimeTrait::time
fn time(self: @DateTime) -> Time
timestamp
Returns the number of non-leap seconds since January 1, 1970 0:00:00 UTC (aka “UNIX timestamp”).
The reverse operation of creating a DateTime from a timestamp can be performed
using from_timestamp or TimeZone::timestamp_opt.
use chrono::{DateTime, TimeZone, Utc};
let dt: DateTime<Utc> = Utc.with_ymd_and_hms(2015, 5, 15, 0, 0, 0).unwrap();
assert_eq!(dt.timestamp(), 1431648000);
assert_eq!(DateTime::from_timestamp(dt.timestamp(), dt.timestamp_subsec_nanos()).unwrap(),
dt);
Fully qualified path: chrono::datetime::DateTimeTrait::timestamp
fn timestamp(self: @DateTime) -> i64
checked_add_signed
Adds given TimeDelta to the current date and time.
As a part of Chrono’s leap second handling,
the addition assumes that there is no leap second ever,
except when the NaiveDateTime itself represents a leap second
in which case the assumption becomes that there is exactly a single leap second ever.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, TimeDelta};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let d = from_ymd(2016, 7, 8);
let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap();
assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::zero()), Some(hms(3, 5, 7)));
assert_eq!(
hms(3, 5, 7).checked_add_signed(TimeDelta::try_seconds(1).unwrap()),
Some(hms(3, 5, 8))
);
assert_eq!(
hms(3, 5, 7).checked_add_signed(TimeDelta::try_seconds(-1).unwrap()),
Some(hms(3, 5, 6))
);
assert_eq!(
hms(3, 5, 7).checked_add_signed(TimeDelta::try_seconds(3600 + 60).unwrap()),
Some(hms(4, 6, 7))
);
assert_eq!(
hms(3, 5, 7).checked_add_signed(TimeDelta::try_seconds(86_400).unwrap()),
Some(from_ymd(2016, 7, 9).and_hms_opt(3, 5, 7).unwrap())
);
let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap();
assert_eq!(
hmsm(3, 5, 7, 980).checked_add_signed(TimeDelta::try_milliseconds(450).unwrap()),
Some(hmsm(3, 5, 8, 430))
);
Overflow returns None.
use chrono::{TimeDelta, NaiveDate};
let hms = |h, m, s| NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(h, m,
s).unwrap();
assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::try_days(1_000_000_000).unwrap()),
None);
Leap seconds are handled, but the addition assumes that it is the only leap second happened.
use chrono::{TimeDelta, NaiveDate};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli_opt(h, m, s,
milli).unwrap();
let leap = hmsm(3, 5, 59, 1_300);
assert_eq!(leap.checked_add_signed(TimeDelta::zero()),
Some(hmsm(3, 5, 59, 1_300)));
assert_eq!(leap.checked_add_signed(TimeDelta::try_milliseconds(-500).unwrap()),
Some(hmsm(3, 5, 59, 800)));
assert_eq!(leap.checked_add_signed(TimeDelta::try_milliseconds(500).unwrap()),
Some(hmsm(3, 5, 59, 1_800)));
assert_eq!(leap.checked_add_signed(TimeDelta::try_milliseconds(800).unwrap()),
Some(hmsm(3, 6, 0, 100)));
assert_eq!(leap.checked_add_signed(TimeDelta::try_seconds(10).unwrap()),
Some(hmsm(3, 6, 9, 300)));
assert_eq!(leap.checked_add_signed(TimeDelta::try_seconds(-10).unwrap()),
Some(hmsm(3, 5, 50, 300)));
assert_eq!(leap.checked_add_signed(TimeDelta::try_days(1).unwrap()),
Some(from_ymd(2016, 7, 9).and_hms_milli_opt(3, 5, 59, 300).unwrap()));
Fully qualified path: chrono::datetime::DateTimeTrait::checked_add_signed
fn checked_add_signed(self: @DateTime, rhs: TimeDelta) -> Option<DateTime>
checked_add_months
Adds given Months to the current date and time.
Uses the last day of the month if the day does not exist in the resulting month.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{Months, NaiveDate};
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1)
.unwrap()
.and_hms_opt(1, 0, 0)
.unwrap()
.checked_add_months(Months::new(1)),
Some(NaiveDate::from_ymd_opt(2014, 2, 1).unwrap().and_hms_opt(1, 0, 0).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1)
.unwrap()
.and_hms_opt(1, 0, 0)
.unwrap()
.checked_add_months(Months::new(core::i32::MAX as u32 + 1)),
None
);
Fully qualified path: chrono::datetime::DateTimeTrait::checked_add_months
fn checked_add_months(self: @DateTime, rhs: Months) -> Option<DateTime>
checked_sub_signed
Subtracts given TimeDelta from the current date and time.
As a part of Chrono’s leap second handling,
the subtraction assumes that there is no leap second ever,
except when the NaiveDateTime itself represents a leap second
in which case the assumption becomes that there is exactly a single leap second ever.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, TimeDelta};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let d = from_ymd(2016, 7, 8);
let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap();
assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::zero()), Some(hms(3, 5, 7)));
assert_eq!(
hms(3, 5, 7).checked_sub_signed(TimeDelta::try_seconds(1).unwrap()),
Some(hms(3, 5, 6))
);
assert_eq!(
hms(3, 5, 7).checked_sub_signed(TimeDelta::try_seconds(-1).unwrap()),
Some(hms(3, 5, 8))
);
assert_eq!(
hms(3, 5, 7).checked_sub_signed(TimeDelta::try_seconds(3600 + 60).unwrap()),
Some(hms(2, 4, 7))
);
assert_eq!(
hms(3, 5, 7).checked_sub_signed(TimeDelta::try_seconds(86_400).unwrap()),
Some(from_ymd(2016, 7, 7).and_hms_opt(3, 5, 7).unwrap())
);
let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap();
assert_eq!(
hmsm(3, 5, 7, 450).checked_sub_signed(TimeDelta::try_milliseconds(670).unwrap()),
Some(hmsm(3, 5, 6, 780))
);
Overflow returns None.
use chrono::{TimeDelta, NaiveDate};
let hms = |h, m, s| NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(h, m,
s).unwrap();
assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::try_days(1_000_000_000).unwrap()),
None);
Leap seconds are handled, but the subtraction assumes that it is the only leap second happened.
use chrono::{TimeDelta, NaiveDate};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli_opt(h, m, s,
milli).unwrap();
let leap = hmsm(3, 5, 59, 1_300);
assert_eq!(leap.checked_sub_signed(TimeDelta::zero()),
Some(hmsm(3, 5, 59, 1_300)));
assert_eq!(leap.checked_sub_signed(TimeDelta::try_milliseconds(200).unwrap()),
Some(hmsm(3, 5, 59, 1_100)));
assert_eq!(leap.checked_sub_signed(TimeDelta::try_milliseconds(500).unwrap()),
Some(hmsm(3, 5, 59, 800)));
assert_eq!(leap.checked_sub_signed(TimeDelta::try_seconds(60).unwrap()),
Some(hmsm(3, 5, 0, 300)));
assert_eq!(leap.checked_sub_signed(TimeDelta::try_days(1).unwrap()),
Some(from_ymd(2016, 7, 7).and_hms_milli_opt(3, 6, 0, 300).unwrap()));
Fully qualified path: chrono::datetime::DateTimeTrait::checked_sub_signed
fn checked_sub_signed(self: @DateTime, rhs: TimeDelta) -> Option<DateTime>
checked_sub_months
Subtracts given Months from the current date and time.
Uses the last day of the month if the day does not exist in the resulting month.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{Months, NaiveDate};
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1)
.unwrap()
.and_hms_opt(1, 0, 0)
.unwrap()
.checked_sub_months(Months::new(1)),
Some(NaiveDate::from_ymd_opt(2013, 12, 1).unwrap().and_hms_opt(1, 0, 0).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1)
.unwrap()
.and_hms_opt(1, 0, 0)
.unwrap()
.checked_sub_months(Months::new(core::i32::MAX as u32 + 1)),
None
);
Fully qualified path: chrono::datetime::DateTimeTrait::checked_sub_months
fn checked_sub_months(self: @DateTime, rhs: Months) -> Option<DateTime>
checked_add_days
Add a duration in Days to the date part of the NaiveDateTime
Returns None if the resulting date would be out of range.
Fully qualified path: chrono::datetime::DateTimeTrait::checked_add_days
fn checked_add_days(self: @DateTime, days: Days) -> Option<DateTime>
checked_sub_days
Subtract a duration in Days from the date part of the NaiveDateTime
Returns None if the resulting date would be out of range.
Fully qualified path: chrono::datetime::DateTimeTrait::checked_sub_days
fn checked_sub_days(self: @DateTime, days: Days) -> Option<DateTime>
signed_duration_since
Subtracts another NaiveDateTime from the current date and time.
This does not overflow or underflow at all.
As a part of Chrono’s leap second handling,
the subtraction assumes that there is no leap second ever,
except when any of the NaiveDateTimes themselves represents a leap second
in which case the assumption becomes that
there are exactly one (or two) leap second(s) ever.
Example
use chrono::{NaiveDate, TimeDelta};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let d = from_ymd(2016, 7, 8);
assert_eq!(
d.and_hms_opt(3, 5, 7).unwrap().signed_duration_since(d.and_hms_opt(2, 4, 6).unwrap()),
TimeDelta::try_seconds(3600 + 60 + 1).unwrap()
);
// July 8 is 190th day in the year 2016
let d0 = from_ymd(2016, 1, 1);
assert_eq!(
d.and_hms_milli_opt(0, 7, 6, 500)
.unwrap()
.signed_duration_since(d0.and_hms_opt(0, 0, 0).unwrap()),
TimeDelta::try_seconds(189 * 86_400 + 7 * 60 + 6).unwrap()
+ TimeDelta::try_milliseconds(500).unwrap()
);
Leap seconds are handled, but the subtraction assumes that there were no other leap seconds happened.
use chrono::{TimeDelta, NaiveDate};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let leap = from_ymd(2015, 6, 30).and_hms_milli_opt(23, 59, 59, 1_500).unwrap();
assert_eq!(
leap.signed_duration_since(from_ymd(2015, 6, 30).and_hms_opt(23, 0, 0).unwrap()),
TimeDelta::try_seconds(3600).unwrap() + TimeDelta::try_milliseconds(500).unwrap()
);
assert_eq!(
from_ymd(2015, 7, 1).and_hms_opt(1, 0, 0).unwrap().signed_duration_since(leap),
TimeDelta::try_seconds(3600).unwrap() - TimeDelta::try_milliseconds(500).unwrap()
);
Fully qualified path: chrono::datetime::DateTimeTrait::signed_duration_since
fn signed_duration_since(self: @DateTime, rhs: DateTime) -> TimeDelta
Impls
Impls
DateTimeImpl
Fully qualified path: chrono::datetime::DateTimeImpl
pub impl DateTimeImpl of DateTimeTrait;
Impl constants
MIN
The minimum possible NaiveDateTime.
Fully qualified path: chrono::datetime::DateTimeImpl::MIN
const MIN: DateTime = DateTime { date: DateTrait::MIN, time: TimeTrait::MIN };
MAX
The maximum possible NaiveDateTime.
Fully qualified path: chrono::datetime::DateTimeImpl::MAX
const MAX: DateTime = DateTime { date: DateTrait::MAX, time: TimeTrait::MAX };
UNIX_EPOCH
The datetime of the Unix Epoch, 1970-01-01 00:00:00.
Note that while this may look like the UNIX epoch, it is missing the
time zone. The actual UNIX epoch cannot be expressed by this type,
however it is available as DateTime::UNIX_EPOCH.
Fully qualified path: chrono::datetime::DateTimeImpl::UNIX_EPOCH
const UNIX_EPOCH: DateTime = DateTime { date: Date { yof: 16138266 }, time: Time { secs: 0 } };
Impl functions
new
Makes a new NaiveDateTime from date and time components.
Equivalent to date.and_time(time)
and many other helper constructors on NaiveDate.
Example
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
let t = NaiveTime::from_hms_milli_opt(12, 34, 56, 789).unwrap();
let dt = NaiveDateTime::new(d, t);
assert_eq!(dt.date(), d);
assert_eq!(dt.time(), t);
Fully qualified path: chrono::datetime::DateTimeImpl::new
fn new(date: Date, time: Time) -> DateTime
from_timestamp
Makes a new NaiveDateTime corresponding to a UTC date and time,
from the number of non-leap seconds
since the midnight UTC on January 1, 1970 (aka “UNIX timestamp”)
and the number of nanoseconds since the last whole non-leap second.
For a non-naive version of this function see TimeZone::timestamp.
The nanosecond part can exceed 1,000,000,000 in order to represent a
leap second, but only when secs % 60 == 59.
(The true “UNIX timestamp” cannot represent a leap second unambiguously.)
Panics
Panics if the number of seconds would be out of range for a NaiveDateTime (more than
ca. 262,000 years away from common era), and panics on an invalid nanosecond (2 seconds or
more).
Fully qualified path: chrono::datetime::DateTimeImpl::from_timestamp
fn from_timestamp(secs: i64) -> Option<DateTime>
from_block_timestamp
Fully qualified path: chrono::datetime::DateTimeImpl::from_block_timestamp
fn from_block_timestamp(block_timestamp: u64) -> Option<DateTime>
date
Retrieves a date component.
Example
use chrono::NaiveDate;
let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(9, 10, 11).unwrap();
assert_eq!(dt.date(), NaiveDate::from_ymd_opt(2016, 7, 8).unwrap());
Fully qualified path: chrono::datetime::DateTimeImpl::date
fn date(self: @DateTime) -> Date
time
Retrieves a time component.
Example
use chrono::{NaiveDate, NaiveTime};
let dt = NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(9, 10, 11).unwrap();
assert_eq!(dt.time(), NaiveTime::from_hms_opt(9, 10, 11).unwrap());
Fully qualified path: chrono::datetime::DateTimeImpl::time
fn time(self: @DateTime) -> Time
timestamp
Returns the number of non-leap seconds since January 1, 1970 0:00:00 UTC (aka “UNIX timestamp”).
The reverse operation of creating a DateTime from a timestamp can be performed
using from_timestamp or TimeZone::timestamp_opt.
use chrono::{DateTime, TimeZone, Utc};
let dt: DateTime<Utc> = Utc.with_ymd_and_hms(2015, 5, 15, 0, 0, 0).unwrap();
assert_eq!(dt.timestamp(), 1431648000);
assert_eq!(DateTime::from_timestamp(dt.timestamp(), dt.timestamp_subsec_nanos()).unwrap(),
dt);
Fully qualified path: chrono::datetime::DateTimeImpl::timestamp
fn timestamp(self: @DateTime) -> i64
checked_add_signed
Adds given TimeDelta to the current date and time.
As a part of Chrono’s leap second handling,
the addition assumes that there is no leap second ever,
except when the NaiveDateTime itself represents a leap second
in which case the assumption becomes that there is exactly a single leap second ever.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, TimeDelta};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let d = from_ymd(2016, 7, 8);
let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap();
assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::zero()), Some(hms(3, 5, 7)));
assert_eq!(
hms(3, 5, 7).checked_add_signed(TimeDelta::try_seconds(1).unwrap()),
Some(hms(3, 5, 8))
);
assert_eq!(
hms(3, 5, 7).checked_add_signed(TimeDelta::try_seconds(-1).unwrap()),
Some(hms(3, 5, 6))
);
assert_eq!(
hms(3, 5, 7).checked_add_signed(TimeDelta::try_seconds(3600 + 60).unwrap()),
Some(hms(4, 6, 7))
);
assert_eq!(
hms(3, 5, 7).checked_add_signed(TimeDelta::try_seconds(86_400).unwrap()),
Some(from_ymd(2016, 7, 9).and_hms_opt(3, 5, 7).unwrap())
);
let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap();
assert_eq!(
hmsm(3, 5, 7, 980).checked_add_signed(TimeDelta::try_milliseconds(450).unwrap()),
Some(hmsm(3, 5, 8, 430))
);
Overflow returns None.
use chrono::{TimeDelta, NaiveDate};
let hms = |h, m, s| NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(h, m,
s).unwrap();
assert_eq!(hms(3, 5, 7).checked_add_signed(TimeDelta::try_days(1_000_000_000).unwrap()),
None);
Leap seconds are handled, but the addition assumes that it is the only leap second happened.
use chrono::{TimeDelta, NaiveDate};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli_opt(h, m, s,
milli).unwrap();
let leap = hmsm(3, 5, 59, 1_300);
assert_eq!(leap.checked_add_signed(TimeDelta::zero()),
Some(hmsm(3, 5, 59, 1_300)));
assert_eq!(leap.checked_add_signed(TimeDelta::try_milliseconds(-500).unwrap()),
Some(hmsm(3, 5, 59, 800)));
assert_eq!(leap.checked_add_signed(TimeDelta::try_milliseconds(500).unwrap()),
Some(hmsm(3, 5, 59, 1_800)));
assert_eq!(leap.checked_add_signed(TimeDelta::try_milliseconds(800).unwrap()),
Some(hmsm(3, 6, 0, 100)));
assert_eq!(leap.checked_add_signed(TimeDelta::try_seconds(10).unwrap()),
Some(hmsm(3, 6, 9, 300)));
assert_eq!(leap.checked_add_signed(TimeDelta::try_seconds(-10).unwrap()),
Some(hmsm(3, 5, 50, 300)));
assert_eq!(leap.checked_add_signed(TimeDelta::try_days(1).unwrap()),
Some(from_ymd(2016, 7, 9).and_hms_milli_opt(3, 5, 59, 300).unwrap()));
Fully qualified path: chrono::datetime::DateTimeImpl::checked_add_signed
fn checked_add_signed(self: @DateTime, rhs: TimeDelta) -> Option<DateTime>
checked_add_months
Adds given Months to the current date and time.
Uses the last day of the month if the day does not exist in the resulting month.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{Months, NaiveDate};
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1)
.unwrap()
.and_hms_opt(1, 0, 0)
.unwrap()
.checked_add_months(Months::new(1)),
Some(NaiveDate::from_ymd_opt(2014, 2, 1).unwrap().and_hms_opt(1, 0, 0).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1)
.unwrap()
.and_hms_opt(1, 0, 0)
.unwrap()
.checked_add_months(Months::new(core::i32::MAX as u32 + 1)),
None
);
Fully qualified path: chrono::datetime::DateTimeImpl::checked_add_months
fn checked_add_months(self: @DateTime, rhs: Months) -> Option<DateTime>
checked_sub_signed
Subtracts given TimeDelta from the current date and time.
As a part of Chrono’s leap second handling,
the subtraction assumes that there is no leap second ever,
except when the NaiveDateTime itself represents a leap second
in which case the assumption becomes that there is exactly a single leap second ever.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{NaiveDate, TimeDelta};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let d = from_ymd(2016, 7, 8);
let hms = |h, m, s| d.and_hms_opt(h, m, s).unwrap();
assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::zero()), Some(hms(3, 5, 7)));
assert_eq!(
hms(3, 5, 7).checked_sub_signed(TimeDelta::try_seconds(1).unwrap()),
Some(hms(3, 5, 6))
);
assert_eq!(
hms(3, 5, 7).checked_sub_signed(TimeDelta::try_seconds(-1).unwrap()),
Some(hms(3, 5, 8))
);
assert_eq!(
hms(3, 5, 7).checked_sub_signed(TimeDelta::try_seconds(3600 + 60).unwrap()),
Some(hms(2, 4, 7))
);
assert_eq!(
hms(3, 5, 7).checked_sub_signed(TimeDelta::try_seconds(86_400).unwrap()),
Some(from_ymd(2016, 7, 7).and_hms_opt(3, 5, 7).unwrap())
);
let hmsm = |h, m, s, milli| d.and_hms_milli_opt(h, m, s, milli).unwrap();
assert_eq!(
hmsm(3, 5, 7, 450).checked_sub_signed(TimeDelta::try_milliseconds(670).unwrap()),
Some(hmsm(3, 5, 6, 780))
);
Overflow returns None.
use chrono::{TimeDelta, NaiveDate};
let hms = |h, m, s| NaiveDate::from_ymd_opt(2016, 7, 8).unwrap().and_hms_opt(h, m,
s).unwrap();
assert_eq!(hms(3, 5, 7).checked_sub_signed(TimeDelta::try_days(1_000_000_000).unwrap()),
None);
Leap seconds are handled, but the subtraction assumes that it is the only leap second happened.
use chrono::{TimeDelta, NaiveDate};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let hmsm = |h, m, s, milli| from_ymd(2016, 7, 8).and_hms_milli_opt(h, m, s,
milli).unwrap();
let leap = hmsm(3, 5, 59, 1_300);
assert_eq!(leap.checked_sub_signed(TimeDelta::zero()),
Some(hmsm(3, 5, 59, 1_300)));
assert_eq!(leap.checked_sub_signed(TimeDelta::try_milliseconds(200).unwrap()),
Some(hmsm(3, 5, 59, 1_100)));
assert_eq!(leap.checked_sub_signed(TimeDelta::try_milliseconds(500).unwrap()),
Some(hmsm(3, 5, 59, 800)));
assert_eq!(leap.checked_sub_signed(TimeDelta::try_seconds(60).unwrap()),
Some(hmsm(3, 5, 0, 300)));
assert_eq!(leap.checked_sub_signed(TimeDelta::try_days(1).unwrap()),
Some(from_ymd(2016, 7, 7).and_hms_milli_opt(3, 6, 0, 300).unwrap()));
Fully qualified path: chrono::datetime::DateTimeImpl::checked_sub_signed
fn checked_sub_signed(self: @DateTime, rhs: TimeDelta) -> Option<DateTime>
checked_sub_months
Subtracts given Months from the current date and time.
Uses the last day of the month if the day does not exist in the resulting month.
Errors
Returns None if the resulting date would be out of range.
Example
use chrono::{Months, NaiveDate};
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1)
.unwrap()
.and_hms_opt(1, 0, 0)
.unwrap()
.checked_sub_months(Months::new(1)),
Some(NaiveDate::from_ymd_opt(2013, 12, 1).unwrap().and_hms_opt(1, 0, 0).unwrap())
);
assert_eq!(
NaiveDate::from_ymd_opt(2014, 1, 1)
.unwrap()
.and_hms_opt(1, 0, 0)
.unwrap()
.checked_sub_months(Months::new(core::i32::MAX as u32 + 1)),
None
);
Fully qualified path: chrono::datetime::DateTimeImpl::checked_sub_months
fn checked_sub_months(self: @DateTime, rhs: Months) -> Option<DateTime>
checked_add_days
Add a duration in Days to the date part of the NaiveDateTime
Returns None if the resulting date would be out of range.
Fully qualified path: chrono::datetime::DateTimeImpl::checked_add_days
fn checked_add_days(self: @DateTime, days: Days) -> Option<DateTime>
checked_sub_days
Subtract a duration in Days from the date part of the NaiveDateTime
Returns None if the resulting date would be out of range.
Fully qualified path: chrono::datetime::DateTimeImpl::checked_sub_days
fn checked_sub_days(self: @DateTime, days: Days) -> Option<DateTime>
signed_duration_since
Subtracts another NaiveDateTime from the current date and time.
This does not overflow or underflow at all.
As a part of Chrono’s leap second handling,
the subtraction assumes that there is no leap second ever,
except when any of the NaiveDateTimes themselves represents a leap second
in which case the assumption becomes that
there are exactly one (or two) leap second(s) ever.
Example
use chrono::{NaiveDate, TimeDelta};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let d = from_ymd(2016, 7, 8);
assert_eq!(
d.and_hms_opt(3, 5, 7).unwrap().signed_duration_since(d.and_hms_opt(2, 4, 6).unwrap()),
TimeDelta::try_seconds(3600 + 60 + 1).unwrap()
);
// July 8 is 190th day in the year 2016
let d0 = from_ymd(2016, 1, 1);
assert_eq!(
d.and_hms_milli_opt(0, 7, 6, 500)
.unwrap()
.signed_duration_since(d0.and_hms_opt(0, 0, 0).unwrap()),
TimeDelta::try_seconds(189 * 86_400 + 7 * 60 + 6).unwrap()
+ TimeDelta::try_milliseconds(500).unwrap()
);
Leap seconds are handled, but the subtraction assumes that there were no other leap seconds happened.
use chrono::{TimeDelta, NaiveDate};
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let leap = from_ymd(2015, 6, 30).and_hms_milli_opt(23, 59, 59, 1_500).unwrap();
assert_eq!(
leap.signed_duration_since(from_ymd(2015, 6, 30).and_hms_opt(23, 0, 0).unwrap()),
TimeDelta::try_seconds(3600).unwrap() + TimeDelta::try_milliseconds(500).unwrap()
);
assert_eq!(
from_ymd(2015, 7, 1).and_hms_opt(1, 0, 0).unwrap().signed_duration_since(leap),
TimeDelta::try_seconds(3600).unwrap() - TimeDelta::try_milliseconds(500).unwrap()
);
Fully qualified path: chrono::datetime::DateTimeImpl::signed_duration_since
fn signed_duration_since(self: @DateTime, rhs: DateTime) -> TimeDelta
days
Fully qualified path: chrono::days
Structs
| Days | A duration in calendar days. This is useful because when using TimeDelta it is possible that adding TimeDelta::days(1)… |
Traits
Impls
Structs
Structs
| Days | A duration in calendar days. This is useful because when using TimeDelta it is possible that adding TimeDelta::days(1)… |
Days
A duration in calendar days.
This is useful because when using TimeDelta it is possible that adding TimeDelta::days(1)
doesn’t increment the day value as expected due to it being a fixed number of seconds. This
difference applies only when dealing with DateTime<TimeZone> data types and in other cases
TimeDelta::days(n) and Days::new(n) are equivalent.
Fully qualified path: chrono::days::Days
[derive(Clone, Copy, PartialEq, Drop, Debug)]
pub struct Days { /* private fields */ }
Traits
Traits
DaysTrait
Fully qualified path: chrono::days::DaysTrait
pub trait DaysTrait
Trait functions
new
Construct a new Days from a number of days
Fully qualified path: chrono::days::DaysTrait::new
fn new(num: u64) -> Days
Impls
Impls
DaysImpl
Fully qualified path: chrono::days::DaysImpl
pub impl DaysImpl of DaysTrait;
Impl functions
new
Construct a new Days from a number of days
Fully qualified path: chrono::days::DaysImpl::new
fn new(num: u64) -> Days
internals
Internal helper types for working with dates.
Fully qualified path: chrono::internals
Constants
Structs
| YearFlags | Year flags (aka the dominical letter). YearFlags are used as the last four bits of NaiveDate , Mdf and IsoWeek . There are 14 possible classes of year in the Gregorian calendar:… |
| Mdf | Month, day of month and year flags: `(month << 9) |
Traits
Impls
Constants
Constants
A
Fully qualified path: chrono::internals::A
pub const A: YearFlags = YearFlags { flags: COMMON_YEAR | YEAR_STARTS_AFTER_SATURDAY };
AG
Fully qualified path: chrono::internals::AG
pub const AG: YearFlags = YearFlags { flags: LEAP_YEAR | YEAR_STARTS_AFTER_SATURDAY };
B
Fully qualified path: chrono::internals::B
pub const B: YearFlags = YearFlags { flags: COMMON_YEAR | YEAR_STARTS_AFTER_FRIDAY };
BA
Fully qualified path: chrono::internals::BA
pub const BA: YearFlags = YearFlags { flags: LEAP_YEAR | YEAR_STARTS_AFTER_FRIDAY };
C
Fully qualified path: chrono::internals::C
pub const C: YearFlags = YearFlags { flags: COMMON_YEAR | YEAR_STARTS_AFTER_THURSDAY };
CB
Fully qualified path: chrono::internals::CB
pub const CB: YearFlags = YearFlags { flags: LEAP_YEAR | YEAR_STARTS_AFTER_THURSDAY };
D
Fully qualified path: chrono::internals::D
pub const D: YearFlags = YearFlags { flags: COMMON_YEAR | YEAR_STARTS_AFTER_WEDNESDAY };
DC
Fully qualified path: chrono::internals::DC
pub const DC: YearFlags = YearFlags { flags: LEAP_YEAR | YEAR_STARTS_AFTER_WEDNESDAY };
E
Fully qualified path: chrono::internals::E
pub const E: YearFlags = YearFlags { flags: COMMON_YEAR | YEAR_STARTS_AFTER_THUESDAY };
ED
Fully qualified path: chrono::internals::ED
pub const ED: YearFlags = YearFlags { flags: LEAP_YEAR | YEAR_STARTS_AFTER_THUESDAY };
F
Fully qualified path: chrono::internals::F
pub const F: YearFlags = YearFlags { flags: COMMON_YEAR | YEAR_STARTS_AFTER_MONDAY };
FE
Fully qualified path: chrono::internals::FE
pub const FE: YearFlags = YearFlags { flags: LEAP_YEAR | YEAR_STARTS_AFTER_MONDAY };
G
Fully qualified path: chrono::internals::G
pub const G: YearFlags = YearFlags { flags: COMMON_YEAR | YEAR_STARTS_AFTER_SUNDAY };
GF
Fully qualified path: chrono::internals::GF
pub const GF: YearFlags = YearFlags { flags: LEAP_YEAR | YEAR_STARTS_AFTER_SUNDAY };
Structs
Structs
| YearFlags | Year flags (aka the dominical letter). YearFlags are used as the last four bits of NaiveDate , Mdf and IsoWeek . There are 14 possible classes of year in the Gregorian calendar:… |
| Mdf | Month, day of month and year flags: `(month << 9) |
YearFlags
Year flags (aka the dominical letter).
YearFlags are used as the last four bits of NaiveDate, Mdf and IsoWeek.
There are 14 possible classes of year in the Gregorian calendar: common and leap years starting with Monday through Sunday.
The YearFlags stores this information into 4 bits LWWW. L is the leap year flag, with 1
for the common year (this simplifies validating an ordinal in NaiveDate). WWW is a non-zero
Weekday of the last day in the preceding year.
Fully qualified path: chrono::internals::YearFlags
[derive(Clone, Copy, PartialEq, Drop, Debug)]
pub struct YearFlags { /* private fields */ }
Mdf
Month, day of month and year flags: (month << 9) | (day << 4) | flags
M_MMMD_DDDD_LFFF
The whole bits except for the least 3 bits are referred as Mdl (month, day of month, and leap
year flag), which is an index to the MDL_TO_OL lookup table.
The conversion between the packed calendar date (Mdf) and the ordinal date (NaiveDate) is
based on the moderately-sized lookup table (~1.5KB) and the packed representation is chosen for
efficient lookup.
The methods of Mdf validate their inputs as late as possible. Dates that can’t exist, like
February 30, can still be represented. This allows the validation to be combined with the final
table lookup, which is good for performance.
Fully qualified path: chrono::internals::Mdf
[derive(Clone, Copy, PartialEq, Drop, Debug)]
pub struct Mdf { /* private fields */ }
Traits
Traits
YearFlagsTrait
Fully qualified path: chrono::internals::YearFlagsTrait
pub trait YearFlagsTrait
Trait functions
from_year
Fully qualified path: chrono::internals::YearFlagsTrait::from_year
fn from_year(year: i32) -> YearFlags
from_year_mod_400
Fully qualified path: chrono::internals::YearFlagsTrait::from_year_mod_400
fn from_year_mod_400(year: u32) -> YearFlags
ndays
Fully qualified path: chrono::internals::YearFlagsTrait::ndays
fn ndays(self: @YearFlags) -> u32
isoweek_delta
Fully qualified path: chrono::internals::YearFlagsTrait::isoweek_delta
fn isoweek_delta(self: @YearFlags) -> u32
nisoweeks
Fully qualified path: chrono::internals::YearFlagsTrait::nisoweeks
fn nisoweeks(self: @YearFlags) -> u32
MdfTrait
Fully qualified path: chrono::internals::MdfTrait
pub trait MdfTrait
Trait functions
new
Makes a new Mdf value from month, day and YearFlags.
This method doesn’t fully validate the range of the month and day parameters, only as
much as what can’t be deferred until later. The year flags are trusted to be correct.
Errors
Returns None if month > 12 or day > 31.
Fully qualified path: chrono::internals::MdfTrait::new
fn new(month: u32, day: u32, flags: YearFlags) -> Option<Mdf>
from_ol
Makes a new Mdf value from an i32 with an ordinal and a leap year flag, and year
flags.
The ol is trusted to be valid, and the flags are trusted to match it.
Fully qualified path: chrono::internals::MdfTrait::from_ol
fn from_ol(ol: i32, flags: YearFlags) -> Mdf
month
Returns the month of this Mdf.
Fully qualified path: chrono::internals::MdfTrait::month
fn month(self: @Mdf) -> u32
with_month
Replaces the month of this Mdf, keeping the day and flags.
Errors
Returns None if month > 12.
Fully qualified path: chrono::internals::MdfTrait::with_month
fn with_month(self: @Mdf, month: u32) -> Option<Mdf>
day
Returns the day of this Mdf.
Fully qualified path: chrono::internals::MdfTrait::day
fn day(self: @Mdf) -> u32
with_day
Replaces the day of this Mdf, keeping the month and flags.
Errors
Returns None if day > 31.
Fully qualified path: chrono::internals::MdfTrait::with_day
fn with_day(self: @Mdf, day: u32) -> Option<Mdf>
with_flags
Replaces the flags of this Mdf, keeping the month and day.
Fully qualified path: chrono::internals::MdfTrait::with_flags
fn with_flags(self: @Mdf, flags: YearFlags) -> Mdf
ordinal
Returns the ordinal that corresponds to this Mdf.
This does a table lookup to calculate the corresponding ordinal. It will return an error if
the Mdl turns out not to be a valid date.
Errors
Returns None if month == 0 or day == 0, or if a the given day does not exist in the
given month.
Fully qualified path: chrono::internals::MdfTrait::ordinal
fn ordinal(self: @Mdf) -> Option<u32>
ordinal_and_flags
Returns the ordinal that corresponds to this Mdf, encoded as a value including year flags.
This does a table lookup to calculate the corresponding ordinal. It will return an error if
the Mdl turns out not to be a valid date.
Errors
Returns None if month == 0 or day == 0, or if a the given day does not exist in the
given month.
Fully qualified path: chrono::internals::MdfTrait::ordinal_and_flags
fn ordinal_and_flags(self: @Mdf) -> Option<u32>
valid
Fully qualified path: chrono::internals::MdfTrait::valid
fn valid(self: @Mdf) -> bool
Impls
Impls
YearFlagsImpl
Fully qualified path: chrono::internals::YearFlagsImpl
pub impl YearFlagsImpl of YearFlagsTrait;
Impl functions
from_year
Fully qualified path: chrono::internals::YearFlagsImpl::from_year
fn from_year(year: i32) -> YearFlags
from_year_mod_400
Fully qualified path: chrono::internals::YearFlagsImpl::from_year_mod_400
fn from_year_mod_400(year: u32) -> YearFlags
ndays
Fully qualified path: chrono::internals::YearFlagsImpl::ndays
fn ndays(self: @YearFlags) -> u32
isoweek_delta
Fully qualified path: chrono::internals::YearFlagsImpl::isoweek_delta
fn isoweek_delta(self: @YearFlags) -> u32
nisoweeks
Fully qualified path: chrono::internals::YearFlagsImpl::nisoweeks
fn nisoweeks(self: @YearFlags) -> u32
MdfImpl
Fully qualified path: chrono::internals::MdfImpl
pub impl MdfImpl of MdfTrait;
Impl functions
new
Makes a new Mdf value from month, day and YearFlags.
This method doesn’t fully validate the range of the month and day parameters, only as
much as what can’t be deferred until later. The year flags are trusted to be correct.
Errors
Returns None if month > 12 or day > 31.
Fully qualified path: chrono::internals::MdfImpl::new
fn new(month: u32, day: u32, flags: YearFlags) -> Option<Mdf>
from_ol
Makes a new Mdf value from an i32 with an ordinal and a leap year flag, and year
flags.
The ol is trusted to be valid, and the flags are trusted to match it.
Fully qualified path: chrono::internals::MdfImpl::from_ol
fn from_ol(ol: i32, flags: YearFlags) -> Mdf
month
Returns the month of this Mdf.
Fully qualified path: chrono::internals::MdfImpl::month
fn month(self: @Mdf) -> u32
with_month
Replaces the month of this Mdf, keeping the day and flags.
Errors
Returns None if month > 12.
Fully qualified path: chrono::internals::MdfImpl::with_month
fn with_month(self: @Mdf, month: u32) -> Option<Mdf>
day
Returns the day of this Mdf.
Fully qualified path: chrono::internals::MdfImpl::day
fn day(self: @Mdf) -> u32
with_day
Replaces the day of this Mdf, keeping the month and flags.
Errors
Returns None if day > 31.
Fully qualified path: chrono::internals::MdfImpl::with_day
fn with_day(self: @Mdf, day: u32) -> Option<Mdf>
with_flags
Replaces the flags of this Mdf, keeping the month and day.
Fully qualified path: chrono::internals::MdfImpl::with_flags
fn with_flags(self: @Mdf, flags: YearFlags) -> Mdf
ordinal
Returns the ordinal that corresponds to this Mdf.
This does a table lookup to calculate the corresponding ordinal. It will return an error if
the Mdl turns out not to be a valid date.
Errors
Returns None if month == 0 or day == 0, or if a the given day does not exist in the
given month.
Fully qualified path: chrono::internals::MdfImpl::ordinal
fn ordinal(self: @Mdf) -> Option<u32>
ordinal_and_flags
Returns the ordinal that corresponds to this Mdf, encoded as a value including year flags.
This does a table lookup to calculate the corresponding ordinal. It will return an error if
the Mdl turns out not to be a valid date.
Errors
Returns None if month == 0 or day == 0, or if a the given day does not exist in the
given month.
Fully qualified path: chrono::internals::MdfImpl::ordinal_and_flags
fn ordinal_and_flags(self: @Mdf) -> Option<u32>
valid
Fully qualified path: chrono::internals::MdfImpl::valid
fn valid(self: @Mdf) -> bool
isoweek
ISO 8601 week.
Fully qualified path: chrono::isoweek
Structs
| IsoWeek | ISO 8601 week. This type, combined with Weekday , constitutes the ISO 8601 week date . One can retrieve this type from the existing Datelike types via the Datelike::iso_week method. |
Traits
Impls
Structs
Structs
| IsoWeek | ISO 8601 week. This type, combined with Weekday , constitutes the ISO 8601 week date . One can retrieve this type from the existing Datelike types via the Datelike::iso_week method. |
IsoWeek
ISO 8601 week.
This type, combined with Weekday,
constitutes the ISO 8601 week date.
One can retrieve this type from the existing Datelike types
via the Datelike::iso_week method.
Fully qualified path: chrono::isoweek::IsoWeek
[derive(Clone, Copy, PartialEq, Drop)]
pub struct IsoWeek { /* private fields */ }
Traits
Traits
IsoWeekTrait
Fully qualified path: chrono::isoweek::IsoWeekTrait
pub trait IsoWeekTrait
Trait functions
from_yof
Returns the corresponding IsoWeek from the year and the Of internal value.
Fully qualified path: chrono::isoweek::IsoWeekTrait::from_yof
fn from_yof(year: i32, ordinal: u32, year_flags: YearFlags) -> IsoWeek
year
Returns the year number for this ISO week.
Example
use chrono::{Datelike, NaiveDate, Weekday};
let d = NaiveDate::from_isoywd_opt(2015, 1, Weekday::Mon).unwrap();
assert_eq!(d.iso_week().year(), 2015);
This year number might not match the calendar year number. Continuing the example…
use chrono::{NaiveDate, Datelike, Weekday};
let d = NaiveDate::from_isoywd_opt(2015, 1, Weekday::Mon).unwrap();
assert_eq!(d.year(), 2014);
assert_eq!(d, NaiveDate::from_ymd_opt(2014, 12, 29).unwrap());
Fully qualified path: chrono::isoweek::IsoWeekTrait::year
fn year(self: @IsoWeek) -> u32
week
Returns the ISO week number starting from 1.
The return value ranges from 1 to 53. (The last week of year differs by years.)
Example
use chrono::{Datelike, NaiveDate, Weekday};
let d = NaiveDate::from_isoywd_opt(2015, 15, Weekday::Mon).unwrap();
assert_eq!(d.iso_week().week(), 15);
Fully qualified path: chrono::isoweek::IsoWeekTrait::week
fn week(self: @IsoWeek) -> u32
week0
Returns the ISO week number starting from 0.
The return value ranges from 0 to 52. (The last week of year differs by years.)
Example
use chrono::{Datelike, NaiveDate, Weekday};
let d = NaiveDate::from_isoywd_opt(2015, 15, Weekday::Mon).unwrap();
assert_eq!(d.iso_week().week0(), 14);
Fully qualified path: chrono::isoweek::IsoWeekTrait::week0
fn week0(self: @IsoWeek) -> u32
Impls
Impls
IsoWeekImpl
Fully qualified path: chrono::isoweek::IsoWeekImpl
pub impl IsoWeekImpl of IsoWeekTrait;
Impl functions
from_yof
Returns the corresponding IsoWeek from the year and the Of internal value.
Fully qualified path: chrono::isoweek::IsoWeekImpl::from_yof
fn from_yof(year: i32, ordinal: u32, year_flags: YearFlags) -> IsoWeek
year
Returns the year number for this ISO week.
Example
use chrono::{Datelike, NaiveDate, Weekday};
let d = NaiveDate::from_isoywd_opt(2015, 1, Weekday::Mon).unwrap();
assert_eq!(d.iso_week().year(), 2015);
This year number might not match the calendar year number. Continuing the example…
use chrono::{NaiveDate, Datelike, Weekday};
let d = NaiveDate::from_isoywd_opt(2015, 1, Weekday::Mon).unwrap();
assert_eq!(d.year(), 2014);
assert_eq!(d, NaiveDate::from_ymd_opt(2014, 12, 29).unwrap());
Fully qualified path: chrono::isoweek::IsoWeekImpl::year
fn year(self: @IsoWeek) -> u32
week
Returns the ISO week number starting from 1.
The return value ranges from 1 to 53. (The last week of year differs by years.)
Example
use chrono::{Datelike, NaiveDate, Weekday};
let d = NaiveDate::from_isoywd_opt(2015, 15, Weekday::Mon).unwrap();
assert_eq!(d.iso_week().week(), 15);
Fully qualified path: chrono::isoweek::IsoWeekImpl::week
fn week(self: @IsoWeek) -> u32
week0
Returns the ISO week number starting from 0.
The return value ranges from 0 to 52. (The last week of year differs by years.)
Example
use chrono::{Datelike, NaiveDate, Weekday};
let d = NaiveDate::from_isoywd_opt(2015, 15, Weekday::Mon).unwrap();
assert_eq!(d.iso_week().week0(), 14);
Fully qualified path: chrono::isoweek::IsoWeekImpl::week0
fn week0(self: @IsoWeek) -> u32
months
Fully qualified path: chrono::months
Structs
| Months | A duration in calendar months |
Enums
| Month | The month of the year. This enum is just a convenience implementation. The month in dates created by DateLike objects does not return this enum…. |
Traits
Impls
Structs
Structs
| Months | A duration in calendar months |
Months
A duration in calendar months
Fully qualified path: chrono::months::Months
[derive(Clone, Copy, PartialEq, Drop, Debug)]
pub struct Months { /* private fields */ }
Enums
Enums
| Month | The month of the year. This enum is just a convenience implementation. The month in dates created by DateLike objects does not return this enum…. |
Month
The month of the year.
This enum is just a convenience implementation. The month in dates created by DateLike objects does not return this enum.
It is possible to convert from a date to a month independently
use chrono::prelude::*;
let date = Utc.with_ymd_and_hms(2019, 10, 28, 9, 10, 11).unwrap();
// `2019-10-28T09:10:11Z`
let month = Month::try_from(u8::try_from(date.month()).unwrap()).ok();
assert_eq!(month, Some(Month::October))
Or from a Month to an integer usable by dates
use chrono::prelude::*;
let month = Month::January;
let dt = Utc.with_ymd_and_hms(2019, month.number_from_month(), 28, 9, 10, 11).unwrap();
assert_eq!((dt.year(), dt.month(), dt.day()), (2019, 1, 28));
Allows mapping from and to month, from 1-January to 12-December. Can be Serialized/Deserialized with serde
Fully qualified path: chrono::months::Month
pub enum Month {
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December,
}
Variants
January
January
Fully qualified path: chrono::months::Month::January
January
February
February
Fully qualified path: chrono::months::Month::February
February
March
March
Fully qualified path: chrono::months::Month::March
March
April
April
Fully qualified path: chrono::months::Month::April
April
May
May
Fully qualified path: chrono::months::Month::May
May
June
June
Fully qualified path: chrono::months::Month::June
June
July
July
Fully qualified path: chrono::months::Month::July
July
August
August
Fully qualified path: chrono::months::Month::August
August
September
September
Fully qualified path: chrono::months::Month::September
September
October
October
Fully qualified path: chrono::months::Month::October
October
November
November
Fully qualified path: chrono::months::Month::November
November
December
December
Fully qualified path: chrono::months::Month::December
December
Traits
Traits
MonthTrait
Fully qualified path: chrono::months::MonthTrait
pub trait MonthTrait
Trait functions
succ
The next month.
m: | January | February | ... | December |
|---|---|---|---|---|
m.succ(): | February | March | ... | January |
Fully qualified path: chrono::months::MonthTrait::succ
fn succ(self: @Month) -> Month
pred
The previous month.
m: | January | February | ... | December |
|---|---|---|---|---|
m.pred(): | December | January | ... | November |
Fully qualified path: chrono::months::MonthTrait::pred
fn pred(self: @Month) -> Month
number_from_month
Returns a month-of-year number starting from January = 1.
m: | January | February | ... | December |
|---|---|---|---|---|
m.number_from_month(): | 1 | 2 | ... | 12 |
Fully qualified path: chrono::months::MonthTrait::number_from_month
fn number_from_month(self: @Month) -> u32
name
Get the name of the month
use chrono::Month;
assert_eq!(Month::January.name(), "January")
Fully qualified path: chrono::months::MonthTrait::name
fn name(self: @Month) -> felt252
num_days
Get the length in days of the month
Yields None if year is out of range for NaiveDate.
Fully qualified path: chrono::months::MonthTrait::num_days
fn num_days(self: @Month, year: u32) -> Option<u8>
from_u64
Fully qualified path: chrono::months::MonthTrait::from_u64
fn from_u64(n: u64) -> Option<Month>
from_i64
Fully qualified path: chrono::months::MonthTrait::from_i64
fn from_i64(n: i64) -> Option<Month>
from_u32
Fully qualified path: chrono::months::MonthTrait::from_u32
fn from_u32(n: u32) -> Option<Month>
MonthsTrait
Fully qualified path: chrono::months::MonthsTrait
pub trait MonthsTrait
Trait functions
new
Construct a new Months from a number of months
Fully qualified path: chrono::months::MonthsTrait::new
fn new(months: u32) -> Months
as_u32
Returns the total number of months in the Months instance.
Fully qualified path: chrono::months::MonthsTrait::as_u32
fn as_u32(self: @Months) -> u32
Impls
Impls
MonthImpl
Fully qualified path: chrono::months::MonthImpl
pub impl MonthImpl of MonthTrait;
Impl functions
succ
The next month.
m: | January | February | ... | December |
|---|---|---|---|---|
m.succ(): | February | March | ... | January |
Fully qualified path: chrono::months::MonthImpl::succ
fn succ(self: @Month) -> Month
pred
The previous month.
m: | January | February | ... | December |
|---|---|---|---|---|
m.pred(): | December | January | ... | November |
Fully qualified path: chrono::months::MonthImpl::pred
fn pred(self: @Month) -> Month
number_from_month
Returns a month-of-year number starting from January = 1.
m: | January | February | ... | December |
|---|---|---|---|---|
m.number_from_month(): | 1 | 2 | ... | 12 |
Fully qualified path: chrono::months::MonthImpl::number_from_month
fn number_from_month(self: @Month) -> u32
name
Get the name of the month
use chrono::Month;
assert_eq!(Month::January.name(), "January")
Fully qualified path: chrono::months::MonthImpl::name
fn name(self: @Month) -> felt252
num_days
Get the length in days of the month
Yields None if year is out of range for NaiveDate.
Fully qualified path: chrono::months::MonthImpl::num_days
fn num_days(self: @Month, year: u32) -> Option<u8>
from_u64
Fully qualified path: chrono::months::MonthImpl::from_u64
fn from_u64(n: u64) -> Option<Month>
from_i64
Fully qualified path: chrono::months::MonthImpl::from_i64
fn from_i64(n: i64) -> Option<Month>
from_u32
Fully qualified path: chrono::months::MonthImpl::from_u32
fn from_u32(n: u32) -> Option<Month>
MonthsImpl
Fully qualified path: chrono::months::MonthsImpl
pub impl MonthsImpl of MonthsTrait;
Impl functions
new
Construct a new Months from a number of months
Fully qualified path: chrono::months::MonthsImpl::new
fn new(months: u32) -> Months
as_u32
Returns the total number of months in the Months instance.
Fully qualified path: chrono::months::MonthsImpl::as_u32
fn as_u32(self: @Months) -> u32
time
ISO 8601 time without timezone.
Fully qualified path: chrono::time
Structs
| Time | — |
Traits
Impls
Structs
Structs
| Time | — |
Time
Fully qualified path: chrono::time::Time
[derive(Clone, Copy, PartialEq, Drop, Serde, starknet::Store)]
pub struct Time { /* private fields */ }
Traits
Traits
TimeTrait
Fully qualified path: chrono::time::TimeTrait
pub trait TimeTrait
Trait constants
MIN
The earliest possible NaiveTime
Fully qualified path: chrono::time::TimeTrait::MIN
const MIN: Time;
MAX
Fully qualified path: chrono::time::TimeTrait::MAX
const MAX: Time;
Trait functions
from_hms_opt
Makes a new NaiveTime from hour, minute and second.
The millisecond part is allowed to exceed 1,000,000,000 in order to represent a
leap second, but only when sec == 59.
Errors
Returns None on invalid hour, minute and/or second.
Example
use chrono::NaiveTime;
let from_hms_opt = NaiveTime::from_hms_opt;
assert!(from_hms_opt(0, 0, 0).is_some());
assert!(from_hms_opt(23, 59, 59).is_some());
assert!(from_hms_opt(24, 0, 0).is_none());
assert!(from_hms_opt(23, 60, 0).is_none());
assert!(from_hms_opt(23, 59, 60).is_none());
Fully qualified path: chrono::time::TimeTrait::from_hms_opt
fn from_hms_opt(hour: u32, min: u32, sec: u32) -> Option<Time>
from_num_seconds_from_midnight_opt
Makes a new NaiveTime from the number of seconds since midnight and nanosecond.
The nanosecond part is allowed to exceed 1,000,000,000 in order to represent a
leap second, but only when secs % 60 == 59.
Errors
Returns None on invalid number of seconds and/or nanosecond.
Example
use chrono::NaiveTime;
let from_nsecs_opt = NaiveTime::from_num_seconds_from_midnight_opt;
assert!(from_nsecs_opt(0, 0).is_some());
assert!(from_nsecs_opt(86399, 999_999_999).is_some());
assert!(from_nsecs_opt(86399, 1_999_999_999).is_some()); // a leap second after 23:59:59
assert!(from_nsecs_opt(86_400, 0).is_none());
assert!(from_nsecs_opt(86399, 2_000_000_000).is_none());
Fully qualified path: chrono::time::TimeTrait::from_num_seconds_from_midnight_opt
fn from_num_seconds_from_midnight_opt(secs: u32) -> Option<Time>
overflowing_add_signed
Adds given TimeDelta to the current time, and also returns the number of seconds
in the integral number of days ignored from the addition.
Example
use chrono::{NaiveTime, TimeDelta};
let from_hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap();
assert_eq!(
from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::try_hours(11).unwrap()),
(from_hms(14, 4, 5), 0)
);
assert_eq!(
from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::try_hours(23).unwrap()),
(from_hms(2, 4, 5), 86_400)
);
assert_eq!(
from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::try_hours(-7).unwrap()),
(from_hms(20, 4, 5), -86_400)
);
Fully qualified path: chrono::time::TimeTrait::overflowing_add_signed
fn overflowing_add_signed(self: @Time, rhs: TimeDelta) -> (Time, i64)
overflowing_sub_signed
Subtracts given TimeDelta from the current time, and also returns the number of seconds
in the integral number of days ignored from the subtraction.
Example
use chrono::{NaiveTime, TimeDelta};
let from_hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap();
assert_eq!(
from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::try_hours(2).unwrap()),
(from_hms(1, 4, 5), 0)
);
assert_eq!(
from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::try_hours(17).unwrap()),
(from_hms(10, 4, 5), 86_400)
);
assert_eq!(
from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::try_hours(-22).unwrap()),
(from_hms(1, 4, 5), -86_400)
);
Fully qualified path: chrono::time::TimeTrait::overflowing_sub_signed
fn overflowing_sub_signed(self: @Time, rhs: TimeDelta) -> (Time, i64)
signed_duration_since
Subtracts another NaiveTime from the current time.
Returns a TimeDelta within +/- 1 day.
This does not overflow or underflow at all.
As a part of Chrono’s leap second handling,
the subtraction assumes that there is no leap second ever,
except when any of the NaiveTimes themselves represents a leap second
in which case the assumption becomes that
there are exactly one (or two) leap second(s) ever.
Example
use chrono::{NaiveTime, TimeDelta};
let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap();
let since = NaiveTime::signed_duration_since;
assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 900)), TimeDelta::zero());
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 875)),
TimeDelta::try_milliseconds(25).unwrap()
);
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 6, 925)),
TimeDelta::try_milliseconds(975).unwrap()
);
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 0, 900)),
TimeDelta::try_seconds(7).unwrap()
);
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 0, 7, 900)),
TimeDelta::try_seconds(5 * 60).unwrap()
);
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(0, 5, 7, 900)),
TimeDelta::try_seconds(3 * 3600).unwrap()
);
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(4, 5, 7, 900)),
TimeDelta::try_seconds(-3600).unwrap()
);
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(2, 4, 6, 800)),
TimeDelta::try_seconds(3600 + 60 + 1).unwrap() +
TimeDelta::try_milliseconds(100).unwrap()
);
Leap seconds are handled, but the subtraction assumes that there were no other leap seconds happened.
use chrono::{TimeDelta, NaiveTime};
let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap()
};
let since = NaiveTime::signed_duration_since;
assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 59, 0)),
TimeDelta::try_seconds(1).unwrap());
assert_eq!(since(from_hmsm(3, 0, 59, 1_500), from_hmsm(3, 0, 59, 0)),
TimeDelta::try_milliseconds(1500).unwrap());
assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 0, 0)),
TimeDelta::try_seconds(60).unwrap());
assert_eq!(since(from_hmsm(3, 0, 0, 0), from_hmsm(2, 59, 59, 1_000)),
TimeDelta::try_seconds(1).unwrap());
assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(2, 59, 59, 1_000)),
TimeDelta::try_seconds(61).unwrap());
Fully qualified path: chrono::time::TimeTrait::signed_duration_since
fn signed_duration_since(self: @Time, rhs: Time) -> TimeDelta
hms
Returns a triple of the hour, minute and second numbers.
Fully qualified path: chrono::time::TimeTrait::hms
fn hms(self: @Time) -> (u32, u32, u32)
Impls
Impls
TimeImpl
Fully qualified path: chrono::time::TimeImpl
pub impl TimeImpl of TimeTrait;
Impl constants
MIN
The earliest possible NaiveTime
Fully qualified path: chrono::time::TimeImpl::MIN
const MIN: Time = Time { secs: 0 };
MAX
Fully qualified path: chrono::time::TimeImpl::MAX
const MAX: Time = Time { secs: 23 * 3600 + 59 * 60 + 59 };
Impl functions
from_hms_opt
Makes a new NaiveTime from hour, minute and second.
The millisecond part is allowed to exceed 1,000,000,000 in order to represent a
leap second, but only when sec == 59.
Errors
Returns None on invalid hour, minute and/or second.
Example
use chrono::NaiveTime;
let from_hms_opt = NaiveTime::from_hms_opt;
assert!(from_hms_opt(0, 0, 0).is_some());
assert!(from_hms_opt(23, 59, 59).is_some());
assert!(from_hms_opt(24, 0, 0).is_none());
assert!(from_hms_opt(23, 60, 0).is_none());
assert!(from_hms_opt(23, 59, 60).is_none());
Fully qualified path: chrono::time::TimeImpl::from_hms_opt
fn from_hms_opt(hour: u32, min: u32, sec: u32) -> Option<Time>
from_num_seconds_from_midnight_opt
Makes a new NaiveTime from the number of seconds since midnight and nanosecond.
The nanosecond part is allowed to exceed 1,000,000,000 in order to represent a
leap second, but only when secs % 60 == 59.
Errors
Returns None on invalid number of seconds and/or nanosecond.
Example
use chrono::NaiveTime;
let from_nsecs_opt = NaiveTime::from_num_seconds_from_midnight_opt;
assert!(from_nsecs_opt(0, 0).is_some());
assert!(from_nsecs_opt(86399, 999_999_999).is_some());
assert!(from_nsecs_opt(86399, 1_999_999_999).is_some()); // a leap second after 23:59:59
assert!(from_nsecs_opt(86_400, 0).is_none());
assert!(from_nsecs_opt(86399, 2_000_000_000).is_none());
Fully qualified path: chrono::time::TimeImpl::from_num_seconds_from_midnight_opt
fn from_num_seconds_from_midnight_opt(secs: u32) -> Option<Time>
overflowing_add_signed
Adds given TimeDelta to the current time, and also returns the number of seconds
in the integral number of days ignored from the addition.
Example
use chrono::{NaiveTime, TimeDelta};
let from_hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap();
assert_eq!(
from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::try_hours(11).unwrap()),
(from_hms(14, 4, 5), 0)
);
assert_eq!(
from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::try_hours(23).unwrap()),
(from_hms(2, 4, 5), 86_400)
);
assert_eq!(
from_hms(3, 4, 5).overflowing_add_signed(TimeDelta::try_hours(-7).unwrap()),
(from_hms(20, 4, 5), -86_400)
);
Fully qualified path: chrono::time::TimeImpl::overflowing_add_signed
fn overflowing_add_signed(self: @Time, rhs: TimeDelta) -> (Time, i64)
overflowing_sub_signed
Subtracts given TimeDelta from the current time, and also returns the number of seconds
in the integral number of days ignored from the subtraction.
Example
use chrono::{NaiveTime, TimeDelta};
let from_hms = |h, m, s| NaiveTime::from_hms_opt(h, m, s).unwrap();
assert_eq!(
from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::try_hours(2).unwrap()),
(from_hms(1, 4, 5), 0)
);
assert_eq!(
from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::try_hours(17).unwrap()),
(from_hms(10, 4, 5), 86_400)
);
assert_eq!(
from_hms(3, 4, 5).overflowing_sub_signed(TimeDelta::try_hours(-22).unwrap()),
(from_hms(1, 4, 5), -86_400)
);
Fully qualified path: chrono::time::TimeImpl::overflowing_sub_signed
fn overflowing_sub_signed(self: @Time, rhs: TimeDelta) -> (Time, i64)
signed_duration_since
Subtracts another NaiveTime from the current time.
Returns a TimeDelta within +/- 1 day.
This does not overflow or underflow at all.
As a part of Chrono’s leap second handling,
the subtraction assumes that there is no leap second ever,
except when any of the NaiveTimes themselves represents a leap second
in which case the assumption becomes that
there are exactly one (or two) leap second(s) ever.
Example
use chrono::{NaiveTime, TimeDelta};
let from_hmsm = |h, m, s, milli| NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap();
let since = NaiveTime::signed_duration_since;
assert_eq!(since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 900)), TimeDelta::zero());
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 7, 875)),
TimeDelta::try_milliseconds(25).unwrap()
);
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 6, 925)),
TimeDelta::try_milliseconds(975).unwrap()
);
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 5, 0, 900)),
TimeDelta::try_seconds(7).unwrap()
);
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(3, 0, 7, 900)),
TimeDelta::try_seconds(5 * 60).unwrap()
);
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(0, 5, 7, 900)),
TimeDelta::try_seconds(3 * 3600).unwrap()
);
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(4, 5, 7, 900)),
TimeDelta::try_seconds(-3600).unwrap()
);
assert_eq!(
since(from_hmsm(3, 5, 7, 900), from_hmsm(2, 4, 6, 800)),
TimeDelta::try_seconds(3600 + 60 + 1).unwrap() +
TimeDelta::try_milliseconds(100).unwrap()
);
Leap seconds are handled, but the subtraction assumes that there were no other leap seconds happened.
use chrono::{TimeDelta, NaiveTime};
let from_hmsm = |h, m, s, milli| { NaiveTime::from_hms_milli_opt(h, m, s, milli).unwrap()
};
let since = NaiveTime::signed_duration_since;
assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 59, 0)),
TimeDelta::try_seconds(1).unwrap());
assert_eq!(since(from_hmsm(3, 0, 59, 1_500), from_hmsm(3, 0, 59, 0)),
TimeDelta::try_milliseconds(1500).unwrap());
assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(3, 0, 0, 0)),
TimeDelta::try_seconds(60).unwrap());
assert_eq!(since(from_hmsm(3, 0, 0, 0), from_hmsm(2, 59, 59, 1_000)),
TimeDelta::try_seconds(1).unwrap());
assert_eq!(since(from_hmsm(3, 0, 59, 1_000), from_hmsm(2, 59, 59, 1_000)),
TimeDelta::try_seconds(61).unwrap());
Fully qualified path: chrono::time::TimeImpl::signed_duration_since
fn signed_duration_since(self: @Time, rhs: Time) -> TimeDelta
hms
Returns a triple of the hour, minute and second numbers.
Fully qualified path: chrono::time::TimeImpl::hms
fn hms(self: @Time) -> (u32, u32, u32)
time_delta
Temporal quantification
Fully qualified path: chrono::time_delta
Constants
| MILLIS_PER_SEC | The number of milliseconds per second. |
| SECS_PER_MINUTE | — |
| SECS_PER_HOUR | The number of seconds in an hour. |
| SECS_PER_DAY | The number of (non-leap) seconds in days. |
| SECS_PER_WEEK | The number of (non-leap) seconds in a week. |
Structs
| TimeDelta | Time duration with nanosecond precision. This also allows for negative durations; see individual methods for details. A TimeDelta is represented internally as a complement of seconds and… |
Traits
Impls
Constants
Constants
| MILLIS_PER_SEC | The number of milliseconds per second. |
| SECS_PER_MINUTE | — |
| SECS_PER_HOUR | The number of seconds in an hour. |
| SECS_PER_DAY | The number of (non-leap) seconds in days. |
| SECS_PER_WEEK | The number of (non-leap) seconds in a week. |
MILLIS_PER_SEC
The number of milliseconds per second.
Fully qualified path: chrono::time_delta::MILLIS_PER_SEC
pub const MILLIS_PER_SEC: i64 = 1000;
SECS_PER_MINUTE
Fully qualified path: chrono::time_delta::SECS_PER_MINUTE
pub const SECS_PER_MINUTE: i64 = 60;
SECS_PER_HOUR
The number of seconds in an hour.
Fully qualified path: chrono::time_delta::SECS_PER_HOUR
pub const SECS_PER_HOUR: i64 = 3600;
SECS_PER_DAY
The number of (non-leap) seconds in days.
Fully qualified path: chrono::time_delta::SECS_PER_DAY
pub const SECS_PER_DAY: i64 = 86400;
SECS_PER_WEEK
The number of (non-leap) seconds in a week.
Fully qualified path: chrono::time_delta::SECS_PER_WEEK
pub const SECS_PER_WEEK: i64 = 604800;
Structs
Structs
| TimeDelta | Time duration with nanosecond precision. This also allows for negative durations; see individual methods for details. A TimeDelta is represented internally as a complement of seconds and… |
TimeDelta
Time duration with nanosecond precision.
This also allows for negative durations; see individual methods for details.
A TimeDelta is represented internally as a complement of seconds and
nanoseconds. The range is restricted to that of i64 milliseconds, with the
minimum value notably being set to -i64::MAX rather than allowing the full
range of i64::MIN. This is to allow easy flipping of sign, so that for
instance abs() can be called without any checks.
Fully qualified path: chrono::time_delta::TimeDelta
[derive(Clone, Copy, PartialEq, Drop)]
pub struct TimeDelta { /* private fields */ }
Traits
Traits
TimeDeltaTrait
Fully qualified path: chrono::time_delta::TimeDeltaTrait
pub trait TimeDeltaTrait
Trait constants
MIN
The minimum possible TimeDelta: -i64::MAX milliseconds.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::MIN
const MIN: TimeDelta;
MAX
The maximum possible TimeDelta: i64::MAX milliseconds.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::MAX
const MAX: TimeDelta;
Trait functions
new
Makes a new TimeDelta with given number of seconds and nanoseconds.
Errors
Returns None when the duration is out of bounds, or if nanos ≥ 1,000,000,000.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::new
fn new(secs: i64) -> Option<TimeDelta>
weeks
Makes a new TimeDelta with the given number of weeks.
Equivalent to TimeDelta::seconds(weeks * 7 * 24 * 60 * 60) with
overflow checks.
Panics
Panics when the duration is out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::weeks
fn weeks(weeks: i64) -> TimeDelta
try_weeks
Makes a new TimeDelta with the given number of weeks.
Equivalent to TimeDelta::try_seconds(weeks * 7 * 24 * 60 * 60) with
overflow checks.
Errors
Returns None when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::try_weeks
fn try_weeks(weeks: i64) -> Option<TimeDelta>
days
Makes a new TimeDelta with the given number of days.
Equivalent to TimeDelta::seconds(days * 24 * 60 * 60) with overflow
checks.
Panics
Panics when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::days
fn days(days: i64) -> TimeDelta
try_days
Makes a new TimeDelta with the given number of days.
Equivalent to TimeDelta::try_seconds(days * 24 * 60 * 60) with overflow
checks.
Errors
Returns None when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::try_days
fn try_days(days: i64) -> Option<TimeDelta>
hours
Makes a new TimeDelta with the given number of hours.
Equivalent to TimeDelta::seconds(hours * 60 * 60) with overflow checks.
Panics
Panics when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::hours
fn hours(hours: i64) -> TimeDelta
try_hours
Makes a new TimeDelta with the given number of hours.
Equivalent to TimeDelta::try_seconds(hours * 60 * 60) with overflow checks.
Errors
Returns None when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::try_hours
fn try_hours(hours: i64) -> Option<TimeDelta>
minutes
Makes a new TimeDelta with the given number of minutes.
Equivalent to TimeDelta::seconds(minutes * 60) with overflow checks.
Panics
Panics when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::minutes
fn minutes(minutes: i64) -> TimeDelta
try_minutes
Makes a new TimeDelta with the given number of minutes.
Equivalent to TimeDelta::try_seconds(minutes * 60) with overflow checks.
Errors
Returns None when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::try_minutes
fn try_minutes(minutes: i64) -> Option<TimeDelta>
seconds
Makes a new TimeDelta with the given number of seconds.
Panics
Panics when seconds is more than i64::MAX / 1_000 or less than -i64::MAX / 1_000
(in this context, this is the same as i64::MIN / 1_000 due to rounding).
Fully qualified path: chrono::time_delta::TimeDeltaTrait::seconds
fn seconds(seconds: i64) -> TimeDelta
try_seconds
Makes a new TimeDelta with the given number of seconds.
Errors
Returns None when seconds is more than i64::MAX / 1_000 or less than
-i64::MAX / 1_000 (in this context, this is the same as i64::MIN / 1_000 due to
rounding).
Fully qualified path: chrono::time_delta::TimeDeltaTrait::try_seconds
fn try_seconds(seconds: i64) -> Option<TimeDelta>
num_weeks
Returns the total number of whole weeks in the TimeDelta.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::num_weeks
fn num_weeks(self: TimeDelta) -> i64
num_days
Returns the total number of whole days in the TimeDelta.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::num_days
fn num_days(self: TimeDelta) -> i64
num_hours
Returns the total number of whole hours in the TimeDelta.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::num_hours
fn num_hours(self: TimeDelta) -> i64
num_minutes
Returns the total number of whole minutes in the TimeDelta.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::num_minutes
fn num_minutes(self: TimeDelta) -> i64
num_seconds
Returns the total number of whole seconds in the TimeDelta.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::num_seconds
fn num_seconds(self: @TimeDelta) -> i64
checked_add
Add two TimeDeltas, returning None if overflow occurred.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::checked_add
fn checked_add(self: @TimeDelta, rhs: TimeDelta) -> Option<TimeDelta>
checked_sub
Subtract two TimeDeltas, returning None if overflow occurred.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::checked_sub
fn checked_sub(self: @TimeDelta, rhs: TimeDelta) -> Option<TimeDelta>
checked_mul
Multiply a TimeDelta with a i32, returning None if overflow occurred.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::checked_mul
fn checked_mul(self: @TimeDelta, rhs: i32) -> Option<TimeDelta>
checked_div
Divide a TimeDelta with a i32, returning None if dividing by 0.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::checked_div
fn checked_div(self: @TimeDelta, rhs: i32) -> Option<TimeDelta>
abs
Returns the TimeDelta as an absolute (non-negative) value.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::abs
fn abs(self: @TimeDelta) -> TimeDelta
zero
A TimeDelta where the stored seconds and nanoseconds are equal to zero.
Fully qualified path: chrono::time_delta::TimeDeltaTrait::zero
fn zero() -> TimeDelta
is_zero
Returns true if the TimeDelta equals TimeDelta::zero().
Fully qualified path: chrono::time_delta::TimeDeltaTrait::is_zero
fn is_zero(self: @TimeDelta) -> bool
Impls
Impls
TimeDeltaImpl
Fully qualified path: chrono::time_delta::TimeDeltaImpl
pub impl TimeDeltaImpl of TimeDeltaTrait;
Impl constants
MIN
The minimum possible TimeDelta: -i64::MAX milliseconds.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::MIN
const MIN: TimeDelta = TimeDelta { secs: -Bounded::<i64>::MAX / MILLIS_PER_SEC };
MAX
The maximum possible TimeDelta: i64::MAX milliseconds.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::MAX
const MAX: TimeDelta = TimeDelta { secs: Bounded::<i64>::MAX / MILLIS_PER_SEC };
Impl functions
new
Makes a new TimeDelta with given number of seconds and nanoseconds.
Errors
Returns None when the duration is out of bounds, or if nanos ≥ 1,000,000,000.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::new
fn new(secs: i64) -> Option<TimeDelta>
weeks
Makes a new TimeDelta with the given number of weeks.
Equivalent to TimeDelta::seconds(weeks * 7 * 24 * 60 * 60) with
overflow checks.
Panics
Panics when the duration is out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::weeks
fn weeks(weeks: i64) -> TimeDelta
try_weeks
Makes a new TimeDelta with the given number of weeks.
Equivalent to TimeDelta::try_seconds(weeks * 7 * 24 * 60 * 60) with
overflow checks.
Errors
Returns None when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::try_weeks
fn try_weeks(weeks: i64) -> Option<TimeDelta>
days
Makes a new TimeDelta with the given number of days.
Equivalent to TimeDelta::seconds(days * 24 * 60 * 60) with overflow
checks.
Panics
Panics when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::days
fn days(days: i64) -> TimeDelta
try_days
Makes a new TimeDelta with the given number of days.
Equivalent to TimeDelta::try_seconds(days * 24 * 60 * 60) with overflow
checks.
Errors
Returns None when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::try_days
fn try_days(days: i64) -> Option<TimeDelta>
hours
Makes a new TimeDelta with the given number of hours.
Equivalent to TimeDelta::seconds(hours * 60 * 60) with overflow checks.
Panics
Panics when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::hours
fn hours(hours: i64) -> TimeDelta
try_hours
Makes a new TimeDelta with the given number of hours.
Equivalent to TimeDelta::try_seconds(hours * 60 * 60) with overflow checks.
Errors
Returns None when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::try_hours
fn try_hours(hours: i64) -> Option<TimeDelta>
minutes
Makes a new TimeDelta with the given number of minutes.
Equivalent to TimeDelta::seconds(minutes * 60) with overflow checks.
Panics
Panics when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::minutes
fn minutes(minutes: i64) -> TimeDelta
try_minutes
Makes a new TimeDelta with the given number of minutes.
Equivalent to TimeDelta::try_seconds(minutes * 60) with overflow checks.
Errors
Returns None when the TimeDelta would be out of bounds.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::try_minutes
fn try_minutes(minutes: i64) -> Option<TimeDelta>
seconds
Makes a new TimeDelta with the given number of seconds.
Panics
Panics when seconds is more than i64::MAX / 1_000 or less than -i64::MAX / 1_000
(in this context, this is the same as i64::MIN / 1_000 due to rounding).
Fully qualified path: chrono::time_delta::TimeDeltaImpl::seconds
fn seconds(seconds: i64) -> TimeDelta
try_seconds
Makes a new TimeDelta with the given number of seconds.
Errors
Returns None when seconds is more than i64::MAX / 1_000 or less than
-i64::MAX / 1_000 (in this context, this is the same as i64::MIN / 1_000 due to
rounding).
Fully qualified path: chrono::time_delta::TimeDeltaImpl::try_seconds
fn try_seconds(seconds: i64) -> Option<TimeDelta>
num_weeks
Returns the total number of whole weeks in the TimeDelta.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::num_weeks
fn num_weeks(self: TimeDelta) -> i64
num_days
Returns the total number of whole days in the TimeDelta.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::num_days
fn num_days(self: TimeDelta) -> i64
num_hours
Returns the total number of whole hours in the TimeDelta.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::num_hours
fn num_hours(self: TimeDelta) -> i64
num_minutes
Returns the total number of whole minutes in the TimeDelta.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::num_minutes
fn num_minutes(self: TimeDelta) -> i64
num_seconds
Returns the total number of whole seconds in the TimeDelta.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::num_seconds
fn num_seconds(self: @TimeDelta) -> i64
checked_add
Add two TimeDeltas, returning None if overflow occurred.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::checked_add
fn checked_add(self: @TimeDelta, rhs: TimeDelta) -> Option<TimeDelta>
checked_sub
Subtract two TimeDeltas, returning None if overflow occurred.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::checked_sub
fn checked_sub(self: @TimeDelta, rhs: TimeDelta) -> Option<TimeDelta>
checked_mul
Multiply a TimeDelta with a i32, returning None if overflow occurred.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::checked_mul
fn checked_mul(self: @TimeDelta, rhs: i32) -> Option<TimeDelta>
checked_div
Divide a TimeDelta with a i32, returning None if dividing by 0.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::checked_div
fn checked_div(self: @TimeDelta, rhs: i32) -> Option<TimeDelta>
abs
Returns the TimeDelta as an absolute (non-negative) value.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::abs
fn abs(self: @TimeDelta) -> TimeDelta
zero
A TimeDelta where the stored seconds and nanoseconds are equal to zero.
Fully qualified path: chrono::time_delta::TimeDeltaImpl::zero
fn zero() -> TimeDelta
is_zero
Returns true if the TimeDelta equals TimeDelta::zero().
Fully qualified path: chrono::time_delta::TimeDeltaImpl::is_zero
fn is_zero(self: @TimeDelta) -> bool
traits
Fully qualified path: chrono::traits
Traits
| Datelike | The common set of methods for date component. Methods such as year , month , day and weekday can be used to get basic information about the date. The with_* methods can change the date…. |
| Timelike | The common set of methods for time component. |
Traits
Traits
| Datelike | The common set of methods for date component. Methods such as year , month , day and weekday can be used to get basic information about the date. The with_* methods can change the date…. |
| Timelike | The common set of methods for time component. |
Datelike
The common set of methods for date component.
Methods such as year, month, day and weekday can be used to get basic
information about the date.
The with_* methods can change the date.
Warning
The with_* methods can be convenient to change a single component of a date, but they must be
used with some care. Examples to watch out for:
with_yearchanges the year component of a year-month-day value. Don’t use this method if you want the ordinal to stay the same after changing the year, of if you want the week and weekday values to stay the same.- Don’t combine two
with_*methods to change two components of the date. For example to change both the year and month components of a date. This could fail because an intermediate value does not exist, while the final date would be valid.
For more complex changes to a date, it is best to use the methods on NaiveDate to create a
new value instead of altering an existing date.
Fully qualified path: chrono::traits::Datelike
pub trait Datelike<T>
Trait functions
year
Returns the year number in the calendar date.
Fully qualified path: chrono::traits::Datelike::year
fn year(self: @T) -> u32
year_ce
Returns the absolute year number starting from 1 with a boolean flag, which is false when the year predates the epoch (BCE/BC) and true otherwise (CE/AD).
Fully qualified path: chrono::traits::Datelike::year_ce
fn year_ce(self: @T) -> (bool, u32)
quarter
Returns the quarter number starting from 1.
The return value ranges from 1 to 4.
Fully qualified path: chrono::traits::Datelike::quarter
fn quarter(self: @T) -> u32
month
Returns the month number starting from 1.
The return value ranges from 1 to 12.
Fully qualified path: chrono::traits::Datelike::month
fn month(self: @T) -> u32
month0
Returns the month number starting from 0.
The return value ranges from 0 to 11.
Fully qualified path: chrono::traits::Datelike::month0
fn month0(self: @T) -> u32
day
Returns the day of month starting from 1.
The return value ranges from 1 to 31. (The last day of month differs by months.)
Fully qualified path: chrono::traits::Datelike::day
fn day(self: @T) -> u32
day0
Returns the day of month starting from 0.
The return value ranges from 0 to 30. (The last day of month differs by months.)
Fully qualified path: chrono::traits::Datelike::day0
fn day0(self: @T) -> u32
ordinal
Returns the day of year starting from 1.
The return value ranges from 1 to 366. (The last day of year differs by years.)
Fully qualified path: chrono::traits::Datelike::ordinal
fn ordinal(self: @T) -> u32
ordinal0
Returns the day of year starting from 0.
The return value ranges from 0 to 365. (The last day of year differs by years.)
Fully qualified path: chrono::traits::Datelike::ordinal0
fn ordinal0(self: @T) -> u32
weekday
Returns the day of week.
Fully qualified path: chrono::traits::Datelike::weekday
fn weekday(self: @T) -> Weekday
iso_week
Returns the ISO week.
Fully qualified path: chrono::traits::Datelike::iso_week
fn iso_week(self: @T) -> IsoWeek
with_year
Makes a new value with the year number changed, while keeping the same month and day.
This method assumes you want to work on the date as a year-month-day value. Don’t use it if you want the ordinal to stay the same after changing the year, of if you want the week and weekday values to stay the same.
Errors
Returns None when:
- The resulting date does not exist (February 29 in a non-leap year).
- The year is out of range for
NaiveDate. - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time.
Examples
use chrono::{Datelike, NaiveDate};
assert_eq!(
NaiveDate::from_ymd_opt(2020, 5, 13).unwrap().with_year(2023).unwrap(),
NaiveDate::from_ymd_opt(2023, 5, 13).unwrap()
);
// Resulting date 2023-02-29 does not exist:
assert!(NaiveDate::from_ymd_opt(2020, 2, 29).unwrap().with_year(2023).is_none());
// Don't use `with_year` if you want the ordinal date to stay the same:
assert_ne!(
NaiveDate::from_yo_opt(2020, 100).unwrap().with_year(2023).unwrap(),
NaiveDate::from_yo_opt(2023, 100).unwrap() // result is 2023-101
);
Fully qualified path: chrono::traits::Datelike::with_year
fn with_year(self: @T, year: u32) -> Option<T>
with_month
Makes a new value with the month number (starting from 1) changed.
Errors
Returns None when:
- The resulting date does not exist (for example
month(4)when day of the month is 31). - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time. - The value for
monthis out of range.
Examples
use chrono::{Datelike, NaiveDate};
assert_eq!(
NaiveDate::from_ymd_opt(2023, 5, 12).unwrap().with_month(9).unwrap(),
NaiveDate::from_ymd_opt(2023, 9, 12).unwrap()
);
// Resulting date 2023-09-31 does not exist:
assert!(NaiveDate::from_ymd_opt(2023, 5, 31).unwrap().with_month(9).is_none());
Don’t combine multiple Datelike::with_* methods. The intermediate value may not exist.
use chrono::{Datelike, NaiveDate};
fn with_year_month(date: NaiveDate, year: i32, month: u32) -> Option<NaiveDate> {
date.with_year(year)?.with_month(month)
}
let d = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
assert!(with_year_month(d, 2019, 1).is_none()); // fails because of invalid intermediate
value
// Correct version:
fn with_year_month_fixed(date: NaiveDate, year: i32, month: u32) -> Option<NaiveDate> {
NaiveDate::from_ymd_opt(year, month, date.day())
}
let d = NaiveDate::from_ymd_opt(2020, 2, 29).unwrap();
assert_eq!(with_year_month_fixed(d, 2019, 1), NaiveDate::from_ymd_opt(2019, 1, 29));
Fully qualified path: chrono::traits::Datelike::with_month
fn with_month(self: @T, month: u32) -> Option<T>
with_month0
Makes a new value with the month number (starting from 0) changed.
Errors
Returns None when:
- The resulting date does not exist (for example
month0(3)when day of the month is 31). - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time. - The value for
month0is out of range.
Fully qualified path: chrono::traits::Datelike::with_month0
fn with_month0(self: @T, month0: u32) -> Option<T>
with_day
Makes a new value with the day of month (starting from 1) changed.
Errors
Returns None when:
- The resulting date does not exist (for example
day(31)in April). - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time. - The value for
dayis out of range.
Fully qualified path: chrono::traits::Datelike::with_day
fn with_day(self: @T, day: u32) -> Option<T>
with_day0
Makes a new value with the day of month (starting from 0) changed.
Errors
Returns None when:
- The resulting date does not exist (for example
day0(30)in April). - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time. - The value for
day0is out of range.
Fully qualified path: chrono::traits::Datelike::with_day0
fn with_day0(self: @T, day0: u32) -> Option<T>
with_ordinal
Makes a new value with the day of year (starting from 1) changed.
Errors
Returns None when:
- The resulting date does not exist (
with_ordinal(366)in a non-leap year). - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time. - The value for
ordinalis out of range.
Fully qualified path: chrono::traits::Datelike::with_ordinal
fn with_ordinal(self: @T, ordinal: u32) -> Option<T>
with_ordinal0
Makes a new value with the day of year (starting from 0) changed.
Errors
Returns None when:
- The resulting date does not exist (
with_ordinal0(365)in a non-leap year). - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time. - The value for
ordinal0is out of range.
Fully qualified path: chrono::traits::Datelike::with_ordinal0
fn with_ordinal0(self: @T, ordinal0: u32) -> Option<T>
num_days_from_ce
Counts the days in the proleptic Gregorian calendar, with January 1, Year 1 (CE) as day 1.
Examples
use chrono::{Datelike, NaiveDate};
assert_eq!(NaiveDate::from_ymd_opt(1970, 1, 1).unwrap().num_days_from_ce(), 719_163);
assert_eq!(NaiveDate::from_ymd_opt(2, 1, 1).unwrap().num_days_from_ce(), 366);
assert_eq!(NaiveDate::from_ymd_opt(1, 1, 1).unwrap().num_days_from_ce(), 1);
assert_eq!(NaiveDate::from_ymd_opt(0, 1, 1).unwrap().num_days_from_ce(), -365);
Fully qualified path: chrono::traits::Datelike::num_days_from_ce
fn num_days_from_ce(self: @T) -> i32
num_days_in_month
Get the length in days of the month
Fully qualified path: chrono::traits::Datelike::num_days_in_month
fn num_days_in_month(self: @T) -> u8
Timelike
The common set of methods for time component.
Fully qualified path: chrono::traits::Timelike
pub trait Timelike<T>
Trait functions
hour
Returns the hour number from 0 to 23.
Fully qualified path: chrono::traits::Timelike::hour
fn hour(self: @T) -> u32
hour12
Returns the hour number from 1 to 12 with a boolean flag, which is false for AM and true for PM.
Fully qualified path: chrono::traits::Timelike::hour12
fn hour12(self: @T) -> (bool, u32)
minute
Returns the minute number from 0 to 59.
Fully qualified path: chrono::traits::Timelike::minute
fn minute(self: @T) -> u32
second
Returns the second number from 0 to 59.
Fully qualified path: chrono::traits::Timelike::second
fn second(self: @T) -> u32
with_hour
Makes a new value with the hour number changed.
Returns None when the resulting value would be invalid.
Fully qualified path: chrono::traits::Timelike::with_hour
fn with_hour(self: @T, hour: u32) -> Option<T>
with_minute
Makes a new value with the minute number changed.
Returns None when the resulting value would be invalid.
Fully qualified path: chrono::traits::Timelike::with_minute
fn with_minute(self: @T, min: u32) -> Option<T>
with_second
Makes a new value with the second number changed.
Returns None when the resulting value would be invalid.
As with the second method,
the input range is restricted to 0 through 59.
Fully qualified path: chrono::traits::Timelike::with_second
fn with_second(self: @T, sec: u32) -> Option<T>
num_seconds_from_midnight
Returns the number of non-leap seconds past the last midnight.
Every value in 00:00:00-23:59:59 maps to an integer in 0-86399.
This method is not intended to provide the real number of seconds since midnight on a given day. It does not take things like DST transitions into account.
Fully qualified path: chrono::traits::Timelike::num_seconds_from_midnight
fn num_seconds_from_midnight(self: @T) -> u32
utils
Fully qualified path: chrono::utils
Free functions
Free functions
Free functions
rem_euclid
Fully qualified path: chrono::utils::rem_euclid
pub fn rem_euclid<T, +Rem<T>, +Add<T>, +PartialOrd<T>, +Neg<T>, +Zero<T>, +Copy<T>, +Drop<T>>(
val: T, div: T,
) -> T
div_euclid
Fully qualified path: chrono::utils::div_euclid
pub fn div_euclid<
T,
+CheckedSub<T>,
+Div<T>,
+Rem<T>,
+Add<T>,
+PartialOrd<T>,
+Neg<T>,
+Zero<T>,
+Copy<T>,
+Drop<T>,
>(
val: T, div: T,
) -> Option<T>
week
Fully qualified path: chrono::week
Structs
Traits
Impls
Structs
Structs
Week
A week represented by a NaiveDate and a Weekday which is the first
day of the week.
Fully qualified path: chrono::week::Week
[derive(Copy, PartialEq, Drop, Debug)]
pub struct Week { /* private fields */ }
Traits
Traits
WeekTrait
Fully qualified path: chrono::week::WeekTrait
pub trait WeekTrait
Trait functions
new
Create a new NaiveWeek
Fully qualified path: chrono::week::WeekTrait::new
fn new(date: Date, start: Weekday) -> Week
first_day
Returns a date representing the first day of the week.
Panics
Panics if the first day of the week happens to fall just out of range of NaiveDate
(more than ca. 262,000 years away from common era).
Examples
use chrono::{NaiveDate, Weekday};
let date = NaiveDate::from_ymd_opt(2022, 4, 18).unwrap();
let week = date.week(Weekday::Mon);
assert!(week.first_day() <= date);
Fully qualified path: chrono::week::WeekTrait::first_day
fn first_day(self: @Week) -> Date
checked_first_day
Returns a date representing the first day of the week or
None if the date is out of NaiveDate’s range
(more than ca. 262,000 years away from common era).
Examples
use chrono::{NaiveDate, Weekday};
let date = NaiveDate::MIN;
let week = date.week(Weekday::Mon);
if let Some(first_day) = week.checked_first_day() {
assert!(first_day == date);
} else {
// error handling code
return;
};
Fully qualified path: chrono::week::WeekTrait::checked_first_day
fn checked_first_day(self: Week) -> Option<Date>
last_day
Returns a date representing the last day of the week.
Panics
Panics if the last day of the week happens to fall just out of range of NaiveDate
(more than ca. 262,000 years away from common era).
Examples
use chrono::{NaiveDate, Weekday};
let date = NaiveDate::from_ymd_opt(2022, 4, 18).unwrap();
let week = date.week(Weekday::Mon);
assert!(week.last_day() >= date);
Fully qualified path: chrono::week::WeekTrait::last_day
fn last_day(self: @Week) -> Date
checked_last_day
Returns a date representing the last day of the week or
None if the date is out of NaiveDate’s range
(more than ca. 262,000 years away from common era).
Examples
use chrono::{NaiveDate, Weekday};
let date = NaiveDate::MAX;
let week = date.week(Weekday::Mon);
if let Some(last_day) = week.checked_last_day() {
assert!(last_day == date);
} else {
// error handling code
return;
};
Fully qualified path: chrono::week::WeekTrait::checked_last_day
fn checked_last_day(self: @Week) -> Option<Date>
days
Returns a RangeInclusive<T> representing the whole week bounded by
first_day and last_day functions.
Panics
Panics if the either the first or last day of the week happens to fall just out of range of
NaiveDate (more than ca. 262,000 years away from common era).
Examples
use chrono::{NaiveDate, Weekday};
let date = NaiveDate::from_ymd_opt(2022, 4, 18).unwrap();
let week = date.week(Weekday::Mon);
let days = week.days();
assert!(days.contains(&date));
Fully qualified path: chrono::week::WeekTrait::days
fn days(self: @Week) -> RangeInclusive<Date>
checked_days
Returns an Option<RangeInclusive<T>> representing the whole week bounded by
checked_first_day and
checked_last_day functions.
Returns None if either of the boundaries are out of NaiveDate’s range
(more than ca. 262,000 years away from common era).
Examples
use chrono::{NaiveDate, Weekday};
let date = NaiveDate::MAX;
let week = date.week(Weekday::Mon);
let _days = match week.checked_days() {
Some(d) => d,
None => {
// error handling code
return;
}
};
Fully qualified path: chrono::week::WeekTrait::checked_days
fn checked_days(self: @Week) -> Option<RangeInclusive<Date>>
Impls
Impls
WeekImpl
Fully qualified path: chrono::week::WeekImpl
pub impl WeekImpl of WeekTrait;
Impl functions
new
Create a new NaiveWeek
Fully qualified path: chrono::week::WeekImpl::new
fn new(date: Date, start: Weekday) -> Week
first_day
Returns a date representing the first day of the week.
Panics
Panics if the first day of the week happens to fall just out of range of NaiveDate
(more than ca. 262,000 years away from common era).
Examples
use chrono::{NaiveDate, Weekday};
let date = NaiveDate::from_ymd_opt(2022, 4, 18).unwrap();
let week = date.week(Weekday::Mon);
assert!(week.first_day() <= date);
Fully qualified path: chrono::week::WeekImpl::first_day
fn first_day(self: @Week) -> Date
checked_first_day
Returns a date representing the first day of the week or
None if the date is out of NaiveDate’s range
(more than ca. 262,000 years away from common era).
Examples
use chrono::{NaiveDate, Weekday};
let date = NaiveDate::MIN;
let week = date.week(Weekday::Mon);
if let Some(first_day) = week.checked_first_day() {
assert!(first_day == date);
} else {
// error handling code
return;
};
Fully qualified path: chrono::week::WeekImpl::checked_first_day
fn checked_first_day(self: Week) -> Option<Date>
last_day
Returns a date representing the last day of the week.
Panics
Panics if the last day of the week happens to fall just out of range of NaiveDate
(more than ca. 262,000 years away from common era).
Examples
use chrono::{NaiveDate, Weekday};
let date = NaiveDate::from_ymd_opt(2022, 4, 18).unwrap();
let week = date.week(Weekday::Mon);
assert!(week.last_day() >= date);
Fully qualified path: chrono::week::WeekImpl::last_day
fn last_day(self: @Week) -> Date
checked_last_day
Returns a date representing the last day of the week or
None if the date is out of NaiveDate’s range
(more than ca. 262,000 years away from common era).
Examples
use chrono::{NaiveDate, Weekday};
let date = NaiveDate::MAX;
let week = date.week(Weekday::Mon);
if let Some(last_day) = week.checked_last_day() {
assert!(last_day == date);
} else {
// error handling code
return;
};
Fully qualified path: chrono::week::WeekImpl::checked_last_day
fn checked_last_day(self: @Week) -> Option<Date>
days
Returns a RangeInclusive<T> representing the whole week bounded by
first_day and last_day functions.
Panics
Panics if the either the first or last day of the week happens to fall just out of range of
NaiveDate (more than ca. 262,000 years away from common era).
Examples
use chrono::{NaiveDate, Weekday};
let date = NaiveDate::from_ymd_opt(2022, 4, 18).unwrap();
let week = date.week(Weekday::Mon);
let days = week.days();
assert!(days.contains(&date));
Fully qualified path: chrono::week::WeekImpl::days
fn days(self: @Week) -> RangeInclusive<Date>
checked_days
Returns an Option<RangeInclusive<T>> representing the whole week bounded by
checked_first_day and
checked_last_day functions.
Returns None if either of the boundaries are out of NaiveDate’s range
(more than ca. 262,000 years away from common era).
Examples
use chrono::{NaiveDate, Weekday};
let date = NaiveDate::MAX;
let week = date.week(Weekday::Mon);
let _days = match week.checked_days() {
Some(d) => d,
None => {
// error handling code
return;
}
};
Fully qualified path: chrono::week::WeekImpl::checked_days
fn checked_days(self: @Week) -> Option<RangeInclusive<Date>>
weekday
Fully qualified path: chrono::weekday
Enums
| Weekday | The day of week. The order of the days of week depends on the context. (This is why this type does _ not _ implement PartialOrd or Ord traits.) One should prefer *_from_monday or… |
Traits
Impls
Enums
Enums
| Weekday | The day of week. The order of the days of week depends on the context. (This is why this type does _ not _ implement PartialOrd or Ord traits.) One should prefer *_from_monday or… |
Weekday
The day of week.
The order of the days of week depends on the context.
(This is why this type does not implement PartialOrd or Ord traits.)
One should prefer *_from_monday or *_from_sunday methods to get the correct result.
Example
use chrono::Weekday;
let monday = "Monday".parse::<Weekday>().unwrap();
assert_eq!(monday, Weekday::Mon);
let sunday = Weekday::try_from(6).unwrap();
assert_eq!(sunday, Weekday::Sun);
assert_eq!(sunday.num_days_from_monday(), 6); // starts counting with Monday = 0
assert_eq!(sunday.number_from_monday(), 7); // starts counting with Monday = 1
assert_eq!(sunday.num_days_from_sunday(), 0); // starts counting with Sunday = 0
assert_eq!(sunday.number_from_sunday(), 1); // starts counting with Sunday = 1
assert_eq!(sunday.succ(), monday);
assert_eq!(sunday.pred(), Weekday::Sat);
Fully qualified path: chrono::weekday::Weekday
pub enum Weekday {
Mon,
Tue,
Wed,
Thu,
Fri,
Sat,
Sun,
}
Variants
Mon
Monday.
Fully qualified path: chrono::weekday::Weekday::Mon
Mon
Tue
Tuesday.
Fully qualified path: chrono::weekday::Weekday::Tue
Tue
Wed
Wednesday.
Fully qualified path: chrono::weekday::Weekday::Wed
Wed
Thu
Thursday.
Fully qualified path: chrono::weekday::Weekday::Thu
Thu
Fri
Friday.
Fully qualified path: chrono::weekday::Weekday::Fri
Fri
Sat
Saturday.
Fully qualified path: chrono::weekday::Weekday::Sat
Sat
Sun
Sunday.
Fully qualified path: chrono::weekday::Weekday::Sun
Sun
Traits
Traits
WeekdayTrait
Fully qualified path: chrono::weekday::WeekdayTrait
pub trait WeekdayTrait
Trait functions
succ
The next day in the week.
w: | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
w.succ(): | Tue | Wed | Thu | Fri | Sat | Sun | Mon |
Fully qualified path: chrono::weekday::WeekdayTrait::succ
fn succ(self: @Weekday) -> Weekday
pred
The previous day in the week.
w: | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
w.pred(): | Sun | Mon | Tue | Wed | Thu | Fri | Sat |
Fully qualified path: chrono::weekday::WeekdayTrait::pred
fn pred(self: @Weekday) -> Weekday
number_from_monday
Returns a day-of-week number starting from Monday = 1. (ISO 8601 weekday number)
w: | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
w.number_from_monday(): | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Fully qualified path: chrono::weekday::WeekdayTrait::number_from_monday
fn number_from_monday(self: @Weekday) -> u32
number_from_sunday
Returns a day-of-week number starting from Sunday = 1.
w: | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
w.number_from_sunday(): | 2 | 3 | 4 | 5 | 6 | 7 | 1 |
Fully qualified path: chrono::weekday::WeekdayTrait::number_from_sunday
fn number_from_sunday(self: @Weekday) -> u32
num_days_from_monday
Returns a day-of-week number starting from Monday = 0.
w: | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
w.num_days_from_monday(): | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
Example
#[cfg(feature = "clock")] {
use chrono::{Local, Datelike};
// MTWRFSU is occasionally used as a single-letter abbreviation of the weekdays.
// Use `num_days_from_monday` to index into the array.
const MTWRFSU: [char; 7] = ['M', 'T', 'W', 'R', 'F', 'S', 'U'];
let today = Local::now().weekday();
println!("{}", MTWRFSU[today.num_days_from_monday() as usize]);
}
Fully qualified path: chrono::weekday::WeekdayTrait::num_days_from_monday
fn num_days_from_monday(self: @Weekday) -> u32
num_days_from_sunday
Returns a day-of-week number starting from Sunday = 0.
w: | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
w.num_days_from_sunday(): | 1 | 2 | 3 | 4 | 5 | 6 | 0 |
Fully qualified path: chrono::weekday::WeekdayTrait::num_days_from_sunday
fn num_days_from_sunday(self: @Weekday) -> u32
days_since
The number of days since the given day.
Examples
use chrono::Weekday::*;
assert_eq!(Mon.days_since(Mon), 0);
assert_eq!(Sun.days_since(Tue), 5);
assert_eq!(Wed.days_since(Sun), 3);
Fully qualified path: chrono::weekday::WeekdayTrait::days_since
fn days_since(self: @Weekday, other: Weekday) -> u32
Impls
Impls
WeekdayImpl
Fully qualified path: chrono::weekday::WeekdayImpl
pub impl WeekdayImpl of WeekdayTrait;
Impl functions
succ
The next day in the week.
w: | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
w.succ(): | Tue | Wed | Thu | Fri | Sat | Sun | Mon |
Fully qualified path: chrono::weekday::WeekdayImpl::succ
fn succ(self: @Weekday) -> Weekday
pred
The previous day in the week.
w: | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
w.pred(): | Sun | Mon | Tue | Wed | Thu | Fri | Sat |
Fully qualified path: chrono::weekday::WeekdayImpl::pred
fn pred(self: @Weekday) -> Weekday
number_from_monday
Returns a day-of-week number starting from Monday = 1. (ISO 8601 weekday number)
w: | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
w.number_from_monday(): | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Fully qualified path: chrono::weekday::WeekdayImpl::number_from_monday
fn number_from_monday(self: @Weekday) -> u32
number_from_sunday
Returns a day-of-week number starting from Sunday = 1.
w: | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
w.number_from_sunday(): | 2 | 3 | 4 | 5 | 6 | 7 | 1 |
Fully qualified path: chrono::weekday::WeekdayImpl::number_from_sunday
fn number_from_sunday(self: @Weekday) -> u32
num_days_from_monday
Returns a day-of-week number starting from Monday = 0.
w: | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
w.num_days_from_monday(): | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
Example
#[cfg(feature = "clock")] {
use chrono::{Local, Datelike};
// MTWRFSU is occasionally used as a single-letter abbreviation of the weekdays.
// Use `num_days_from_monday` to index into the array.
const MTWRFSU: [char; 7] = ['M', 'T', 'W', 'R', 'F', 'S', 'U'];
let today = Local::now().weekday();
println!("{}", MTWRFSU[today.num_days_from_monday() as usize]);
}
Fully qualified path: chrono::weekday::WeekdayImpl::num_days_from_monday
fn num_days_from_monday(self: @Weekday) -> u32
num_days_from_sunday
Returns a day-of-week number starting from Sunday = 0.
w: | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
w.num_days_from_sunday(): | 1 | 2 | 3 | 4 | 5 | 6 | 0 |
Fully qualified path: chrono::weekday::WeekdayImpl::num_days_from_sunday
fn num_days_from_sunday(self: @Weekday) -> u32
days_since
The number of days since the given day.
Examples
use chrono::Weekday::*;
assert_eq!(Mon.days_since(Mon), 0);
assert_eq!(Sun.days_since(Tue), 5);
assert_eq!(Wed.days_since(Sun), 3);
Fully qualified path: chrono::weekday::WeekdayImpl::days_since
fn days_since(self: @Weekday, other: Weekday) -> u32
prelude
A convenience module appropriate for glob imports (use chrono::prelude::*;).
Fully qualified path: chrono::prelude
Re-exports:
| Date | ISO 8601 calendar date without timezone. Allows for every proleptic Gregorian date from Jan 1, 262145 BCE to Dec 31, 262143 CE. Also supports the conversion from ISO 8601 ordinal and week date…. |
| DateTime | ISO 8601 combined date and time without timezone…. |
| Days | A duration in calendar days. This is useful because when using TimeDelta it is possible that adding TimeDelta::days(1)… |
| IsoWeek | ISO 8601 week. This type, combined with Weekday , constitutes the ISO 8601 week date . One can retrieve this type from the existing Datelike types via the Datelike::iso_week method. |
| Months | A duration in calendar months |
| Time | — |
| TimeDelta | Time duration with nanosecond precision. This also allows for negative durations; see individual methods for details. A TimeDelta is represented internally as a complement of seconds and… |
| Week | A week represented by a NaiveDate and a Weekday which is the first day of the week. |
| Month | The month of the year. This enum is just a convenience implementation. The month in dates created by DateLike objects does not return this enum…. |
| Weekday | The day of week. The order of the days of week depends on the context. (This is why this type does _ not _ implement PartialOrd or Ord traits.) One should prefer *_from_monday or… |
| DateTrait | — |
| DateTimeTrait | — |
| DaysTrait | — |
| IsoWeekTrait | — |
| MonthTrait | — |
| MonthsTrait | — |
| TimeTrait | — |
| TimeDeltaTrait | — |
| Datelike | The common set of methods for date component. Methods such as year , month , day and weekday can be used to get basic information about the date. The with_* methods can change the date…. |
| Timelike | The common set of methods for time component. |
| WeekTrait | — |
| WeekdayTrait | — |