Skip to content

Commit

Permalink
use chrono add/sub months (#3132)
Browse files Browse the repository at this point in the history
* use cargo add/sub months

* update all chrono versions

* clippy
  • Loading branch information
waitingkuo committed Nov 21, 2022
1 parent 475e079 commit e1b5657
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 77 deletions.
2 changes: 1 addition & 1 deletion arrow-array/Cargo.toml
Expand Up @@ -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"] }
Expand Down
82 changes: 14 additions & 68 deletions arrow-array/src/delta.rs
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion arrow-cast/Cargo.toml
Expand Up @@ -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"] }

Expand Down
2 changes: 1 addition & 1 deletion arrow-csv/Cargo.toml
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion arrow-json/Cargo.toml
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions arrow/Cargo.toml
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion object_store/Cargo.toml
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion parquet/Cargo.toml
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion parquet_derive_test/Cargo.toml
Expand Up @@ -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" ] }

0 comments on commit e1b5657

Please sign in to comment.