Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify the time formatting between the time panel and the plot #1369

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions crates/re_log_types/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ impl Time {
20 <= years_since_epoch && years_since_epoch <= 150
}

/// Returns the absolute datetime, if this is a valid, unambiguous, absolute time.
pub fn to_chrono(&self) -> Option<chrono::DateTime<chrono::Utc>> {
let ns_since_epoch = self.nanos_since_epoch();
if self.is_abolute_date() {
use chrono::TimeZone as _;
if let chrono::LocalResult::Single(datetime) = chrono::Utc.timestamp_opt(
ns_since_epoch / 1_000_000_000,
(ns_since_epoch % 1_000_000_000) as _,
) {
Some(datetime)
} else {
None
}
} else {
None
}
}

pub fn is_exactly_midnight(&self) -> bool {
// This is correct despite leap seconds because
// during positive leap seconds, UTC actually has a discontinuity
// (the same integer is reused for two different times).
// See https://en.wikipedia.org/wiki/Unix_time#Leap_seconds
self.nanos_since_epoch() % (24 * 60 * 60 * 1_000_000_000) == 0
emilk marked this conversation as resolved.
Show resolved Hide resolved
}

/// Human-readable formatting
pub fn format(&self) -> String {
let nanos_since_epoch = self.nanos_since_epoch();
Expand Down
72 changes: 72 additions & 0 deletions crates/re_viewer/src/misc/format_time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/// Useful when showing dates/times on a timeline
/// and you want it compact.
///
/// Shows dates when zoomed out, shows times when zoomed in,
/// shows relative millisecond when really zoomed in.
pub fn format_time_compact(time: re_log_types::Time) -> String {
let ns = time.nanos_since_epoch();
let relative_ns = ns % 1_000_000_000;
let is_whole_second = relative_ns == 0;
if is_whole_second {
if let Some(datetime) = time.to_chrono() {
return if time.is_exactly_midnight() {
// Show just the date:
datetime.format("%Y-%m-%dZ").to_string()
} else {
// Show just the time:
let is_whole_minute = ns % 60_000_000_000 == 0;
let is_whole_second = ns % 1_000_000_000 == 0;
if is_whole_minute {
datetime.time().format("%H:%MZ").to_string()
} else if is_whole_second {
datetime.time().format("%H:%M:%SZ").to_string()
} else {
datetime.time().format("%H:%M:%S%.3fZ").to_string()
}
};
}

re_log_types::Duration::from_nanos(ns).to_string()
} else {
// We are in the sub-second resolution.
// Showing the full time (HH:MM:SS.XXX or 3h 2m 6s …) becomes too long,
// so instead we switch to showing the time as milliseconds since the last whole second:
let ms = relative_ns as f64 * 1e-6;
if relative_ns % 1_000_000 == 0 {
format!("{ms:+.0} ms")
} else if relative_ns % 100_000 == 0 {
format!("{ms:+.1} ms")
} else if relative_ns % 10_000 == 0 {
format!("{ms:+.2} ms")
} else if relative_ns % 1_000 == 0 {
format!("{ms:+.3} ms")
} else if relative_ns % 100 == 0 {
format!("{ms:+.4} ms")
} else if relative_ns % 10 == 0 {
format!("{ms:+.5} ms")
} else {
format!("{ms:+.6} ms")
}
}
}

/// When showing grid-lines representing time.
///
/// Given some spacing (e.g. 10s), return the next spacing (60s).
pub fn next_grid_tick_magnitude_ns(spacing_ns: i64) -> i64 {
if spacing_ns <= 1_000_000_000 {
spacing_ns * 10 // up to 10 second ticks
} else if spacing_ns == 10_000_000_000 {
spacing_ns * 6 // to the whole minute
} else if spacing_ns == 60_000_000_000 {
spacing_ns * 10 // to ten minutes
} else if spacing_ns == 600_000_000_000 {
spacing_ns * 6 // to an hour
} else if spacing_ns == 60 * 60 * 1_000_000_000 {
spacing_ns * 12 // to 12 h
} else if spacing_ns == 12 * 60 * 60 * 1_000_000_000 {
spacing_ns * 2 // to a day
} else {
spacing_ns.checked_mul(10).unwrap_or(spacing_ns) // multiple of ten days
}
}
1 change: 1 addition & 0 deletions crates/re_viewer/src/misc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod app_options;
pub mod caches;
pub mod color_map;
pub mod format_time;
mod item;
pub(crate) mod mesh_loader;
mod selection_state;
Expand Down
54 changes: 3 additions & 51 deletions crates/re_viewer/src/ui/time_panel/paint_ticks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use egui::{lerp, pos2, remap_clamp, Align2, Color32, Rect, Rgba, Shape, Stroke};

use re_log_types::{Time, TimeRangeF, TimeReal, TimeType};

use crate::misc::format_time::next_grid_tick_magnitude_ns;

use super::time_ranges_ui::TimeRangesUi;

pub fn paint_time_ranges_and_ticks(
Expand Down Expand Up @@ -56,56 +58,6 @@ fn paint_time_range_ticks(

match time_type {
TimeType::Time => {
fn next_grid_tick_magnitude_ns(spacing_ns: i64) -> i64 {
if spacing_ns <= 1_000_000_000 {
spacing_ns * 10 // up to 10 second ticks
} else if spacing_ns == 10_000_000_000 {
spacing_ns * 6 // to the whole minute
} else if spacing_ns == 60_000_000_000 {
spacing_ns * 10 // to ten minutes
} else if spacing_ns == 600_000_000_000 {
spacing_ns * 6 // to an hour
} else if spacing_ns == 60 * 60 * 1_000_000_000 {
spacing_ns * 12 // to 12 h
} else if spacing_ns == 12 * 60 * 60 * 1_000_000_000 {
spacing_ns * 2 // to a day
} else {
spacing_ns.checked_mul(10).unwrap_or(spacing_ns) // multiple of ten days
}
}

fn grid_text_from_ns(ns: i64) -> String {
let relative_ns = ns % 1_000_000_000;
if relative_ns == 0 {
let time = Time::from_ns_since_epoch(ns);
if time.is_abolute_date() {
time.format_time("%H:%M:%S")
} else {
re_log_types::Duration::from_nanos(ns).to_string()
}
} else {
// We are in the sub-second resolution.
// Showing the full time (HH:MM:SS.XXX or 3h 2m 6s …) becomes too long,
// so instead we switch to showing the time as milliseconds since the last whole second:
let ms = relative_ns as f64 * 1e-6;
if relative_ns % 1_000_000 == 0 {
format!("{ms:+.0} ms")
} else if relative_ns % 100_000 == 0 {
format!("{ms:+.1} ms")
} else if relative_ns % 10_000 == 0 {
format!("{ms:+.2} ms")
} else if relative_ns % 1_000 == 0 {
format!("{ms:+.3} ms")
} else if relative_ns % 100 == 0 {
format!("{ms:+.4} ms")
} else if relative_ns % 10 == 0 {
format!("{ms:+.5} ms")
} else {
format!("{ms:+.6} ms")
}
}
}

paint_ticks(
ui.ctx(),
ui.visuals().dark_mode,
Expand All @@ -114,7 +66,7 @@ fn paint_time_range_ticks(
&ui.clip_rect(),
time_range, // ns
next_grid_tick_magnitude_ns,
grid_text_from_ns,
|ns| crate::misc::format_time::format_time_compact(Time::from_ns_since_epoch(ns)),
)
}
TimeType::Sequence => {
Expand Down
64 changes: 12 additions & 52 deletions crates/re_viewer/src/ui/view_time_series/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use egui::{

use re_arrow_store::TimeType;

use crate::{ui::view_time_series::scene::PlotSeriesKind, ViewerContext};
use crate::{
misc::format_time::next_grid_tick_magnitude_ns, ui::view_time_series::scene::PlotSeriesKind,
ViewerContext,
};

use super::SceneTimeSeries;

Expand Down Expand Up @@ -140,72 +143,29 @@ pub(crate) fn view_time_series(

fn format_time(time_type: TimeType, time_int: i64) -> String {
if time_type == TimeType::Time {
let ns_since_epoch = time_int;
let time = re_log_types::Time::from_ns_since_epoch(ns_since_epoch);
if time.is_abolute_date() {
use chrono::TimeZone as _;
if let chrono::LocalResult::Single(datetime) = chrono::Utc.timestamp_opt(
ns_since_epoch / 1_000_000_000,
(ns_since_epoch % 1_000_000_000) as _,
) {
let is_start_of_new_day =
datetime.time() == chrono::NaiveTime::from_hms_opt(0, 0, 0).unwrap();
if is_start_of_new_day {
// Show just the date:
return datetime.format("%Y-%m-%dZ").to_string();
} else {
// Show just the time:
let is_whole_minute = ns_since_epoch % 60_000_000_000 == 0;
let is_whole_second = ns_since_epoch % 1_000_000_000 == 0;
if is_whole_minute {
return datetime.time().format("%H:%MZ").to_string();
} else if is_whole_second {
return datetime.time().format("%H:%M:%SZ").to_string();
} else {
return datetime.time().format("%H:%M:%S%.3fZ").to_string();
}
}
}
}
let time = re_log_types::Time::from_ns_since_epoch(time_int);
crate::misc::format_time::format_time_compact(time)
} else {
time_type.format(re_log_types::TimeInt::from(time_int))
}

time_type.format(re_log_types::TimeInt::from(time_int))
}

fn ns_grid_spacer(
canvas_size: egui::Vec2,
input: &egui::plot::GridInput,
) -> Vec<egui::plot::GridMark> {
fn next_time_step(spacing_ns: i64) -> i64 {
if spacing_ns <= 1_000_000_000 {
spacing_ns * 10 // up to 10 second ticks
} else if spacing_ns == 10_000_000_000 {
spacing_ns * 6 // to the whole minute
} else if spacing_ns == 60_000_000_000 {
spacing_ns * 10 // to ten minutes
} else if spacing_ns == 600_000_000_000 {
spacing_ns * 6 // to an hour
} else if spacing_ns == 60 * 60 * 1_000_000_000 {
spacing_ns * 12 // to 12 h
} else if spacing_ns == 12 * 60 * 60 * 1_000_000_000 {
spacing_ns * 2 // to a day
} else {
spacing_ns.checked_mul(10).unwrap_or(spacing_ns) // multiple of ten days
}
}

let minimum_medium_line_spacing = 150.0; // ≈min size of a label
let max_medium_lines = canvas_size.x as f64 / minimum_medium_line_spacing;

let (min_ns, max_ns) = input.bounds;
let width_ns = max_ns - min_ns;

let mut small_spacing_ns = 1;
while width_ns / (next_time_step(small_spacing_ns) as f64) > max_medium_lines {
small_spacing_ns = next_time_step(small_spacing_ns);
while width_ns / (next_grid_tick_magnitude_ns(small_spacing_ns) as f64) > max_medium_lines {
small_spacing_ns = next_grid_tick_magnitude_ns(small_spacing_ns);
}
let medium_spacing_ns = next_time_step(small_spacing_ns);
let big_spacing_ns = next_time_step(medium_spacing_ns);
let medium_spacing_ns = next_grid_tick_magnitude_ns(small_spacing_ns);
let big_spacing_ns = next_grid_tick_magnitude_ns(medium_spacing_ns);

let mut current_ns = (min_ns.floor() as i64) / small_spacing_ns * small_spacing_ns;
let mut marks = vec![];
Expand Down