blvm_protocol/time.rs
1//! Safe time utilities for fault tolerance
2//!
3//! Provides time operations that handle system clock errors gracefully,
4//! avoiding panics from misconfigured clocks (e.g. before UNIX epoch).
5
6use std::time::{SystemTime, UNIX_EPOCH};
7
8/// Get current Unix timestamp (seconds since epoch)
9///
10/// Returns 0 if system time is before epoch (misconfigured VMs, containers).
11/// Prevents panics from `SystemTime::now().duration_since(UNIX_EPOCH).unwrap()`.
12#[inline]
13pub fn current_timestamp() -> u64 {
14 SystemTime::now()
15 .duration_since(UNIX_EPOCH)
16 .map(|d| d.as_secs())
17 .unwrap_or(0)
18}