Struct chrono::naive::date::NaiveDate
[−]
[src]
pub struct NaiveDate { // some fields omitted }
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.
Methods
impl NaiveDate
[src]
fn from_ymd(year: i32, month: u32, day: u32) -> NaiveDate
Makes a new NaiveDate
from year, month and day.
This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
Fails on the out-of-range date, invalid month and/or day.
Example
use chrono::{NaiveDate, Datelike, Weekday}; let d = NaiveDate::from_ymd(2015, 3, 14); assert_eq!(d.year(), 2015); assert_eq!(d.month(), 3); assert_eq!(d.day(), 14); assert_eq!(d.ordinal(), 73); // day of year assert_eq!(d.isoweekdate(), (2015, 11, Weekday::Sat)); // ISO week and weekday assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE
fn from_ymd_opt(year: i32, month: u32, day: u32) -> Option<NaiveDate>
Makes a new NaiveDate
from year, month and day.
This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
Returns None
on the out-of-range date, invalid month and/or day.
Example
use chrono::NaiveDate; let ymd = |y,m,d| NaiveDate::from_ymd_opt(y, m, d); assert!(ymd(2015, 3, 14).is_some()); assert!(ymd(2015, 0, 14).is_none()); assert!(ymd(2015, 2, 29).is_none()); assert!(ymd(-4, 2, 29).is_some()); // 5 BCE is a leap year assert!(ymd(400000, 1, 1).is_none()); assert!(ymd(-400000, 1, 1).is_none());
fn from_yo(year: i32, ordinal: u32) -> NaiveDate
Makes a new NaiveDate
from year and day of year (DOY or "ordinal").
This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
Fails on the out-of-range date and/or invalid DOY.
Example
use chrono::{NaiveDate, Datelike, Weekday}; let d = NaiveDate::from_yo(2015, 73); assert_eq!(d.ordinal(), 73); assert_eq!(d.year(), 2015); assert_eq!(d.month(), 3); assert_eq!(d.day(), 14); assert_eq!(d.isoweekdate(), (2015, 11, Weekday::Sat)); // ISO week and weekday assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE
fn from_yo_opt(year: i32, ordinal: u32) -> Option<NaiveDate>
Makes a new NaiveDate
from year and day of year (DOY or "ordinal").
This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
Returns None
on the out-of-range date and/or invalid DOY.
Example
use chrono::NaiveDate; let yo = |y,o| NaiveDate::from_yo_opt(y, o); assert!(yo(2015, 100).is_some()); assert!(yo(2015, 0).is_none()); assert!(yo(2015, 365).is_some()); assert!(yo(2015, 366).is_none()); assert!(yo(-4, 366).is_some()); // 5 BCE is a leap year assert!(yo(400000, 1).is_none()); assert!(yo(-400000, 1).is_none());
fn from_isoywd(year: i32, week: u32, weekday: Weekday) -> NaiveDate
Makes a new NaiveDate
from ISO week date (year and week number) and day of the week (DOW).
This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
The resulting NaiveDate
may have a different year from the input year.
Fails on the out-of-range date and/or invalid week number.
Example
use chrono::{NaiveDate, Datelike, Weekday}; let d = NaiveDate::from_isoywd(2015, 11, Weekday::Sat); assert_eq!(d.isoweekdate(), (2015, 11, Weekday::Sat)); assert_eq!(d.year(), 2015); assert_eq!(d.month(), 3); assert_eq!(d.day(), 14); assert_eq!(d.ordinal(), 73); // day of year assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE
fn from_isoywd_opt(year: i32, week: u32, weekday: Weekday) -> Option<NaiveDate>
Makes a new NaiveDate
from ISO week date (year and week number) and day of the week (DOW).
This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.
The resulting NaiveDate
may have a different year from the input year.
Returns None
on the out-of-range date and/or invalid week number.
Example
use chrono::{NaiveDate, Weekday}; let ymd = |y,m,d| NaiveDate::from_ymd(y, m, d); let isoywd = |y,w,d| NaiveDate::from_isoywd_opt(y, w, d); assert_eq!(isoywd(2015, 0, Weekday::Sun), None); assert_eq!(isoywd(2015, 10, Weekday::Sun), Some(ymd(2015, 3, 8))); assert_eq!(isoywd(2015, 30, Weekday::Mon), Some(ymd(2015, 7, 20))); assert_eq!(isoywd(2015, 60, Weekday::Mon), None); // out-of-range dates assert_eq!(isoywd(400000, 10, Weekday::Fri), None); assert_eq!(isoywd(-400000, 10, Weekday::Sat), None); // year boundary behaviors // // 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!(isoywd(2014, 52, Weekday::Sun), Some(ymd(2014, 12, 28))); assert_eq!(isoywd(2014, 53, Weekday::Mon), None); assert_eq!(isoywd(2015, 1, Weekday::Mon), Some(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!(isoywd(2015, 52, Weekday::Sun), Some(ymd(2015, 12, 27))); assert_eq!(isoywd(2015, 53, Weekday::Sun), Some(ymd(2016, 1, 3))); assert_eq!(isoywd(2015, 54, Weekday::Mon), None); assert_eq!(isoywd(2016, 1, Weekday::Mon), Some(ymd(2016, 1, 4)));
fn from_num_days_from_ce(days: i32) -> NaiveDate
Makes a new NaiveDate
from the number of days since January 1, 1 (Day 1)
in the proleptic Gregorian calendar.
Fails on the out-of-range date.
Example
use chrono::{NaiveDate, Datelike, Weekday}; let d = NaiveDate::from_num_days_from_ce(735671); assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE assert_eq!(d.year(), 2015); assert_eq!(d.month(), 3); assert_eq!(d.day(), 14); assert_eq!(d.ordinal(), 73); // day of year assert_eq!(d.isoweekdate(), (2015, 11, Weekday::Sat)); // ISO week and weekday
fn from_num_days_from_ce_opt(days: i32) -> Option<NaiveDate>
Makes a new NaiveDate
from the number of days since January 1, 1 (Day 1)
in the proleptic Gregorian calendar.
Returns None
on the out-of-range date.
Example
use chrono::NaiveDate; let days = |ndays| NaiveDate::from_num_days_from_ce_opt(ndays); assert_eq!(days(730000), Some(NaiveDate::from_ymd(1999, 9, 3))); assert_eq!(days(1), Some(NaiveDate::from_ymd(1, 1, 1))); assert_eq!(days(0), Some(NaiveDate::from_ymd(0, 12, 31))); assert_eq!(days(-1), Some(NaiveDate::from_ymd(0, 12, 30))); assert_eq!(days(100000000), None); assert_eq!(days(-100000000), None);
fn parse_from_str(s: &str, fmt: &str) -> ParseResult<NaiveDate>
Parses a string with the specified format string and returns a new NaiveDate
.
See the format::strftime
module
on the supported escape sequences.
Example
use chrono::NaiveDate; assert_eq!(NaiveDate::parse_from_str("2015-09-05", "%Y-%m-%d"), Ok(NaiveDate::from_ymd(2015, 9, 5))); assert_eq!(NaiveDate::parse_from_str("5sep2015", "%d%b%Y"), Ok(NaiveDate::from_ymd(2015, 9, 5))); // time and offset is ignored for the purpose of parsing assert_eq!(NaiveDate::parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"), Ok(NaiveDate::from_ymd(2014, 5, 17))); // either out-of-bound dates or insufficient fields are errors assert!(NaiveDate::parse_from_str("2015/9", "%Y/%m").is_err()); assert!(NaiveDate::parse_from_str("2015/9/31", "%Y/%m/%d").is_err()); // all parsed fields should be consistent to each other, otherwise it's an error assert!(NaiveDate::parse_from_str("Sat, 09 Aug 2013", "%a, %d %b %Y").is_err());
fn and_time(&self, time: NaiveTime) -> NaiveDateTime
Makes a new NaiveDateTime
from the current date and given NaiveTime
.
Example
use chrono::{NaiveDate, NaiveTime, NaiveDateTime}; let d = NaiveDate::from_ymd(2015, 6, 3); let t = NaiveTime::from_hms_milli(12, 34, 56, 789); let dt: NaiveDateTime = d.and_time(t); assert_eq!(dt.date(), d); assert_eq!(dt.time(), t);
fn and_hms(&self, hour: u32, min: u32, sec: u32) -> NaiveDateTime
Makes a new NaiveDateTime
from the current date, hour, minute and second.
Fails on invalid hour, minute and/or second.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday}; let d = NaiveDate::from_ymd(2015, 6, 3); let dt: NaiveDateTime = d.and_hms(12, 34, 56); assert_eq!(dt.year(), 2015); assert_eq!(dt.weekday(), Weekday::Wed); assert_eq!(dt.second(), 56);
fn and_hms_opt(&self, hour: u32, min: u32, sec: u32) -> Option<NaiveDateTime>
Makes a new NaiveDateTime
from the current date, hour, minute and second.
Returns None
on invalid hour, minute and/or second.
Example
use chrono::NaiveDate; let d = NaiveDate::from_ymd(2015, 6, 3); 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());
fn and_hms_milli(&self, hour: u32, min: u32, sec: u32, milli: u32) -> NaiveDateTime
Makes a new NaiveDateTime
from the current date, hour, minute, second and millisecond.
The millisecond part can exceed 1,000 in order to represent the leap second.
Fails on invalid hour, minute, second and/or millisecond.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday}; let d = NaiveDate::from_ymd(2015, 6, 3); let dt: NaiveDateTime = d.and_hms_milli(12, 34, 56, 789); assert_eq!(dt.year(), 2015); assert_eq!(dt.weekday(), Weekday::Wed); assert_eq!(dt.second(), 56); assert_eq!(dt.nanosecond(), 789_000_000);
fn and_hms_milli_opt(&self, hour: u32, min: u32, sec: u32, milli: u32) -> Option<NaiveDateTime>
Makes a new NaiveDateTime
from the current date, hour, minute, second and millisecond.
The millisecond part can exceed 1,000 in order to represent the leap second.
Returns None
on invalid hour, minute, second and/or millisecond.
Example
use chrono::NaiveDate; let d = NaiveDate::from_ymd(2015, 6, 3); assert!(d.and_hms_milli_opt(12, 34, 56, 789).is_some()); assert!(d.and_hms_milli_opt(12, 34, 59, 1_789).is_some()); // leap second assert!(d.and_hms_milli_opt(12, 34, 59, 2_789).is_none()); assert!(d.and_hms_milli_opt(12, 34, 60, 789).is_none()); assert!(d.and_hms_milli_opt(12, 60, 56, 789).is_none()); assert!(d.and_hms_milli_opt(24, 34, 56, 789).is_none());
fn and_hms_micro(&self, hour: u32, min: u32, sec: u32, micro: u32) -> NaiveDateTime
Makes a new NaiveDateTime
from the current date, hour, minute, second and microsecond.
The microsecond part can exceed 1,000,000 in order to represent the leap second.
Fails on invalid hour, minute, second and/or microsecond.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday}; let d = NaiveDate::from_ymd(2015, 6, 3); let dt: NaiveDateTime = d.and_hms_micro(12, 34, 56, 789_012); assert_eq!(dt.year(), 2015); assert_eq!(dt.weekday(), Weekday::Wed); assert_eq!(dt.second(), 56); assert_eq!(dt.nanosecond(), 789_012_000);
fn and_hms_micro_opt(&self, hour: u32, min: u32, sec: u32, micro: u32) -> Option<NaiveDateTime>
Makes a new NaiveDateTime
from the current date, hour, minute, second and microsecond.
The microsecond part can exceed 1,000,000 in order to represent the leap second.
Returns None
on invalid hour, minute, second and/or microsecond.
Example
use chrono::NaiveDate; let d = NaiveDate::from_ymd(2015, 6, 3); assert!(d.and_hms_micro_opt(12, 34, 56, 789_012).is_some()); assert!(d.and_hms_micro_opt(12, 34, 59, 1_789_012).is_some()); // leap second assert!(d.and_hms_micro_opt(12, 34, 59, 2_789_012).is_none()); assert!(d.and_hms_micro_opt(12, 34, 60, 789_012).is_none()); assert!(d.and_hms_micro_opt(12, 60, 56, 789_012).is_none()); assert!(d.and_hms_micro_opt(24, 34, 56, 789_012).is_none());
fn and_hms_nano(&self, hour: u32, min: u32, sec: u32, nano: u32) -> NaiveDateTime
Makes a new NaiveDateTime
from the current date, hour, minute, second and nanosecond.
The nanosecond part can exceed 1,000,000,000 in order to represent the leap second.
Fails on invalid hour, minute, second and/or nanosecond.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday}; let d = NaiveDate::from_ymd(2015, 6, 3); let dt: NaiveDateTime = d.and_hms_nano(12, 34, 56, 789_012_345); assert_eq!(dt.year(), 2015); assert_eq!(dt.weekday(), Weekday::Wed); assert_eq!(dt.second(), 56); assert_eq!(dt.nanosecond(), 789_012_345);
fn and_hms_nano_opt(&self, hour: u32, min: u32, sec: u32, nano: u32) -> Option<NaiveDateTime>
Makes a new NaiveDateTime
from the current date, hour, minute, second and nanosecond.
The nanosecond part can exceed 1,000,000,000 in order to represent the leap second.
Returns None
on invalid hour, minute, second and/or nanosecond.
Example
use chrono::NaiveDate; let d = NaiveDate::from_ymd(2015, 6, 3); assert!(d.and_hms_nano_opt(12, 34, 56, 789_012_345).is_some()); assert!(d.and_hms_nano_opt(12, 34, 59, 1_789_012_345).is_some()); // leap second assert!(d.and_hms_nano_opt(12, 34, 59, 2_789_012_345).is_none()); assert!(d.and_hms_nano_opt(12, 34, 60, 789_012_345).is_none()); assert!(d.and_hms_nano_opt(12, 60, 56, 789_012_345).is_none()); assert!(d.and_hms_nano_opt(24, 34, 56, 789_012_345).is_none());
fn succ(&self) -> NaiveDate
Makes a new NaiveDate
for the next date.
Fails when self
is the last representable date.
Example
use chrono::NaiveDate; assert_eq!(NaiveDate::from_ymd(2015, 6, 3).succ(), NaiveDate::from_ymd(2015, 6, 4)); assert_eq!(NaiveDate::from_ymd(2015, 6, 30).succ(), NaiveDate::from_ymd(2015, 7, 1)); assert_eq!(NaiveDate::from_ymd(2015, 12, 31).succ(), NaiveDate::from_ymd(2016, 1, 1));
fn succ_opt(&self) -> Option<NaiveDate>
Makes a new NaiveDate
for the next date.
Returns None
when self
is the last representable date.
Example
use chrono::NaiveDate; use chrono::naive::date::MAX; assert_eq!(NaiveDate::from_ymd(2015, 6, 3).succ_opt(), Some(NaiveDate::from_ymd(2015, 6, 4))); assert_eq!(MAX.succ_opt(), None);
fn pred(&self) -> NaiveDate
Makes a new NaiveDate
for the prior date.
Fails when self
is the first representable date.
Example
use chrono::NaiveDate; assert_eq!(NaiveDate::from_ymd(2015, 6, 3).pred(), NaiveDate::from_ymd(2015, 6, 2)); assert_eq!(NaiveDate::from_ymd(2015, 6, 1).pred(), NaiveDate::from_ymd(2015, 5, 31)); assert_eq!(NaiveDate::from_ymd(2015, 1, 1).pred(), NaiveDate::from_ymd(2014, 12, 31));
fn pred_opt(&self) -> Option<NaiveDate>
Makes a new NaiveDate
for the prior date.
Returns None
when self
is the first representable date.
Example
use chrono::NaiveDate; use chrono::naive::date::MIN; assert_eq!(NaiveDate::from_ymd(2015, 6, 3).pred_opt(), Some(NaiveDate::from_ymd(2015, 6, 2))); assert_eq!(MIN.pred_opt(), None);
fn checked_add(self, rhs: Duration) -> Option<NaiveDate>
Adds the days
part of given Duration
to the current date.
Returns None
when it will result in overflow.
Example
use chrono::{NaiveDate, Duration}; use chrono::naive::date::MAX; let d = NaiveDate::from_ymd(2015, 9, 5); assert_eq!(d.checked_add(Duration::days(40)), Some(NaiveDate::from_ymd(2015, 10, 15))); assert_eq!(d.checked_add(Duration::days(-40)), Some(NaiveDate::from_ymd(2015, 7, 27))); assert_eq!(d.checked_add(Duration::days(1000_000_000)), None); assert_eq!(d.checked_add(Duration::days(-1000_000_000)), None); assert_eq!(MAX.checked_add(Duration::days(1)), None);
fn checked_sub(self, rhs: Duration) -> Option<NaiveDate>
Subtracts the days
part of given Duration
from the current date.
Returns None
when it will result in overflow.
Example
use chrono::{NaiveDate, Duration}; use chrono::naive::date::MIN; let d = NaiveDate::from_ymd(2015, 9, 5); assert_eq!(d.checked_sub(Duration::days(40)), Some(NaiveDate::from_ymd(2015, 7, 27))); assert_eq!(d.checked_sub(Duration::days(-40)), Some(NaiveDate::from_ymd(2015, 10, 15))); assert_eq!(d.checked_sub(Duration::days(1000_000_000)), None); assert_eq!(d.checked_sub(Duration::days(-1000_000_000)), None); assert_eq!(MIN.checked_sub(Duration::days(1)), None);
fn format_with_items<'a, I>(&self, items: I) -> DelayedFormat<I> where I: Iterator<Item=Item<'a>> + Clone
Formats the date with the specified formatting items.
Otherwise it is same to the ordinary format
method.
The Iterator
of items should be Clone
able,
since the resulting DelayedFormat
value may be formatted multiple times.
Example
use chrono::NaiveDate; use chrono::format::strftime::StrftimeItems; let fmt = StrftimeItems::new("%Y-%m-%d"); let d = NaiveDate::from_ymd(2015, 9, 5); assert_eq!(d.format_with_items(fmt.clone()).to_string(), "2015-09-05"); assert_eq!(d.format("%Y-%m-%d").to_string(), "2015-09-05");
fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>>
Formats the date with the specified format string.
See the format::strftime
module
on the supported escape sequences.
This returns a DelayedFormat
,
which gets converted to a string only when actual formatting happens.
You may use the to_string
method to get a String
,
or just feed it into print!
and other formatting macros.
(In this way it avoids the excees memory allocation.)
A wrong format string does not issue an error immediately.
Rather, converting or formatting the DelayedFormat
fails.
You are recommended to immediately use DelayedFormat
for this reason.
Example
use chrono::NaiveDate; let d = NaiveDate::from_ymd(2015, 9, 5); assert_eq!(d.format("%Y-%m-%d").to_string(), "2015-09-05"); assert_eq!(d.format("%A, %-d %B, %C%y").to_string(), "Saturday, 5 September, 2015");
Trait Implementations
impl Clone for NaiveDate
[src]
fn clone(&self) -> NaiveDate
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl Copy for NaiveDate
[src]
impl Ord for NaiveDate
[src]
fn cmp(&self, __arg_0: &NaiveDate) -> Ordering
This method returns an Ordering
between self
and other
. Read more
impl PartialOrd for NaiveDate
[src]
fn partial_cmp(&self, __arg_0: &NaiveDate) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &NaiveDate) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &NaiveDate) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &NaiveDate) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &NaiveDate) -> bool
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl Eq for NaiveDate
[src]
impl PartialEq for NaiveDate
[src]
fn eq(&self, __arg_0: &NaiveDate) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &NaiveDate) -> bool
This method tests for !=
.
impl Datelike for NaiveDate
[src]
fn year(&self) -> i32
Returns the year number.
fn month(&self) -> u32
Returns the month number starting from 1.
fn month0(&self) -> u32
Returns the month number starting from 0.
fn day(&self) -> u32
Returns the day of month starting from 1.
fn day0(&self) -> u32
Returns the day of month starting from 0.
fn ordinal(&self) -> u32
Returns the day of year starting from 1.
fn ordinal0(&self) -> u32
Returns the day of year starting from 0.
fn weekday(&self) -> Weekday
Returns the day of week.
fn isoweekdate(&self) -> (i32, u32, Weekday)
Returns the ISO week date: an adjusted year, week number and day of week. The adjusted year may differ from that of the calendar date. Read more
fn with_year(&self, year: i32) -> Option<NaiveDate>
Makes a new value with the year number changed. Read more
fn with_month(&self, month: u32) -> Option<NaiveDate>
Makes a new value with the month number (starting from 1) changed. Read more
fn with_month0(&self, month0: u32) -> Option<NaiveDate>
Makes a new value with the month number (starting from 0) changed. Read more
fn with_day(&self, day: u32) -> Option<NaiveDate>
Makes a new value with the day of month (starting from 1) changed. Read more
fn with_day0(&self, day0: u32) -> Option<NaiveDate>
Makes a new value with the day of month (starting from 0) changed. Read more
fn with_ordinal(&self, ordinal: u32) -> Option<NaiveDate>
Makes a new value with the day of year (starting from 1) changed. Read more
fn with_ordinal0(&self, ordinal0: u32) -> Option<NaiveDate>
Makes a new value with the day of year (starting from 0) changed. Read more
fn year_ce(&self) -> (bool, u32)
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). Read more
fn num_days_from_ce(&self) -> i32
Returns the number of days since January 1, 1 (Day 1) in the proleptic Gregorian calendar.
impl Hash for NaiveDate
[src]
fn hash<H: Hasher>(&self, state: &mut H)
Feeds this value into the state given, updating the hasher as necessary.
fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher
1.3.0
Feeds a slice of this type into the state provided.
impl Add<Duration> for NaiveDate
[src]
An addition of Duration
to NaiveDate
discards the fractional days,
rounding to the closest integral number of days towards Duration::zero()
.
Fails on underflow or overflow.
Use NaiveDate::checked_add
for detecting that.
Example
use chrono::{NaiveDate, Duration}; let ymd = |y,m,d| NaiveDate::from_ymd(y, m, d); assert_eq!(ymd(2014, 1, 1) + Duration::zero(), ymd(2014, 1, 1)); assert_eq!(ymd(2014, 1, 1) + Duration::seconds(86399), ymd(2014, 1, 1)); assert_eq!(ymd(2014, 1, 1) + Duration::seconds(-86399), ymd(2014, 1, 1)); assert_eq!(ymd(2014, 1, 1) + Duration::days(1), ymd(2014, 1, 2)); assert_eq!(ymd(2014, 1, 1) + Duration::days(-1), ymd(2013, 12, 31)); assert_eq!(ymd(2014, 1, 1) + Duration::days(364), ymd(2014, 12, 31)); assert_eq!(ymd(2014, 1, 1) + Duration::days(365*4 + 1), ymd(2018, 1, 1)); assert_eq!(ymd(2014, 1, 1) + Duration::days(365*400 + 97), ymd(2414, 1, 1));
type Output = NaiveDate
The resulting type after applying the +
operator
fn add(self, rhs: Duration) -> NaiveDate
The method for the +
operator
impl Sub<NaiveDate> for NaiveDate
[src]
A subtraction of NaiveDate
from NaiveDate
yields a Duration
of integral numbers,
and does not overflow or underflow at all.
Example
use chrono::{NaiveDate, Duration}; let ymd = |y,m,d| NaiveDate::from_ymd(y, m, d); assert_eq!(ymd(2014, 1, 1) - ymd(2014, 1, 1), Duration::zero()); assert_eq!(ymd(2014, 1, 1) - ymd(2013, 12, 31), Duration::days(1)); assert_eq!(ymd(2014, 1, 1) - ymd(2014, 1, 2), Duration::days(-1)); assert_eq!(ymd(2014, 1, 1) - ymd(2013, 9, 23), Duration::days(100)); assert_eq!(ymd(2014, 1, 1) - ymd(2013, 1, 1), Duration::days(365)); assert_eq!(ymd(2014, 1, 1) - ymd(2010, 1, 1), Duration::days(365*4 + 1)); assert_eq!(ymd(2014, 1, 1) - ymd(1614, 1, 1), Duration::days(365*400 + 97));
type Output = Duration
The resulting type after applying the -
operator
fn sub(self, rhs: NaiveDate) -> Duration
The method for the -
operator
impl Sub<Duration> for NaiveDate
[src]
A subtraction of Duration
from NaiveDate
discards the fractional days,
rounding to the closest integral number of days towards Duration::zero()
.
Fails on underflow or overflow.
Use NaiveDate::checked_sub
for detecting that.
Example
use chrono::{NaiveDate, Duration}; let ymd = |y,m,d| NaiveDate::from_ymd(y, m, d); assert_eq!(ymd(2014, 1, 1) - Duration::zero(), ymd(2014, 1, 1)); assert_eq!(ymd(2014, 1, 1) - Duration::seconds(86399), ymd(2014, 1, 1)); assert_eq!(ymd(2014, 1, 1) - Duration::seconds(-86399), ymd(2014, 1, 1)); assert_eq!(ymd(2014, 1, 1) - Duration::days(1), ymd(2013, 12, 31)); assert_eq!(ymd(2014, 1, 1) - Duration::days(-1), ymd(2014, 1, 2)); assert_eq!(ymd(2014, 1, 1) - Duration::days(364), ymd(2013, 1, 2)); assert_eq!(ymd(2014, 1, 1) - Duration::days(365*4 + 1), ymd(2010, 1, 1)); assert_eq!(ymd(2014, 1, 1) - Duration::days(365*400 + 97), ymd(1614, 1, 1));
type Output = NaiveDate
The resulting type after applying the -
operator
fn sub(self, rhs: Duration) -> NaiveDate
The method for the -
operator
impl Debug for NaiveDate
[src]
The Debug
output of the naive date d
is same to d.format("%Y-%m-%d")
.
Note that ISO 8601 requires an explicit sign for years before 1 BCE or after 9999 CE.
The string printed can be readily parsed via the parse
method on str
.
Example
use chrono::NaiveDate; assert_eq!(format!("{:?}", NaiveDate::from_ymd(2015, 9, 5)), "2015-09-05"); assert_eq!(format!("{:?}", NaiveDate::from_ymd( 0, 1, 1)), "0000-01-01"); assert_eq!(format!("{:?}", NaiveDate::from_ymd(9999, 12, 31)), "9999-12-31"); // examples of an explicit year sign assert_eq!(format!("{:?}", NaiveDate::from_ymd( -1, 1, 1)), "-0001-01-01"); assert_eq!(format!("{:?}", NaiveDate::from_ymd(10000, 12, 31)), "+10000-12-31");
impl Display for NaiveDate
[src]
The Display
output of the naive date d
is same to d.format("%Y-%m-%d")
.
Note that ISO 8601 requires an explicit sign for years before 1 BCE or after 9999 CE.
The string printed can be readily parsed via the parse
method on str
.
Example
use chrono::NaiveDate; assert_eq!(format!("{}", NaiveDate::from_ymd(2015, 9, 5)), "2015-09-05"); assert_eq!(format!("{}", NaiveDate::from_ymd( 0, 1, 1)), "0000-01-01"); assert_eq!(format!("{}", NaiveDate::from_ymd(9999, 12, 31)), "9999-12-31"); // examples of an explicit year sign assert_eq!(format!("{}", NaiveDate::from_ymd( -1, 1, 1)), "-0001-01-01"); assert_eq!(format!("{}", NaiveDate::from_ymd(10000, 12, 31)), "+10000-12-31");
impl FromStr for NaiveDate
[src]
Parsing a str into a NaiveDate
uses the same format, %Y-%m-%d
, as Debug
and Display
.
Example
use chrono::NaiveDate; let d = NaiveDate::from_ymd(2015, 9, 18); assert_eq!(format!("{}", d).parse::<NaiveDate>(), Ok(d)); let d = NaiveDate::from_ymd(12345, 6, 7); assert_eq!(format!("{}", d).parse::<NaiveDate>(), Ok(d)); assert!("foo".parse::<NaiveDate>().is_err());
type Err = ParseError
The associated error which can be returned from parsing.
fn from_str(s: &str) -> ParseResult<NaiveDate>
Parses a string s
to return a value of this type. Read more