Parse the timestamp correctly

The timestamp comes from OpenBSD's struct timeval, which defines the
number of seconds as a time_t (i64) and the number of micro-seconds as a
suseconds_t (long, hence i64 too). They are separated by a dot.
https://man.openbsd.org/gettimeofday.2
This commit is contained in:
Rodolphe Breard 2019-01-05 13:02:20 +01:00
parent 2b87c9c3c3
commit 98dd194dca

View file

@ -33,11 +33,23 @@ pub enum Event {
FilterResponse, FilterResponse,
} }
#[derive(Debug)]
pub struct TimeVal {
pub sec: i64,
pub usec: i64,
}
impl TimeVal {
pub fn to_string(&self) -> String {
format!("{}.{}", self.sec, self.usec)
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct Entry { pub struct Entry {
pub kind: Kind, pub kind: Kind,
pub version: u8, pub version: u8,
pub timestamp: u64, pub timestamp: TimeVal,
pub subsystem: Subsystem, pub subsystem: Subsystem,
pub event: Event, pub event: Event,
pub token: Option<u64>, pub token: Option<u64>,
@ -57,11 +69,7 @@ fn to_u8(s: &str) -> Result<u8, std::num::ParseIntError> {
s.parse() s.parse()
} }
fn to_i32(s: &str) -> Result<i32, std::num::ParseIntError> { fn to_i64(s: &str) -> Result<i64, std::num::ParseIntError> {
s.parse()
}
fn to_u64(s: &str) -> Result<u64, std::num::ParseIntError> {
s.parse() s.parse()
} }
@ -103,12 +111,12 @@ named!(parse_event<&str, Event>,
) )
); );
named!(parse_timestamp<&str, u64>, named!(parse_timestamp<&str, TimeVal>,
do_parse!( do_parse!(
sec: parse_u64 >> sec: parse_i64 >>
tag!(".") >> tag!(".") >>
nsec: parse_i32 >> usec: parse_i64 >>
(sec) (TimeVal { sec, usec})
) )
); );
@ -116,12 +124,8 @@ named!(parse_version<&str, u8>,
map_res!(take_while!(is_ascii_digit), to_u8) map_res!(take_while!(is_ascii_digit), to_u8)
); );
named!(parse_i32<&str, i32>, named!(parse_i64<&str, i64>,
map_res!(take_while!(is_ascii_digit), to_i32) map_res!(take_while!(is_ascii_digit), to_i64)
);
named!(parse_u64<&str, u64>,
map_res!(take_while!(is_ascii_digit), to_u64)
); );
named!(parse_u64_hex<&str, u64>, named!(parse_u64_hex<&str, u64>,