{"record":{"id":"4e6d555663135fb9","repo":"clockworklabs/SpacetimeDB","slug":"timestamp-with-i64-microseconds-since-unix-epoch-o","errorCode":null,"errorMessage":"Timestamp with i64 microseconds since Unix epoch overflows SystemTime","messagePattern":"Timestamp with i64 microseconds since Unix epoch overflows SystemTime","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/sats/src/timestamp.rs","lineNumber":94,"sourceCode":"                .as_micros()\n                .try_into()\n                .expect(\"Duration since Unix epoch overflows i64 microseconds\"),\n        )\n    }\n\n    /// Convert `self` into a [`SystemTime`] which refers to approximately the same point in time.\n    ///\n    /// This conversion may lose precision, as [`SystemTime`]'s prevision varies depending on platform.\n    /// E.g. Unix targets have microsecond precision, but Windows only 100-microsecond precision.\n    ///\n    /// This conversion may panic if `self` is out of bounds for [`SystemTime`].\n    /// We are not aware of any platforms for which [`SystemTime`] offers a smaller range than [`Timestamp`],\n    /// but such a platform may exist.\n    pub fn to_system_time(self) -> SystemTime {\n        match self.to_duration_since_unix_epoch() {\n            Ok(positive) => SystemTime::UNIX_EPOCH\n                .checked_add(positive)\n                .expect(\"Timestamp with i64 microseconds since Unix epoch overflows SystemTime\"),\n            Err(negative) => SystemTime::UNIX_EPOCH\n                .checked_sub(negative)\n                .expect(\"Timestamp with i64 microseconds before Unix epoch overflows SystemTime\"),\n        }\n    }\n\n    /// Convert a [`SystemTime`] into a [`Timestamp`] which refers to approximately the same point in time.\n    ///\n    /// This conversion may panic if `system_time` is out of bounds for [`Duration`].\n    /// [`SystemTime`]'s range is larger than [`Timestamp`] on both Unix and Windows targets,\n    /// so times in the far past or far future may panic.\n    /// [`Timestamp`]'s range is approximately 292 years before and after the Unix epoch.\n    pub fn from_system_time(system_time: SystemTime) -> Self {\n        let duration = system_time\n            .duration_since(SystemTime::UNIX_EPOCH)\n            .expect(\"SystemTime predates the Unix epoch\");\n        Self::from_duration_since_unix_epoch(duration)\n    }","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/crates/sats/src/timestamp.rs#L76-L112","documentation":"`Timestamp::to_system_time` adds the (positive, post-epoch) micros to `SystemTime::UNIX_EPOCH` via `checked_add(...).expect(...)`. On mainstream platforms SystemTime spans at least i64 seconds, so an i64-micros Timestamp fits easily; the panic is only reachable on exotic platforms whose clock range is narrower than Timestamp's (~±292 years).","triggerScenarios":"Converting an extreme far-future Timestamp (near i64 micros max, year ~294,247) to SystemTime on a platform with a limited clock representation — not on Linux/macOS/Windows where the range is far larger.","commonSituations":"Cross-compiling to a niche RTOS/wasm target with a restricted SystemTime; feeding maxed-out timestamps from fuzzing into to_system_time.","solutions":["Prefer staying in Timestamp arithmetic; only convert to SystemTime at the final boundary (file times, HTTP dates) where values are sane.","Clamp or validate Timestamp values against the target platform's clock range before converting.","For fuzzed/extreme inputs, range-check to_micros() against your platform's bounds first."],"exampleFix":"// before\nlet st = ts.to_system_time(); // panics on platforms with narrow clock range\n\n// after: keep arithmetic in Timestamp; convert only known-sane values\nlet st = if ts.to_micros().abs() < sane_bound { ts.to_system_time() } else { fallback_time() };","handlingStrategy":"validation","validationCode":"// Only convert known-sane timestamps; keep arithmetic in Timestamp elsewhere\nlet sane = ts.to_micros().abs() < 86_400_000_000i64 * 366 * 400; // ~400 years\nif sane { let st = ts.to_system_time(); }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Convert to SystemTime only at final OS boundaries with validated values.","Avoid feeding fuzzed or maxed-out timestamps into to_system_time.","On niche platforms, document and test the clock range before relying on this conversion."],"tags":["rust","timestamp","systemtime","overflow","portability"],"backgroundTag":"timestamp-overflow","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}