From 9300ac33624e0b1992a84b3e9a6e76d362e4b739 Mon Sep 17 00:00:00 2001 From: mm Date: Sun, 10 Mar 2024 22:06:33 +0100 Subject: [PATCH 1/4] add timezone option unix epoch --- crates/re_log_types/src/time.rs | 35 +++++++++++++++++++++++---- crates/re_viewer/src/ui/rerun_menu.rs | 10 ++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/crates/re_log_types/src/time.rs b/crates/re_log_types/src/time.rs index caebf376b84b..059867fd6eea 100644 --- a/crates/re_log_types/src/time.rs +++ b/crates/re_log_types/src/time.rs @@ -6,6 +6,7 @@ use time::{format_description::FormatItem, OffsetDateTime, UtcOffset}; pub enum TimeZone { Local, Utc, + UnixEpoch, } /// A date-time represented as nanoseconds since unix epoch @@ -91,6 +92,16 @@ impl Time { TimeZone::Utc => { format!("{}Z", datetime.format(&parsed_format).unwrap()) } + TimeZone::UnixEpoch => { + format!("{}", datetime.format(&parsed_format).unwrap()) + } + } + } + + fn get_time_prefix(time_zone: &TimeZone) -> &str { + match time_zone { + TimeZone::UnixEpoch => "[unix_timestamp]", + TimeZone::Utc | TimeZone::Local => "[hour]:[minute]:[second]", } } @@ -101,19 +112,20 @@ impl Time { if let Some(datetime) = self.to_datetime() { let is_whole_second = nanos_since_epoch % 1_000_000_000 == 0; let is_whole_millisecond = nanos_since_epoch % 1_000_000 == 0; + let prefix = Self::get_time_prefix(&time_zone_for_timestamps); let time_format = if is_whole_second { - "[hour]:[minute]:[second]" + prefix.to_owned() } else if is_whole_millisecond { - "[hour]:[minute]:[second].[subsecond digits:3]" + format!("{prefix}.[subsecond digits:3]") } else { - "[hour]:[minute]:[second].[subsecond digits:6]" + format!("{prefix}.[subsecond digits:6]") }; let date_is_today = datetime.date() == OffsetDateTime::now_utc().date(); let date_format = format!("[year]-[month]-[day] {time_format}"); let parsed_format = if date_is_today { - time::format_description::parse(time_format).unwrap() + time::format_description::parse(&time_format).unwrap() } else { time::format_description::parse(&date_format).unwrap() }; @@ -149,7 +161,7 @@ impl Time { } else if is_whole_minute { "[hour]:[minute]" } else { - "[hour]:[minute]:[second]" + Self::get_time_prefix(&time_zone_for_timestamps) }; let parsed_format = time::format_description::parse(time_format).unwrap(); @@ -302,6 +314,19 @@ mod tests { &Time::from_us_since_epoch(69_900).format(TimeZone::Local), "+0.070s" ); + + assert_eq!( + &Time::from_us_since_epoch(42_000_000).format(TimeZone::UnixEpoch), + "+42s" + ); + assert_eq!( + &Time::from_us_since_epoch(69_000).format(TimeZone::UnixEpoch), + "+0.069s" + ); + assert_eq!( + &Time::from_us_since_epoch(69_900).format(TimeZone::UnixEpoch), + "+0.070s" + ); } #[test] diff --git a/crates/re_viewer/src/ui/rerun_menu.rs b/crates/re_viewer/src/ui/rerun_menu.rs index 32c8fb867e93..58e8b53687b4 100644 --- a/crates/re_viewer/src/ui/rerun_menu.rs +++ b/crates/re_viewer/src/ui/rerun_menu.rs @@ -318,12 +318,22 @@ fn options_menu_ui( ui.horizontal(|ui| { ui.label("Timezone:"); + }); + ui.horizontal(|ui| { re_ui .radio_value(ui, &mut app_options.time_zone, TimeZone::Utc, "UTC") .on_hover_text("Display timestamps in UTC"); re_ui .radio_value(ui, &mut app_options.time_zone, TimeZone::Local, "Local") .on_hover_text("Display timestamps in the local timezone"); + re_ui + .radio_value( + ui, + &mut app_options.time_zone, + TimeZone::UnixEpoch, + "Unix Epoch", + ) + .on_hover_text("Display timestamps in seconds since unix epoch"); }); { From 171de525ffb6f3adad133bf6ad5aee4672b52037 Mon Sep 17 00:00:00 2001 From: Marko Murgelj Date: Wed, 13 Mar 2024 19:57:52 +0100 Subject: [PATCH 2/4] always show unix epoch time when option set --- crates/re_log_types/src/time.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/crates/re_log_types/src/time.rs b/crates/re_log_types/src/time.rs index 059867fd6eea..2026f8d9d97a 100644 --- a/crates/re_log_types/src/time.rs +++ b/crates/re_log_types/src/time.rs @@ -98,13 +98,6 @@ impl Time { } } - fn get_time_prefix(time_zone: &TimeZone) -> &str { - match time_zone { - TimeZone::UnixEpoch => "[unix_timestamp]", - TimeZone::Utc | TimeZone::Local => "[hour]:[minute]:[second]", - } - } - /// Human-readable formatting pub fn format(&self, time_zone_for_timestamps: TimeZone) -> String { let nanos_since_epoch = self.nanos_since_epoch(); @@ -112,7 +105,10 @@ impl Time { if let Some(datetime) = self.to_datetime() { let is_whole_second = nanos_since_epoch % 1_000_000_000 == 0; let is_whole_millisecond = nanos_since_epoch % 1_000_000 == 0; - let prefix = Self::get_time_prefix(&time_zone_for_timestamps); + let prefix = match time_zone_for_timestamps { + TimeZone::UnixEpoch => "[unix_timestamp]", + TimeZone::Utc | TimeZone::Local => "[hour]:[minute]:[second]", + }; let time_format = if is_whole_second { prefix.to_owned() @@ -159,9 +155,15 @@ impl Time { let time_format = if self.is_exactly_midnight() { "[year]-[month]-[day]" } else if is_whole_minute { - "[hour]:[minute]" + match time_zone_for_timestamps { + TimeZone::UnixEpoch => "[unix_timestamp]", + TimeZone::Utc | TimeZone::Local => "[hour]:[minute]", + } } else { - Self::get_time_prefix(&time_zone_for_timestamps) + match time_zone_for_timestamps { + TimeZone::UnixEpoch => "[unix_timestamp]", + TimeZone::Utc | TimeZone::Local => "[hour]:[minute]:[second]", + } }; let parsed_format = time::format_description::parse(time_format).unwrap(); From 0ea129672c59bee19b34635934a0b77625f733a2 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 14 Mar 2024 09:23:04 +0100 Subject: [PATCH 3/4] Fix clippy --- crates/re_log_types/src/time.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/re_log_types/src/time.rs b/crates/re_log_types/src/time.rs index 2026f8d9d97a..6271e0060803 100644 --- a/crates/re_log_types/src/time.rs +++ b/crates/re_log_types/src/time.rs @@ -92,9 +92,7 @@ impl Time { TimeZone::Utc => { format!("{}Z", datetime.format(&parsed_format).unwrap()) } - TimeZone::UnixEpoch => { - format!("{}", datetime.format(&parsed_format).unwrap()) - } + TimeZone::UnixEpoch => datetime.format(&parsed_format).unwrap(), } } From 7e0f542930b6d1279c55190e3f375f0dcf2b2603 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 14 Mar 2024 09:43:28 +0100 Subject: [PATCH 4/4] Add docstring --- crates/re_log_types/src/time.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/re_log_types/src/time.rs b/crates/re_log_types/src/time.rs index 6271e0060803..c7fb28719cf8 100644 --- a/crates/re_log_types/src/time.rs +++ b/crates/re_log_types/src/time.rs @@ -6,6 +6,8 @@ use time::{format_description::FormatItem, OffsetDateTime, UtcOffset}; pub enum TimeZone { Local, Utc, + + /// Seconds since unix epoch UnixEpoch, }