Skip to content
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ rust-version = "1.71.1"
[dependencies]
winnow = "1.0.0"
num-traits = "0.2.19"
jiff = { version = "0.2.15", default-features = false, features = ["tz-system", "tzdb-bundle-platform", "tzdb-zoneinfo"] }
jiff = { version = "0.2.43", default-features = false, features = ["tz-system", "tzdb-bundle-platform", "tzdb-zoneinfo"] }
jiff-core = "0.1.5"

[dev-dependencies]
rstest = "0.28"
Expand Down
106 changes: 75 additions & 31 deletions src/items/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@

use winnow::{
ascii::alpha1,
combinator::{alt, eof, opt, preceded, terminated},
error::ErrMode,
combinator::{alt, dispatch, eof, fail, not, opt, peek, preceded, terminated},
error::{ContextError, ErrMode},
stream::AsChar,
token::take_while,
token::{any, one_of, take_while},
ModalResult, Parser,
};

use super::{
primitive::{ctx_err, dec_uint, multispace1, s},
primitive::{ctx_err, dec_uint, dec_uint_str, multispace1, s},
year::{year_from_str, year_str},
};

Expand Down Expand Up @@ -130,13 +130,30 @@ impl TryFrom<Date> for jiff::civil::Date {
/// Parse a date that is not in ISO format. ISO dates are parsed by [`iso1`]
/// and [`iso2`], which the caller tries first.
pub(super) fn parse_non_iso(input: &mut &str) -> ModalResult<Date> {
alt((dotted, us, literal1, literal2, literal3)).parse_next(input)
dispatch! { s(peek(any));
'0'..='9' => numeric,
'a'..='z' => literal_month_first,
_ => fail,
}
.parse_next(input)
}

/// Parse the European `DD.MM.[YYYY]` spelling.
fn dotted(input: &mut &str) -> ModalResult<Date> {
let (day, _, month, _, year) = (
s(dec_uint),
fn numeric(input: &mut &str) -> ModalResult<Date> {
let first = s(dec_uint_str).parse_next(input)?;

dispatch! { s(peek(any));
'.' => move |input: &mut &str| dotted_after(first, input),
'/' => move |input: &mut &str| us_after(first, input),
'-' | 'a'..='z' => move |input: &mut &str| literal1_after(first, input),
_ => fail,
}
.parse_next(input)
}

/// Parse the European `DD.MM.[YYYY]` spelling after the shared numeric prefix.
fn dotted_after(first: &str, input: &mut &str) -> ModalResult<Date> {
let day = day_from_str(first)?;
let (_, month, _, year) = (
s('.'),
s(dec_uint),
s('.'),
Expand Down Expand Up @@ -181,49 +198,70 @@ pub(super) fn iso2(input: &mut &str) -> ModalResult<Date> {
.map_err(|e| ErrMode::Backtrack(ctx_err(e)))
}

/// Parse `[year]/[month]/[day]` or `[month]/[day]/[year]` or `[month]/[day]`.
fn us(input: &mut &str) -> ModalResult<Date> {
let (s1, _, n, s2) = (
s(take_while(1.., AsChar::is_dec_digit)),
/// Parse either of the compact or separated ISO date forms.
///
/// Compact dates contain at least five consecutive digits and do not have a
/// possible ISO separator after that digit run. In that case, skip the failed
/// `iso1` attempt. Inputs that could use whitespace/comments before a separator
/// retain the original alternative ordering for compatibility.
pub(super) fn iso(input: &mut &str) -> ModalResult<Date> {
if compact_iso_candidate(input) {
iso2.parse_next(input)
} else {
alt((iso1, iso2)).parse_next(input)
}
}

fn compact_iso_candidate(input: &str) -> bool {
(
take_while(5.., AsChar::is_dec_digit),
peek(not(one_of::<_, _, ErrMode<ContextError>>([
' ', '\t', '\n', '\x0B', '\x0C', '\r', '(', '+', '-',
]))),
)
.parse_peek(input)
.is_ok()
}

fn us_after(first: &str, input: &mut &str) -> ModalResult<Date> {
let (_, n, s2) = (
s('/'),
s(dec_uint),
opt(preceded(s('/'), s(take_while(1.., AsChar::is_dec_digit)))),
)
.parse_next(input)?;

match s2 {
Some(s2) if s1.len() >= 4 => {
Some(s2) if first.len() >= 4 => {
// [year]/[month]/[day]
//
// GNU quirk: interpret as [year]/[month]/[day] if the first part is at
// least 4 characters long.
let day = day_from_str(s2)?;
(s1, n, day)
(first, n, day)
.try_into()
.map_err(|e| ErrMode::Backtrack(ctx_err(e)))
}
Some(s2) => {
// [month]/[day]/[year]
let month = month_from_str(s1)?;
let month = month_from_str(first)?;
(s2, month, n)
.try_into()
.map_err(|e| ErrMode::Backtrack(ctx_err(e)))
}
None => {
// [month]/[day]
let month = month_from_str(s1)?;
let month = month_from_str(first)?;
(month, n)
.try_into()
.map_err(|e| ErrMode::Backtrack(ctx_err(e)))
}
}
}

/// Parse `14 November 2022`, `14 Nov 2022`, "14nov2022", "14-nov-2022",
/// "14-nov2022", "14nov-2022".
fn literal1(input: &mut &str) -> ModalResult<Date> {
let (day, _, month, year) = (
s(dec_uint),
fn literal1_after(first: &str, input: &mut &str) -> ModalResult<Date> {
let day = day_from_str(first)?;
let (_, month, year) = (
opt(s('-')),
s(literal_month),
opt(terminated(
Expand All @@ -244,10 +282,19 @@ fn literal1(input: &mut &str) -> ModalResult<Date> {
}
}

/// Parse `November 14, 2022`, `Nov 14, 2022`, and `Nov 14 2022`.
fn literal2(input: &mut &str) -> ModalResult<Date> {
let (month, day, year) = (
s(literal_month),
fn literal_month_first(input: &mut &str) -> ModalResult<Date> {
let month = s(literal_month).parse_next(input)?;

dispatch! { s(peek(any));
'-' => move |input: &mut &str| literal3_after(month, input),
'0'..='9' => move |input: &mut &str| literal2_after(month, input),
_ => fail,
}
.parse_next(input)
}

fn literal2_after(month: u8, input: &mut &str) -> ModalResult<Date> {
let (day, year) = (
s(dec_uint),
opt(terminated(
preceded(
Expand All @@ -273,11 +320,8 @@ fn literal2(input: &mut &str) -> ModalResult<Date> {
}
}

/// Parse `November-14-2022` and `Nov-14-2022`. Unlike `literal2`, the year is mandatory
/// to match the behavior of GNU `date`.
fn literal3(input: &mut &str) -> ModalResult<Date> {
let (month, _, day, _, year) =
(s(literal_month), s('-'), s(dec_uint), s('-'), year_str).parse_next(input)?;
fn literal3_after(month: u8, input: &mut &str) -> ModalResult<Date> {
let (_, day, _, year) = (s('-'), s(dec_uint), s('-'), year_str).parse_next(input)?;

// Map err to Backtrack instead of Cut to avoid early termination of parsing
(year, month, day)
Expand Down
10 changes: 5 additions & 5 deletions src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use crate::ParsedDateTime;
use jiff::Zoned;
use primitive::space;
use winnow::{
combinator::{alt, eof, preceded, repeat_till, terminated, trace},
combinator::{alt, eof, opt, preceded, repeat_till, terminated, trace},
error::{AddContext, ContextError, ErrMode, StrContext, StrContextValue},
stream::Stream,
ModalResult, Parser,
Expand Down Expand Up @@ -216,7 +216,7 @@ fn parse(input: &mut &str) -> ModalResult<DateTimeBuilder> {
/// - Trailing input (aside from optional whitespaces) is rejected.
fn parse_timestamp(input: &mut &str) -> ModalResult<DateTimeBuilder> {
// Parse and ignore an optional leading timezone rule.
let _ = timezone::parse(input);
let _ = opt(timezone::parse).parse_next(input)?;

trace(
"parse_timestamp",
Expand All @@ -238,7 +238,7 @@ fn parse_timestamp(input: &mut &str) -> ModalResult<DateTimeBuilder> {
/// - Trailing input (aside from optional whitespaces) is rejected.
fn parse_items(input: &mut &str) -> ModalResult<DateTimeBuilder> {
// Parse and consume an optional leading timezone rule.
let tz = timezone::parse(input).map(Item::TimeZone);
let tz = opt(timezone::parse).parse_next(input)?.map(Item::TimeZone);

// Convert input to lowercase for case-insensitive parsing.
let lower: Cow<str> = if input.bytes().any(|b| b.is_ascii_uppercase()) {
Expand All @@ -255,7 +255,7 @@ fn parse_items(input: &mut &str) -> ModalResult<DateTimeBuilder> {
)
.parse_next(input)?;

if let Ok(tz) = tz {
if let Some(tz) = tz {
items.push(tz);
}

Expand All @@ -267,7 +267,7 @@ fn parse_item(input: &mut &str) -> ModalResult<Item> {
// An ISO date is either the start of a combined date and time item or a
// date item on its own. Parse it once instead of once per alternative.
let start = input.checkpoint();
if let Ok(date) = alt((date::iso1, date::iso2)).parse_next(input) {
if let Ok(date) = date::iso.parse_next(input) {
let after_date = input.checkpoint();
match combined::parse_time_after_date(input, date.clone()) {
Ok(dt) => return Ok(Item::DateTime(dt)),
Expand Down
Loading
Loading