Skip to content

Commit

Permalink
Merge pull request #2 from mikewzr/remove-comma-in-date
Browse files Browse the repository at this point in the history
Remove comma in date with month literal, as it is ambigous to date/time separator
  • Loading branch information
devinvs authored Dec 27, 2023
2 parents 8768fd6 + b74e232 commit 1b3002e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
58 changes: 53 additions & 5 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@ impl Date {
let (day, t) = Num::parse(&l[tokens..])?;
tokens += t;

if l.get(tokens) == Some(&Lexeme::Comma) {
tokens += 1;
}

if let Some((year, t)) = Num::parse(&l[tokens..]) {
tokens += t;
return Some((Self::MonthDayYear(month, day, year), tokens));
Expand Down Expand Up @@ -1204,6 +1200,32 @@ fn test_year_before() {
assert_eq!(date.day(), 5);
}

#[test]
fn test_month_literals_with_time_and_year() {
use chrono::Timelike;

let lexemes = vec![
Lexeme::February,
Lexeme::Num(16),
Lexeme::Num(2022),
Lexeme::Comma,
Lexeme::Num(5),
Lexeme::Colon,
Lexeme::Num(27),
Lexeme::PM,
];

let (date, t) = DateTime::parse(lexemes.as_slice()).unwrap();
let date = date.to_chrono(Local::now().naive_local().time()).unwrap();

assert_eq!(t, 8);
assert_eq!(date.year(), 2022);
assert_eq!(date.month(), 2);
assert_eq!(date.day(), 16);
assert_eq!(date.hour(), 17);
assert_eq!(date.minute(), 27);
}

#[test]
fn test_slash_separated_date() {
let lexemes = vec![
Expand All @@ -1213,15 +1235,41 @@ fn test_slash_separated_date() {
Lexeme::Slash,
Lexeme::Num(2023),
];

let (date, t) = DateTime::parse(lexemes.as_slice()).unwrap();
let date = date.to_chrono(Local::now().naive_local().time()).unwrap();

assert_eq!(t, 5);
assert_eq!(date.year(), 2023);
assert_eq!(date.month(), 5);
assert_eq!(date.day(), 12);
}

#[test]
fn test_month_literals_with_time_and_no_year() {
use chrono::Timelike;

let lexemes = vec![
Lexeme::February,
Lexeme::Num(16),
Lexeme::Comma,
Lexeme::Num(5),
Lexeme::Colon,
Lexeme::Num(27),
Lexeme::PM,
];
let (date, t) = DateTime::parse(lexemes.as_slice()).unwrap();
let date = date.to_chrono(Local::now().naive_local().time()).unwrap();
let current_year = Local::now().naive_local().year();

assert_eq!(t, 7);
assert_eq!(date.year(), current_year);
assert_eq!(date.month(), 2);
assert_eq!(date.day(), 16);
assert_eq!(date.hour(), 17);
assert_eq!(date.minute(), 27);
}

#[test]
fn test_slash_separated_invalid_month() {
let lexemes = vec![
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
//! | <num> - <num> - <num>
//! | <num> . <num> . <num>
//! | <month> <num> <num>
//! | <month> <num> , <num>
//! | <relative_specifier> <unit>
//! | <relative_specifier> <weekday>
//! | <weekday>
Expand Down

0 comments on commit 1b3002e

Please sign in to comment.