You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2023/01/09 21:59:12 UTC

[arrow-rs] branch master updated: Fix: Added support to cast string without time (#3494)

This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new fb36dd980 Fix: Added support to cast string without time (#3494)
fb36dd980 is described below

commit fb36dd980b398deabe5547af114982326b97e078
Author: Wenjun L <47...@users.noreply.github.com>
AuthorDate: Mon Jan 9 22:59:07 2023 +0100

    Fix: Added support to cast string without time (#3494)
    
    * Fix: Added support casting strings without time to timestamp
    
    * Fix: Added support casting strings without time to timestamp
    
    Co-authored-by: Wenjun Liu <we...@gmail.com>
---
 arrow-cast/src/parse.rs | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/arrow-cast/src/parse.rs b/arrow-cast/src/parse.rs
index 6de336351..e885ec5b6 100644
--- a/arrow-cast/src/parse.rs
+++ b/arrow-cast/src/parse.rs
@@ -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
@@ -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
+    if let Ok(dt) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
+        if let Some(ts) = dt.and_hms_opt(0, 0, 0) {
+            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
@@ -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]