{"record":{"id":"7b97e129508147c8","repo":"nautechsystems/nautilus_trader","slug":"error-calling-systemtime","errorCode":null,"errorMessage":"Error calling `SystemTime`","messagePattern":"Error calling `SystemTime`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/core/src/time.rs","lineNumber":92,"sourceCode":"    ATOMIC_CLOCK_STATIC.get_or_init(|| AtomicTime::new(false, UnixNanos::default()))\n}\n\n/// Returns the duration since the UNIX epoch based on [`SystemTime::now()`].\n///\n/// # Panics\n///\n/// Panics if the system time is set before the UNIX epoch.\n#[inline(always)]\n#[must_use]\npub fn duration_since_unix_epoch() -> Duration {\n    // The expect() is acceptable here because:\n    // - SystemTime failure indicates catastrophic system clock issues\n    // - This would affect the entire application's ability to function\n    // - Alternative error handling would complicate all time-dependent code paths\n    // - Such failures are extremely rare in practice and indicate hardware/OS problems\n    wall_clock_now()\n        .duration_since(UNIX_EPOCH)\n        .expect(\"Error calling `SystemTime`\")\n}\n\n/// Returns the current wall-clock time as [`SystemTime`].\n///\n/// Under simulation (`simulation` + `cfg(madsim)`), returns virtual wall-clock\n/// time from the madsim deterministic scheduler when called from inside a\n/// madsim runtime. When called outside a runtime (e.g. plain `#[rstest]` test\n/// bodies), falls back to [`SystemTime::now()`], which under `cfg(madsim)` is\n/// libc-intercepted by madsim and resolves to the same real syscall it would\n/// in a normal build. Under normal builds, returns [`SystemTime::now()`].\n///\n/// This is the wall-clock seam. It preserves Unix-epoch semantics (unlike\n/// `tokio::time::Instant` which is monotonic and carries no epoch).\n#[inline(always)]\n#[must_use]\nfn wall_clock_now() -> SystemTime {\n    #[cfg(not(all(feature = \"simulation\", madsim)))]\n    {","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/core/src/time.rs#L74-L110","documentation":"duration_since_unix_epoch in crates/core/src/time.rs:92 reads the wall clock via wall_clock_now() and computes duration_since(UNIX_EPOCH), unwrapping with expect(\"Error calling `SystemTime`\"). This can only fail if the system clock is set before 1970-01-01 (or the OS clock API misbehaves), which the comments describe as a catastrophic environment condition affecting all time-dependent code, hence the deliberate panic.","triggerScenarios":"Calling duration_since_unix_epoch (directly or via nanos_since_unix_epoch, drain_buffer timestamps) on a machine whose system clock is set earlier than the Unix epoch — e.g. a device with a dead RTC battery booting to its epoch default, a VM with a broken clock, or an embedded board with an unset RTC.","commonSituations":"Embedded/IoT devices and SBCs without battery-backed RTC booting at 1970-01-01; containers/VMs with incorrect clock sync; misconfigured NTP leaving a host far in the past; test environments faking the clock incorrectly.","solutions":["Fix the system clock: run NTP/chrony sync or set the date manually before launching the application.","Replace the RTC battery or configure the device to sync time at boot (e.g. fake-hwclock, systemd-time-wait-sync).","Ensure the host/VM clock is synchronized before starting the trading process; gate startup on time sync if needed.","If the platform cannot guarantee a sane clock, seed the clock from a reliable source at process startup and verify it is past the epoch before calling time APIs."],"exampleFix":"# before (host clock before 1970 due to dead RTC)\n./nautilus_trader  # panics: Error calling `SystemTime`\n# after\nsudo chronyd -q || sudo ntpdate pool.ntp.org   # sync clock first\n./nautilus_trader","handlingStrategy":"fallback","validationCode":"# Ops/startup gate\nimport time\nnow = time.time()\nif now < 0:\n    raise SystemExit(\"system clock is before Unix epoch; sync NTP before starting\")","typeGuard":"fn clock_is_sane() -> bool {\n    std::time::SystemTime::now()\n        .duration_since(std::time::UNIX_EPOCH)\n        .is_ok()\n}","tryCatchPattern":"// If you must call time APIs on questionable hosts, gate first\nif !clock_is_sane() {\n    return Err(\"system clock before Unix epoch; refusing to start\");\n}\nlet nanos = nanos_since_unix_epoch();","preventionTips":["Run NTP/chrony on all hosts and gate startup on successful time sync.","Replace RTC batteries on embedded devices and sync time at boot.","Verify VM/container clock sync in deployment health checks.","Check the clock once at process startup before any time-dependent work."],"tags":["time","system-clock","environment","panic"],"backgroundTag":"invalid-env-var-value","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"}