diff --git a/src/journal.rs b/src/journal.rs index aa4835e..0e0e177 100644 --- a/src/journal.rs +++ b/src/journal.rs @@ -996,6 +996,16 @@ impl JournalRef { Ok(system_time_from_realtime_usec(timestamp_us)) } + /// Returns timestamp at which current journal entry was recorded in u64 format. + pub fn timestamp_u64(&self) -> Result { + let mut timestamp_us: u64 = 0; + sd_try!(ffi::sd_journal_get_realtime_usec( + self.as_ptr(), + &mut timestamp_us + )); + Ok(timestamp_us) + } + /// Returns monotonic timestamp and boot ID at which current journal entry was recorded. pub fn monotonic_timestamp(&self) -> Result<(u64, Id128)> { let mut monotonic_timestamp_us: u64 = 0; diff --git a/tests/journal.rs b/tests/journal.rs index 7139784..9cf6de8 100644 --- a/tests/journal.rs +++ b/tests/journal.rs @@ -69,6 +69,27 @@ fn ts() { assert_eq!(u1, u2); } +#[test] +fn test_timestamp() { + if !have_journal() { + return; + } + + let mut j = journal::OpenOptions::default().open().unwrap(); + log!(log::Level::Info, "rust-systemd ts entry"); + j.seek(journal::JournalSeek::Head).unwrap(); + j.next().unwrap(); + let timestamp_system_time = j.timestamp().unwrap(); + let timestamp_u64 = j.timestamp_u64().unwrap(); + + let since_the_epoch = timestamp_system_time + .duration_since(std::time::UNIX_EPOCH) + .expect("Time went backwards"); + let timestamp_system_time_u64 = since_the_epoch.as_micros() as u64; + + assert_eq!(timestamp_u64, timestamp_system_time_u64); +} + #[test] fn test_seek() { let mut j = journal::OpenOptions::default().open().unwrap();