Trait python_comm::prelude::Datelike [−][src]
pub trait Datelike {
Show methods
fn year(&self) -> i32;
fn month(&self) -> u32;
fn month0(&self) -> u32;
fn day(&self) -> u32;
fn day0(&self) -> u32;
fn ordinal(&self) -> u32;
fn ordinal0(&self) -> u32;
fn weekday(&self) -> Weekday;
fn iso_week(&self) -> IsoWeek;
fn with_year(&self, year: i32) -> Option<Self>;
fn with_month(&self, month: u32) -> Option<Self>;
fn with_month0(&self, month0: u32) -> Option<Self>;
fn with_day(&self, day: u32) -> Option<Self>;
fn with_day0(&self, day0: u32) -> Option<Self>;
fn with_ordinal(&self, ordinal: u32) -> Option<Self>;
fn with_ordinal0(&self, ordinal0: u32) -> Option<Self>;
fn year_ce(&self) -> (bool, u32) { ... }
fn num_days_from_ce(&self) -> i32 { ... }
}Expand description
The common set of methods for date component.
Required methods
Returns the year number in the calendar date.
Returns the month number starting from 1.
The return value ranges from 1 to 12.
Returns the month number starting from 0.
The return value ranges from 0 to 11.
Returns the day of month starting from 1.
The return value ranges from 1 to 31. (The last day of month differs by months.)
Returns the day of month starting from 0.
The return value ranges from 0 to 30. (The last day of month differs by months.)
Returns the day of year starting from 1.
The return value ranges from 1 to 366. (The last day of year differs by years.)
Returns the day of year starting from 0.
The return value ranges from 0 to 365. (The last day of year differs by years.)
Makes a new value with the year number changed.
Returns None when the resulting value would be invalid.
fn with_month(&self, month: u32) -> Option<Self>
fn with_month(&self, month: u32) -> Option<Self>Makes a new value with the month number (starting from 1) changed.
Returns None when the resulting value would be invalid.
fn with_month0(&self, month0: u32) -> Option<Self>
fn with_month0(&self, month0: u32) -> Option<Self>Makes a new value with the month number (starting from 0) changed.
Returns None when the resulting value would be invalid.
Makes a new value with the day of month (starting from 1) changed.
Returns None when the resulting value would be invalid.
Makes a new value with the day of month (starting from 0) changed.
Returns None when the resulting value would be invalid.
fn with_ordinal(&self, ordinal: u32) -> Option<Self>
fn with_ordinal(&self, ordinal: u32) -> Option<Self>Makes a new value with the day of year (starting from 1) changed.
Returns None when the resulting value would be invalid.
fn with_ordinal0(&self, ordinal0: u32) -> Option<Self>
fn with_ordinal0(&self, ordinal0: u32) -> Option<Self>Makes a new value with the day of year (starting from 0) changed.
Returns None when the resulting value would be invalid.
Provided methods
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).
fn num_days_from_ce(&self) -> i32
fn num_days_from_ce(&self) -> i32Counts the days in the proleptic Gregorian calendar, with January 1, Year 1 (CE) as day 1.
Examples
use chrono::{NaiveDate, Datelike}; assert_eq!(NaiveDate::from_ymd(1970, 1, 1).num_days_from_ce(), 719_163); assert_eq!(NaiveDate::from_ymd(2, 1, 1).num_days_from_ce(), 366); assert_eq!(NaiveDate::from_ymd(1, 1, 1).num_days_from_ce(), 1); assert_eq!(NaiveDate::from_ymd(0, 1, 1).num_days_from_ce(), -365);
Implementations on Foreign Types
Returns the year number in the calendar date.
See also the NaiveDate::year method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56); assert_eq!(dt.year(), 2015);
Returns the month number starting from 1.
The return value ranges from 1 to 12.
See also the NaiveDate::month method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56); assert_eq!(dt.month(), 9);
Returns the month number starting from 0.
The return value ranges from 0 to 11.
See also the NaiveDate::month0 method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56); assert_eq!(dt.month0(), 8);
Returns the day of month starting from 1.
The return value ranges from 1 to 31. (The last day of month differs by months.)
See also the NaiveDate::day method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56); assert_eq!(dt.day(), 25);
Returns the day of month starting from 0.
The return value ranges from 0 to 30. (The last day of month differs by months.)
See also the NaiveDate::day0 method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56); assert_eq!(dt.day0(), 24);
Returns the day of year starting from 1.
The return value ranges from 1 to 366. (The last day of year differs by years.)
See also the NaiveDate::ordinal method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56); assert_eq!(dt.ordinal(), 268);
Returns the day of year starting from 0.
The return value ranges from 0 to 365. (The last day of year differs by years.)
See also the NaiveDate::ordinal0 method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56); assert_eq!(dt.ordinal0(), 267);
Returns the day of week.
See also the NaiveDate::weekday method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike, Weekday}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56); assert_eq!(dt.weekday(), Weekday::Fri);
Makes a new NaiveDateTime with the year number changed.
Returns None when the resulting NaiveDateTime would be invalid.
See also the
NaiveDate::with_year method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56); assert_eq!(dt.with_year(2016), Some(NaiveDate::from_ymd(2016, 9, 25).and_hms(12, 34, 56))); assert_eq!(dt.with_year(-308), Some(NaiveDate::from_ymd(-308, 9, 25).and_hms(12, 34, 56)));
Makes a new NaiveDateTime with the month number (starting from 1) changed.
Returns None when the resulting NaiveDateTime would be invalid.
See also the
NaiveDate::with_month method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 30).and_hms(12, 34, 56); assert_eq!(dt.with_month(10), Some(NaiveDate::from_ymd(2015, 10, 30).and_hms(12, 34, 56))); assert_eq!(dt.with_month(13), None); // no month 13 assert_eq!(dt.with_month(2), None); // no February 30
Makes a new NaiveDateTime with the month number (starting from 0) changed.
Returns None when the resulting NaiveDateTime would be invalid.
See also the
NaiveDate::with_month0 method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 30).and_hms(12, 34, 56); assert_eq!(dt.with_month0(9), Some(NaiveDate::from_ymd(2015, 10, 30).and_hms(12, 34, 56))); assert_eq!(dt.with_month0(12), None); // no month 13 assert_eq!(dt.with_month0(1), None); // no February 30
Makes a new NaiveDateTime with the day of month (starting from 1) changed.
Returns None when the resulting NaiveDateTime would be invalid.
See also the
NaiveDate::with_day method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms(12, 34, 56); assert_eq!(dt.with_day(30), Some(NaiveDate::from_ymd(2015, 9, 30).and_hms(12, 34, 56))); assert_eq!(dt.with_day(31), None); // no September 31
Makes a new NaiveDateTime with the day of month (starting from 0) changed.
Returns None when the resulting NaiveDateTime would be invalid.
See also the
NaiveDate::with_day0 method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms(12, 34, 56); assert_eq!(dt.with_day0(29), Some(NaiveDate::from_ymd(2015, 9, 30).and_hms(12, 34, 56))); assert_eq!(dt.with_day0(30), None); // no September 31
Makes a new NaiveDateTime with the day of year (starting from 1) changed.
Returns None when the resulting NaiveDateTime would be invalid.
See also the
NaiveDate::with_ordinal method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms(12, 34, 56); assert_eq!(dt.with_ordinal(60), Some(NaiveDate::from_ymd(2015, 3, 1).and_hms(12, 34, 56))); assert_eq!(dt.with_ordinal(366), None); // 2015 had only 365 days let dt: NaiveDateTime = NaiveDate::from_ymd(2016, 9, 8).and_hms(12, 34, 56); assert_eq!(dt.with_ordinal(60), Some(NaiveDate::from_ymd(2016, 2, 29).and_hms(12, 34, 56))); assert_eq!(dt.with_ordinal(366), Some(NaiveDate::from_ymd(2016, 12, 31).and_hms(12, 34, 56)));
Makes a new NaiveDateTime with the day of year (starting from 0) changed.
Returns None when the resulting NaiveDateTime would be invalid.
See also the
NaiveDate::with_ordinal0 method.
Example
use chrono::{NaiveDate, NaiveDateTime, Datelike}; let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms(12, 34, 56); assert_eq!(dt.with_ordinal0(59), Some(NaiveDate::from_ymd(2015, 3, 1).and_hms(12, 34, 56))); assert_eq!(dt.with_ordinal0(365), None); // 2015 had only 365 days let dt: NaiveDateTime = NaiveDate::from_ymd(2016, 9, 8).and_hms(12, 34, 56); assert_eq!(dt.with_ordinal0(59), Some(NaiveDate::from_ymd(2016, 2, 29).and_hms(12, 34, 56))); assert_eq!(dt.with_ordinal0(365), Some(NaiveDate::from_ymd(2016, 12, 31).and_hms(12, 34, 56)));
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)));