dmesg: change Record timestamp_us data type to i64.

This commit is contained in:
Fuad Ismail
2024-11-27 16:05:31 +07:00
parent c4944f4c85
commit dff0a8ea0b
3 changed files with 11 additions and 14 deletions

View File

@@ -158,13 +158,10 @@ impl Dmesg<'_> {
for record in records {
match self.time_format {
TimeFormat::Delta => {
print!("[{}] ", delta_formatter.format(record.timestamp_us as i64))
print!("[{}] ", delta_formatter.format(record.timestamp_us))
}
TimeFormat::Reltime => {
print!(
"[{}] ",
reltime_formatter.format(record.timestamp_us as i64)
)
print!("[{}] ", reltime_formatter.format(record.timestamp_us))
}
TimeFormat::Ctime => {
print!("[{}] ", time_formatter::ctime(record.timestamp_us))
@@ -200,7 +197,7 @@ enum TimeFormat {
struct Record {
priority_facility: u32,
_sequence: u64,
timestamp_us: u64,
timestamp_us: i64,
message: String,
}

View File

@@ -23,7 +23,7 @@ struct Dmesg<'a> {
#[derive(serde::Serialize)]
struct Record<'a> {
pri: u32,
time: u64,
time: i64,
msg: &'a str,
}
@@ -129,11 +129,11 @@ impl serde_json::ser::Formatter for DmesgFormatter {
}
}
fn write_u64<W>(&mut self, writer: &mut W, value: u64) -> io::Result<()>
fn write_i64<W>(&mut self, writer: &mut W, value: i64) -> io::Result<()>
where
W: ?Sized + io::Write,
{
// The only u64 field in Dmesg is time, which requires a specific format
// The only i64 field in Dmesg is time, which requires a specific format
let seconds = value / 1000000;
let sub_seconds = value % 1000000;
let repr = format!("{:>5}.{:0>6}", seconds, sub_seconds);

View File

@@ -3,22 +3,22 @@ use chrono::{DateTime, FixedOffset, TimeDelta};
use chrono::{NaiveDate, NaiveTime};
use std::sync::OnceLock;
pub fn raw(timestamp_us: u64) -> String {
pub fn raw(timestamp_us: i64) -> String {
let seconds = timestamp_us / 1000000;
let sub_seconds = timestamp_us % 1000000;
format!("{:>5}.{:0>6}", seconds, sub_seconds)
}
pub fn ctime(timestamp_us: u64) -> String {
pub fn ctime(timestamp_us: i64) -> String {
let date_time = boot_time()
.checked_add_signed(TimeDelta::microseconds(timestamp_us as i64))
.checked_add_signed(TimeDelta::microseconds(timestamp_us))
.unwrap();
date_time.format("%a %b %d %H:%M:%S %Y").to_string()
}
pub fn iso(timestamp_us: u64) -> String {
pub fn iso(timestamp_us: i64) -> String {
let date_time = boot_time()
.checked_add_signed(TimeDelta::microseconds(timestamp_us as i64))
.checked_add_signed(TimeDelta::microseconds(timestamp_us))
.unwrap();
date_time.format("%Y-%m-%dT%H:%M:%S,%6f%:z").to_string()
}