You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by tu...@apache.org on 2022/11/21 14:15:44 UTC

[arrow-rs] branch master updated: use chrono add/sub months (#3132)

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

tustvold 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 e1b5657eb use chrono add/sub months (#3132)
e1b5657eb is described below

commit e1b5657eb1206ce67eb079f6e72615982a70480a
Author: Wei-Ting Kuo <wa...@gmail.com>
AuthorDate: Mon Nov 21 22:15:38 2022 +0800

    use chrono add/sub months (#3132)
    
    * use cargo add/sub months
    
    * update all chrono versions
    
    * clippy
---
 arrow-array/Cargo.toml         |  2 +-
 arrow-array/src/delta.rs       | 82 ++++++++----------------------------------
 arrow-cast/Cargo.toml          |  2 +-
 arrow-csv/Cargo.toml           |  2 +-
 arrow-json/Cargo.toml          |  2 +-
 arrow/Cargo.toml               |  4 +--
 object_store/Cargo.toml        |  2 +-
 parquet/Cargo.toml             |  2 +-
 parquet_derive_test/Cargo.toml |  2 +-
 9 files changed, 23 insertions(+), 77 deletions(-)

diff --git a/arrow-array/Cargo.toml b/arrow-array/Cargo.toml
index 816843d31..d0c556a00 100644
--- a/arrow-array/Cargo.toml
+++ b/arrow-array/Cargo.toml
@@ -48,7 +48,7 @@ ahash = { version = "0.8", default-features = false, features = ["runtime-rng"]
 arrow-buffer = { version = "27.0.0", path = "../arrow-buffer" }
 arrow-schema = { version = "27.0.0", path = "../arrow-schema" }
 arrow-data = { version = "27.0.0", path = "../arrow-data" }
-chrono = { version = "0.4", default-features = false, features = ["clock"] }
+chrono = { version = "0.4.23", default-features = false, features = ["clock"] }
 chrono-tz = { version = "0.8", optional = true }
 num = { version = "0.4", default-features = false, features = ["std"] }
 half = { version = "2.1", default-features = false, features = ["num-traits"] }
diff --git a/arrow-array/src/delta.rs b/arrow-array/src/delta.rs
index b9b7a11e2..029168242 100644
--- a/arrow-array/src/delta.rs
+++ b/arrow-array/src/delta.rs
@@ -23,86 +23,32 @@
 // Copied from chronoutil crate
 
 //! Contains utility functions for shifting Date objects.
-use chrono::Datelike;
-
-/// Returns true if the year is a leap-year, as naively defined in the Gregorian calendar.
-#[inline]
-pub(crate) fn is_leap_year(year: i32) -> bool {
-    year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
-}
-
-// If the day lies within the month, this function has no effect. Otherwise, it shifts
-// day backwards to the final day of the month.
-// XXX: No attempt is made to handle days outside the 1-31 range.
-#[inline]
-fn normalise_day(year: i32, month: u32, day: u32) -> u32 {
-    if day <= 28 {
-        day
-    } else if month == 2 {
-        28 + is_leap_year(year) as u32
-    } else if day == 31 && (month == 4 || month == 6 || month == 9 || month == 11) {
-        30
-    } else {
-        day
-    }
-}
+use chrono::{Datelike, Months};
+use std::cmp::Ordering;
 
 /// Shift a date by the given number of months.
-/// Ambiguous month-ends are shifted backwards as necessary.
-pub(crate) fn shift_months<D: Datelike>(date: D, months: i32) -> D {
-    let mut year = date.year() + (date.month() as i32 + months) / 12;
-    let mut month = (date.month() as i32 + months) % 12;
-    let mut day = date.day();
-
-    if month < 1 {
-        year -= 1;
-        month += 12;
-    }
-
-    day = normalise_day(year, month as u32, day);
-
-    // This is slow but guaranteed to succeed (short of interger overflow)
-    if day <= 28 {
-        date.with_day(day)
-            .unwrap()
-            .with_month(month as u32)
-            .unwrap()
-            .with_year(year)
-            .unwrap()
-    } else {
-        date.with_day(1)
-            .unwrap()
-            .with_month(month as u32)
-            .unwrap()
-            .with_year(year)
-            .unwrap()
-            .with_day(day)
-            .unwrap()
+pub(crate) fn shift_months<
+    D: Datelike
+        + std::ops::Add<chrono::Months, Output = D>
+        + std::ops::Sub<chrono::Months, Output = D>,
+>(
+    date: D,
+    months: i32,
+) -> D {
+    match months.cmp(&0) {
+        Ordering::Equal => date,
+        Ordering::Greater => date + Months::new(months as u32),
+        Ordering::Less => date - Months::new(-months as u32),
     }
 }
 
 #[cfg(test)]
 mod tests {
-    use std::collections::HashSet;
 
     use chrono::naive::{NaiveDate, NaiveDateTime, NaiveTime};
 
     use super::*;
 
-    #[test]
-    fn test_leap_year_cases() {
-        let _leap_years: Vec<i32> = vec![
-            1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952,
-            1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004,
-            2008, 2012, 2016, 2020,
-        ];
-        let leap_years_1900_to_2020: HashSet<i32> = _leap_years.into_iter().collect();
-
-        for year in 1900..2021 {
-            assert_eq!(is_leap_year(year), leap_years_1900_to_2020.contains(&year))
-        }
-    }
-
     #[test]
     fn test_shift_months() {
         let base = NaiveDate::from_ymd_opt(2020, 1, 31).unwrap();
diff --git a/arrow-cast/Cargo.toml b/arrow-cast/Cargo.toml
index fe3f5e257..5f52a3283 100644
--- a/arrow-cast/Cargo.toml
+++ b/arrow-cast/Cargo.toml
@@ -43,7 +43,7 @@ arrow-buffer = { version = "27.0.0", path = "../arrow-buffer" }
 arrow-data = { version = "27.0.0", path = "../arrow-data" }
 arrow-schema = { version = "27.0.0", path = "../arrow-schema" }
 arrow-select = { version = "27.0.0", path = "../arrow-select" }
-chrono = { version = "0.4", default-features = false, features = ["clock"] }
+chrono = { version = "0.4.23", default-features = false, features = ["clock"] }
 num = { version = "0.4", default-features = false, features = ["std"] }
 lexical-core = { version = "^0.8", default-features = false, features = ["write-integers", "write-floats", "parse-integers", "parse-floats"] }
 
diff --git a/arrow-csv/Cargo.toml b/arrow-csv/Cargo.toml
index 81c97c684..fc4c177bd 100644
--- a/arrow-csv/Cargo.toml
+++ b/arrow-csv/Cargo.toml
@@ -43,7 +43,7 @@ arrow-buffer = { version = "27.0.0", path = "../arrow-buffer" }
 arrow-cast = { version = "27.0.0", path = "../arrow-cast" }
 arrow-data = { version = "27.0.0", path = "../arrow-data" }
 arrow-schema = { version = "27.0.0", path = "../arrow-schema" }
-chrono = { version = "0.4", default-features = false, features = ["clock"] }
+chrono = { version = "0.4.23", default-features = false, features = ["clock"] }
 csv = { version = "1.1", default-features = false }
 lazy_static = { version = "1.4", default-features = false }
 lexical-core = { version = "^0.8", default-features = false }
diff --git a/arrow-json/Cargo.toml b/arrow-json/Cargo.toml
index dd7064946..3454b4c1d 100644
--- a/arrow-json/Cargo.toml
+++ b/arrow-json/Cargo.toml
@@ -47,7 +47,7 @@ half = { version = "2.1", default-features = false }
 indexmap = { version = "1.9", default-features = false, features = ["std"] }
 num = { version = "0.4", default-features = false, features = ["std"] }
 serde_json = { version = "1.0", default-features = false, features = ["std"] }
-chrono = { version = "0.4", default-features = false, features = ["clock"] }
+chrono = { version = "0.4.23", default-features = false, features = ["clock"] }
 
 [dev-dependencies]
 tempfile = "3.3"
diff --git a/arrow/Cargo.toml b/arrow/Cargo.toml
index 2acad2c17..ab8963b9c 100644
--- a/arrow/Cargo.toml
+++ b/arrow/Cargo.toml
@@ -60,7 +60,7 @@ hashbrown = { version = "0.13", default-features = false }
 regex = { version = "1.5.6", default-features = false, features = ["std", "unicode"] }
 regex-syntax = { version = "0.6.27", default-features = false, features = ["unicode"] }
 packed_simd = { version = "0.3", default-features = false, optional = true, package = "packed_simd_2" }
-chrono = { version = "0.4", default-features = false, features = ["clock"] }
+chrono = { version = "0.4.23", default-features = false, features = ["clock"] }
 comfy-table = { version = "6.0", optional = true, default-features = false }
 pyo3 = { version = "0.17", default-features = false, optional = true }
 multiversion = { version = "0.6.1", default-features = false }
@@ -98,7 +98,7 @@ dyn_arith_dict = []
 chrono-tz = ["arrow-array/chrono-tz"]
 
 [dev-dependencies]
-chrono = { version = "0.4", default-features = false, features = ["clock"] }
+chrono = { version = "0.4.23", default-features = false, features = ["clock"] }
 criterion = { version = "0.4", default-features = false }
 rand = { version = "0.8", default-features = false, features = ["std", "std_rng"] }
 tempfile = { version = "3", default-features = false }
diff --git a/object_store/Cargo.toml b/object_store/Cargo.toml
index fc80cb577..fd7442f9e 100644
--- a/object_store/Cargo.toml
+++ b/object_store/Cargo.toml
@@ -31,7 +31,7 @@ all-features = true
 [dependencies] # In alphabetical order
 async-trait = "0.1.53"
 bytes = "1.0"
-chrono = { version = "0.4", default-features = false, features = ["clock"] }
+chrono = { version = "0.4.23", default-features = false, features = ["clock"] }
 futures = "0.3"
 itertools = "0.10.1"
 parking_lot = { version = "0.12" }
diff --git a/parquet/Cargo.toml b/parquet/Cargo.toml
index fc7c8218a..515da585e 100644
--- a/parquet/Cargo.toml
+++ b/parquet/Cargo.toml
@@ -47,7 +47,7 @@ brotli = { version = "3.3", default-features = false, features = ["std"], option
 flate2 = { version = "1.0", default-features = false, features = ["rust_backend"], optional = true }
 lz4 = { version = "1.23", default-features = false, optional = true }
 zstd = { version = "0.11.1", optional = true, default-features = false }
-chrono = { version = "0.4", default-features = false, features = ["alloc"] }
+chrono = { version = "0.4.23", default-features = false, features = ["alloc"] }
 num = { version = "0.4", default-features = false }
 num-bigint = { version = "0.4", default-features = false }
 base64 = { version = "0.13", default-features = false, features = ["std"], optional = true }
diff --git a/parquet_derive_test/Cargo.toml b/parquet_derive_test/Cargo.toml
index a10d34e86..047e0196c 100644
--- a/parquet_derive_test/Cargo.toml
+++ b/parquet_derive_test/Cargo.toml
@@ -31,4 +31,4 @@ rust-version = "1.62"
 [dependencies]
 parquet = { path = "../parquet", version = "27.0.0", default-features = false }
 parquet_derive = { path = "../parquet_derive", version = "27.0.0", default-features = false }
-chrono = { version="0.4.19", default-features = false, features = [ "clock" ] }
+chrono = { version="0.4.23", default-features = false, features = [ "clock" ] }