Skip to content

Commit 51e2947

Browse files
authored
Merge pull request #99 from jfinkels/parse-dates-with-slashes
Parse calendar dates with slashes, like 2/29/1996
2 parents fa53ec7 + 06aac3f commit 51e2947

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
@@ -63,6 +63,10 @@ impl From<RegexError> for ParseDateTimeError {
6363
mod format {
6464
pub const ISO_8601: &str = "%Y-%m-%d";
6565
pub const ISO_8601_NO_SEP: &str = "%Y%m%d";
66+
// US format for calendar date items:
67+
// https://www.gnu.org/software/coreutils/manual/html_node/Calendar-date-items.html
68+
pub const MMDDYYYY_SLASH: &str = "%m/%d/%Y";
69+
pub const MMDDYY_SLASH: &str = "%m/%d/%y";
6670
pub const POSIX_LOCALE: &str = "%a %b %e %H:%M:%S %Y";
6771
pub const YYYYMMDDHHMM_DOT_SS: &str = "%Y%m%d%H%M.%S";
6872
pub const YYYYMMDDHHMMSS: &str = "%Y-%m-%d %H:%M:%S.%f";
@@ -208,7 +212,12 @@ pub fn parse_datetime_at_date<S: AsRef<str> + Clone>(
208212

209213
let ts = s.as_ref().to_owned() + " 0000";
210214
// Parse date only formats - assume midnight local timezone
211-
for fmt in [format::ISO_8601, format::ISO_8601_NO_SEP] {
215+
for fmt in [
216+
format::ISO_8601,
217+
format::ISO_8601_NO_SEP,
218+
format::MMDDYYYY_SLASH,
219+
format::MMDDYY_SLASH,
220+
] {
212221
let f = fmt.to_owned() + " %H%M";
213222
if let Ok(parsed) = NaiveDateTime::parse_from_str(&ts, &f) {
214223
if let Ok(dt) = naive_dt_to_fixed_offset(date, parsed) {
@@ -339,7 +348,7 @@ mod tests {
339348
}
340349

341350
#[cfg(test)]
342-
mod formats {
351+
mod calendar_date_items {
343352
use crate::parse_datetime;
344353
use chrono::{DateTime, Local, TimeZone};
345354

@@ -352,6 +361,10 @@ mod tests {
352361
assert_eq!(Ok(expected), parse_datetime("1987-5-07"));
353362
assert_eq!(Ok(expected), parse_datetime("1987-05-7"));
354363
assert_eq!(Ok(expected), parse_datetime("1987-5-7"));
364+
assert_eq!(Ok(expected), parse_datetime("5/7/1987"));
365+
assert_eq!(Ok(expected), parse_datetime("5/07/1987"));
366+
assert_eq!(Ok(expected), parse_datetime("05/7/1987"));
367+
assert_eq!(Ok(expected), parse_datetime("05/07/1987"));
355368
}
356369
}
357370

0 commit comments

Comments
 (0)