Skip to content

Commit

Permalink
factor out parse_timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Aug 24, 2023
1 parent 38e3e7d commit 35a9703
Showing 1 changed file with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use chrono_tz::Tz;
use polars_arrow::kernels::replace_time_zone as replace_time_zone_kernel;
use polars_core::prelude::*;

fn parse_time_zone(s: &str) -> PolarsResult<Tz> {
s.parse()
.map_err(|e| polars_err!(ComputeError: format!("unable to parse time zone: '{s}': {e}")))
}

pub fn replace_time_zone(
ca: &DatetimeChunked,
time_zone: Option<&str>,
use_earliest: Option<bool>,
) -> PolarsResult<DatetimeChunked> {
let out: PolarsResult<_> = {
let from = ca.time_zone().as_deref().unwrap_or("UTC");
let to = time_zone.unwrap_or("UTC");
let (from_tz, to_tz) = match from.parse::<chrono_tz::Tz>() {
Ok(from_tz) => match to.parse::<chrono_tz::Tz>() {
Ok(to_tz) => (from_tz, to_tz),
Err(_) => polars_bail!(ComputeError: "unable to parse time zone: '{}'", to),
},
Err(_) => polars_bail!(ComputeError: "unable to parse time zone: '{}'", from),
};
let from_tz = parse_time_zone(ca.time_zone().as_deref().unwrap_or("UTC"))?;
let to_tz = parse_time_zone(time_zone.unwrap_or("UTC"))?;
let chunks = ca.downcast_iter().map(|arr| {
replace_time_zone_kernel(
arr,
Expand Down

0 comments on commit 35a9703

Please sign in to comment.