This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12102-eb9994a417afe9aa417929dc7d8b617a194d9854 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit fb8b1268ccfb1103fa8c559759014934f528b660 Author: hsdfat <[email protected]> AuthorDate: Mon Nov 9 23:30:27 2026 +0000 fix(arrow-cast): write timestamps whose zone offset has seconds in UTC (#12102) # Which issue does this PR close? - Closes #11698 # Rationale for this change With the default format, a timestamp with a timezone is written with `to_rfc3339_opts`. RFC 3339 offsets have minute precision, so for zones whose offset has seconds (LMT offsets from tzdb such as Africa/Monrovia's `-00:44:30` before 1972, or Europe/Paris before 1911) the offset is rounded while the wall time uses the exact offset. The written string then points to an instant up to 30 s away from the stored one. # What changes are included in this PR? In `write_timestamp`, when the default format is used and the offset is not a whole number of minutes, the instant is written in UTC with a `Z` suffix. Writing the offset with seconds (`-00:44:30`) would also be lossless, but arrow's own parser (`parse_fixed_offset`) only accepts `±HH:MM`, `±HHMM` and `±HH`, so that output wouldn't read back through arrow-csv/arrow-json. Whole-minute offsets and custom formats are unchanged. # Are these changes tested? Yes. `test_timestamp_tz_offset_with_seconds` formats Africa/Monrovia (1960) and Europe/Paris (1900) timestamps and checks they parse back to the same instant, and checks that Asia/Kolkata still prints `+05:30`. # Are there any significant user-facing changes? Timestamps in zones with second-level offsets are now written with `Z` instead of a rounded offset when the default format is used. --- AI disclosure: the patch and test were drafted with AI assistance (Claude); I reviewed every line, checked the rounding against the parser's accepted offset forms, and ran fmt/clippy and the arrow-cast, arrow-csv and arrow-json tests. --- arrow-cast/src/display.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/arrow-cast/src/display.rs b/arrow-cast/src/display.rs index e69f687c68..8735ca14b2 100644 --- a/arrow-cast/src/display.rs +++ b/arrow-cast/src/display.rs @@ -23,7 +23,7 @@ use arrow_buffer::ArrowNativeType; use arrow_data::decimal::write_decimal; use arrow_schema::*; use chrono::format::{Item, StrftimeItems}; -use chrono::{NaiveDate, NaiveDateTime, SecondsFormat, TimeZone, Utc}; +use chrono::{NaiveDate, NaiveDateTime, Offset, SecondsFormat, TimeZone, Utc}; use lexical_core::FormattedSize; type TimeFormat<'a> = Option<&'a str>; @@ -748,11 +748,16 @@ fn write_timestamp( ) -> FormatResult { match timezone { Some(tz) => { - let date = Utc.from_utc_datetime(&naive).with_timezone(&tz); + let utc = Utc.from_utc_datetime(&naive); + let date = utc.with_timezone(&tz); match format { CompiledTimeFormat::Custom(items) => { write!(f, "{}", date.format_with_items(items.0.iter()))? } + // RFC 3339 offsets have no seconds and the arrow parser rejects them, so only Z round-trips + CompiledTimeFormat::Default if date.offset().fix().local_minus_utc() % 60 != 0 => { + write!(f, "{}", utc.to_rfc3339_opts(SecondsFormat::AutoSi, true))? + } CompiledTimeFormat::Default => { write!(f, "{}", date.to_rfc3339_opts(SecondsFormat::AutoSi, true))? } @@ -1466,6 +1471,30 @@ mod tests { (0..array.len()).map(|x| fmt.value(x).to_string()).collect() } + #[test] + fn test_timestamp_tz_offset_with_seconds() { + let options = FormatOptions::new(); + let cases = [ + ("Africa/Monrovia", "1960-06-01T12:00:00.250Z"), + ("Europe/Paris", "1900-06-01T12:00:00.250Z"), + ]; + for (tz, ts) in cases { + let expected = crate::parse::string_to_datetime(&Utc, ts).unwrap(); + let array = TimestampMillisecondArray::from(vec![expected.timestamp_millis()]) + .with_timezone(tz); + let formatted = format_array(&array, &options); + assert_eq!(formatted, [ts]); + let parsed = crate::parse::string_to_datetime(&Utc, &formatted[0]).unwrap(); + assert_eq!(parsed, expected); + } + + let array = TimestampSecondArray::from(vec![0]).with_timezone("Asia/Kolkata"); + assert_eq!( + format_array(&array, &options), + ["1970-01-01T05:30:00+05:30"] + ); + } + #[test] fn test_temporal_custom_format() { let options = FormatOptions::new()
