Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 18 additions & 10 deletions decoder/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use crate::{Arg, BitflagsKey, Table};
use chrono::TimeZone;
use colored::Colorize;
use defmt_parser::{DisplayHint, Fragment, Level, ParserMode, Type};
use defmt_parser::{DisplayHint, Fragment, Level, ParserMode, TimePrecision, Type};

/// A log frame
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -116,8 +116,8 @@ impl<'t> Frame<'t> {
}
}
_ => match hint {
Some(DisplayHint::ISO8601) => {
self.format_iso8601(*x as u64, &mut buf)?
Some(DisplayHint::ISO8601(precision)) => {
self.format_iso8601(*x as u64, precision, &mut buf)?
}
Some(DisplayHint::Debug) => {
self.format_u128(*x as u128, parent_hint, &mut buf)?
Expand Down Expand Up @@ -353,13 +353,21 @@ impl<'t> Frame<'t> {
Ok(())
}

fn format_iso8601(&self, timestamp: u64, buf: &mut String) -> Result<(), fmt::Error> {
let date_time = chrono::Utc.timestamp_millis(timestamp as i64);
write!(
buf,
"{}",
date_time.to_rfc3339_opts(chrono::SecondsFormat::Millis, true)
)?;
fn format_iso8601(
&self,
timestamp: u64,
precision: &TimePrecision,
buf: &mut String,
) -> Result<(), fmt::Error> {
let format = match precision {
TimePrecision::Millis => chrono::SecondsFormat::Millis,
TimePrecision::Seconds => chrono::SecondsFormat::Secs,
};
Comment thread
justahero marked this conversation as resolved.
let date_time = match precision {
TimePrecision::Millis => chrono::Utc.timestamp_millis(timestamp as i64),
TimePrecision::Seconds => chrono::Utc.timestamp(timestamp as i64, 0),
};
write!(buf, "{}", date_time.to_rfc3339_opts(format, true))?;
Ok(())
}
}
Expand Down
6 changes: 5 additions & 1 deletion decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,11 @@ mod tests {
36, 188, 151, 238, 120, 1, 0, 0, // unix timestamp in bytes: 1618910624804
];

decode_and_expect("{=u64:+}", &bytes, "0.000002 INFO 2021-04-20T09:23:44.804Z");
decode_and_expect(
"{=u64:iso8601ms}",
&bytes,
"0.000002 INFO 2021-04-20T09:23:44.804Z",
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion firmware/qemu/src/bin/hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn main() -> ! {
defmt::info!("Debug {=0..4:?}", x);

// ISO 8601 timestamps
defmt::info!("ISO8601 {:+}", 1618910624804_u64);
defmt::info!("ISO8601 {:iso8601ms}", 1618910624804_u64);
Comment thread
justahero marked this conversation as resolved.

loop {
debug::exit(debug::EXIT_SUCCESS)
Expand Down
22 changes: 15 additions & 7 deletions parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ pub struct Parameter {
pub hint: Option<DisplayHint>,
}

/// Precision of ISO8601 datetime
#[derive(Clone, Debug, PartialEq)]
pub enum TimePrecision {
Millis,
Seconds,
}

/// All display hints
#[derive(Clone, Debug, PartialEq)]
pub enum DisplayHint {
Expand All @@ -50,8 +57,8 @@ pub enum DisplayHint {
Debug,
/// `:us`, formats integers as timestamps in microseconds
Microseconds,
/// `:+`, formats integers as timestamp in milliseconds in ISO6801 date time format
ISO8601,
/// `:iso8601{ms,s}`, formats integers as timestamp in ISO6801 date time format
ISO8601(TimePrecision),
/// `__internal_bitflags_NAME` instructs the decoder to print the flags that are set, instead of
/// the raw value.
Bitflags {
Expand Down Expand Up @@ -118,7 +125,8 @@ fn parse_display_hint(mut s: &str) -> Option<DisplayHint> {
uppercase: true,
zero_pad,
},
"+" => DisplayHint::ISO8601,
"iso8601ms" => DisplayHint::ISO8601(TimePrecision::Millis),
"iso8601s" => DisplayHint::ISO8601(TimePrecision::Seconds),
"?" => DisplayHint::Debug,
_ => return None,
})
Expand Down Expand Up @@ -690,11 +698,11 @@ mod tests {
);

assert_eq!(
parse_param(":+", ParserMode::Strict),
parse_param(":iso8601ms", ParserMode::Strict),
Ok(Param {
index: None,
ty: Type::Format,
hint: Some(DisplayHint::ISO8601),
hint: Some(DisplayHint::ISO8601(TimePrecision::Millis)),
})
);

Expand Down Expand Up @@ -828,11 +836,11 @@ mod tests {
);

assert_eq!(
parse_param("=u128:+", ParserMode::Strict),
parse_param("=u128:iso8601s", ParserMode::Strict),
Ok(Param {
index: None,
ty: Type::U128,
hint: Some(DisplayHint::ISO8601),
hint: Some(DisplayHint::ISO8601(TimePrecision::Seconds)),
})
);

Expand Down