Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 18 additions & 63 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ polars-core = { version = "0.55.2", default-features = false, features = [
"dtype-decimal",
], optional = true }
polars-arrow = { version = "0.55.2", optional = true }
time = { version = "0.3", optional = true, features = ["parsing", "formatting", "macros", "local-offset"] }
phf = { version = "0.13.1", features = ["macros"], optional = true }
time = { version = "0.3", optional = true, features = ["parsing"] }
# IANA Time Zone Database as static data, for the `time_zones` feature.
tzdb_data = { version = "0.2.5", optional = true }
num-traits = "0.2.19"
rayon = { version = "1.11.0", optional = true }
ryu = { version = "1.0.23", optional = true }
Expand Down Expand Up @@ -211,7 +212,21 @@ simd = []
# Without this feature, datetime values are raw integer offsets.
# The `ArrowType` stored in `Field` and/or `FieldArray` specifies the
# logical type (Date32, Date64, Timestamp, etc.) for Arrow FFI compatibility.
datetime_ops = ["dep:time", "dep:phf", "datetime"]
datetime_ops = ["dep:time", "datetime"]

# Adds time zone conversion for timestamps with a time zone:
# - Wall times and UTC offsets for IANA identifiers such as `Australia/Sydney`,
# including daylight saving and historical transitions
# - Fixed offsets for abbreviations such as `AEST`, and for offsets such as `+10:00`
# - Display, component extraction, truncation and month arithmetic on the wall time
# of the time zone
#
# Stored values are still UTC integers with this feature on. The IANA Time Zone Database
# is compiled into the binary as static data, which adds about 440 KB to the release binary
# (roughly ~%40 of the total size).
#
# Without this feature, timestamps with a time zone are printed and computed in UTC.
time_zones = ["datetime_ops", "dep:tzdb_data"]

