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

Fix: Added support to cast string without time #3494

Merged
merged 2 commits into from
Jan 9, 2023
Merged
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
22 changes: 22 additions & 0 deletions arrow-cast/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use chrono::prelude::*;
/// * `1997-01-31T09:26:56.123` # close to RCF3339 but no timezone offset specified
/// * `1997-01-31 09:26:56.123` # close to RCF3339 but uses a space and no timezone offset
/// * `1997-01-31 09:26:56` # close to RCF3339, no fractional seconds
/// * `1997-01-31` # close to RCF3339, only date no time
//
/// Internally, this function uses the `chrono` library for the
/// datetime parsing
Expand Down Expand Up @@ -121,6 +122,14 @@ pub fn string_to_timestamp_nanos(s: &str) -> Result<i64, ArrowError> {
return Ok(ts.timestamp_nanos());
}

// without a timezone specifier as a local time, only date
// Example: 2020-09-08
gaelwjl marked this conversation as resolved.
Show resolved Hide resolved
if let Ok(dt) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
if let Some(ts) = dt.and_hms_opt(0, 0, 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return Ok(ts.timestamp_nanos());
}
}

// Note we don't pass along the error message from the underlying
// chrono parsing because we tried several different format
// strings and we don't know which the user was trying to
Expand Down Expand Up @@ -494,6 +503,19 @@ mod tests {
naive_datetime_whole_secs.timestamp_nanos(),
parse_timestamp("2020-09-08 13:42:29").unwrap()
);

// ensure without time work
// no time, should be the nano second at
// 2020-09-08 0:0:0
let naive_datetime_no_time = NaiveDateTime::new(
NaiveDate::from_ymd_opt(2020, 9, 8).unwrap(),
NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
);

assert_eq!(
naive_datetime_no_time.timestamp_nanos(),
parse_timestamp("2020-09-08").unwrap()
)
}

#[test]
Expand Down