Skip to main content

rustflexstack/security/
time_service.rs

1//! ITS time utilities.
2//!
3//! The ITS epoch is 2004-01-01 00:00:00 UTC (TAI), which corresponds to
4//! Unix timestamp 1072915200.  A leap-second offset of 5 s is added to
5//! convert from UTC to TAI.
6
7use std::time::{SystemTime, UNIX_EPOCH};
8
9/// Unix timestamp of the ITS epoch (2004-01-01T00:00:00 UTC).
10pub const ITS_EPOCH: u64 = 1_072_915_200;
11
12/// [`ITS_EPOCH`] in milliseconds.
13pub const ITS_EPOCH_MS: u64 = ITS_EPOCH * 1_000;
14
15/// Leap seconds inserted since the ITS epoch (TAI − UTC drift since 2004:
16/// 2005, 2008, 2012, 2015 and 2016). TimestampIts counts TAI milliseconds
17/// (ETSI TS 102 894-2), so this offset is added to UTC time.
18pub const ELAPSED_SECONDS: u64 = 5;
19
20/// [`ELAPSED_SECONDS`] in milliseconds.
21pub const ELAPSED_MILLISECONDS: u64 = ELAPSED_SECONDS * 1_000;
22
23/// Convert a Unix (UTC) timestamp in ms to ITS time (TAI ms since the ITS
24/// epoch), i.e. the TimestampIts of ETSI TS 102 894-2. Saturates at 0.
25pub fn unix_ms_to_its_ms(unix_ms: u64) -> u64 {
26    (unix_ms + ELAPSED_MILLISECONDS).saturating_sub(ITS_EPOCH_MS)
27}
28
29/// Return the current UTC timestamp in seconds (as `f64`).
30pub fn unix_time_secs() -> f64 {
31    SystemTime::now()
32        .duration_since(UNIX_EPOCH)
33        .expect("system clock before Unix epoch")
34        .as_secs_f64()
35}
36
37/// Parity alias for Python `TimeService.time()`.
38pub fn time() -> f64 {
39    unix_time_secs()
40}
41
42/// Return the current ITS timestamp in **microseconds** (TAI, ITS epoch).
43///
44/// This matches the `Time64` type used in `generationTime` fields inside
45/// IEEE 1609.2 headers.
46pub fn timestamp_its_microseconds() -> u64 {
47    let now = SystemTime::now()
48        .duration_since(UNIX_EPOCH)
49        .expect("system clock before Unix epoch");
50    let its_secs = now.as_secs() - ITS_EPOCH + ELAPSED_SECONDS;
51    its_secs * 1_000_000 + u64::from(now.subsec_micros())
52}
53
54/// Parity alias for Python `TimeService.timestamp_its()` (returns milliseconds).
55pub fn timestamp_its() -> u64 {
56    timestamp_its_microseconds() / 1_000
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn unix_time_secs_positive() {
65        let t = unix_time_secs();
66        // Should be well past the ITS epoch
67        assert!(t > ITS_EPOCH as f64);
68    }
69
70    #[test]
71    fn timestamp_its_microseconds_positive() {
72        let t = timestamp_its_microseconds();
73        assert!(t > 0);
74    }
75
76    #[test]
77    fn timestamp_its_microseconds_monotonic() {
78        let t1 = timestamp_its_microseconds();
79        std::thread::sleep(std::time::Duration::from_millis(1));
80        let t2 = timestamp_its_microseconds();
81        assert!(t2 > t1);
82    }
83
84    #[test]
85    fn its_epoch_constant() {
86        assert_eq!(ITS_EPOCH, 1_072_915_200);
87    }
88
89    #[test]
90    fn elapsed_seconds_constant() {
91        assert_eq!(ELAPSED_SECONDS, 5);
92    }
93}