Enum timelang::TimeExpression
source · pub enum TimeExpression {
Specific(PointInTime),
Range(TimeRange),
Duration(Duration),
}
Expand description
The top-level entry-point for the timelang AST.
Typically you will want to use a more specific type like Duration, PointInTime, or TimeRange, but this top-level node-type is provided so that we can consider timelang to be a distinct language.
Note that TimeExpression is Sized, and thus all expressions in timelang have a predictable memory size and do not require any heap allocations. That said, parsing expressions in timelang does require some temporary allocations that go away when parsing is complete.
Examples
Specific Date:
use timelang::*;
assert_eq!(
"20/4/2021".parse::<TimeExpression>().unwrap(),
TimeExpression::Specific(PointInTime::Absolute(AbsoluteTime::Date(Date(
Month::April,
DayOfMonth(20),
Year(2021)
))))
);
Specific DateTime:
use timelang::*;
assert_eq!(
"15/6/2022 at 14:00".parse::<AbsoluteTime>().unwrap(),
AbsoluteTime::DateTime(DateTime(
Date(Month::June, DayOfMonth(15), Year(2022)),
Time(Hour::Hour24(14), Minute(0))
))
);
Time Range:
use timelang::*;
assert_eq!(
"from 1/1/2023 to 15/1/2023"
.parse::<TimeExpression>()
.unwrap(),
TimeExpression::Range(TimeRange(
PointInTime::Absolute(AbsoluteTime::Date(Date(
Month::January,
DayOfMonth(1),
Year(2023)
))),
PointInTime::Absolute(AbsoluteTime::Date(Date(
Month::January,
DayOfMonth(15),
Year(2023)
)))
))
);
Duration (multiple units with comma):
use timelang::*;
assert_eq!(
"2 hours, 30 minutes".parse::<TimeExpression>().unwrap(),
TimeExpression::Duration(Duration {
hours: Number(2),
minutes: Number(30),
days: Number(0),
weeks: Number(0),
months: Number(0),
years: Number(0)
})
);
Duration (multiple units with and
):
use timelang::*;
assert_eq!(
"1 year and 6 months".parse::<TimeExpression>().unwrap(),
TimeExpression::Duration(Duration {
years: Number(1),
months: Number(6),
days: Number(0),
weeks: Number(0),
hours: Number(0),
minutes: Number(0)
})
);
Relative Time (using ago
):
use timelang::*;
assert_eq!(
"3 days ago".parse::<TimeExpression>().unwrap(),
TimeExpression::Specific(PointInTime::Relative(RelativeTime::Directional {
duration: Duration {
days: Number(3),
minutes: Number(0),
hours: Number(0),
weeks: Number(0),
months: Number(0),
years: Number(0)
},
dir: TimeDirection::Ago
}))
);
Relative Time (using from now
):
use timelang::*;
assert_eq!(
"5 days, 10 hours, and 35 minutes from now"
.parse::<TimeExpression>()
.unwrap(),
TimeExpression::Specific(PointInTime::Relative(RelativeTime::Directional {
duration: Duration {
minutes: Number(35),
hours: Number(10),
days: Number(5),
weeks: Number(0),
months: Number(0),
years: Number(0)
},
dir: TimeDirection::FromNow
}))
);
Relative Time (after
a specific date):
use timelang::*;
assert_eq!(
"2 hours, 3 minutes after 10/10/2022"
.parse::<TimeExpression>()
.unwrap(),
TimeExpression::Specific(PointInTime::Relative(RelativeTime::Directional {
duration: Duration {
hours: Number(2),
minutes: Number(3),
days: Number(0),
weeks: Number(0),
months: Number(0),
years: Number(0)
},
dir: TimeDirection::AfterAbsolute(AbsoluteTime::Date(Date(
Month::October,
DayOfMonth(10),
Year(2022)
)))
}))
);
Relative Time (before
a specific date/time):
use timelang::*;
assert_eq!(
"1 day before 31/12/2023 at 11:13 PM"
.parse::<TimeExpression>()
.unwrap(),
TimeExpression::Specific(PointInTime::Relative(RelativeTime::Directional {
duration: Duration {
days: Number(1),
minutes: Number(0),
hours: Number(0),
weeks: Number(0),
months: Number(0),
years: Number(0)
},
dir: TimeDirection::BeforeAbsolute(AbsoluteTime::DateTime(DateTime(
Date(Month::December, DayOfMonth(31), Year(2023)),
Time(Hour::Hour12(11, AmPm::PM), Minute(13))
)))
}))
);
Time Range (with specific date/times):
use timelang::*;
assert_eq!(
"from 1/1/2024 at 10:00 to 2/1/2024 at 15:30"
.parse::<TimeExpression>()
.unwrap(),
TimeExpression::Range(TimeRange(
PointInTime::Absolute(AbsoluteTime::DateTime(DateTime(
Date(Month::January, DayOfMonth(1), Year(2024)),
Time(Hour::Hour24(10), Minute(0))
))),
PointInTime::Absolute(AbsoluteTime::DateTime(DateTime(
Date(Month::January, DayOfMonth(2), Year(2024)),
Time(Hour::Hour24(15), Minute(30))
)))
))
);
Relative Time (Named):
use timelang::*;
assert_eq!("now".parse::<RelativeTime>().unwrap(), RelativeTime::Named(NamedRelativeTime::Now));
assert_eq!(
"tomorrow".parse::<RelativeTime>().unwrap(),
RelativeTime::Named(NamedRelativeTime::Tomorrow)
);
assert_eq!(
"yesterday".parse::<RelativeTime>().unwrap(),
RelativeTime::Named(NamedRelativeTime::Yesterday)
);
assert_eq!(
"day before yesterday".parse::<RelativeTime>().unwrap(),
RelativeTime::Named(NamedRelativeTime::DayBeforeYesterday)
);
// note the optional `the`
assert_eq!(
"the day after tomorrow".parse::<RelativeTime>().unwrap(),
RelativeTime::Named(NamedRelativeTime::DayAfterTomorrow)
);
assert_eq!(
"next tuesday".parse::<RelativeTime>().unwrap(),
RelativeTime::Next(RelativeTimeUnit::Tuesday)
);
assert_eq!(
"last wednesday".parse::<RelativeTime>().unwrap(),
RelativeTime::Last(RelativeTimeUnit::Wednesday)
);
assert_eq!(
"3 days before yesterday".parse::<RelativeTime>().unwrap(),
RelativeTime::Directional {
duration: Duration {
minutes: Number(0),
hours: Number(0),
days: Number(3),
weeks: Number(0),
months: Number(0),
years: Number(0)
},
dir: TimeDirection::BeforeNamed(NamedRelativeTime::Yesterday)
}
);
assert_eq!(
"2 days and 14 hours after the day after tomorrow".parse::<RelativeTime>().unwrap(),
RelativeTime::Directional {
duration: Duration {
minutes: Number(0),
hours: Number(14),
days: Number(2),
weeks: Number(0),
months: Number(0),
years: Number(0)
},
dir: TimeDirection::AfterNamed(NamedRelativeTime::DayAfterTomorrow)
}
);
assert_eq!(
"2 weeks before last sunday".parse::<RelativeTime>().unwrap(),
RelativeTime::Directional {
duration: Duration {
minutes: Number(0),
hours: Number(0),
days: Number(0),
weeks: Number(2),
months: Number(0),
years: Number(0)
},
dir: TimeDirection::BeforeLast(RelativeTimeUnit::Sunday)
}
);
assert_eq!(
"3 years, 2 weeks after next thursday".parse::<RelativeTime>().unwrap(),
RelativeTime::Directional {
duration: Duration {
minutes: Number(0),
hours: Number(0),
days: Number(0),
weeks: Number(2),
months: Number(0),
years: Number(3)
},
dir: TimeDirection::AfterNext(RelativeTimeUnit::Thursday)
}
);
Variants§
Specific(PointInTime)
Represents a PointInTime expression.
Range(TimeRange)
Represents a TimeRange expression.
Duration(Duration)
Represents a Duration expression.
Trait Implementations§
source§impl Clone for TimeExpression
impl Clone for TimeExpression
source§fn clone(&self) -> TimeExpression
fn clone(&self) -> TimeExpression
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for TimeExpression
impl Debug for TimeExpression
source§impl Display for TimeExpression
impl Display for TimeExpression
source§impl FromStr for TimeExpression
impl FromStr for TimeExpression
source§impl Hash for TimeExpression
impl Hash for TimeExpression
source§impl Ord for TimeExpression
impl Ord for TimeExpression
source§fn cmp(&self, other: &TimeExpression) -> Ordering
fn cmp(&self, other: &TimeExpression) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl Parse for TimeExpression
impl Parse for TimeExpression
fn parse(input: ParseStream<'_>) -> Result<Self>
source§impl PartialEq for TimeExpression
impl PartialEq for TimeExpression
source§fn eq(&self, other: &TimeExpression) -> bool
fn eq(&self, other: &TimeExpression) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for TimeExpression
impl PartialOrd for TimeExpression
source§fn partial_cmp(&self, other: &TimeExpression) -> Option<Ordering>
fn partial_cmp(&self, other: &TimeExpression) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more