Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle-localtime: drop TryFrom SystemTime
Adrian Duke committed 3 months ago
commit 36a474bd6ff960b9618cd6d72a8e202f3d275aa0
parent a9a703da0472c5669d0e5affef0a4660ffac444a
1 file changed +13 -18
modified crates/radicle-localtime/src/lib.rs
@@ -3,7 +3,7 @@
//! Taken from <https://github.com/cloudhead/localtime>

use std::sync::atomic;
-
use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH};
+
use std::time::{SystemTime, UNIX_EPOCH};

/// Local time.
///
@@ -30,15 +30,21 @@ impl LocalTime {
    pub fn now() -> Self {
        static LAST: atomic::AtomicU64 = atomic::AtomicU64::new(0);

-
        let now = Self::try_from(SystemTime::now()).unwrap().as_secs();
-
        let last = LAST.load(atomic::Ordering::SeqCst);
+
        let now = SystemTime::now()
+
            .duration_since(UNIX_EPOCH)
+
            .map(|duration| Self {
+
                millis: duration.as_millis(),
+
            })
+
            .unwrap();
+
        let last_in_secs = LAST.load(atomic::Ordering::SeqCst);
+
        let now_in_secs = now.as_secs();

        // If the current time is in the past, return the last recorded time instead.
-
        if now < last {
-
            Self::from_secs(last)
+
        if now_in_secs < last_in_secs {
+
            Self::from_secs(last_in_secs)
        } else {
-
            LAST.store(now, atomic::Ordering::SeqCst);
-
            LocalTime::from_secs(now)
+
            LAST.store(now_in_secs, atomic::Ordering::SeqCst);
+
            now
        }
    }

@@ -100,17 +106,6 @@ impl LocalTime {
    }
}

-
/// Convert a `SystemTime` into a local time.
-
impl TryFrom<SystemTime> for LocalTime {
-
    type Error = SystemTimeError;
-

-
    fn try_from(system: SystemTime) -> Result<Self, Self::Error> {
-
        system.duration_since(UNIX_EPOCH).map(|duration| Self {
-
            millis: duration.as_millis(),
-
        })
-
    }
-
}
-

/// Subtract two local times. Yields a duration.
impl std::ops::Sub<LocalTime> for LocalTime {
    type Output = LocalDuration;