use std::borrow::Cow;
use std::fmt;
use std::ops::{Add, Sub};
use std::str::FromStr;
use std::time::{Duration, SystemTime};
use humantime::TimestampError;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use thiserror::Error;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Timestamp(u64);
impl Timestamp {
pub const UNIX_EPOCH: Self = Self(0);
const MAX: u64 = 253_402_300_799;
pub fn now() -> Self {
Self::try_from(SystemTime::now()).expect("system clock outside timestamp range")
}
pub fn from_unix_secs(seconds: u64) -> Result<Self, InvalidTimestamp> {
if seconds <= Self::MAX {
Ok(Self(seconds))
} else {
Err(InvalidTimestamp)
}
}
pub fn from_unix_micros(micros: i64) -> Result<Self, InvalidTimestamp> {
let micros = u64::try_from(micros).map_err(|_| InvalidTimestamp)?;
Self::from_unix_secs(micros.div_ceil(1_000_000))
}
pub fn from_rfc3339(value: &str) -> Result<Self, TimestampError> {
let time = humantime::parse_rfc3339(value)?;
Self::try_from(time).map_err(|_| TimestampError::OutOfRange)
}
pub fn as_secs(self) -> u64 {
self.0
}
pub fn as_micros(self) -> u64 {
self.0 * 1_000_000
}
pub fn as_rfc3339(self) -> Rfc3339Timestamp {
Rfc3339Timestamp(self)
}
pub fn checked_add(self, duration: Duration) -> Option<Self> {
let seconds = self.0.checked_add(duration.as_secs())?;
let seconds = seconds.checked_add(u64::from(duration.subsec_nanos() != 0))?;
Self::from_unix_secs(seconds).ok()
}
pub fn saturating_add(self, duration: Duration) -> Self {
self.checked_add(duration).unwrap_or(Self(Self::MAX))
}
pub fn checked_sub(self, duration: Duration) -> Option<Self> {
if duration > Duration::from_secs(self.0) {
return None;
}
Some(Self(self.0 - duration.as_secs()))
}
pub fn checked_duration_since(self, earlier: Self) -> Option<Duration> {
self.0.checked_sub(earlier.0).map(Duration::from_secs)
}
}
impl TryFrom<SystemTime> for Timestamp {
type Error = InvalidTimestamp;
fn try_from(time: SystemTime) -> Result<Self, Self::Error> {
let duration = time
.duration_since(SystemTime::UNIX_EPOCH)
.map_err(|_| InvalidTimestamp)?;
let seconds = duration
.as_secs()
.checked_add(u64::from(duration.subsec_nanos() != 0))
.ok_or(InvalidTimestamp)?;
Self::from_unix_secs(seconds)
}
}
impl From<Timestamp> for SystemTime {
fn from(time: Timestamp) -> Self {
Self::UNIX_EPOCH + Duration::from_secs(time.0)
}
}
impl Add<Duration> for Timestamp {
type Output = Self;
fn add(self, duration: Duration) -> Self {
self.checked_add(duration)
.expect("timestamp addition out of range")
}
}
impl Sub<Duration> for Timestamp {
type Output = Self;
fn sub(self, duration: Duration) -> Self {
self.checked_sub(duration)
.expect("timestamp subtraction out of range")
}
}
impl Serialize for Timestamp {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
SystemTime::from(*self).serialize(serializer)
}
}
impl<'de> Deserialize<'de> for Timestamp {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Self::try_from(SystemTime::deserialize(deserializer)?).map_err(serde::de::Error::custom)
}
}
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[error("timestamp is outside the Unix epoch through year 9999")]
pub struct InvalidTimestamp;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Rfc3339Timestamp(Timestamp);
impl Rfc3339Timestamp {
pub fn into_inner(self) -> Timestamp {
self.0
}
}
impl fmt::Display for Rfc3339Timestamp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
humantime::format_rfc3339_seconds(self.0.into()).fmt(f)
}
}
impl FromStr for Rfc3339Timestamp {
type Err = TimestampError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
Timestamp::from_rfc3339(value).map(Self)
}
}
impl Serialize for Rfc3339Timestamp {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(self)
}
}
impl<'de> Deserialize<'de> for Rfc3339Timestamp {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
#[derive(Deserialize)]
#[serde(transparent)]
struct BorrowedStr<'a>(#[serde(borrow)] Cow<'a, str>);
let BorrowedStr(value) = BorrowedStr::deserialize(deserializer)?;
value.parse().map_err(serde::de::Error::custom)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rounding_and_arithmetic() {
let epoch = SystemTime::UNIX_EPOCH;
for (nanos, seconds) in [(0, 0), (1, 1), (999_999_999, 1), (1_000_000_000, 1)] {
let time = Timestamp::try_from(epoch + Duration::from_nanos(nanos)).unwrap();
assert_eq!(time.as_secs(), seconds);
assert_eq!(time.as_micros(), seconds * 1_000_000);
assert_eq!(Timestamp::try_from(SystemTime::from(time)).unwrap(), time);
}
let time = Timestamp::from_unix_secs(10).unwrap();
assert_eq!((time + Duration::from_millis(1500)).as_secs(), 12);
assert_eq!((time - Duration::from_millis(1500)).as_secs(), 9);
assert_eq!(time.checked_duration_since(time), Some(Duration::ZERO));
assert!(Timestamp::try_from(epoch - Duration::from_nanos(1)).is_err());
assert!(Timestamp::from_unix_micros(-1).is_err());
assert_eq!(Timestamp::from_unix_micros(1).unwrap().as_secs(), 1);
assert!(Timestamp::from_unix_micros(i64::MAX).is_err());
assert!(
Timestamp::UNIX_EPOCH
.checked_sub(Duration::from_nanos(1))
.is_none()
);
let max = Timestamp::from_unix_secs(Timestamp::MAX).unwrap();
assert!(max.checked_add(Duration::from_nanos(1)).is_none());
assert!(max.checked_add(Duration::MAX).is_none());
assert_eq!(max.saturating_add(Duration::from_nanos(1)), max);
assert_eq!(
time.saturating_add(Duration::from_millis(1500)).as_secs(),
12
);
assert_eq!(max.as_rfc3339().to_string(), "9999-12-31T23:59:59Z");
}
#[test]
fn serialization_formats() {
let legacy = r#"{"secs_since_epoch":1700000000,"nanos_since_epoch":1}"#;
let time: Timestamp = serde_json::from_str(legacy).unwrap();
assert_eq!(time.as_secs(), 1_700_000_001);
let json = serde_json::to_string(&time).unwrap();
assert_eq!(
json,
r#"{"secs_since_epoch":1700000001,"nanos_since_epoch":0}"#
);
assert_eq!(serde_json::from_str::<Timestamp>(&json).unwrap(), time);
assert_eq!(
serde_json::from_str::<SystemTime>(&json).unwrap(),
time.into()
);
let rfc: Rfc3339Timestamp =
serde_json::from_str(r#""2023-11-14T22:13:20.000001Z""#).unwrap();
assert_eq!(rfc.into_inner(), time);
assert_eq!(
serde_json::to_string(&rfc).unwrap(),
r#""2023-11-14T22:13:21Z""#
);
assert_eq!(rfc.to_string(), "2023-11-14T22:13:21Z");
assert!(
"9999-12-31T23:59:59.1Z"
.parse::<Rfc3339Timestamp>()
.is_err()
);
}
}