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>
impl OpeningHours<NoLocation>
Sourcepub fn parse(raw_oh: &str) -> Result<Self, ParserError>
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>
impl<L: Localize> OpeningHours<L>
Sourcepub fn with_context<L2: Localize>(self, ctx: Context<L2>) -> OpeningHours<L2>
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());Sourcepub fn normalize(&self) -> Self
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");Sourcepub fn schedule_at(&self, date: NaiveDate) -> Schedule
pub fn schedule_at(&self, date: NaiveDate) -> Schedule
Get the schedule at a given day.
Sourcepub fn iter_range(
&self,
from: L::DateTime,
to: L::DateTime,
) -> impl Iterator<Item = DateTimeRange<L::DateTime>> + Send + Sync + use<L>
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.
pub fn iter_from( &self, from: L::DateTime, ) -> impl Iterator<Item = DateTimeRange<L::DateTime>> + Send + Sync + use<L>
Sourcepub fn next_change(&self, current_time: L::DateTime) -> Option<L::DateTime>
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));Sourcepub fn state(&self, current_time: L::DateTime) -> RuleKind
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);Sourcepub fn is_open(&self, current_time: L::DateTime) -> bool
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));Sourcepub fn is_closed(&self, current_time: L::DateTime) -> bool
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));Sourcepub fn is_unknown(&self, current_time: L::DateTime) -> bool
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>
impl<L: Clone + Localize> Clone for OpeningHours<L>
Source§fn clone(&self) -> OpeningHours<L>
fn clone(&self) -> OpeningHours<L>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<L: Localize> Display for OpeningHours<L>
impl<L: Localize> Display for OpeningHours<L>
Source§impl FromStr for OpeningHours
impl FromStr for OpeningHours
impl<L: Eq + Localize> Eq for OpeningHours<L>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more