Skip to content

Commit

Permalink
Extend string parsing support for Date32 to encompass the timestamp f…
Browse files Browse the repository at this point in the history
…ormat
  • Loading branch information
gruuya committed Jan 5, 2024
1 parent 8fda518 commit fdfddd1
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions arrow-cast/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,11 @@ const ERR_NANOSECONDS_NOT_SUPPORTED: &str = "The dates that can be represented a

fn parse_date(string: &str) -> Option<NaiveDate> {
if string.len() > 10 {
return None;
}
// Try to parse as datetime and return just the date part
return string_to_datetime(&Utc, string)
.map(|dt| dt.date_naive())
.ok();
};
let mut digits = [0; 10];
let mut mask = 0;

Expand Down Expand Up @@ -1488,10 +1491,13 @@ mod tests {
"2020-9-08",
"2020-12-1",
"1690-2-5",
"2020-09-08 01:02:03",
];
for case in cases {
let v = date32_to_datetime(Date32Type::parse(case).unwrap()).unwrap();
let expected: NaiveDate = case.parse().unwrap();
let expected = NaiveDate::parse_from_str(case, "%Y-%m-%d")
.or(NaiveDate::parse_from_str(case, "%Y-%m-%d %H:%M:%S"))
.unwrap();
assert_eq!(v.date(), expected);
}

Expand All @@ -1503,6 +1509,11 @@ mod tests {
"2020-09-08-03",
"2020--04-03",
"2020--",
"2020-09-08 01",
"2020-09-08 01:02",
"2020-09-08 01-02-03",
"2020-9-8 01:02:03",
"2020-09-08 1:2:3",
];
for case in err_cases {
assert_eq!(Date32Type::parse(case), None);
Expand Down

0 comments on commit fdfddd1

Please sign in to comment.