use crate::{Error, Result};
use core::fmt;
use core::fmt::Formatter;
use encoding::{Decode, Encode, Reader, Writer};
#[cfg(feature = "std")]
use {
core::time::Duration,
std::time::{SystemTime, UNIX_EPOCH},
};
#[allow(clippy::as_conversions, reason = "constant")]
pub(super) const MAX_SECS: u64 = i64::MAX as u64;
pub(super) const FOREVER_SECS: u64 = u64::MAX;
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub(super) struct UnixTime {
secs: u64,
#[cfg(feature = "std")]
time: SystemTime,
}
impl UnixTime {
#[cfg(not(feature = "std"))]
pub(super) fn new(secs: u64) -> Result<Self> {
if secs == FOREVER_SECS || secs <= MAX_SECS {
Ok(Self { secs })
} else {
Err(Error::Time)
}
}
#[cfg(feature = "std")]
pub(super) fn new(secs: u64) -> Result<Self> {
let time_secs = if secs == FOREVER_SECS { MAX_SECS } else { secs };
if time_secs > MAX_SECS {
return Err(Error::Time);
}
match UNIX_EPOCH.checked_add(Duration::from_secs(time_secs)) {
Some(time) => Ok(Self { secs, time }),
None => Err(Error::Time),
}
}
#[cfg(feature = "std")]
pub(super) fn now() -> Result<Self> {
SystemTime::now().try_into()
}
}
impl Decode for UnixTime {
type Error = Error;
fn decode(reader: &mut impl Reader) -> Result<Self> {
u64::decode(reader)?.try_into()
}
}
impl Encode for UnixTime {
fn encoded_len(&self) -> encoding::Result<usize> {
self.secs.encoded_len()
}
fn encode(&self, writer: &mut impl Writer) -> encoding::Result<()> {
self.secs.encode(writer)?;
Ok(())
}
}
impl From<UnixTime> for u64 {
fn from(unix_time: UnixTime) -> u64 {
unix_time.secs
}
}
#[cfg(feature = "std")]
impl From<UnixTime> for SystemTime {
fn from(unix_time: UnixTime) -> SystemTime {
unix_time.time
}
}
impl TryFrom<u64> for UnixTime {
type Error = Error;
fn try_from(unix_secs: u64) -> Result<UnixTime> {
Self::new(unix_secs)
}
}
#[cfg(feature = "std")]
impl TryFrom<SystemTime> for UnixTime {
type Error = Error;
fn try_from(time: SystemTime) -> Result<UnixTime> {
Self::new(time.duration_since(UNIX_EPOCH)?.as_secs())
}
}
impl fmt::Debug for UnixTime {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.secs)
}
}
#[cfg(test)]
mod tests {
use super::{FOREVER_SECS, MAX_SECS, UnixTime};
use crate::Error;
#[test]
fn new_with_max_secs() {
assert!(UnixTime::new(MAX_SECS).is_ok());
}
#[test]
fn new_over_max_secs_returns_error() {
assert_eq!(UnixTime::new(MAX_SECS + 1), Err(Error::Time));
}
#[test]
fn new_with_forever_secs_is_ok() {
assert!(UnixTime::new(FOREVER_SECS).is_ok());
}
#[test]
fn forever_secs_preserves_raw_value() {
let t = UnixTime::new(FOREVER_SECS).unwrap();
assert_eq!(u64::from(t), FOREVER_SECS);
}
#[test]
fn forever_secs_greater_than_any_normal_timestamp() {
let forever = UnixTime::new(FOREVER_SECS).unwrap();
let now = UnixTime::new(MAX_SECS).unwrap();
assert!(forever > now);
}
}