Struct Time

Source
pub struct Time(/* private fields */);
Expand description

A time with a resolution

Start with Time::new(), which is a timestamp at the lowest resolution possible, and covers the whole day. Increase the resolution using the Time::with_* methods. Reducude the resolution using Time::reduce_to.

Consider these two times:

let t1 = Time::new().with_hour(15).with_minute(27).with_second(17);
assert_eq!(t1.to_string(), "15:27:17");

let t2 = t1.with_millis(0);
assert_eq!(t2.to_string(), "15:27:17.000");

assert_ne!(t1, t2);

In most date/time libraries, if you don’t specify the number of sub-second millis, it just sets them to zero. As a result, these values like t1 and t2 here would be considered equal. Not so with compactor::Time! Adding .with_millis(0) resulted in a different value. Previously the number of sub-second millis was unknown. Now it is known to be zero.

§Equality and ordering

As illustrated above, the Eq impl considers resolution significant. Two Times at different resolutions will never compare equal, even if one contains the other.

Likewise, ordering only exists between Times of the same resolution. Within a resolution, Times are totally ordered in the expected way. If you want to compare values of different resolutions, take a look at Time::coarse_cmp.

§A day-spanning tree of time intervals

Including a resolution means each Time value actually identifies a certain time interval. You can lower the resolution, which increases the width of the interval.

The intervals at one resolution all fall completely inside a single interval at any lower resolution. This means that the intervals representable by Time form a tree, where each node is contained by its parent, and spanned by its children. You can think of values of Time as paths into that tree, and the resolution is how deep into the tree the path goes.

At maximum resolution, this type identifies a specific second within a year. You can think of it as a “second-of-year” type, but where the bit pattern is designed to go through all the commonly-used time units (week, half-hour, etc). Lower-resolution variants are the same type, but with some number of lower bits unavailable. In other words, the resolution is simply the number of available bits.

§How it’s encoded

We indicate the resolution with a unary encoding in the lower bits. Starting from the LSB, there are some number of zeroes, followed by a one. The number of zeroes gives you the resolution. After the one, all bits represent actual time data.

This is a nice encoding, because (A) storing the resolution only costs a single bit, and (B) the positions of the time-data bits always have the same meaning, regardless of resolution (eg. bit 18, if it exists, always tells you whether it’s morning or afternoon).

A bonus: since there’s always a one-bit somewhere, the “all-zeroes” bit pattern is invalid, and can be used to represent the None case of Option<Time>.

Implementations§

Source§

impl Time

Source

pub fn resolution(self) -> Resolution

Source

pub fn reduce_to(&mut self, res: Resolution)

Has no effect if res is higher than the current resolution

Source

pub fn with_res(self, res: Resolution) -> Option<Self>

Source

pub fn coarse_cmp(self, other: Time) -> Ordering

Compare two values by first coarsening them to the lower of their two resolutions. This gives results consistent with partial_cmp(), but not eq(). This function will return Ordering::Eq when one value is inside the other, whereas eq() would return false.

Source§

impl Time

Source

pub fn new() -> Self

Covers the whole day

Source§

impl Time

Source

pub const WHOLE_DAY: Self

Source

pub const AM: Self

assert_eq!(Time::AM, Time::new().with_am_pm(AmPm::AM));
Source

pub const PM: Self

assert_eq!(Time::PM, Time::new().with_am_pm(AmPm::PM));
Source

pub const NIGHT: Self

assert_eq!(Time::NIGHT, Time::new().with_time_of_day(SixHour::Night));
Source

pub const MORNING: Self

assert_eq!(Time::MORNING, Time::new().with_time_of_day(SixHour::Morning));
Source

pub const AFTERNOON: Self

assert_eq!(Time::AFTERNOON, Time::new().with_time_of_day(SixHour::Afternoon));
Source

pub const EVENING: Self

assert_eq!(Time::EVENING, Time::new().with_time_of_day(SixHour::Evening));
Source

pub fn from_hour(h: u8) -> Self

Source§

impl Time

Source

pub fn try_with_am_pm(self, x: AmPm) -> Option<Self>

Source

pub fn try_with_time_of_day(self, x: SixHour) -> Option<Self>

Source

pub fn try_with_hour(self, x: u8) -> Option<Self>

Source

pub fn try_with_minute(self, x: u8) -> Option<Self>

Source

pub fn try_with_second(self, x: u8) -> Option<Self>

Source

pub fn try_with_millis(self, x: u16) -> Option<Self>

Source§

impl Time

Source

pub fn with_am_pm(self, x: AmPm) -> Self

Source

pub fn with_time_of_day(self, x: SixHour) -> Self

Source

pub fn with_hour(self, x: u8) -> Self

0-23

Source

pub fn with_minute(self, x: u8) -> Self

0-59

Source

pub fn with_second(self, x: u8) -> Self

0-59

Source

pub fn with_millis(self, x: u16) -> Self

0-999

Source

pub fn set_millis(&mut self, x: u16)

0-999

Source

pub fn set_second(&mut self, x: u8)

0-59

Source

pub fn set_minute(&mut self, x: u8)

0-59

Source

pub fn set_hour(&mut self, x: u8)

0-23

Source

pub fn set_time_of_day(&mut self, x: SixHour)

Source

pub fn set_am_pm(&mut self, x: AmPm)

Source§

impl Time

Source

pub fn millis(self) -> u16

0-999

Source

pub fn second(self) -> u8

0-59

Source

pub fn minute(self) -> u8

0-59

Source

pub fn hour(self) -> u8

0-23

Source

pub fn time_of_day(self) -> Option<SixHour>

Source

pub fn am_pm(self) -> Option<AmPm>

Trait Implementations§

Source§

impl Clone for Time

Source§

fn clone(&self) -> Time

Returns a duplicate 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

Formats the value using the given formatter. Read more
Source§

impl Default for Time

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Time

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Time

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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: &Self) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Time

Source§

impl Eq for Time

Source§

impl StructuralPartialEq for Time

Auto Trait Implementations§

§

impl Freeze for Time

§

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 T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

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

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.