/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use std::time::{Duration, SystemTime, UNIX_EPOCH};
/// The maximum number of whole milliseconds that can be represented in /// an `f64` without loss of precision. const MAX_FLOAT_MILLISECONDS: f64 = ((1u64 << f64::MANTISSA_DIGITS) - 1) as f64;
/// Typesafe way to manage server timestamps without accidentally mixing them up with /// local ones. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Default)] pubstruct ServerTimestamp(pub i64);
pubfn from_millis(ts: i64) -> Self { // Catch it in tests, but just complain and replace with 0 otherwise.
debug_assert!(ts >= 0, "Bad timestamp: {}", ts); if ts >= 0 { Self(ts)
} else {
error_support::report_error!( "sync15-illegal-timestamp", "Illegal timestamp, substituting 0: {}",
ts
); Self(0)
}
}
}
// This lets us use these in hyper header! blocks. impl std::str::FromStr for ServerTimestamp { type Err = std::num::ParseFloatError; fn from_str(s: &str) -> Result<Self, Self::Err> { let val = f64::from_str(s)?;
Ok(Self::from_float_seconds(val))
}
}
/// Returns None if `other` is later than `self` (Duration may not represent /// negative timespans in rust). #[inline] pubfn duration_since(self, other: ServerTimestamp) -> Option<Duration> { let delta = self.0 - other.0; if delta < 0 {
None
} else {
Some(Duration::from_millis(delta as u64))
}
}
/// Get the milliseconds for the timestamp. #[inline] pubfn as_millis(self) -> i64 { self.0
}
}
/// Exposed only for tests that need to create a server timestamp from the system time /// Please be cautious when constructing the timestamp directly, as constructing the server /// timestamp from system time is almost certainly not what you'd want to do for non-test code impl TryFrom<SystemTime> for ServerTimestamp { type Error = std::time::SystemTimeError;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.