pub struct DateTime(pub Date, pub Time);Tuple Fields§
§0: Date§1: TimeImplementations§
Source§impl DateTime
impl DateTime
Sourcepub const fn try_parse(from: &str) -> Result<Self, ParseError>
pub const fn try_parse(from: &str) -> Result<Self, ParseError>
Try to parse Date and Time separated by space or T
May also accept dates with too much or too little 0-padding for the year. Month, day, hour, minute and second segments must be exactly 2 digits.
Note that unlike the ymd_hms! macro (which only works on literals), this function (because it uses Time::try_parse) also allows for the seconds to be omitted.
So this function accepts hh:mm Time components as well as hh:mm:ss.
use greg::calendar::DateTime;
use greg::ymd_hms;
assert_eq!(
DateTime::try_parse("2022-01-01 00:00:00"),
Ok(ymd_hms!(2022-01-01 00:00:00))
);
assert_eq!(
DateTime::try_parse("2020-06-06 10:15"),
Ok(ymd_hms!(2020-06-06 10:15:00))
);
assert_eq!(
DateTime::try_parse("1234-01-23T12:34:56"),
Ok(ymd_hms!(1234-01-23 12:34:56))
);
use greg::calendar::ParseError;
assert_eq!(
DateTime::try_parse("2021-01-01"),
Err(ParseError::Format)
);
assert_eq!(
DateTime::try_parse("09:30:00"),
Err(ParseError::Format)
);
// no leap seconds
assert!(DateTime::try_parse("2016-12-31 23:59:60").is_err());
assert!(DateTime::try_parse("Jan 1st, 2020 2pm").is_err());Sourcepub const fn parse(from: &str) -> Self
pub const fn parse(from: &str) -> Self
Parse Date and Time and panic if invalid
Just a convenient wrapper around try_parse (since there’s no .unwrap() in const).
use greg::calendar::DateTime;
const NYE: DateTime = DateTime::parse("2023-12-31 23:59:59");
assert_eq!(
DateTime::parse("2023-08-17T13:37"),
greg::ymd_hms!(2023-08-17 13:37:00)
);
assert_eq!(
DateTime::parse("2023-12-24 18:00:00"),
greg::ymd_hms!(2023-12-24 18:00:00)
);This is mainly useful in const contexts, since the panic gets caught at compile-time.
use greg::calendar::DateTime;
// not a leap year
const LEAP: DateTime = DateTime::parse("2023-02-29 05:30");See also the ymd_hms! macro for defining DateTimes with literals at compile-time.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for DateTime
Available on crate feature serde only.
impl<'de> Deserialize<'de> for DateTime
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl FromStr for DateTime
impl FromStr for DateTime
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parse Date and Time separated by space or T
May also accept dates with too much or too little 0-padding for the year.
Month, day, hour, minute and second segments must be exactly 2 digits.
Uses Date::try_parse and Time::try_parse internally, see the documentation there for details.
use greg::calendar::DateTime;
use greg::ymd_hms;
assert_eq!(
"2022-01-01 00:00:00".parse(),
Ok(ymd_hms!(2022-01-01 00:00:00))
);
assert_eq!(
"2020-06-06 10:15".parse(),
Ok(ymd_hms!(2020-06-06 10:15:00))
);
assert_eq!(
"1234-01-23T12:34:56".parse(),
Ok(ymd_hms!(1234-01-23 12:34:56))
);
assert!("2021-01-01".parse::<DateTime>().is_err());
assert!("09:30:00".parse::<DateTime>().is_err());
// no leap seconds
assert!("2016-12-31 23:59:60".parse::<DateTime>().is_err());
assert!("Jan 1st, 2020 2pm".parse::<DateTime>().is_err());