{"record":{"id":"748e886ec582304b","repo":"clockworklabs/SpacetimeDB","slug":"timestamp-with-i64-microseconds-before-unix-epoch","errorCode":null,"errorMessage":"Timestamp with i64 microseconds before Unix epoch overflows SystemTime","messagePattern":"Timestamp with i64 microseconds before Unix epoch overflows SystemTime","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/sats/src/timestamp.rs","lineNumber":97,"sourceCode":"        )\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    }\n\n    /// Returns the [`Duration`] delta between `self` and `earlier`, if `earlier` predates `self`.\n    ///","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/crates/sats/src/timestamp.rs#L79-L115","documentation":"`Timestamp::to_system_time` subtracts the pre-epoch magnitude from `SystemTime::UNIX_EPOCH` via `checked_sub(...).expect(\"...before Unix epoch overflows SystemTime\")`. This covers Timestamps before 1970; the subtraction only overflows on platforms whose SystemTime cannot represent ~292 years before the epoch, which none of Linux/macOS/Windows do — so this is a portability guard, rarely hit in practice.","triggerScenarios":"Converting a Timestamp with a large negative micros value (far pre-epoch) to SystemTime on a platform with a narrow pre-1970 clock range; unreachable on mainstream targets.","commonSituations":"Fuzzing with extreme negative timestamps; exotic embedded/wasm targets with restricted clock representations.","solutions":["Keep pre-epoch math in Timestamp; convert to SystemTime only where the value is known reasonable.","Validate the magnitude against the platform's representable range before converting.","Treat hits of this expect as a signal you are converting unvalidated extreme values."],"exampleFix":"// before\nlet st = ts.to_system_time(); // may panic for far pre-epoch values on niche platforms\n\n// after: gate the conversion\nlet st = if ts >= Timestamp::from_micros_since_unix_epoch(MIN_SAFE) { ts.to_system_time() } else { SystemTime::UNIX_EPOCH };","handlingStrategy":"validation","validationCode":"// Guard pre-epoch magnitudes before converting\nif ts < Timestamp::from_micros_since_unix_epoch(-MIN_SAFE_MICROS) { /* skip/fallback */ } else { let st = ts.to_system_time(); }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep pre-epoch math inside Timestamp; avoid SystemTime round-trips.","Validate negative timestamp magnitudes against platform limits on exotic targets."],"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"}