#[cfg(all(feature = "serde", feature = "chrono"))]
mod serde;
#[cfg(feature = "chrono")]
mod timestamp_conversions;
mod timestamp_impls;
mod timestamp_operations;
use super::*;
use crate::Timestamp;
impl Timestamp {
pub fn normalize(&mut self) {
if self.nanos <= -NANOS_PER_SECOND || self.nanos >= NANOS_PER_SECOND {
if let Some(seconds) = self
.seconds
.checked_add((self.nanos / NANOS_PER_SECOND) as i64)
{
self.seconds = seconds;
self.nanos %= NANOS_PER_SECOND;
} else if self.nanos < 0 {
self.seconds = i64::MIN;
self.nanos = 0;
} else {
self.seconds = i64::MAX;
self.nanos = 999_999_999;
}
}
if self.nanos < 0 {
if let Some(seconds) = self.seconds.checked_sub(1) {
self.seconds = seconds;
self.nanos += NANOS_PER_SECOND;
} else {
debug_assert_eq!(self.seconds, i64::MIN);
self.nanos = 0;
}
}
}
pub fn try_normalize(mut self) -> Result<Timestamp, Timestamp> {
let before = self;
self.normalize();
if (self.seconds == i64::MAX || self.seconds == i64::MIN) && self.seconds != before.seconds {
Err(before)
} else {
Ok(self)
}
}
pub fn normalized(&self) -> Self {
let mut result = *self;
result.normalize();
result
}
pub fn date(year: i64, month: u8, day: u8) -> Result<Timestamp, TimestampError> {
Timestamp::date_time_nanos(year, month, day, 0, 0, 0, 0)
}
pub fn date_time(
year: i64,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
) -> Result<Timestamp, TimestampError> {
Timestamp::date_time_nanos(year, month, day, hour, minute, second, 0)
}
pub fn date_time_nanos(
year: i64,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
nanos: u32,
) -> Result<Timestamp, TimestampError> {
let date_time = datetime::DateTime {
year,
month,
day,
hour,
minute,
second,
nanos,
};
Timestamp::try_from(date_time)
}
}
impl Name for Timestamp {
const PACKAGE: &'static str = PACKAGE_PREFIX;
const NAME: &'static str = "Timestamp";
fn type_url() -> String {
type_url_for::<Self>()
}
}
impl From<std::time::SystemTime> for Timestamp {
fn from(system_time: std::time::SystemTime) -> Timestamp {
let (seconds, nanos) = match system_time.duration_since(std::time::UNIX_EPOCH) {
Ok(duration) => {
let seconds = i64::try_from(duration.as_secs()).unwrap();
(seconds, duration.subsec_nanos() as i32)
}
Err(error) => {
let duration = error.duration();
let seconds = i64::try_from(duration.as_secs()).unwrap();
let nanos = duration.subsec_nanos() as i32;
if nanos == 0 {
(-seconds, 0)
} else {
(-seconds - 1, 1_000_000_000 - nanos)
}
}
};
Timestamp { seconds, nanos }
}
}
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub enum TimestampError {
OutOfSystemRange(Timestamp),
ParseFailure,
InvalidDateTime,
}
impl fmt::Display for TimestampError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TimestampError::OutOfSystemRange(timestamp) => {
write!(
f,
"{} is not representable as a `SystemTime` because it is out of range",
timestamp
)
}
TimestampError::ParseFailure => {
write!(f, "failed to parse RFC-3339 formatted timestamp")
}
TimestampError::InvalidDateTime => {
write!(f, "invalid date or time")
}
}
}
}
impl std::error::Error for TimestampError {}
impl TryFrom<Timestamp> for std::time::SystemTime {
type Error = TimestampError;
fn try_from(mut timestamp: Timestamp) -> Result<std::time::SystemTime, Self::Error> {
let orig_timestamp = timestamp;
timestamp.normalize();
let system_time = if timestamp.seconds >= 0 {
std::time::UNIX_EPOCH.checked_add(time::Duration::from_secs(timestamp.seconds as u64))
} else {
std::time::UNIX_EPOCH.checked_sub(time::Duration::from_secs(
timestamp
.seconds
.checked_neg()
.ok_or(TimestampError::OutOfSystemRange(timestamp))? as u64,
))
};
let system_time = system_time.and_then(|system_time| {
system_time.checked_add(time::Duration::from_nanos(timestamp.nanos as u64))
});
system_time.ok_or(TimestampError::OutOfSystemRange(orig_timestamp))
}
}
impl FromStr for Timestamp {
type Err = TimestampError;
fn from_str(s: &str) -> Result<Timestamp, TimestampError> {
datetime::parse_timestamp(s).ok_or(TimestampError::ParseFailure)
}
}
impl fmt::Display for Timestamp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
datetime::DateTime::from(*self).fmt(f)
}
}