# Adds string arithmetic kernels
# Includes (small) external dependencies, and supports
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ Additional types:
|---------|-------------|
| `datetime` | Temporal array types as raw integer offsets |
| `datetime_ops` | Datetime library functionality: ISO 8601 parsing, timezone-aware operations, arithmetic, component extraction |
| `time_zones` | Time zone conversion through the IANA Time Zone Database, including daylight saving and historical transitions. Display, component extraction, truncation and month arithmetic use the wall time of the time zone, and stored values stay UTC. Without it, timestamps with a time zone are printed and computed in UTC |
| `extended_numeric_types` | i8, i16, u8, u16 variants |
| `default_categorical_8` | 8-bit default categorical width, with Categorical8 and Categorical32 both available |
| `extended_categorical` | Adds Categorical16 and Categorical64 dictionary index widths |
Expand Down
42 changes: 40 additions & 2 deletions src/kernels/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ use crate::{Bitmask, DatetimeArray};
/// Handles the full calendar range (`Year` through `Second`, plus `Week`) and the
/// sub-second steps (`Millisecond`, `Microsecond`). Sub-second steps are no-ops on
/// arrays whose stored resolution is already at or coarser than the target.
///
/// With the `time_zones` feature, calendar periods of timestamps with a time zone are
/// floored on the wall time of that time zone, and `out` receives the UTC value of the
/// start of the period.
pub fn truncate_into<T: Integer + FromPrimitive>(
src: &DatetimeArray<T>,
src_offset: usize,
Expand All @@ -59,6 +63,8 @@ pub fn truncate_into<T: Integer + FromPrimitive>(
) {
let time_unit = src.time_unit();
let len = out.len();
let wall_time_offset = src.wall_time_offset();
let wall_time_values = src.wall_time_values();

// Resolve the unit scaling and sub-second divisor once for the whole window.
// `None` for `per_second` identifies the `Days` unit. A divisor of 1 leaves
Expand Down Expand Up @@ -89,7 +95,24 @@ pub fn truncate_into<T: Integer + FromPrimitive>(
.to_i64()
.and_then(|v| match sub_second_divisor {
Some(divisor) => v.div_euclid(divisor).checked_mul(divisor),
None => floor_calendar(v, period, per_second),
// Calendar periods are floored on the wall time of the array's
// time zone, and written as the UTC value of that wall time.
None => {
let offset = wall_time_offset(v);
v.checked_add(offset)
.and_then(|wall| floor_calendar(wall, period, per_second))
.and_then(|floored| {
// Use the input's offset for the start of the period.
// Resolve the wall time when a transition lies between
// the start of the period and the input.
let value = floored.checked_sub(offset)?;
if wall_time_offset(value) == offset {
Some(value)
} else {
Some(wall_time_values(floored).0)
}
})
}
})
.and_then(T::from_i64)
{
Expand Down Expand Up @@ -285,6 +308,9 @@ pub fn add_duration_into<T: Integer + FromPrimitive>(
/// A day that does not exist in the destination month is clamped to that month's last
/// day, and time-of-day is preserved. A value whose result is not a valid datetime is
/// marked null in `out_mask`.
///
/// With the `time_zones` feature, the months of timestamps with a time zone are added
/// on the wall time of that time zone, and `out` receives the UTC value of the result.
pub fn add_months_into<T: Integer + FromPrimitive>(
src: &DatetimeArray<T>,
src_offset: usize,
Expand All @@ -293,14 +319,18 @@ pub fn add_months_into<T: Integer + FromPrimitive>(
mut out_mask: Option<&mut Bitmask>,
) {
let time_unit = src.time_unit();
let wall_time_offset = src.wall_time_offset();
let wall_time_values = src.wall_time_values();
for i in 0..out.len() {
let original = src.data[src_offset + i];
out[i] = original;
let computed = if src.is_null(src_offset + i) {
None
} else {
// The months are added on the wall time of the array's time zone.
original
.to_i64()
.and_then(|v| v.checked_add(wall_time_offset(v)))
.and_then(|v| DatetimeArray::<T>::i64_to_datetime(v, time_unit))
.and_then(|dt| {
let date = dt.date();
Expand All @@ -313,7 +343,8 @@ pub fn add_months_into<T: Integer + FromPrimitive>(
let new_date =
time::Date::from_calendar_date(new_year, new_month_enum, day).ok()?;
let new_dt = new_date.with_time(dt.time()).assume_utc();
T::from_i64(DatetimeArray::<T>::datetime_to_i64(new_dt, time_unit))
let wall = DatetimeArray::<T>::datetime_to_i64(new_dt, time_unit);
T::from_i64(wall_time_values(wall).1)
})
};
let valid = match computed {
Expand All @@ -333,6 +364,9 @@ pub fn add_months_into<T: Integer + FromPrimitive>(
/// `[src_offset, src_offset + out.len())`, writing into `out`. A null input or a
/// value that is not a representable datetime writes `0` and clears its `out_mask`
/// bit. The allocating extract methods on `DatetimeArray` pass `src_offset = 0`.
///
/// With the `time_zones` feature, fields of timestamps with a time zone are those of
/// the wall time of that time zone.
fn extract_i32_into<T, F>(
src: &DatetimeArray<T>,
src_offset: usize,
Expand All @@ -344,12 +378,14 @@ fn extract_i32_into<T, F>(
F: Fn(time::OffsetDateTime) -> i32,
{
let time_unit = src.time_unit();
let wall_time_offset = src.wall_time_offset();
for i in 0..out.len() {
let dt = if src.is_null(src_offset + i) {
None
} else {
src.data[src_offset + i]
.to_i64()
.and_then(|v| v.checked_add(wall_time_offset(v)))
.and_then(|v| DatetimeArray::<T>::i64_to_datetime(v, time_unit))
};
let valid = match dt {
Expand Down Expand Up @@ -456,9 +492,11 @@ pub fn extract_bool_into<T, F>(
F: Fn(time::OffsetDateTime) -> bool,
{
let time_unit = src.time_unit();
let wall_time_offset = src.wall_time_offset();
for i in 0..out_bits.len() {
match src.data[src_offset + i]
.to_i64()
.and_then(|v| v.checked_add(wall_time_offset(v)))
.and_then(|v| DatetimeArray::<T>::i64_to_datetime(v, time_unit))
{
// SAFETY: `i < out_bits.len()`, so the bit index is within the bitmask's
Expand Down
23 changes: 11 additions & 12 deletions src/structs/field_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,17 @@ fn format_field_array_with_timezone(
field_array: &FieldArray,
timezone: &str,
) -> std::fmt::Result {
use crate::traits::print::MAX_PREVIEW;
use crate::traits::print::{MAX_PREVIEW, format_datetime_value};
use crate::{Array, TemporalArray};

// Time zone conversion requires the `time` crate.
if cfg!(not(feature = "datetime_ops")) {
panic!(
"Timezone functionality requires the 'datetime_ops' feature. \
Enable it in Cargo.toml with: features = [\"datetime_ops\"]"
);
}

let arr = &field_array.array;
let len = arr.len();
let nulls = arr.null_count();
Expand All @@ -591,7 +599,7 @@ fn format_field_array_with_timezone(
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", format_datetime_with_tz(dt.as_ref(), i, timezone))?;
write!(f, "{}", format_datetime_value(dt.as_ref(), i))?;
}
if len > MAX_PREVIEW {
write!(f, ", ...")?;
Expand All @@ -612,7 +620,7 @@ fn format_field_array_with_timezone(
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", format_datetime_with_tz(dt.as_ref(), i, timezone))?;
write!(f, "{}", format_datetime_value(dt.as_ref(), i))?;
}
if len > MAX_PREVIEW {
write!(f, ", ...")?;
Expand All @@ -623,15 +631,6 @@ fn format_field_array_with_timezone(
}
}

#[cfg(feature = "datetime")]
fn format_datetime_with_tz<T>(arr: &crate::DatetimeArray<T>, idx: usize, timezone: &str) -> String
where
T: crate::Integer + std::fmt::Display,
{
use crate::traits::print::format_datetime_value;
format_datetime_value(arr, idx, Some(timezone))
}

impl Shape for FieldArray {
fn shape(&self) -> ShapeDim {
ShapeDim::Rank1(self.len())
Expand Down
Loading
Loading