diff --git a/src/journal.rs b/src/journal.rs index a4a440a..6aa2f87 100644 --- a/src/journal.rs +++ b/src/journal.rs @@ -992,9 +992,14 @@ impl JournalRef { /// Returns timestamp at which current journal entry was recorded. pub fn timestamp(&self) -> Result { + Ok(system_time_from_realtime_usec(self.timestamp_usec()?)) + } + + /// Returns timestamp at which current journal entry was recorded in u64 format. + pub fn timestamp_usec(&self) -> Result { let mut timestamp_us: u64 = 0; ffi_result(unsafe { ffi::sd_journal_get_realtime_usec(self.as_ptr(), &mut timestamp_us) })?; - Ok(system_time_from_realtime_usec(timestamp_us)) + Ok(timestamp_us) } /// Returns monotonic timestamp and boot ID at which current journal entry was recorded. diff --git a/tests/journal.rs b/tests/journal.rs index 16f3423..4ee5ca1 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_usec = j.timestamp_usec().unwrap(); + + let since_the_epoch = timestamp_system_time + .duration_since(std::time::UNIX_EPOCH) + .expect("Time went backwards"); + let timestamp_system_time_usec = since_the_epoch.as_micros() as u64; + + assert_eq!(timestamp_usec, timestamp_system_time_usec); +} + #[test] fn test_seek() { let mut j = journal::OpenOptions::default().open().unwrap();