Skip to main content

esp_idf_svc/
systime.rs

1//! System time
2use core::time::Duration;
3
4use crate::sys::*;
5
6/// Client for the ESP system time
7///
8/// If you have enabled the `std` feature, you can also call
9/// the standard `std::time::SystemTime` API in Rust.
10pub struct EspSystemTime;
11
12impl EspSystemTime {
13    /// Return the current system time
14    pub fn now(&self) -> Duration {
15        let mut tv_now: timeval = Default::default();
16
17        unsafe {
18            gettimeofday(&mut tv_now as *mut _, core::ptr::null_mut());
19        }
20
21        Duration::from_micros(tv_now.tv_sec as u64 * 1000000_u64 + tv_now.tv_usec as u64)
22    }
23}