From e0ad2c4e7ddaaa60d7dc9d7f312bd69567fa5745 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Sat, 3 Dec 2022 17:02:29 +0100 Subject: decode dates in fancy mode (rfc3339 is easier to read than unix timestamps) --- src/fancy.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/fancy.rs b/src/fancy.rs index 477018f..2058129 100644 --- a/src/fancy.rs +++ b/src/fancy.rs @@ -7,6 +7,8 @@ use std::fmt; use std::fmt::Display; use std::fmt::Write; use owo_colors::OwoColorize; +use chrono::naive::NaiveDateTime; +use chrono::{Utc, DateTime, Local}; use protobuf_support::text_format::quote_bytes_to; @@ -82,6 +84,7 @@ fn print_field( field_name: F, value: ReflectValueRef, ) { + let fieldname_str = field_name.to_string(); print_start_field(buf, pretty, indent, first, field_name); match value { @@ -116,13 +119,27 @@ fn print_field( write!(buf, ": {}", v.bright_yellow()).unwrap(); } ReflectValueRef::I64(v) => { - write!(buf, ": {}", v.bright_yellow()).unwrap(); + if fieldname_str.contains("time") { + match NaiveDateTime::from_timestamp_opt(v, 0) { + Some(t) => write!(buf, ": {}", DateTime::::from(t.and_local_timezone(Utc).unwrap()).to_rfc3339().bright_green()), + None => write!(buf, ": {}", "".bright_red()) + }.unwrap(); + } else { + write!(buf, ": {}", v.bright_yellow()).unwrap(); + } } ReflectValueRef::U32(v) => { write!(buf, ": {}", v.bright_yellow()).unwrap(); } ReflectValueRef::U64(v) => { - write!(buf, ": {}", v.bright_yellow()).unwrap(); + if fieldname_str.contains("time") { + match NaiveDateTime::from_timestamp_opt(v.try_into().unwrap(), 0) { + Some(t) => write!(buf, ": {}", DateTime::::from(t.and_local_timezone(Utc).unwrap()).to_rfc3339().bright_green()), + None => write!(buf, ": {}", "".bright_red()) + }.unwrap(); + } else { + write!(buf, ": {}", v.bright_yellow()).unwrap(); + } } ReflectValueRef::Bool(v) => { write!(buf, ": {}", v.bright_yellow()).unwrap(); -- cgit v1.2.3