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
pub fn current_monotime() -> u64 {
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts) };
(ts.tv_sec as u64) * 1000_000_000 + (ts.tv_nsec as u64)
}
pub fn current_realtime() -> u64 {
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, &mut ts) };
(ts.tv_sec as u64) * 1000_000_000 + (ts.tv_nsec as u64)
}
pub fn delta_of_mono_real_time() -> u64 {
let x1 = current_monotime();
let y1 = current_realtime();
let y2 = current_realtime();
let x2 = current_monotime();
(y2 - x2 + y1 - x1) / 2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_timestamp_current_monotime() {
assert_ne!(current_monotime(), 0);
}
#[test]
fn test_timestamp_current_realtime() {
assert_ne!(current_realtime(), 0);
}
#[test]
fn test_timestamp_delta_of_mono_real_time() {
assert_ne!(delta_of_mono_real_time(), 0);
}
}