Skip to content

fix: timestamp cannot be combined with other items #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 4 additions & 6 deletions src/items/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ impl DateTimeBuilder {
self
}

/// Timestamp value is exclusive to other date/time components. Caller of
/// the builder must ensure that it is not combined with other items.
pub(super) fn set_timestamp(mut self, ts: i32) -> Result<Self, &'static str> {
if self.timestamp.is_some() {
Err("timestamp cannot appear more than once")
} else {
self.timestamp = Some(ts);
Ok(self)
}
self.timestamp = Some(ts);
Ok(self)
}

pub(super) fn set_year(mut self, year: u32) -> Result<Self, &'static str> {
Expand Down
7 changes: 7 additions & 0 deletions src/items/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ use winnow::{combinator::preceded, ModalResult, Parser};

use super::primitive::{dec_int, s};

/// Parse a timestamp in the form of `@1234567890`.
///
/// Grammar:
///
/// ```ebnf
/// timestamp = "@" dec_int ;
/// ```
pub fn parse(input: &mut &str) -> ModalResult<i32> {
s(preceded("@", dec_int)).parse_next(input)
}
82 changes: 66 additions & 16 deletions src/items/mod.rs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct that Timestamp is still a variant of Item with the changes of this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is still a variant.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use builder::DateTimeBuilder;
use chrono::{DateTime, FixedOffset};
use primitive::space;
use winnow::{
combinator::{alt, trace},
combinator::{alt, eof, terminated, trace},
error::{AddContext, ContextError, ErrMode, StrContext, StrContextValue},
stream::Stream,
ModalResult, Parser,
Expand All @@ -65,39 +65,49 @@ pub enum Item {
TimeZone(time::Offset),
}

fn expect_error(input: &mut &str, reason: &'static str) -> ErrMode<ContextError> {
ErrMode::Cut(ContextError::new()).add_context(
input,
&input.checkpoint(),
StrContext::Expected(StrContextValue::Description(reason)),
)
}

/// Parse an item.
/// TODO: timestamp values are exclusive with other items. See
/// https://github.com/uutils/parse_datetime/issues/156
pub fn parse_one(input: &mut &str) -> ModalResult<Item> {
///
/// Grammar:
///
/// ```ebnf
/// item = combined | date | time | relative | weekday | timezone | year ;
/// ```
fn parse_item(input: &mut &str) -> ModalResult<Item> {
trace(
"parse_one",
"parse_item",
alt((
combined::parse.map(Item::DateTime),
date::parse.map(Item::Date),
time::parse.map(Item::Time),
relative::parse.map(Item::Relative),
weekday::parse.map(Item::Weekday),
epoch::parse.map(Item::Timestamp),
timezone::parse.map(Item::TimeZone),
date::year.map(Item::Year),
)),
)
.parse_next(input)
}

fn expect_error(input: &mut &str, reason: &'static str) -> ErrMode<ContextError> {
ErrMode::Cut(ContextError::new()).add_context(
input,
&input.checkpoint(),
StrContext::Expected(StrContextValue::Description(reason)),
)
}

pub fn parse(input: &mut &str) -> ModalResult<DateTimeBuilder> {
/// Parse a sequence of items.
///
/// Grammar:
///
/// ```ebnf
/// items = item, { space, item } ;
/// ```
fn parse_items(input: &mut &str) -> ModalResult<DateTimeBuilder> {
let mut builder = DateTimeBuilder::new();

loop {
match parse_one.parse_next(input) {
match parse_item.parse_next(input) {
Ok(item) => match item {
Item::Timestamp(ts) => {
builder = builder
Expand Down Expand Up @@ -147,6 +157,38 @@ pub fn parse(input: &mut &str) -> ModalResult<DateTimeBuilder> {
Ok(builder)
}

/// Parse a timestamp.
///
/// From the GNU docs:
///
/// (Timestamp) Such a number cannot be combined with any other date item, as it
/// specifies a complete timestamp.
fn parse_timestamp(input: &mut &str) -> ModalResult<DateTimeBuilder> {
trace(
"parse_timestamp",
terminated(epoch::parse.map(Item::Timestamp), eof),
)
.verify_map(|ts: Item| {
if let Item::Timestamp(ts) = ts {
DateTimeBuilder::new().set_timestamp(ts).ok()
} else {
None
}
})
.parse_next(input)
}

/// Parse a date and time string.
///
/// Grammar:
///
/// ```ebnf
/// date_time = timestamp | items ;
/// ```
pub(crate) fn parse(input: &mut &str) -> ModalResult<DateTimeBuilder> {
trace("parse", alt((parse_timestamp, parse_items))).parse_next(input)
}

pub(crate) fn at_date(
builder: DateTimeBuilder,
base: DateTime<FixedOffset>,
Expand Down Expand Up @@ -287,6 +329,14 @@ mod tests {
let result = parse(&mut "2025-05-19 abcdef");
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("unexpected input"));

let result = parse(&mut "@1690466034 2025-05-19");
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("unexpected input"));

let result = parse(&mut "2025-05-19 @1690466034");
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("unexpected input"));
}

#[test]
Expand Down