Skip to content

Commit

Permalink
refactor[rust]: simpler timezone cast (#5014)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpatton-gr committed Sep 28, 2022
1 parent 2cf3d7b commit 6eb71ce
Showing 1 changed file with 3 additions and 34 deletions.
37 changes: 3 additions & 34 deletions polars/polars-time/src/chunkedarray/kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,66 +234,35 @@ pub(crate) fn cast_timezone(
to: chrono_tz::Tz,
) -> ArrayRef {
use chrono::TimeZone;
use chrono_tz::OffsetComponents;

match tu {
TimeUnit::Milliseconds => Box::new(unary(
arr,
|value| {
let ndt = timestamp_ms_to_datetime(value);
// find the current offset from utc
let tz_aware = from.from_local_datetime(&ndt).unwrap();
let offset = tz_aware.offset();
let total_offset_from = offset.base_utc_offset() + offset.dst_offset();

// find the new offset from utc
let new_tz_aware = tz_aware.with_timezone(&to);
let offset = new_tz_aware.offset();
let total_offset_to = offset.base_utc_offset() + offset.dst_offset();
let offset = total_offset_to - total_offset_from;

// correct for that offset
(ndt + offset).timestamp_millis()
new_tz_aware.naive_local().timestamp_millis()
},
ArrowDataType::Int64,
)),
TimeUnit::Microseconds => Box::new(unary(
arr,
|value| {
let ndt = timestamp_us_to_datetime(value);
// find the current offset from utc
let tz_aware = from.from_local_datetime(&ndt).unwrap();
let offset = tz_aware.offset();
let total_offset_from = offset.base_utc_offset() + offset.dst_offset();

// find the new offset from utc
let new_tz_aware = tz_aware.with_timezone(&to);
let offset = new_tz_aware.offset();
let total_offset_to = offset.base_utc_offset() + offset.dst_offset();
let offset = total_offset_to - total_offset_from;

// correct for that offset
(ndt + offset).timestamp_micros()
new_tz_aware.naive_local().timestamp_micros()
},
ArrowDataType::Int64,
)),
TimeUnit::Nanoseconds => Box::new(unary(
arr,
|value| {
let ndt = timestamp_ns_to_datetime(value);
// find the current offset from utc
let tz_aware = from.from_local_datetime(&ndt).unwrap();
let offset = tz_aware.offset();
let total_offset_from = offset.base_utc_offset() + offset.dst_offset();

// find the new offset from utc
let new_tz_aware = tz_aware.with_timezone(&to);
let offset = new_tz_aware.offset();
let total_offset_to = offset.base_utc_offset() + offset.dst_offset();
let offset = total_offset_to - total_offset_from;

// correct for that offset
(ndt + offset).timestamp_nanos()
new_tz_aware.naive_local().timestamp_nanos()
},
ArrowDataType::Int64,
)),
Expand Down

0 comments on commit 6eb71ce

Please sign in to comment.