common: Replace unwrap with expect in (Milli)SecondsSinecUnixEpoch constructors

This commit is contained in:
Jonas Platte 2022-04-21 17:15:38 +02:00 committed by Jonas Platte
parent d73f84ebcc
commit eae7fa0a7d

View File

@ -24,7 +24,7 @@ impl MilliSecondsSinceUnixEpoch {
/// The current system time in milliseconds since the unix epoch.
pub fn now() -> Self {
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown", feature = "js")))]
return Self::from_system_time(SystemTime::now()).unwrap();
return Self::from_system_time(SystemTime::now()).expect("date out of range");
#[cfg(all(target_arch = "wasm32", target_os = "unknown", feature = "js"))]
return Self(f64_to_uint(js_sys::Date::now()));
@ -64,7 +64,7 @@ impl SecondsSinceUnixEpoch {
/// The current system-time as seconds since the unix epoch.
pub fn now() -> Self {
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown", feature = "js")))]
return Self::from_system_time(SystemTime::now()).unwrap();
return Self::from_system_time(SystemTime::now()).expect("date out of range");
#[cfg(all(target_arch = "wasm32", target_os = "unknown", feature = "js"))]
return Self(f64_to_uint(js_sys::Date::now() / 1000.0));
@ -84,7 +84,9 @@ impl SecondsSinceUnixEpoch {
#[cfg(all(target_arch = "wasm32", target_os = "unknown", feature = "js"))]
fn f64_to_uint(val: f64) -> UInt {
use std::convert::TryFrom;
UInt::try_from(val as u64).unwrap()
// UInt::MAX milliseconds is ~285 616 years, we do not account for that
// (or for dates before the unix epoch which would have to be negative)
UInt::try_from(val as u64).expect("date out of range")
}
#[cfg(test)]