Skip to content

Commit

Permalink
clean up format_localized
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 7, 2024
1 parent c6d6056 commit fba15c1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,4 @@ exclude = [
".ipynb",
"venv",
]
# Same as Black.
line-length = 80
line-length = 80
51 changes: 20 additions & 31 deletions src/format_localized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use chrono::TimeZone;
use chrono::{self, format::DelayedFormat};
use polars::prelude::*;
use polars_arrow::array::{MutableArray, MutableUtf8Array, Utf8Array};
use polars_arrow::temporal_conversions::MILLISECONDS_IN_DAY;
use std::fmt::Write;
use std::str::FromStr;

Expand All @@ -28,6 +29,15 @@ fn timestamp_ns_to_datetime(timestamp: i64) -> chrono::NaiveDateTime {
)
.unwrap()
}
fn format_ndt(
ndt: chrono::NaiveDateTime,
format: &str,
locale: chrono::prelude::Locale,
tz: chrono_tz::Tz,
) -> DelayedFormat<StrftimeItems<'_>> {
let dt = tz.from_utc_datetime(&ndt);
dt.format_localized(format, locale)
}

pub(crate) fn impl_format_localized(
s: &Series,
Expand All @@ -42,19 +52,10 @@ pub(crate) fn impl_format_localized(
let fmted = format!("{}", dt.format_localized(format, locale));
let name = s.name();

match s.dtype() {
let mut ca: StringChunked = match s.dtype() {
DataType::Date => {
let ca = s.date()?;
fn format_ndt(
ndt: chrono::NaiveDateTime,
format: &str,
locale: chrono::prelude::Locale,
tz: chrono_tz::Tz,
) -> DelayedFormat<StrftimeItems<'_>> {
let dt = tz.from_utc_datetime(&ndt);
dt.format_localized(format, locale)
}
let mut ca: StringChunked = ca.apply_kernel_cast(&|arr| {
ca.apply_kernel_cast(&|arr| {
let mut buf = String::new();
let mut mutarr =
MutableUtf8Array::with_capacities(arr.len(), arr.len() * fmted.len() + 1);
Expand All @@ -64,9 +65,8 @@ pub(crate) fn impl_format_localized(
None => mutarr.push_null(),
Some(timestamp) => {
buf.clear();
let ndt = timestamp_ms_to_datetime(
(*timestamp as i64) * 1_000 * 60 * 60 * 24,
);
let ndt =
timestamp_ms_to_datetime((*timestamp as i64) * MILLISECONDS_IN_DAY);
let fmted = format_ndt(ndt, format, locale, chrono_tz::UTC);
write!(buf, "{fmted}").unwrap();
mutarr.push(Some(&buf))
Expand All @@ -76,9 +76,7 @@ pub(crate) fn impl_format_localized(

let arr: Utf8Array<i64> = mutarr.into();
Box::new(arr)
});
ca.rename(name);
Ok(ca.into_series())
})
}
DataType::Datetime(time_unit, time_zone) => {
let ca = s.datetime()?;
Expand All @@ -91,16 +89,7 @@ pub(crate) fn impl_format_localized(
None => chrono_tz::UTC,
Some(tz) => chrono_tz::Tz::from_str(tz).unwrap(),
};
fn format_ndt(
ndt: chrono::NaiveDateTime,
format: &str,
locale: chrono::prelude::Locale,
tz: chrono_tz::Tz,
) -> DelayedFormat<StrftimeItems<'_>> {
let dt = tz.from_utc_datetime(&ndt);
dt.format_localized(format, locale)
}
let mut ca: StringChunked = ca.apply_kernel_cast(&|arr| {
ca.apply_kernel_cast(&|arr| {
let mut buf = String::new();
let mut mutarr =
MutableUtf8Array::with_capacities(arr.len(), arr.len() * fmted.len() + 1);
Expand All @@ -120,10 +109,10 @@ pub(crate) fn impl_format_localized(

let arr: Utf8Array<i64> = mutarr.into();
Box::new(arr)
});
ca.rename(name);
Ok(ca.into_series())
})
}
_ => unreachable!(),
}
};
ca.rename(name);
Ok(ca.into_series())
}

0 comments on commit fba15c1

Please sign in to comment.