1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//! System time
use core::time::Duration;

use crate::sys::*;

/// Client for the ESP system time
///
/// If you have enabled the `std` feature, you can also call
/// the standard `std::time::SystemTime` API in Rust.
pub struct EspSystemTime;

impl EspSystemTime {
    /// Return the current system time
    pub fn now(&self) -> Duration {
        let mut tv_now: timeval = Default::default();

        unsafe {
            gettimeofday(&mut tv_now as *mut _, core::ptr::null_mut());
        }

        Duration::from_micros(tv_now.tv_sec as u64 * 1000000_u64 + tv_now.tv_usec as u64)
    }
}