Struct greg::calendar::Time

source ·
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: u8

Hours (0..24)

§m: u8

Minutes (0..60)

§s: u8

Seconds (0..60)

Implementations§

source§

impl Time

source

pub const MIDNIGHT: Self = _

The beginning of the day, 00:00:00

source

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

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.

source

pub const fn as_seconds(self) -> u32

Seconds since midnight

source

pub const fn as_span(self) -> Span

Duration since midnight

source§

impl Time

source

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

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 Clone for Time

source§

fn clone(&self) -> Time

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Time

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Same as Display: hh:mm:ss (i.e. 12:34:56)

source§

impl<'de> Deserialize<'de> for Time

Available on crate feature serde only.
source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Time

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the Time as hh:mm:ss (i.e. 12:34:56)

source§

impl FromStr for Time

source§

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

type Err = ParseError

The associated error which can be returned from parsing.
source§

impl Hash for Time

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Time

source§

fn cmp(&self, other: &Time) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Time

source§

fn eq(&self, other: &Time) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Time

source§

fn partial_cmp(&self, other: &Time) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Time

Available on crate feature serde only.
source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Copy for Time

source§

impl Eq for Time

source§

impl StructuralEq for Time

source§

impl StructuralPartialEq for Time

Auto Trait Implementations§

§

impl RefUnwindSafe for Time

§

impl Send for Time

§

impl Sync for Time

§

impl Unpin for Time

§

impl UnwindSafe for Time

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,