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>
👎Deprecated since 2.0.0: Use OpeningHours::from_str(raw_oh: &str) or raw_oh.parse() via the trait std::str::FromStr
pub fn parse(raw_oh: &str) -> Result<Self, ParserError>
Use OpeningHours::from_str(raw_oh: &str) or raw_oh.parse() via the trait std::str::FromStr
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());Sourcepub fn parse_with<F: FnMut(Warning<'_>)>(
parser: &mut Parser<F>,
raw_oh: &str,
) -> Result<Self, ParserError>
pub fn parse_with<F: FnMut(Warning<'_>)>( parser: &mut Parser<F>, raw_oh: &str, ) -> Result<Self, ParserError>
Use a specific parser configuration to parse an expression.
Source§impl<L: Localize> OpeningHours<L>
impl<L: Localize> OpeningHours<L>
Sourcepub fn get_context(&self) -> &Context<L>
pub fn get_context(&self) -> &Context<L>
Get the evaluation context for this expression.
Sourcepub fn get_expression(&self) -> Arc<OpeningHoursExpression>
pub fn get_expression(&self) -> Arc<OpeningHoursExpression>
Get the inner expression object.
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, Arc<str>)
pub fn state(&self, current_time: L::DateTime) -> (RuleKind, Arc<str>)
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, "".into()));
assert_eq!(oh.state(date_2), (RuleKind::Unknown, "".into()));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>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<L: Localize> Display for OpeningHours<L>
impl<L: Localize> Display for OpeningHours<L>
impl<L: Eq + Localize> Eq for OpeningHours<L>
Source§impl FromStr for OpeningHours
impl FromStr for OpeningHours
Source§impl<L: PartialEq + Localize> PartialEq for OpeningHours<L>
impl<L: PartialEq + Localize> PartialEq for OpeningHours<L>
Source§fn eq(&self, other: &OpeningHours<L>) -> bool
fn eq(&self, other: &OpeningHours<L>) -> bool
self and other values to be equal, and is used by ==.