Struct todotxt::Date [] [src]

pub struct Date { /* 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]

Makes a new NaiveDate from the calendar date (year, month and day).

Panics 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

Makes a new NaiveDate from the calendar date (year, month and day).

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());

Makes a new NaiveDate from the ordinal date (year and day of the year).

Panics on the out-of-range date and/or invalid day of year.

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

Makes a new NaiveDate from the ordinal date (year and day of the year).

Returns None on the out-of-range date and/or invalid day of year.

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());

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.

Panics 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

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.

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);

assert_eq!(isoywd(400000, 10, Weekday::Fri), None);
assert_eq!(isoywd(-400000, 10, Weekday::Sat), None);

The year number of ISO week date may differ from that of the calendar date.

//           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)));

Makes a new NaiveDate from the number of days since January 1, 1 (Day 1) in the proleptic Gregorian calendar.

Panics 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

While not directly supported by Chrono, it is easy to convert from the Julian day number (January 1, 4713 BCE in the Julian calendar being Day 0) to Gregorian with this method. (Note that this panics when jd is out of range.)

use chrono::NaiveDate;

fn jd_to_date(jd: i32) -> NaiveDate {
    // keep in mind that the Julian day number is 0-based
    // while this method requires an 1-based number.
    NaiveDate::from_num_days_from_ce(jd - 1721425)
}

// January 1, 4713 BCE in Julian = November 24, 4714 BCE in Gregorian
assert_eq!(jd_to_date(0), NaiveDate::from_ymd(-4713, 11, 24));

assert_eq!(jd_to_date(1721426), NaiveDate::from_ymd(1, 1, 1));
assert_eq!(jd_to_date(2450000), NaiveDate::from_ymd(1995, 10, 9));
assert_eq!(jd_to_date(2451545), NaiveDate::from_ymd(2000, 1, 1));

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);

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)));

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());

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);

Makes a new NaiveDateTime from the current date, hour, minute and second.

No leap second is allowed here; use NaiveDate::and_hms_* methods with a subsecond parameter instead.

Panics 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);

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.

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());

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.

Panics 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);

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());

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.

Panics 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);

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());

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.

Panics 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);

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());

Makes a new NaiveDate for the next calendar date.

Panics 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));

Makes a new NaiveDate for the next calendar 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);

Makes a new NaiveDate for the previous calendar date.

Panics 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));

Makes a new NaiveDate for the previous calendar 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);

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);

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);

Formats the date with the specified formatting items. Otherwise it is same to the ordinary format method.

The Iterator of items should be Cloneable, 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");

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 redundant 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]

impl Display for NaiveDate
[src]

The Display output of the naive date d is same to d.format("%Y-%m-%d").

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");

ISO 8601 requires an explicit sign for years before 1 BCE or after 9999 CE.

assert_eq!(format!("{}", NaiveDate::from_ymd(   -1,  1,  1)),  "-0001-01-01");
assert_eq!(format!("{}", NaiveDate::from_ymd(10000, 12, 31)), "+10000-12-31");

Formats the value using the given formatter. Read more

impl PartialOrd<NaiveDate> for NaiveDate
[src]

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().

Panics on underflow or overflow. Use NaiveDate::checked_sub to detect 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));

impl Sub<NaiveDate> for NaiveDate
[src]

A subtraction of NaiveDate from NaiveDate yields a Duration of integral numbers.

This does not overflow or underflow at all, as all possible output fits in the range of Duration.

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));

impl Copy for NaiveDate
[src]

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().

Panics on underflow or overflow. Use NaiveDate::checked_add to detect 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));

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());

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

impl Ord for NaiveDate
[src]

impl Eq for NaiveDate
[src]

impl Hash for NaiveDate
[src]

NaiveDate can be used as a key to the hash maps.

impl PartialEq<NaiveDate> for NaiveDate
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Debug for NaiveDate
[src]

The Debug output of the naive date d is same to d.format("%Y-%m-%d").

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");

ISO 8601 requires an explicit sign for years before 1 BCE or after 9999 CE.

assert_eq!(format!("{:?}", NaiveDate::from_ymd(   -1,  1,  1)),  "-0001-01-01");
assert_eq!(format!("{:?}", NaiveDate::from_ymd(10000, 12, 31)), "+10000-12-31");

Formats the value using the given formatter.

impl Datelike for NaiveDate
[src]

Returns the year number in the calendar date.

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).year(), 2015);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).year(), -308); // 309 BCE

Returns the month number starting from 1.

The return value ranges from 1 to 12.

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).month(), 9);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).month(), 3);

Returns the month number starting from 0.

The return value ranges from 0 to 11.

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).month0(), 8);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).month0(), 2);

Returns the day of month starting from 1.

The return value ranges from 1 to 31. (The last day of month differs by months.)

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).day(), 8);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).day(), 14);

Combined with NaiveDate::pred, one can determine the number of days in a particular month. (Note that this panics when year is out of range.)

use chrono::{NaiveDate, Datelike};

fn ndays_in_month(year: i32, month: u32) -> u32 {
    // the first day of the next month...
    let (y, m) = if month == 12 { (year + 1, 1) } else { (year, month + 1) };
    let d = NaiveDate::from_ymd(y, m, 1);

    // ...is preceded by the last day of the original month
    d.pred().day()
}

assert_eq!(ndays_in_month(2015, 8), 31);
assert_eq!(ndays_in_month(2015, 9), 30);
assert_eq!(ndays_in_month(2015, 12), 31);
assert_eq!(ndays_in_month(2016, 2), 29);
assert_eq!(ndays_in_month(2017, 2), 28);

Returns the day of month starting from 0.

The return value ranges from 0 to 30. (The last day of month differs by months.)

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).day0(), 7);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).day0(), 13);

Returns the day of year starting from 1.

The return value ranges from 1 to 366. (The last day of year differs by years.)

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).ordinal(), 251);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).ordinal(), 74);

Combined with NaiveDate::pred, one can determine the number of days in a particular year. (Note that this panics when year is out of range.)

use chrono::{NaiveDate, Datelike};

fn ndays_in_year(year: i32) -> u32 {
    // the first day of the next year...
    let d = NaiveDate::from_ymd(year + 1, 1, 1);

    // ...is preceded by the last day of the original year
    d.pred().ordinal()
}

assert_eq!(ndays_in_year(2015), 365);
assert_eq!(ndays_in_year(2016), 366);
assert_eq!(ndays_in_year(2017), 365);
assert_eq!(ndays_in_year(2000), 366);
assert_eq!(ndays_in_year(2100), 365);

Returns the day of year starting from 0.

The return value ranges from 0 to 365. (The last day of year differs by years.)

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).ordinal0(), 250);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).ordinal0(), 73);

Returns the day of week.

Example

use chrono::{NaiveDate, Datelike, Weekday};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).weekday(), Weekday::Tue);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).weekday(), Weekday::Fri);

Makes a new NaiveDate with the year number changed.

Returns None when the resulting NaiveDate would be invalid.

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_year(2016),
           Some(NaiveDate::from_ymd(2016, 9, 8)));
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_year(-308),
           Some(NaiveDate::from_ymd(-308, 9, 8)));

A leap day (February 29) is a good example that this method can return None.

assert!(NaiveDate::from_ymd(2016, 2, 29).with_year(2015).is_none());
assert!(NaiveDate::from_ymd(2016, 2, 29).with_year(2020).is_some());

Makes a new NaiveDate with the month number (starting from 1) changed.

Returns None when the resulting NaiveDate would be invalid.

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_month(10),
           Some(NaiveDate::from_ymd(2015, 10, 8)));
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_month(13), None); // no month 13
assert_eq!(NaiveDate::from_ymd(2015, 9, 30).with_month(2), None); // no February 30

Makes a new NaiveDate with the month number (starting from 0) changed.

Returns None when the resulting NaiveDate would be invalid.

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_month0(9),
           Some(NaiveDate::from_ymd(2015, 10, 8)));
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_month0(12), None); // no month 13
assert_eq!(NaiveDate::from_ymd(2015, 9, 30).with_month0(1), None); // no February 30

Makes a new NaiveDate with the day of month (starting from 1) changed.

Returns None when the resulting NaiveDate would be invalid.

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_day(30),
           Some(NaiveDate::from_ymd(2015, 9, 30)));
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_day(31),
           None); // no September 31

Makes a new NaiveDate with the day of month (starting from 0) changed.

Returns None when the resulting NaiveDate would be invalid.

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_day0(29),
           Some(NaiveDate::from_ymd(2015, 9, 30)));
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_day0(30),
           None); // no September 31

Makes a new NaiveDate with the day of year (starting from 1) changed.

Returns None when the resulting NaiveDate would be invalid.

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 1, 1).with_ordinal(60),
           Some(NaiveDate::from_ymd(2015, 3, 1)));
assert_eq!(NaiveDate::from_ymd(2015, 1, 1).with_ordinal(366),
           None); // 2015 had only 365 days

assert_eq!(NaiveDate::from_ymd(2016, 1, 1).with_ordinal(60),
           Some(NaiveDate::from_ymd(2016, 2, 29)));
assert_eq!(NaiveDate::from_ymd(2016, 1, 1).with_ordinal(366),
           Some(NaiveDate::from_ymd(2016, 12, 31)));

Makes a new NaiveDate with the day of year (starting from 0) changed.

Returns None when the resulting NaiveDate would be invalid.

Example

use chrono::{NaiveDate, Datelike};

assert_eq!(NaiveDate::from_ymd(2015, 1, 1).with_ordinal0(59),
           Some(NaiveDate::from_ymd(2015, 3, 1)));
assert_eq!(NaiveDate::from_ymd(2015, 1, 1).with_ordinal0(365),
           None); // 2015 had only 365 days

assert_eq!(NaiveDate::from_ymd(2016, 1, 1).with_ordinal0(59),
           Some(NaiveDate::from_ymd(2016, 2, 29)));
assert_eq!(NaiveDate::from_ymd(2016, 1, 1).with_ordinal0(365),
           Some(NaiveDate::from_ymd(2016, 12, 31)));