autd3_driver/ethercat/
dc_sys_time.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use time::OffsetDateTime;

use crate::error::AUTDDriverError;

use super::ECAT_DC_SYS_TIME_BASE;

/// The system time of the Distributed Clock
///
/// The system time is the time expressed in 1ns units with 2000-01-01 0:00:00 UTC as the reference.
/// It is expressed as a 64-bit unsigned integer and can represent about 584 years of time.
/// See [EtherCAT Distributed Clock](https://infosys.beckhoff.com/english.php?content=../content/1033/ethercatsystem/2469118347.html) for more information.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[repr(C)]
pub struct DcSysTime {
    dc_sys_time: u64,
}

impl DcSysTime {
    /// The zero point of the DcSysTime (2000-01-01 0:00:00 UTC)
    pub const ZERO: Self = Self { dc_sys_time: 0 };

    /// Returns the system time in nanoseconds
    pub const fn sys_time(&self) -> u64 {
        self.dc_sys_time
    }

    /// Converts the system time to the UTC time
    pub fn to_utc(&self) -> OffsetDateTime {
        ECAT_DC_SYS_TIME_BASE + std::time::Duration::from_nanos(self.dc_sys_time)
    }

    /// Creates a new instance from the UTC time
    pub fn from_utc(utc: OffsetDateTime) -> Result<Self, AUTDDriverError> {
        Ok(Self {
            dc_sys_time: u64::try_from((utc - ECAT_DC_SYS_TIME_BASE).whole_nanoseconds())
                .map_err(|_| AUTDDriverError::InvalidDateTime)?,
        })
    }

    /// Returns the system time of now
    pub fn now() -> Self {
        Self::from_utc(OffsetDateTime::now_utc()).unwrap()
    }
}

impl std::ops::Add<std::time::Duration> for DcSysTime {
    type Output = Self;

    fn add(self, rhs: std::time::Duration) -> Self::Output {
        Self {
            dc_sys_time: self.dc_sys_time + rhs.as_nanos() as u64,
        }
    }
}

impl std::ops::Sub<std::time::Duration> for DcSysTime {
    type Output = Self;

    fn sub(self, rhs: std::time::Duration) -> Self::Output {
        Self {
            dc_sys_time: self.dc_sys_time - rhs.as_nanos() as u64,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn now_dc_sys_time() {
        let t = DcSysTime::now();
        assert!(t.sys_time() > 0);
    }

    #[rstest::rstest]
    #[test]
    #[case(Ok(DcSysTime { dc_sys_time: 0 }), time::macros::datetime!(2000-01-01 0:0:0 UTC))]
    #[case(Ok(DcSysTime { dc_sys_time: 1000000000 }), time::macros::datetime!(2000-01-01 0:0:1 UTC))]
    #[case(Ok(DcSysTime { dc_sys_time: 31622400000000000 }), time::macros::datetime!(2001-01-01 0:0:0 UTC))]
    #[case(Err(AUTDDriverError::InvalidDateTime), time::macros::datetime!(1999-01-01 0:0:1 UTC))]
    #[case(Err(AUTDDriverError::InvalidDateTime), time::macros::datetime!(9999-01-01 0:0:1 UTC))]
    fn from_utc(#[case] expect: Result<DcSysTime, AUTDDriverError>, #[case] utc: OffsetDateTime) {
        assert_eq!(expect, DcSysTime::from_utc(utc));
    }

    #[rstest::rstest]
    #[test]
    #[case(time::macros::datetime!(2000-01-01 0:0:1 UTC))]
    #[case(time::macros::datetime!(2001-01-01 0:0:0 UTC))]
    fn to_utc(#[case] utc: OffsetDateTime) {
        assert_eq!(utc, DcSysTime::from_utc(utc).unwrap().to_utc());
    }

    #[test]
    fn addsub() {
        let utc = time::macros::datetime!(2000-01-01 0:0:0 UTC);
        let t = DcSysTime::from_utc(utc);
        assert!(t.is_ok());
        let t = t.unwrap() + std::time::Duration::from_secs(1);
        assert_eq!(1000000000, t.sys_time());

        let t = t - std::time::Duration::from_secs(1);
        assert_eq!(0, t.sys_time());
    }
}