Skip to content

Commit 31ee6d5

Browse files
committed
Parse calendar dates with slashes, like 2/29/1996
1 parent 00dc90d commit 31ee6d5

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ impl From<RegexError> for ParseDateTimeError {
6262
mod format {
6363
pub const ISO_8601: &str = "%Y-%m-%d";
6464
pub const ISO_8601_NO_SEP: &str = "%Y%m%d";
65+
// US format for calendar date items:
66+
// https://www.gnu.org/software/coreutils/manual/html_node/Calendar-date-items.html
67+
pub const MMDDYYYY_SLASH: &str = "%m/%d/%Y";
68+
pub const MMDDYY_SLASH: &str = "%m/%d/%y";
6569
pub const POSIX_LOCALE: &str = "%a %b %e %H:%M:%S %Y";
6670
pub const YYYYMMDDHHMM_DOT_SS: &str = "%Y%m%d%H%M.%S";
6771
pub const YYYYMMDDHHMMSS: &str = "%Y-%m-%d %H:%M:%S.%f";
@@ -206,7 +210,12 @@ pub fn parse_datetime_at_date<S: AsRef<str> + Clone>(
206210

207211
let ts = s.as_ref().to_owned() + " 0000";
208212
// Parse date only formats - assume midnight local timezone
209-
for fmt in [format::ISO_8601, format::ISO_8601_NO_SEP] {
213+
for fmt in [
214+
format::ISO_8601,
215+
format::ISO_8601_NO_SEP,
216+
format::MMDDYYYY_SLASH,
217+
format::MMDDYY_SLASH,
218+
] {
210219
let f = fmt.to_owned() + " %H%M";
211220
if let Ok(parsed) = NaiveDateTime::parse_from_str(&ts, &f) {
212221
if let Ok(dt) = naive_dt_to_fixed_offset(date, parsed) {
@@ -324,7 +333,7 @@ mod tests {
324333
}
325334

326335
#[cfg(test)]
327-
mod formats {
336+
mod calendar_date_items {
328337
use crate::parse_datetime;
329338
use chrono::{DateTime, Local, TimeZone};
330339

@@ -337,6 +346,10 @@ mod tests {
337346
assert_eq!(Ok(expected), parse_datetime("1987-5-07"));
338347
assert_eq!(Ok(expected), parse_datetime("1987-05-7"));
339348
assert_eq!(Ok(expected), parse_datetime("1987-5-7"));
349+
assert_eq!(Ok(expected), parse_datetime("5/7/1987"));
350+
assert_eq!(Ok(expected), parse_datetime("5/07/1987"));
351+
assert_eq!(Ok(expected), parse_datetime("05/7/1987"));
352+
assert_eq!(Ok(expected), parse_datetime("05/07/1987"));
340353
}
341354
}
342355

0 commit comments

Comments
 (0)