Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved issue #53 - Wrong order of day and month in all dot-family date formats #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -233,4 +232,4 @@ in GitHub Actions:

```shell
make release
```
```
9 changes: 5 additions & 4 deletions dateparser/benches/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}

Expand All @@ -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);
34 changes: 15 additions & 19 deletions dateparser/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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<Result<DateTime<Utc>>> {
// 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<Result<DateTime<Utc>>> {
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;
Expand All @@ -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())
Expand Down Expand Up @@ -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)
Expand All @@ -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]
Expand Down Expand Up @@ -1797,4 +1793,4 @@ mod tests {
}
assert!(parse.chinese_ymd("not-date-time").is_none());
}
}
}