oct/error/
system_time_decode_error.rs

1// Copyright 2024-2025 Gabriel Bjørnager Jensen.
2//
3// This Source Code Form is subject to the terms of
4// the Mozilla Public License, v. 2.0. If a copy of
5// the MPL was not distributed with this file, you
6// can obtain one at:
7// <https://mozilla.org/MPL/2.0/>.
8
9use core::convert::Infallible;
10use core::error::Error;
11use core::fmt::{self, Display, Formatter};
12use core::hint::unreachable_unchecked;
13
14/// The [`SystemTime`](std::time::SystemTime) type could not represent a UNIX timestamp.
15///
16/// Note that a UNIX timestamp is here defined as a signed, 64-bit integer denoting a difference of time to 1 january 1970, as measured in Greenwich using seconds.
17/// This error should therefore not occur on systems that use the same or a more precise counter.
18#[cfg_attr(doc, doc(cfg(feature = "std")))]
19#[derive(Debug, Eq, PartialEq)]
20#[must_use]
21pub struct SystemTimeDecodeError {
22	/// The unrepresentable timestamp.
23	pub timestamp: i64,
24}
25
26#[cfg_attr(doc, doc(cfg(feature = "std")))]
27impl Display for SystemTimeDecodeError {
28	#[inline(always)]
29	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
30		write!(f, "could not represent `{}` as a system timestamp", self.timestamp)
31	}
32}
33
34#[cfg_attr(doc, doc(cfg(feature = "std")))]
35impl Error for SystemTimeDecodeError { }
36
37#[cfg_attr(doc, doc(cfg(feature = "std")))]
38impl From<Infallible> for SystemTimeDecodeError {
39	#[inline(always)]
40	fn from(_value: Infallible) -> Self {
41		// SAFETY: `Infallible` objects can never be con-
42		// structed.
43		unsafe { unreachable_unchecked() };
44	}
45}