{"record":{"id":"d45a8ea736cbc90a","repo":"nautechsystems/nautilus_trader","slug":"failed-to-subtract-n-years-from-datetime-mont","errorCode":null,"errorMessage":"Failed to subtract {n} years from {datetime}: month count overflow","messagePattern":"Failed to subtract (.+?) years from (.+?): month count overflow","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/core/src/datetime.rs","lineNumber":622,"sourceCode":"///\n/// Returns an error if the resulting date would be invalid or out of range.\npub fn add_n_years(datetime: Timestamp, n: u32) -> anyhow::Result<Timestamp> {\n    let months = n.checked_mul(12).ok_or_else(|| {\n        anyhow::anyhow!(\"Failed to add {n} years to {datetime}: month count overflow\")\n    })?;\n\n    shift_months(datetime, i64::from(months))\n        .map_err(|_| anyhow::anyhow!(\"Failed to add {n} years to {datetime}\"))\n}\n\n/// Subtract `n` years from a Jiff [`Timestamp`].\n///\n/// # Errors\n///\n/// Returns an error if the resulting date would be invalid or out of range.\npub fn subtract_n_years(datetime: Timestamp, n: u32) -> anyhow::Result<Timestamp> {\n    let months = n.checked_mul(12).ok_or_else(|| {\n        anyhow::anyhow!(\"Failed to subtract {n} years from {datetime}: month count overflow\")\n    })?;\n\n    shift_months(datetime, -i64::from(months))\n        .map_err(|_| anyhow::anyhow!(\"Failed to subtract {n} years from {datetime}\"))\n}\n\n/// Add `n` years to a given UNIX nanoseconds timestamp.\n///\n/// # Errors\n///\n/// Returns an error if the resulting timestamp is out of range or invalid.\npub fn add_n_years_nanos(unix_nanos: UnixNanos, n: u32) -> anyhow::Result<UnixNanos> {\n    let datetime = unix_nanos.to_datetime_utc();\n    let result = add_n_years(datetime, n)?;\n    let timestamp = result.as_nanosecond();\n\n    let nanos =\n        u64::try_from(timestamp).map_err(|_| anyhow::anyhow!(\"Negative timestamp not allowed\"))?;","sourceCodeStart":604,"sourceCodeEnd":640,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/core/src/datetime.rs#L604-L640","documentation":"subtract_n_years converts n years into a month count via checked_mul(12); if the product exceeds u32 the subtraction cannot be represented and this error is raised. Like the add variant, it prevents silent wraparound in month arithmetic.","triggerScenarios":"Calling subtract_n_years(datetime, n) with n > 357,913,941 so n.checked_mul(12) overflows u32.","commonSituations":"A garbage/huge u32 year offset from malformed config or data, attempting to rewind a timestamp before epoch in backfill logic, and overflow unit tests.","solutions":["Use a sane year offset (n <= 357,913,941; practically small).","Validate n at the boundary before calling subtract_n_years.","Fix upstream parsing that produced the unbounded value."],"exampleFix":"// before\nlet ts = subtract_n_years(now, n).unwrap();\n// after\nanyhow::ensure!(n <= 100_000, \"year offset too large: {n}\");\nlet ts = subtract_n_years(now, n)?;","handlingStrategy":"validation","validationCode":"fn valid_year_offset(n: u32) -> bool { n <= 357_913_941 }\n// reject before calling subtract_n_years","typeGuard":"fn is_sane_years(n: u32) -> bool { n <= 10_000 }","tryCatchPattern":null,"preventionTips":["Bound lookback/year values at the config layer.","Test any code path that can feed unbounded u32 values into date math."],"tags":["datetime","overflow","rust"],"backgroundTag":"value-out-of-range","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}