TimeOfDayPeriod

Struct TimeOfDayPeriod 

pub struct TimeOfDayPeriod<Tz: TimeZone> {
    start: NaiveTime,
    end: NaiveTime,
    tz: Tz,
}
Expand description

A time period defined by a start and end time of day.

This type represents a recurring time period that occurs daily. For example, it can represent “09:00 to 17:00”.

The period is defined as [start, end) (start is inclusive and end is exclusive).

The word “period” is used here instead of “interval” to avoid confusion with Interval, which represents an absolute range between two fixed points in time.

§Format

The string representation is in the format <start>-<end> <offset>. <start> and <end> are either HH:MM, HH:MM:SS, or HH:MM:SS.ssssss and <offset> is a fixed offset in the format ±HH:MM.

If the serde feature is enabled, the type can be serialized and deserialized as a string in this format.

§Wrapping behavior

Since the period repeats every day, the time range is allowed to wrap around midnight. This can be used to represent time ranges like “22:00 to 02:00”, which would be interpreted as “22:00 today to 02:00 tomorrow”.

§Normal time range

Normal time ranges where start < end can be used to represent a time range on a specific day.

00:00           12:00           00:00
  |----------+----|-----------+---|
           start             end

§Time range wrapping around midnight

If start >= end, the time range wraps around midnight. This is useful for conditions that should be satisfied at night.

12:00           00:00           12:00
  |-------+-------|-------+-------|
        start            end

Periods with start == end are considered full-day periods. All full-day periods are equivalent.

§Time zone handling

Due to the messy nature of time zones (daylight saving time, historical changes, etc.), this type currently only supports fixed offset time zones for most operations.

Fields§

§start: NaiveTime

Start time in local time.

We use local time here because unlike DateTime we don’t represent a fixed point in time. Depending on the time zone offset, the actual UTC time will vary.

§end: NaiveTime

Exclusive upper bound in local time.

§tz: Tz

Time zone.

We store the actual time zone, not the offset, because the offset may vary depending on the date (e.g. due to daylight saving time).

Implementations§

§

impl<Tz: TimeZone> TimeOfDayPeriod<Tz>

pub const fn new(start: NaiveTime, end: NaiveTime, timezone: Tz) -> Self

Creates a new period with the given time zone.

pub const fn timezone(&self) -> &Tz

Returns the associated time zone.

pub fn is_full_day(&self) -> bool

Returns true if the period represents a full day.

§

impl TimeOfDayPeriod<FixedOffset>

pub const fn new_fixed( start: NaiveTime, end: NaiveTime, offset: FixedOffset, ) -> Self

Creates a new period with a fixed offset time zone.

start and end are in the local time of the given offset.

pub fn start_utc(&self) -> NaiveTime

Returns the start time in UTC.

This is only implemented for TimeOfDayPeriod<FixedOffset> because for the general case the start time in UTC will vary depending on the date.

pub fn end_utc(&self) -> NaiveTime

Returns the end time in UTC.

See the explanation in start_utc for why this is only implemented for TimeOfDayPeriod<FixedOffset>.

pub fn interval_on_day(&self, date: NaiveDate) -> Option<Interval<FixedOffset>>

Returns the earliest interval that overlaps with a date.

For normal time ranges there is only one interval per day, but for time ranges that wrap around midnight, there are two intervals: one starting on the previous day and one on the current day. As specified, this function always returns the one starting on the previous day as it is the earliest one.

pub fn interval_for_datetime( &self, dt: DateTime<FixedOffset>, ) -> Option<Interval<FixedOffset>>

Returns the next interval after the given datetime.

If the datetime is already within the time range, a interval for the same day is returned (i.e. one that contains the given datetime).

Returns None if the interval is unrepresentable. This only happens if the dt is already at the edge of the representable range.

Trait Implementations§

§

impl<Tz: Clone + TimeZone> Clone for TimeOfDayPeriod<Tz>

§

fn clone(&self) -> TimeOfDayPeriod<Tz>

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
§

impl Debug for TimeOfDayPeriod<FixedOffset>

§

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

Formats the value using the given formatter. Read more
§

impl<'de> Deserialize<'de> for TimeOfDayPeriod<FixedOffset>

Available on crate feature serde only.
§

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

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

impl Display for TimeOfDayPeriod<FixedOffset>

§

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

Formats the value using the given formatter. Read more
§

impl FromStr for TimeOfDayPeriod<FixedOffset>

§

type Err = ParseError

The associated error which can be returned from parsing.
§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
§

impl PartialEq for TimeOfDayPeriod<FixedOffset>

§

fn eq(&self, other: &Self) -> 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.
§

impl Serialize for TimeOfDayPeriod<FixedOffset>

Available on crate feature serde only.
§

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

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

impl Eq for TimeOfDayPeriod<FixedOffset>

Auto Trait Implementations§

§

impl<Tz> Freeze for TimeOfDayPeriod<Tz>
where Tz: Freeze,

§

impl<Tz> RefUnwindSafe for TimeOfDayPeriod<Tz>
where Tz: RefUnwindSafe,

§

impl<Tz> Send for TimeOfDayPeriod<Tz>
where Tz: Send,

§

impl<Tz> Sync for TimeOfDayPeriod<Tz>
where Tz: Sync,

§

impl<Tz> Unpin for TimeOfDayPeriod<Tz>
where Tz: Unpin,

§

impl<Tz> UnwindSafe for TimeOfDayPeriod<Tz>
where Tz: UnwindSafe,

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.
Source§

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