OpeningHours

Struct OpeningHours 

Source
pub struct OpeningHours<L: Localize = NoLocation> { /* private fields */ }
Expand description

A parsed opening hours expression and its evaluation context.

Note that all big inner structures are immutable and wrapped by an Arc so this is safe and fast to clone.

Implementations§

Source§

impl OpeningHours<NoLocation>

Source

pub fn parse(raw_oh: &str) -> Result<Self, ParserError>

Parse a raw opening hours expression.

use opening_hours::{Context, OpeningHours};

assert!(OpeningHours::parse("24/7 open").is_ok());
assert!(OpeningHours::parse("not a valid expression").is_err());
Source§

impl<L: Localize> OpeningHours<L>

Source

pub fn with_context<L2: Localize>(self, ctx: Context<L2>) -> OpeningHours<L2>

Set a new evaluation context for this expression.

use opening_hours::{Context, OpeningHours};

let oh = OpeningHours::parse("Mo-Fr open")
    .unwrap()
    .with_context(Context::default());
Source

pub fn normalize(&self) -> Self

Convert the expression into a normalized form. It will not affect the meaning of the expression and might impact the performance of evaluations.

use opening_hours::OpeningHours;

let oh = OpeningHours::parse("24/7 ; Su closed").unwrap();
assert_eq!(oh.normalize().to_string(), "Mo-Sa");
Source

pub fn schedule_at(&self, date: NaiveDate) -> Schedule

Get the schedule at a given day.

Source

pub fn iter_range( &self, from: L::DateTime, to: L::DateTime, ) -> impl Iterator<Item = DateTimeRange<L::DateTime>> + Send + Sync + use<L>

Iterate over disjoint intervals of different state restricted to the time interval from..to.

Source

pub fn iter_from( &self, from: L::DateTime, ) -> impl Iterator<Item = DateTimeRange<L::DateTime>> + Send + Sync + use<L>

Source

pub fn next_change(&self, current_time: L::DateTime) -> Option<L::DateTime>

Get the next time where the state will change.

use chrono::NaiveDateTime;
use opening_hours::OpeningHours;
use opening_hours_syntax::RuleKind;

let oh = OpeningHours::parse("12:00-18:00 open, 18:00-20:00 unknown").unwrap();
let date_1 = NaiveDateTime::parse_from_str("2024-11-18 15:00", "%Y-%m-%d %H:%M").unwrap();
let date_2 = NaiveDateTime::parse_from_str("2024-11-18 18:00", "%Y-%m-%d %H:%M").unwrap();
assert_eq!(oh.next_change(date_1), Some(date_2));
Source

pub fn state(&self, current_time: L::DateTime) -> RuleKind

Get the state at given time.

use chrono::NaiveDateTime;
use opening_hours::OpeningHours;
use opening_hours_syntax::RuleKind;

let oh = OpeningHours::parse("12:00-18:00 open, 18:00-20:00 unknown").unwrap();
let date_1 = NaiveDateTime::parse_from_str("2024-11-18 15:00", "%Y-%m-%d %H:%M").unwrap();
let date_2 = NaiveDateTime::parse_from_str("2024-11-18 19:00", "%Y-%m-%d %H:%M").unwrap();
assert_eq!(oh.state(date_1), RuleKind::Open);
assert_eq!(oh.state(date_2), RuleKind::Unknown);
Source

pub fn is_open(&self, current_time: L::DateTime) -> bool

Check if this is open at a given time.

use chrono::NaiveDateTime;
use opening_hours::OpeningHours;

let oh = OpeningHours::parse("12:00-18:00 open, 18:00-20:00 unknown").unwrap();
let date_1 = NaiveDateTime::parse_from_str("2024-11-18 15:00", "%Y-%m-%d %H:%M").unwrap();
let date_2 = NaiveDateTime::parse_from_str("2024-11-18 19:00", "%Y-%m-%d %H:%M").unwrap();
assert!(oh.is_open(date_1));
assert!(!oh.is_open(date_2));
Source

pub fn is_closed(&self, current_time: L::DateTime) -> bool

Check if this is closed at a given time.

use chrono::NaiveDateTime;
use opening_hours::OpeningHours;

let oh = OpeningHours::parse("12:00-18:00 open, 18:00-20:00 unknown").unwrap();
let date_1 = NaiveDateTime::parse_from_str("2024-11-18 10:00", "%Y-%m-%d %H:%M").unwrap();
let date_2 = NaiveDateTime::parse_from_str("2024-11-18 19:00", "%Y-%m-%d %H:%M").unwrap();
assert!(oh.is_closed(date_1));
assert!(!oh.is_closed(date_2));
Source

pub fn is_unknown(&self, current_time: L::DateTime) -> bool

Check if this is unknown at a given time.

use chrono::NaiveDateTime;
use opening_hours::OpeningHours;

let oh = OpeningHours::parse("12:00-18:00 open, 18:00-20:00 unknown").unwrap();
let date_1 = NaiveDateTime::parse_from_str("2024-11-18 19:00", "%Y-%m-%d %H:%M").unwrap();
let date_2 = NaiveDateTime::parse_from_str("2024-11-18 15:00", "%Y-%m-%d %H:%M").unwrap();
assert!(oh.is_unknown(date_1));
assert!(!oh.is_unknown(date_2));

Trait Implementations§

Source§

impl<L: Clone + Localize> Clone for OpeningHours<L>

Source§

fn clone(&self) -> OpeningHours<L>

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<L: Debug + Localize> Debug for OpeningHours<L>

Source§

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

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

impl<L: Localize> Display for OpeningHours<L>

Source§

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

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

impl FromStr for OpeningHours

Source§

type Err = Error

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

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

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

impl<L: Hash + Localize> Hash for OpeningHours<L>

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<L: PartialEq + Localize> PartialEq for OpeningHours<L>

Source§

fn eq(&self, other: &OpeningHours<L>) -> 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<L: Eq + Localize> Eq for OpeningHours<L>

Source§

impl<L: Localize> StructuralPartialEq for OpeningHours<L>

Auto Trait Implementations§

§

impl<L> Freeze for OpeningHours<L>
where L: Freeze,

§

impl<L> RefUnwindSafe for OpeningHours<L>
where L: RefUnwindSafe,

§

impl<L> Send for OpeningHours<L>

§

impl<L> Sync for OpeningHours<L>

§

impl<L> Unpin for OpeningHours<L>
where L: Unpin,

§

impl<L> UnwindSafe for OpeningHours<L>
where L: 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.