Skip to content

Commit 3b74161

Browse files
committed
Add time to parsed date after parsing not before
Change the way dates without times are parsed by just parsing the date as-is and applying the placeholder time (00:00:00) after parsing instead of before.
1 parent 65c88f3 commit 3b74161

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/lib.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ mod parse_time_only_str;
2121
mod parse_weekday;
2222

2323
use chrono::{
24-
DateTime, Datelike, Duration, FixedOffset, Local, LocalResult, MappedLocalTime, NaiveDateTime,
25-
TimeZone, Timelike,
24+
DateTime, Datelike, Duration, FixedOffset, Local, LocalResult, MappedLocalTime, NaiveDate,
25+
NaiveDateTime, TimeZone, Timelike,
2626
};
2727

2828
use parse_relative_time::parse_relative_time_at_date;
@@ -297,13 +297,12 @@ pub fn parse_datetime_at_date<S: AsRef<str> + Clone>(
297297
}
298298
}
299299

300-
let ts = s.as_ref().to_owned() + " 0000";
301300
// Parse date only formats - assume midnight local timezone
302301
for (fmt, n) in format::PATTERNS_DATE_NO_TZ {
303-
if ts.len() >= n + 5 {
304-
let f = fmt.to_owned() + " %H%M";
305-
if let Ok(parsed) = NaiveDateTime::parse_from_str(&ts[0..n + 5], &f) {
306-
if let Ok(dt) = naive_dt_to_fixed_offset(date, parsed) {
302+
if s.as_ref().len() >= n {
303+
if let Ok(parsed) = NaiveDate::parse_from_str(&s.as_ref()[0..n], fmt) {
304+
let datetime = parsed.and_hms_opt(0, 0, 0).unwrap();
305+
if let Ok(dt) = naive_dt_to_fixed_offset(date, datetime) {
307306
return Ok(dt);
308307
}
309308
}

0 commit comments

Comments
 (0)