pub struct Time {
pub h: u8,
pub m: u8,
pub s: u8,
}Expand description
Time-of-Day
Warning: if you set the fields yourself it is your responsibility to ensure that they are valid!
Self::is_valid can be used to conveniently check.
Because this library does not respect leap seconds, the s field may never be 60.
Fields§
§h: u8Hours (0..24)
m: u8Minutes (0..60)
s: u8Seconds (0..60)
Implementations§
Source§impl Time
impl Time
Sourcepub const fn hms_checked(h: u8, m: u8, s: u8) -> Self
pub const fn hms_checked(h: u8, m: u8, s: u8) -> Self
Construct from hours, minutes and seconds and assert Self::is_valid
use greg::calendar::Time;
const INVALID: Time = Time::hms_checked(24, 0, 0);Sourcepub const fn is_valid(self) -> bool
pub const fn is_valid(self) -> bool
Checks whether fields are in the valid range
Does not allow for a leap second, self.s must be in 0..=59 to be valid.
Sourcepub const fn as_seconds(self) -> u32
pub const fn as_seconds(self) -> u32
Seconds since midnight
Source§impl Time
impl Time
Sourcepub const fn try_parse(from: &str) -> Result<Self, ParseError>
pub const fn try_parse(from: &str) -> Result<Self, ParseError>
Try to parse Time as hh:mm:ss or hh:mm
Does not accept strings with too much or too little 0-padding.
use greg::calendar::Time;
assert_eq!(Time::try_parse("00:00:00"), Ok(Time::hms_checked(0, 0, 0)));
assert_eq!(Time::try_parse("10:15"), Ok(Time::hms_checked(10, 15, 0)));
assert_eq!(
Time::try_parse("12:34:56"),
Ok(Time::hms_checked(12, 34, 56))
);
// also implemented via FromStr
assert_eq!(
"23:59:59".parse::<Time>(),
Ok(Time::hms_checked(23, 59, 59))
);
use greg::calendar::ParseError;
assert_eq!(Time::try_parse("2021-01-01"), Err(ParseError::Format));
assert_eq!(Time::try_parse("24:00:00"), Err(ParseError::Invalid));
assert_eq!(Time::try_parse("23:1:0"), Err(ParseError::Format));
// no leap seconds
assert!("23:59:60".parse::<Time>().is_err());
// no sub-second precision
assert!("12:34:56.789".parse::<Time>().is_err());
assert!("6pm".parse::<Time>().is_err());Sourcepub const fn parse(from: &str) -> Self
pub const fn parse(from: &str) -> Self
Try to parse Time as hh:mm:ss or hh:mm and panic if invalid
use greg::calendar::Time;
const NOON_ISH: Time = Time::parse("12:34:56");
assert_eq!(Time::parse("00:00:00"), Time::MIDNIGHT);
assert_eq!(Time::parse("10:15"), Time::hms_checked(10, 15, 0));This is mainly useful in const contexts, since the panic gets caught at compile-time.
use greg::calendar::Time;
const MORNING: Time = Time::parse("06;30"); // typo: ';' instead of ':'Trait Implementations§
Source§impl<'de> Deserialize<'de> for Time
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Time
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 Time
impl FromStr for Time
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parse hh:mm:ss or hh:mm time
Does not accept strings with too much or too little 0-padding.
use greg::calendar::Time;
assert_eq!("00:00:00".parse(), Ok(Time::hms_checked(0, 0, 0)));
assert_eq!("10:15".parse(), Ok(Time::hms_checked(10, 15, 0)));
assert_eq!("12:34:56".parse(), Ok(Time::hms_checked(12, 34, 56)));
assert_eq!("23:59:59".parse(), Ok(Time::hms_checked(23, 59, 59)));
assert!("2021-01-01".parse::<Time>().is_err());
assert!("24:00:00".parse::<Time>().is_err());
assert!("23:1:0".parse::<Time>().is_err());
// no leap seconds
assert!("23:59:60".parse::<Time>().is_err());
// no sub-second precision
assert!("12:34:56.789".parse::<Time>().is_err());
assert!("6pm".parse::<Time>().is_err());