aboutsummaryrefslogtreecommitdiffstats
path: root/src/timestamp.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/timestamp.rs18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/timestamp.rs b/src/timestamp.rs
index 52ab154..73fe91f 100644
--- a/src/timestamp.rs
+++ b/src/timestamp.rs
@@ -1,11 +1,27 @@
+use std::time::{SystemTime, UNIX_EPOCH};
+
pub type TAI64N = [u8; 12];
+const TAI64_EPOCH : u64 = 4000000000000000;
+
pub fn zero() -> TAI64N {
[0u8; 12]
}
pub fn now() -> TAI64N {
- [0u8; 12] // TODO, return current timestamp
+ // get system time as duration
+ let sysnow = SystemTime::now();
+ let delta = sysnow.duration_since(UNIX_EPOCH).unwrap();
+
+ // convert to tai64n
+ let tai64_secs = delta.as_secs() + TAI64_EPOCH;
+ let tai64_nano = delta.subsec_nanos();
+
+ // serialize
+ let mut res = [0u8; 12];
+ res[..8].copy_from_slice(&tai64_secs.to_be_bytes()[..]);
+ res[8..].copy_from_slice(&tai64_nano.to_be_bytes()[..]);
+ res
}
pub fn compare(old : &TAI64N, new : &TAI64N) -> bool {