diff --git a/README.md b/README.md index 5d1716d..c6acc12 100644 --- a/README.md +++ b/README.md @@ -150,13 +150,12 @@ More about this crate on [Docs.rs][1] and in [examples][2] folder // yyyy/mm/dd "2014/3/31", "2014/03/31", -// mm.dd.yyyy -"3.31.2014", -"03.31.2014", -"08.21.71", -// yyyy.mm.dd -"2014.03.30", -"2014.03", +// dd.mm.yyyy +"31.3.2014", +"31.03.2014", +"21.08.71", +// yyyy.dd.mm +"2014.30.03", // yymmdd hh:mm:ss mysql log "171113 14:14:20", // chinese yyyy mm dd hh mm ss @@ -233,4 +232,4 @@ in GitHub Actions: ```shell make release -``` +``` \ No newline at end of file diff --git a/dateparser/benches/parse.rs b/dateparser/benches/parse.rs index 0bce38e..fb2245e 100644 --- a/dateparser/benches/parse.rs +++ b/dateparser/benches/parse.rs @@ -25,10 +25,11 @@ lazy_static! { "08/21/71", // slash_mdy "2012/03/19 10:11:59", // slash_ymd_hms "2014/3/31", // slash_ymd - "2014.03.30", // dot_mdy_or_ymd + "2014.30.3", // dot_mdy_or_ymd + "28.09.2023", // dot_mdy_or_ymd "171113 14:14:20", // mysql_log_timestamp - "2014年04月08日11时25分18秒", // chinese_ymd_hms - "2014年04月08日", // chinese_ymd + "2014年04月08日11时25分18秒", // chinese_ymd_hms + "2014年04月08日", // chinese_ymd ]; } @@ -55,4 +56,4 @@ fn bench_parse_each(c: &mut Criterion) { } criterion_group!(benches, bench_parse_all, bench_parse_each); -criterion_main!(benches); +criterion_main!(benches); \ No newline at end of file diff --git a/dateparser/src/datetime.rs b/dateparser/src/datetime.rs index cffaca5..1d8f266 100644 --- a/dateparser/src/datetime.rs +++ b/dateparser/src/datetime.rs @@ -34,7 +34,7 @@ where .or_else(|| self.slash_mdy_family(input)) .or_else(|| self.hyphen_mdy_family(input)) .or_else(|| self.slash_ymd_family(input)) - .or_else(|| self.dot_mdy_or_ymd(input)) + .or_else(|| self.dot_dmy_or_ydm(input)) .or_else(|| self.mysql_log_timestamp(input)) .or_else(|| self.chinese_ymd_family(input)) .unwrap_or_else(|| Err(anyhow!("{} did not match any formats.", input))) @@ -779,16 +779,15 @@ where .map(Ok) } - // mm.dd.yyyy - // - 3.31.2014 - // - 03.31.2014 - // - 08.21.71 - // yyyy.mm.dd - // - 2014.03.30 - // - 2014.03 - fn dot_mdy_or_ymd(&self, input: &str) -> Option>> { + // dd.mm.yyyy + // - 31.3.2014 + // - 31.03.2014 + // - 21.08.71 + // yyyy.dd.mm + // - 2014.30.03 + fn dot_dmy_or_ydm(&self, input: &str) -> Option>> { lazy_static! { - static ref RE: Regex = Regex::new(r"[0-9]{1,4}.[0-9]{1,4}[0-9]{1,4}").unwrap(); + static ref RE: Regex = Regex::new(r"[0-9]{1,4}.[0-9]{1,2}.[0-9]{1,4}").unwrap(); } if !RE.is_match(input) { return None; @@ -800,12 +799,9 @@ where None => Utc::now().with_timezone(self.tz).time(), }; - NaiveDate::parse_from_str(input, "%m.%d.%y") - .or_else(|_| NaiveDate::parse_from_str(input, "%m.%d.%Y")) - .or_else(|_| NaiveDate::parse_from_str(input, "%Y.%m.%d")) - .or_else(|_| { - NaiveDate::parse_from_str(&format!("{}.{}", input, Utc::now().day()), "%Y.%m.%d") - }) + NaiveDate::parse_from_str(input, "%d.%m.%y") + .or_else(|_| NaiveDate::parse_from_str(input, "%d.%m.%Y")) + .or_else(|_| NaiveDate::parse_from_str(input, "%Y.%d.%m")) .ok() .map(|parsed| parsed.and_time(time)) .and_then(|datetime| self.tz.from_local_datetime(&datetime).single()) @@ -1718,7 +1714,7 @@ mod tests { for &(input, want) in test_cases.iter() { assert_eq!( parse - .dot_mdy_or_ymd(input) + .dot_dmy_or_ydm(input) .unwrap() .unwrap() .trunc_subsecs(0) @@ -1729,7 +1725,7 @@ mod tests { input ) } - assert!(parse.dot_mdy_or_ymd("not-date-time").is_none()); + assert!(parse.dot_dmy_or_ydm("not-date-time").is_none()); } #[test] @@ -1797,4 +1793,4 @@ mod tests { } assert!(parse.chinese_ymd("not-date-time").is_none()); } -} +} \ No newline at end of file