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-12345-ab4af8a20c7304b23f7157c665140407c3369621 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 6747ecda382b135ba0fb08868dd859a47c0304ad Author: Zhongxuan (Daniel) Wang <[email protected]> AuthorDate: Mon Nov 23 06:02:16 2026 +0000 refactor(parquet): drop the required chrono dependency (#12345) # Which issue does this PR close? - Closes #12343. # Rationale for this change `parquet` lists `chrono` as a required dependency, so even a build with `default-features = false` and without the `arrow` feature links it, and `cargo audit` / `cargo deny` report RUSTSEC-2026-0543 for every crate that depends on `parquet`. `parquet`'s own use of `chrono` is three display helpers in `record::api`, which need no more than integer arithmetic. # What changes are included in this PR? - `convert_date_to_string`, `convert_timestamp_millis_to_string` and `convert_timestamp_micros_to_string` convert days since the epoch to a civil date with Howard Hinnant's `civil_from_days` (floor division, so values before 1970 are right) and format the fields directly. The output matches chrono's `%Y-%m-%d`, `%Y-%m-%d %H:%M:%S%.3f %:z` and `%Y-%m-%d %H:%M:%S%.6f %:z` for every value chrono accepts, including chrono's `%Y` rule of a sign before years outside 0..=9999 (`-0001`, `+10000`). - `chrono` moves from `[dependencies]` to `[dev-dependencies]`, where the unit tests, the `arrow_reader` integration tests and the `write_parquet` example still use it. - The doc comment on `convert_date_to_string` said the date is displayed in the local timezone; it is UTC. With the `arrow` feature (on by default) `chrono` still comes in through `arrow-array`; that is #9183 and out of scope here. With `--no-default-features`, `cargo tree -p parquet -e normal` no longer lists `chrono`, and with the default features it lists it only under `arrow-array`. # Are these changes tested? Yes. The existing `test_convert_date_to_string`, `test_convert_timestamp_millis_to_string` and `test_convert_timestamp_micros_to_string` tests pin the common case. A new test, `test_convert_date_and_timestamp_to_string_extreme_years`, compares all three helpers with chrono at year 0 (including 0000-02-29), negative years, year 10000, the epoch, the last microsecond before it, and chrono's `NaiveDate::MIN` and `MAX`. Outside this PR, the new helpers were also compared with chrono 0.4.45 (the version in `Cargo.lock`) in a throwaway program: every one of the 191,491,529 days chrono accepts, 400 million random millisecond and microsecond timestamps across chrono's range and near the epoch, and 40,006 timestamps around the ends of chrono's range and the epoch. All matched byte for byte. `cargo test -p parquet` passes with the default features and with `--all-features`. # Are there any significant user-facing changes? No public API changes. One behaviour change: a DATE, TIMESTAMP_MILLIS or TIMESTAMP_MICROS value outside chrono's range (more than about 262,000 years from year 0) used to panic in `Field`'s `Display` and `to_json_value`, through chrono's `unwrap`. It now formats on the same proleptic Gregorian calendar, for example `i64::MAX` milliseconds as `+292278994-08-17 07:12:55.807 +00:00`. # AI usage disclosure The code and tests in this PR were written with Claude Code (an AI assistant). They were verified by the test runs listed in "Are these changes tested?". 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]> --- parquet/Cargo.toml | 2 +- parquet/src/record/api.rs | 88 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 78 insertions(+), 12 deletions(-) diff --git a/parquet/Cargo.toml b/parquet/Cargo.toml index 53fcaf8ae7..c408283439 100644 --- a/parquet/Cargo.toml +++ b/parquet/Cargo.toml @@ -43,7 +43,6 @@ brotli = { version = "9.0", default-features = false, features = ["std"], option flate2 = { version = "1.1", default-features = false, optional = true } lz4_flex = { version = "0.14", default-features = false, features = ["std", "frame"], optional = true } zstd = { version = "0.14", optional = true, default-features = false } -chrono = { workspace = true } num-bigint = { version = "0.5", default-features = false } num-integer = { version = "0.1.46", default-features = false, features = ["std"] } num-traits = { version = "0.2.19", default-features = false, features = ["std"] } @@ -64,6 +63,7 @@ simdutf8 = { workspace = true , optional = true } ring = { version = "0.17", default-features = false, features = ["std"], optional = true } [dev-dependencies] +chrono = { workspace = true } mimalloc = { version = "0.1", default-features = false } base64 = { version = "0.23", default-features = false, features = ["std"] } criterion = { workspace = true, default-features = false, features = ["async_futures"] } diff --git a/parquet/src/record/api.rs b/parquet/src/record/api.rs index 9c64a13df8..6b52dafed4 100644 --- a/parquet/src/record/api.rs +++ b/parquet/src/record/api.rs @@ -6,7 +6,6 @@ use std::fmt; -use chrono::{TimeZone, Utc}; use half::f16; use num_bigint::{BigInt, Sign}; use num_traits::Float; @@ -922,14 +921,10 @@ impl fmt::Display for Field { /// Helper method to convert Parquet date into a string. /// Input `value` is a number of days since the epoch in UTC. -/// Date is displayed in local timezone. +/// Date is displayed in UTC timezone. #[inline] fn convert_date_to_string(value: i32) -> String { - static NUM_SECONDS_IN_DAY: i64 = 60 * 60 * 24; - let dt = Utc - .timestamp_opt(value as i64 * NUM_SECONDS_IN_DAY, 0) - .unwrap(); - format!("{}", dt.format("%Y-%m-%d")) + format_date(value as i64) } /// Helper method to convert Parquet timestamp into a string. @@ -937,8 +932,8 @@ fn convert_date_to_string(value: i32) -> String { /// Datetime is displayed in UTC timezone. #[inline] fn convert_timestamp_millis_to_string(value: i64) -> String { - let dt = Utc.timestamp_millis_opt(value).unwrap(); - format!("{}", dt.format("%Y-%m-%d %H:%M:%S%.3f %:z")) + let (secs, millis) = (value.div_euclid(1_000), value.rem_euclid(1_000)); + format!("{}.{millis:03} +00:00", format_datetime(secs)) } /// Helper method to convert Parquet timestamp into a string. @@ -946,8 +941,40 @@ fn convert_timestamp_millis_to_string(value: i64) -> String { /// Datetime is displayed in UTC timezone. #[inline] fn convert_timestamp_micros_to_string(value: i64) -> String { - let dt = Utc.timestamp_micros(value).unwrap(); - format!("{}", dt.format("%Y-%m-%d %H:%M:%S%.6f %:z")) + let (secs, micros) = (value.div_euclid(1_000_000), value.rem_euclid(1_000_000)); + format!("{}.{micros:06} +00:00", format_datetime(secs)) +} + +/// Formats seconds since the epoch as `YYYY-MM-DD hh:mm:ss` in UTC. +fn format_datetime(secs: i64) -> String { + let time = secs.rem_euclid(86_400); + format!( + "{} {:02}:{:02}:{:02}", + format_date(secs.div_euclid(86_400)), + time / 3_600, + time / 60 % 60, + time % 60 + ) +} + +/// Formats days since the epoch as a proleptic Gregorian `YYYY-MM-DD` date. Years +/// outside 0..=9999 are signed, as in ISO 8601 and chrono's `%Y`. +fn format_date(days: i64) -> String { + // https://howardhinnant.github.io/date_algorithms.html#civil_from_days + let z = days + 719_468; + let era = z.div_euclid(146_097); + let doe = z.rem_euclid(146_097); + let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365; + let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); + let mp = (5 * doy + 2) / 153; + let day = doy - (153 * mp + 2) / 5 + 1; + let month = if mp < 10 { mp + 3 } else { mp - 9 }; + let year = era * 400 + yoe + i64::from(month <= 2); + if (0..=9999).contains(&year) { + format!("{year:04}-{month:02}-{day:02}") + } else { + format!("{year:+05}-{month:02}-{day:02}") + } } /// Helper method to convert Parquet time (milliseconds since midnight) into a string. @@ -1016,6 +1043,8 @@ mod tests { use std::f64::consts::PI; use std::sync::Arc; + use chrono::{TimeZone, Utc}; + use crate::schema::types::{ColumnDescriptor, ColumnPath, PrimitiveTypeBuilder}; /// Creates test column descriptor based on provided type parameters. @@ -1346,6 +1375,43 @@ mod tests { ); } + #[test] + fn test_convert_date_and_timestamp_to_string_extreme_years() { + let epoch = chrono::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap(); + let dates = [ + (-10000, 1, 1), + (-1, 12, 31), + (0, 1, 1), + (0, 2, 29), + (1969, 12, 31), + (9999, 12, 31), + (10000, 1, 1), + ] + .map(|(y, m, d)| chrono::NaiveDate::from_ymd_opt(y, m, d).unwrap()); + let limits = [epoch, chrono::NaiveDate::MIN, chrono::NaiveDate::MAX]; + for date in dates.into_iter().chain(limits) { + let days = (date - epoch).num_days() as i32; + assert_eq!( + convert_date_to_string(days), + date.format("%Y-%m-%d").to_string() + ); + for time in [ + date.and_hms_opt(0, 0, 0), + date.and_hms_micro_opt(23, 59, 59, 999_999), + ] { + let dt = Utc.from_utc_datetime(&time.unwrap()); + assert_eq!( + convert_timestamp_millis_to_string(dt.timestamp_millis()), + dt.format("%Y-%m-%d %H:%M:%S%.3f %:z").to_string() + ); + assert_eq!( + convert_timestamp_micros_to_string(dt.timestamp_micros()), + dt.format("%Y-%m-%d %H:%M:%S%.6f %:z").to_string() + ); + } + } + } + #[test] #[cfg_attr(miri, ignore)] // inline assembly is not supported fn test_convert_float16_to_string() {
