Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use winnow::{
use crate::ParseDateTimeError;

#[derive(PartialEq, Debug)]
pub(crate) enum Item {
enum Item {
Timestamp(epoch::Timestamp),
DateTime(combined::DateTime),
Date(date::Date),
Expand All @@ -68,8 +68,29 @@ pub(crate) enum Item {
Pure(String),
}

/// Parse a date and time string based on a specific date.
pub(crate) fn parse_at_date<S: AsRef<str> + Clone>(
base: Zoned,
input: S,
) -> Result<Zoned, ParseDateTimeError> {
let input = input.as_ref().to_ascii_lowercase();
match parse(&mut input.as_str()) {
Ok(builder) => at_date(builder, base),
Err(_) => Err(ParseDateTimeError::InvalidInput),
}
}

/// Parse a date and time string based on the current local time.
pub(crate) fn parse_at_local<S: AsRef<str> + Clone>(input: S) -> Result<Zoned, ParseDateTimeError> {
let input = input.as_ref().to_ascii_lowercase();
match parse(&mut input.as_str()) {
Ok(builder) => at_local(builder),
Err(_) => Err(ParseDateTimeError::InvalidInput),
}
}

/// Build a `Zoned` object from a `DateTimeBuilder` and a base `Zoned` object.
pub(crate) fn at_date(builder: DateTimeBuilder, base: Zoned) -> Result<Zoned, ParseDateTimeError> {
fn at_date(builder: DateTimeBuilder, base: Zoned) -> Result<Zoned, ParseDateTimeError> {
builder
.set_base(base)
.build()
Expand All @@ -78,7 +99,7 @@ pub(crate) fn at_date(builder: DateTimeBuilder, base: Zoned) -> Result<Zoned, Pa

/// Build a `Zoned` object from a `DateTimeBuilder` and a default `Zoned` object
/// (the current time in the local timezone).
pub(crate) fn at_local(builder: DateTimeBuilder) -> Result<Zoned, ParseDateTimeError> {
fn at_local(builder: DateTimeBuilder) -> Result<Zoned, ParseDateTimeError> {
builder.build().ok_or(ParseDateTimeError::InvalidInput)
}

Expand Down Expand Up @@ -177,7 +198,7 @@ pub(crate) fn at_local(builder: DateTimeBuilder) -> Result<Zoned, ParseDateTimeE
///
/// optional_whitespace = { whitespace } ;
/// ```
pub(crate) fn parse(input: &mut &str) -> ModalResult<DateTimeBuilder> {
fn parse(input: &mut &str) -> ModalResult<DateTimeBuilder> {
trace("parse", alt((parse_timestamp, parse_items))).parse_next(input)
}

Expand Down
13 changes: 3 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,9 @@ impl Error for ParseDateTimeError {}
/// This function will return `Err(ParseDateTimeError::InvalidInput)` if the
/// input string cannot be parsed as a relative time.
pub fn parse_datetime<S: AsRef<str> + Clone>(input: S) -> Result<Zoned, ParseDateTimeError> {
let input = input.as_ref().to_ascii_lowercase();
match items::parse(&mut input.as_str()) {
Ok(x) => items::at_local(x),
Err(_) => Err(ParseDateTimeError::InvalidInput),
}
items::parse_at_local(input)
}

/// Parses a time string at a specific date and returns a `Zoned` object
/// representing the absolute time of the string.
///
Expand Down Expand Up @@ -107,11 +104,7 @@ pub fn parse_datetime_at_date<S: AsRef<str> + Clone>(
date: Zoned,
input: S,
) -> Result<Zoned, ParseDateTimeError> {
let input = input.as_ref().to_ascii_lowercase();
match items::parse(&mut input.as_str()) {
Ok(x) => items::at_date(x, date),
Err(_) => Err(ParseDateTimeError::InvalidInput),
}
items::parse_at_date(date, input)
}

#[cfg(test)]
Expand Down
Loading