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 clippy by avoiding deprecated functions in chrono #3096

Merged
merged 3 commits into from Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
207 changes: 159 additions & 48 deletions arrow-array/src/delta.rs
Expand Up @@ -105,75 +105,186 @@ mod tests {

#[test]
fn test_shift_months() {
let base = NaiveDate::from_ymd(2020, 1, 31);

assert_eq!(shift_months(base, 0), NaiveDate::from_ymd(2020, 1, 31));
assert_eq!(shift_months(base, 1), NaiveDate::from_ymd(2020, 2, 29));
assert_eq!(shift_months(base, 2), NaiveDate::from_ymd(2020, 3, 31));
assert_eq!(shift_months(base, 3), NaiveDate::from_ymd(2020, 4, 30));
assert_eq!(shift_months(base, 4), NaiveDate::from_ymd(2020, 5, 31));
assert_eq!(shift_months(base, 5), NaiveDate::from_ymd(2020, 6, 30));
assert_eq!(shift_months(base, 6), NaiveDate::from_ymd(2020, 7, 31));
assert_eq!(shift_months(base, 7), NaiveDate::from_ymd(2020, 8, 31));
assert_eq!(shift_months(base, 8), NaiveDate::from_ymd(2020, 9, 30));
assert_eq!(shift_months(base, 9), NaiveDate::from_ymd(2020, 10, 31));
assert_eq!(shift_months(base, 10), NaiveDate::from_ymd(2020, 11, 30));
assert_eq!(shift_months(base, 11), NaiveDate::from_ymd(2020, 12, 31));
assert_eq!(shift_months(base, 12), NaiveDate::from_ymd(2021, 1, 31));
assert_eq!(shift_months(base, 13), NaiveDate::from_ymd(2021, 2, 28));

assert_eq!(shift_months(base, -1), NaiveDate::from_ymd(2019, 12, 31));
assert_eq!(shift_months(base, -2), NaiveDate::from_ymd(2019, 11, 30));
assert_eq!(shift_months(base, -3), NaiveDate::from_ymd(2019, 10, 31));
assert_eq!(shift_months(base, -4), NaiveDate::from_ymd(2019, 9, 30));
assert_eq!(shift_months(base, -5), NaiveDate::from_ymd(2019, 8, 31));
assert_eq!(shift_months(base, -6), NaiveDate::from_ymd(2019, 7, 31));
assert_eq!(shift_months(base, -7), NaiveDate::from_ymd(2019, 6, 30));
assert_eq!(shift_months(base, -8), NaiveDate::from_ymd(2019, 5, 31));
assert_eq!(shift_months(base, -9), NaiveDate::from_ymd(2019, 4, 30));
assert_eq!(shift_months(base, -10), NaiveDate::from_ymd(2019, 3, 31));
assert_eq!(shift_months(base, -11), NaiveDate::from_ymd(2019, 2, 28));
assert_eq!(shift_months(base, -12), NaiveDate::from_ymd(2019, 1, 31));
assert_eq!(shift_months(base, -13), NaiveDate::from_ymd(2018, 12, 31));

assert_eq!(shift_months(base, 1265), NaiveDate::from_ymd(2125, 6, 30));
let base = NaiveDate::from_ymd_opt(2020, 1, 31).unwrap();

assert_eq!(
shift_months(base, 0),
NaiveDate::from_ymd_opt(2020, 1, 31).unwrap()
);
assert_eq!(
shift_months(base, 1),
NaiveDate::from_ymd_opt(2020, 2, 29).unwrap()
);
assert_eq!(
shift_months(base, 2),
NaiveDate::from_ymd_opt(2020, 3, 31).unwrap()
);
assert_eq!(
shift_months(base, 3),
NaiveDate::from_ymd_opt(2020, 4, 30).unwrap()
);
assert_eq!(
shift_months(base, 4),
NaiveDate::from_ymd_opt(2020, 5, 31).unwrap()
);
assert_eq!(
shift_months(base, 5),
NaiveDate::from_ymd_opt(2020, 6, 30).unwrap()
);
assert_eq!(
shift_months(base, 6),
NaiveDate::from_ymd_opt(2020, 7, 31).unwrap()
);
assert_eq!(
shift_months(base, 7),
NaiveDate::from_ymd_opt(2020, 8, 31).unwrap()
);
assert_eq!(
shift_months(base, 8),
NaiveDate::from_ymd_opt(2020, 9, 30).unwrap()
);
assert_eq!(
shift_months(base, 9),
NaiveDate::from_ymd_opt(2020, 10, 31).unwrap()
);
assert_eq!(
shift_months(base, 10),
NaiveDate::from_ymd_opt(2020, 11, 30).unwrap()
);
assert_eq!(
shift_months(base, 11),
NaiveDate::from_ymd_opt(2020, 12, 31).unwrap()
);
assert_eq!(
shift_months(base, 12),
NaiveDate::from_ymd_opt(2021, 1, 31).unwrap()
);
assert_eq!(
shift_months(base, 13),
NaiveDate::from_ymd_opt(2021, 2, 28).unwrap()
);

assert_eq!(
shift_months(base, -1),
NaiveDate::from_ymd_opt(2019, 12, 31).unwrap()
);
assert_eq!(
shift_months(base, -2),
NaiveDate::from_ymd_opt(2019, 11, 30).unwrap()
);
assert_eq!(
shift_months(base, -3),
NaiveDate::from_ymd_opt(2019, 10, 31).unwrap()
);
assert_eq!(
shift_months(base, -4),
NaiveDate::from_ymd_opt(2019, 9, 30).unwrap()
);
assert_eq!(
shift_months(base, -5),
NaiveDate::from_ymd_opt(2019, 8, 31).unwrap()
);
assert_eq!(
shift_months(base, -6),
NaiveDate::from_ymd_opt(2019, 7, 31).unwrap()
);
assert_eq!(
shift_months(base, -7),
NaiveDate::from_ymd_opt(2019, 6, 30).unwrap()
);
assert_eq!(
shift_months(base, -8),
NaiveDate::from_ymd_opt(2019, 5, 31).unwrap()
);
assert_eq!(
shift_months(base, -9),
NaiveDate::from_ymd_opt(2019, 4, 30).unwrap()
);
assert_eq!(
shift_months(base, -10),
NaiveDate::from_ymd_opt(2019, 3, 31).unwrap()
);
assert_eq!(
shift_months(base, -11),
NaiveDate::from_ymd_opt(2019, 2, 28).unwrap()
);
assert_eq!(
shift_months(base, -12),
NaiveDate::from_ymd_opt(2019, 1, 31).unwrap()
);
assert_eq!(
shift_months(base, -13),
NaiveDate::from_ymd_opt(2018, 12, 31).unwrap()
);

assert_eq!(
shift_months(base, 1265),
NaiveDate::from_ymd_opt(2125, 6, 30).unwrap()
);
}

#[test]
fn test_shift_months_with_overflow() {
let base = NaiveDate::from_ymd(2020, 12, 31);
let base = NaiveDate::from_ymd_opt(2020, 12, 31).unwrap();

assert_eq!(shift_months(base, 0), base);
assert_eq!(shift_months(base, 1), NaiveDate::from_ymd(2021, 1, 31));
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 this feels very familiar / similar to the work in https://github.com/apache/arrow-datafusion/pull/4189/files#diff-880863b84a09b210db1fc00012b1f3fbf0ade7f23c4297e15b54474ec65cb814R107

I'll see if I can find a way to remove the copy in DataFusion

assert_eq!(shift_months(base, 2), NaiveDate::from_ymd(2021, 2, 28));
assert_eq!(shift_months(base, 12), NaiveDate::from_ymd(2021, 12, 31));
assert_eq!(shift_months(base, 18), NaiveDate::from_ymd(2022, 6, 30));

assert_eq!(shift_months(base, -1), NaiveDate::from_ymd(2020, 11, 30));
assert_eq!(shift_months(base, -2), NaiveDate::from_ymd(2020, 10, 31));
assert_eq!(shift_months(base, -10), NaiveDate::from_ymd(2020, 2, 29));
assert_eq!(shift_months(base, -12), NaiveDate::from_ymd(2019, 12, 31));
assert_eq!(shift_months(base, -18), NaiveDate::from_ymd(2019, 6, 30));
assert_eq!(
shift_months(base, 1),
NaiveDate::from_ymd_opt(2021, 1, 31).unwrap()
);
assert_eq!(
shift_months(base, 2),
NaiveDate::from_ymd_opt(2021, 2, 28).unwrap()
);
assert_eq!(
shift_months(base, 12),
NaiveDate::from_ymd_opt(2021, 12, 31).unwrap()
);
assert_eq!(
shift_months(base, 18),
NaiveDate::from_ymd_opt(2022, 6, 30).unwrap()
);

assert_eq!(
shift_months(base, -1),
NaiveDate::from_ymd_opt(2020, 11, 30).unwrap()
);
assert_eq!(
shift_months(base, -2),
NaiveDate::from_ymd_opt(2020, 10, 31).unwrap()
);
assert_eq!(
shift_months(base, -10),
NaiveDate::from_ymd_opt(2020, 2, 29).unwrap()
);
assert_eq!(
shift_months(base, -12),
NaiveDate::from_ymd_opt(2019, 12, 31).unwrap()
);
assert_eq!(
shift_months(base, -18),
NaiveDate::from_ymd_opt(2019, 6, 30).unwrap()
);
}

#[test]
fn test_shift_months_datetime() {
let date = NaiveDate::from_ymd(2020, 1, 31);
let o_clock = NaiveTime::from_hms(1, 2, 3);
let date = NaiveDate::from_ymd_opt(2020, 1, 31).unwrap();
let o_clock = NaiveTime::from_hms_opt(1, 2, 3).unwrap();

let base = NaiveDateTime::new(date, o_clock);

assert_eq!(
shift_months(base, 0).date(),
NaiveDate::from_ymd(2020, 1, 31)
NaiveDate::from_ymd_opt(2020, 1, 31).unwrap()
);
assert_eq!(
shift_months(base, 1).date(),
NaiveDate::from_ymd(2020, 2, 29)
NaiveDate::from_ymd_opt(2020, 2, 29).unwrap()
);
assert_eq!(
shift_months(base, 2).date(),
NaiveDate::from_ymd(2020, 3, 31)
NaiveDate::from_ymd_opt(2020, 3, 31).unwrap()
);
assert_eq!(shift_months(base, 0).time(), o_clock);
assert_eq!(shift_months(base, 1).time(), o_clock);
Expand Down
2 changes: 1 addition & 1 deletion arrow-array/src/temporal_conversions.rs
Expand Up @@ -252,7 +252,7 @@ pub fn as_time<T: ArrowPrimitiveType>(v: i64) -> Option<NaiveTime> {
_ => None,
},
DataType::Timestamp(_, _) => as_datetime::<T>(v).map(|datetime| datetime.time()),
DataType::Date32 | DataType::Date64 => Some(NaiveTime::from_hms(0, 0, 0)),
DataType::Date32 | DataType::Date64 => NaiveTime::from_hms_opt(0, 0, 0),
DataType::Interval(_) => None,
_ => None,
}
Expand Down
34 changes: 21 additions & 13 deletions arrow-array/src/timezone.rs
Expand Up @@ -158,8 +158,8 @@ mod private {
#[test]
fn test_with_timezone() {
let vals = [
Utc.timestamp_millis(37800000),
Utc.timestamp_millis(86339000),
Utc.timestamp_millis_opt(37800000).unwrap(),
Utc.timestamp_millis_opt(86339000).unwrap(),
];

assert_eq!(10, vals[0].hour());
Expand All @@ -175,8 +175,8 @@ mod private {
fn test_using_chrono_tz_and_utc_naive_date_time() {
let sydney_tz = "Australia/Sydney".to_string();
let tz: Tz = sydney_tz.parse().unwrap();
let sydney_offset_without_dst = FixedOffset::east(10 * 60 * 60);
let sydney_offset_with_dst = FixedOffset::east(11 * 60 * 60);
let sydney_offset_without_dst = FixedOffset::east_opt(10 * 60 * 60).unwrap();
let sydney_offset_with_dst = FixedOffset::east_opt(11 * 60 * 60).unwrap();
// Daylight savings ends
// When local daylight time was about to reach
// Sunday, 4 April 2021, 3:00:00 am clocks were turned backward 1 hour to
Expand All @@ -188,32 +188,40 @@ mod private {
// Sunday, 3 October 2021, 3:00:00 am local daylight time instead.

// Sydney 2021-04-04T02:30:00+11:00 is 2021-04-03T15:30:00Z
let utc_just_before_sydney_dst_ends =
NaiveDate::from_ymd(2021, 4, 3).and_hms_nano(15, 30, 0, 0);
let utc_just_before_sydney_dst_ends = NaiveDate::from_ymd_opt(2021, 4, 3)
.unwrap()
.and_hms_nano_opt(15, 30, 0, 0)
.unwrap();
assert_eq!(
tz.offset_from_utc_datetime(&utc_just_before_sydney_dst_ends)
.fix(),
sydney_offset_with_dst
);
// Sydney 2021-04-04T02:30:00+10:00 is 2021-04-03T16:30:00Z
let utc_just_after_sydney_dst_ends =
NaiveDate::from_ymd(2021, 4, 3).and_hms_nano(16, 30, 0, 0);
let utc_just_after_sydney_dst_ends = NaiveDate::from_ymd_opt(2021, 4, 3)
.unwrap()
.and_hms_nano_opt(16, 30, 0, 0)
.unwrap();
assert_eq!(
tz.offset_from_utc_datetime(&utc_just_after_sydney_dst_ends)
.fix(),
sydney_offset_without_dst
);
// Sydney 2021-10-03T01:30:00+10:00 is 2021-10-02T15:30:00Z
let utc_just_before_sydney_dst_starts =
NaiveDate::from_ymd(2021, 10, 2).and_hms_nano(15, 30, 0, 0);
let utc_just_before_sydney_dst_starts = NaiveDate::from_ymd_opt(2021, 10, 2)
.unwrap()
.and_hms_nano_opt(15, 30, 0, 0)
.unwrap();
assert_eq!(
tz.offset_from_utc_datetime(&utc_just_before_sydney_dst_starts)
.fix(),
sydney_offset_without_dst
);
// Sydney 2021-04-04T03:30:00+11:00 is 2021-10-02T16:30:00Z
let utc_just_after_sydney_dst_starts =
NaiveDate::from_ymd(2022, 10, 2).and_hms_nano(16, 30, 0, 0);
let utc_just_after_sydney_dst_starts = NaiveDate::from_ymd_opt(2022, 10, 2)
.unwrap()
.and_hms_nano_opt(16, 30, 0, 0)
.unwrap();
assert_eq!(
tz.offset_from_utc_datetime(&utc_just_after_sydney_dst_starts)
.fix(),
Expand Down Expand Up @@ -300,7 +308,7 @@ mod tests {

#[test]
fn test_with_offset() {
let t = NaiveDate::from_ymd(2000, 1, 1);
let t = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap();

let tz: Tz = "-00:00".parse().unwrap();
assert_eq!(tz.offset_from_utc_date(&t).fix().local_minus_utc(), 0);
Expand Down
8 changes: 4 additions & 4 deletions arrow-array/src/types.rs
Expand Up @@ -327,7 +327,7 @@ impl Date32Type {
///
/// * `i` - The Date32Type to convert
pub fn to_naive_date(i: <Date32Type as ArrowPrimitiveType>::Native) -> NaiveDate {
let epoch = NaiveDate::from_ymd(1970, 1, 1);
let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
epoch.add(Duration::days(i as i64))
}

Expand All @@ -337,7 +337,7 @@ impl Date32Type {
///
/// * `d` - The NaiveDate to convert
pub fn from_naive_date(d: NaiveDate) -> <Date32Type as ArrowPrimitiveType>::Native {
let epoch = NaiveDate::from_ymd(1970, 1, 1);
let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
d.sub(epoch).num_days() as <Date32Type as ArrowPrimitiveType>::Native
}

Expand Down Expand Up @@ -400,7 +400,7 @@ impl Date64Type {
///
/// * `i` - The Date64Type to convert
pub fn to_naive_date(i: <Date64Type as ArrowPrimitiveType>::Native) -> NaiveDate {
let epoch = NaiveDate::from_ymd(1970, 1, 1);
let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
epoch.add(Duration::milliseconds(i as i64))
}

Expand All @@ -410,7 +410,7 @@ impl Date64Type {
///
/// * `d` - The NaiveDate to convert
pub fn from_naive_date(d: NaiveDate) -> <Date64Type as ArrowPrimitiveType>::Native {
let epoch = NaiveDate::from_ymd(1970, 1, 1);
let epoch = NaiveDate::from_ymd_opt(1970, 1, 1).unwrap();
d.sub(epoch).num_milliseconds() as <Date64Type as ArrowPrimitiveType>::Native
}

Expand Down
16 changes: 11 additions & 5 deletions arrow-cast/src/cast.rs
Expand Up @@ -6120,7 +6120,7 @@ mod tests {
#[test]
fn test_cast_utf8_to_date32() {
use chrono::NaiveDate;
let from_ymd = chrono::NaiveDate::from_ymd;
let from_ymd = chrono::NaiveDate::from_ymd_opt;
let since = chrono::NaiveDate::signed_duration_since;

let a = StringArray::from(vec![
Expand All @@ -6135,13 +6135,19 @@ mod tests {
let c = b.as_any().downcast_ref::<Date32Array>().unwrap();

// test valid inputs
let date_value = since(NaiveDate::from_ymd(2000, 1, 1), from_ymd(1970, 1, 1))
.num_days() as i32;
let date_value = since(
NaiveDate::from_ymd_opt(2000, 1, 1).unwrap(),
from_ymd(1970, 1, 1).unwrap(),
)
.num_days() as i32;
assert!(c.is_valid(0)); // "2000-01-01"
assert_eq!(date_value, c.value(0));

let date_value = since(NaiveDate::from_ymd(2000, 2, 2), from_ymd(1970, 1, 1))
.num_days() as i32;
let date_value = since(
NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
from_ymd(1970, 1, 1).unwrap(),
)
.num_days() as i32;
assert!(c.is_valid(1)); // "2000-2-2"
assert_eq!(date_value, c.value(1));

Expand Down