{"record":{"id":"040bb505d15f0329","repo":"pola-rs/polars","slug":"invalid-or-out-of-range-datetime","errorCode":null,"errorMessage":"invalid or out-of-range datetime","messagePattern":"invalid or out-of-range datetime","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-arrow/src/temporal_conversions.rs","lineNumber":34,"sourceCode":"pub const NANOSECONDS: i64 = 1_000_000_000;\n/// Number of milliseconds in a day\npub const MILLISECONDS_IN_DAY: i64 = SECONDS_IN_DAY * MILLISECONDS;\n/// Number of microseconds in a day\npub const MICROSECONDS_IN_DAY: i64 = SECONDS_IN_DAY * MICROSECONDS;\n/// Number of nanoseconds in a day\npub const NANOSECONDS_IN_DAY: i64 = SECONDS_IN_DAY * NANOSECONDS;\n/// Number of days between 0001-01-01 and 1970-01-01\npub const EPOCH_DAYS_FROM_CE: i32 = 719_163;\n\n#[inline]\nfn unix_epoch() -> NaiveDateTime {\n    DateTime::UNIX_EPOCH.naive_utc()\n}\n\n/// converts a `i32` representing a `date32` to [`NaiveDateTime`]\n#[inline]\npub fn date32_to_datetime(v: i32) -> NaiveDateTime {\n    date32_to_datetime_opt(v).expect(\"invalid or out-of-range datetime\")\n}\n\n/// converts a `i32` representing a `date32` to [`NaiveDateTime`]\n#[inline]\npub fn date32_to_datetime_opt(v: i32) -> Option<NaiveDateTime> {\n    let delta = TimeDelta::try_days(v.into())?;\n    unix_epoch().checked_add_signed(delta)\n}\n\n/// converts a `i32` representing a `date32` to [`NaiveDate`]\n#[inline]\npub fn date32_to_date(days: i32) -> NaiveDate {\n    date32_to_date_opt(days).expect(\"out-of-range date\")\n}\n\n/// converts a `i32` representing a `date32` to [`NaiveDate`]\n#[inline]\npub fn date32_to_date_opt(days: i32) -> Option<NaiveDate> {","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-arrow/src/temporal_conversions.rs#L16-L52","documentation":"`date32_to_datetime` converts an i32 `date32` (days since Unix epoch) to a `NaiveDateTime`. The conversion goes through the fallible `date32_to_datetime_opt`, which returns `None` when the day count (via `TimeDelta::try_days`) or the resulting date is out of chrono's representable range. The infallible wrapper `.expect(\"invalid or out-of-range datetime\")` panics in that case.","triggerScenarios":"Calling `date32_to_datetime(v)` with a `v` so large or negative that `TimeDelta::try_days` fails or the resulting timestamp overflows chrono's `NaiveDateTime` range (roughly ±262,000 years).","commonSituations":"Corrupt or sentinel i32 values (e.g. i32::MAX) in a date32 column; unit confusion (passing seconds/milliseconds instead of days); extreme synthetic test data.","solutions":["Call the fallible `date32_to_datetime_opt(v)` instead and handle the `None` case.","Validate `v` is a plausible day offset before conversion (e.g. within a sane year range around the epoch).","Sanitize/replace sentinel values (i32::MAX/MIN, u32-cast garbage) in the source column before conversion."],"exampleFix":"// before\nlet dt = date32_to_datetime(v); // panics on i32::MAX\n// after\nmatch date32_to_datetime_opt(v) {\n    Some(dt) => dt,\n    None => return Err(format!(\"date32 value {v} out of representable range\")),\n}","handlingStrategy":"validation","validationCode":"fn date32_in_range(v: i32) -> bool {\n    (-100_000..100_000).contains(&v) // days within ±~274 years of epoch\n}","typeGuard":null,"tryCatchPattern":"// Prefer the fallible variant over catching the panic:\nmatch date32_to_datetime_opt(v) {\n    Some(dt) => /* use dt */,\n    None => /* handle out-of-range */,\n}","preventionTips":["Use the `_opt` variants in application code","Filter sentinel values (i32::MAX/MIN) from date32 columns before conversion","Confirm units: date32 is days since 1970-01-01"],"tags":["arrow","chrono","datetime","out-of-range"],"backgroundTag":"value-out-of-range","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}