Skip to content

New function timestamp_u64 #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> {
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;
Expand Down
21 changes: 21 additions & 0 deletions tests/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down