blvm-protocol 0.1.5

Bitcoin Commons BLVM: Bitcoin protocol abstraction layer for multiple variants and evolution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//! Safe time utilities for fault tolerance
//!
//! Provides time operations that handle system clock errors gracefully,
//! avoiding panics from misconfigured clocks (e.g. before UNIX epoch).

use std::time::{SystemTime, UNIX_EPOCH};

/// Get current Unix timestamp (seconds since epoch)
///
/// Returns 0 if system time is before epoch (misconfigured VMs, containers).
/// Prevents panics from `SystemTime::now().duration_since(UNIX_EPOCH).unwrap()`.
#[inline]
pub fn current_timestamp() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}