ui: format date

This commit is contained in:
ardocrat 2023-08-18 15:43:06 +03:00
parent 36296f647d
commit 94b6b54e79
4 changed files with 23 additions and 20 deletions

View file

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use chrono::{DateTime, NaiveDateTime, Utc};
use egui::{RichText, Rounding, ScrollArea, vec2}; use egui::{RichText, Rounding, ScrollArea, vec2};
use grin_servers::DiffBlock; use grin_servers::DiffBlock;
@ -176,14 +175,12 @@ fn block_item_ui(ui: &mut egui::Ui, db: &DiffBlock, rounding: Rounding) {
// Draw block date. // Draw block date.
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.add_space(6.0); ui.add_space(6.0);
let block_time = NaiveDateTime::from_timestamp_opt(db.time as i64, 0).unwrap(); let block_time = View::format_time(db.time as i64);
let block_date: DateTime<Utc> = DateTime::from_utc(block_time, Utc);
let block_time_text = block_date.format("%d/%m/%Y %H:%M:%S UTC").to_string();
ui.label(RichText::new(format!("{} {}s {} {}", ui.label(RichText::new(format!("{} {}s {} {}",
TIMER, TIMER,
db.duration, db.duration,
HOURGLASS_LOW, HOURGLASS_LOW,
block_time_text)) block_time))
.color(Colors::GRAY) .color(Colors::GRAY)
.size(16.0)); .size(16.0));
}); });

View file

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use chrono::{DateTime, NaiveDateTime, Utc};
use egui::{RichText, Rounding, ScrollArea}; use egui::{RichText, Rounding, ScrollArea};
use grin_chain::SyncStatus; use grin_chain::SyncStatus;
use grin_servers::WorkerStats; use grin_servers::WorkerStats;
@ -264,13 +263,10 @@ fn worker_item_ui(ui: &mut egui::Ui, ws: &WorkerStats, rounding: Rounding) {
ui.add_space(6.0); ui.add_space(6.0);
// Draw block time // Draw block time
let seen = ws.last_seen.duration_since(std::time::UNIX_EPOCH).unwrap().as_secs(); let seen_ts = ws.last_seen.duration_since(std::time::UNIX_EPOCH).unwrap().as_secs();
let naive_datetime = NaiveDateTime::from_timestamp_opt(seen as i64, 0).unwrap(); let seen_time = View::format_time(seen_ts as i64);
let datetime: DateTime<Utc> = DateTime::from_utc(naive_datetime, Utc); let seen_text = format!("{} {}", CLOCK_AFTERNOON, seen_time);
let date_text = format!("{} {}", ui.heading(RichText::new(seen_text)
CLOCK_AFTERNOON,
datetime.format("%d/%m/%Y %H:%M:%S UTC"));
ui.heading(RichText::new(date_text)
.color(Colors::GRAY) .color(Colors::GRAY)
.size(16.0)); .size(16.0));
}); });

View file

@ -77,10 +77,11 @@ impl NetworkTab for NetworkNode {
[false, false, true, false]); [false, false, true, false]);
}); });
columns[1].vertical_centered(|ui| { columns[1].vertical_centered(|ui| {
let h_ts = stats.header_stats.latest_timestamp; let h_ts = stats.header_stats.latest_timestamp.timestamp();
let h_time = View::format_time(h_ts);
View::rounded_box(ui, View::rounded_box(ui,
h_ts.format("%d/%m/%Y %H:%M").to_string(), h_time,
t!("network_node.time_utc"), t!("network_node.time"),
[false, false, false, true]); [false, false, false, true]);
}); });
}); });
@ -110,10 +111,11 @@ impl NetworkTab for NetworkNode {
[false, false, true, false]); [false, false, true, false]);
}); });
columns[1].vertical_centered(|ui| { columns[1].vertical_centered(|ui| {
let b_ts = stats.chain_stats.latest_timestamp; let b_ts = stats.chain_stats.latest_timestamp.timestamp();
let b_time = View::format_time(b_ts);
View::rounded_box(ui, View::rounded_box(ui,
format!("{}", b_ts.format("%d/%m/%Y %H:%M")), b_time,
t!("network_node.time_utc"), t!("network_node.time"),
[false, false, false, true]); [false, false, false, true]);
}); });
}); });

View file

@ -417,7 +417,7 @@ impl View {
} }
} }
/// Draw horizontal line /// Draw horizontal line.
pub fn horizontal_line(ui: &mut egui::Ui, color: Color32) { pub fn horizontal_line(ui: &mut egui::Ui, color: Color32) {
let line_size = egui::Vec2::new(ui.available_width(), 1.0); let line_size = egui::Vec2::new(ui.available_width(), 1.0);
let (line_rect, _) = ui.allocate_exact_size(line_size, Sense::hover()); let (line_rect, _) = ui.allocate_exact_size(line_size, Sense::hover());
@ -427,6 +427,14 @@ impl View {
Stroke { width: 1.0, color }); Stroke { width: 1.0, color });
} }
/// Format timestamp in seconds with local UTC offset.
pub fn format_time(ts: i64) -> String {
let utc_offset = chrono::Local::now().offset().local_minus_utc();
let utc_time = ts + utc_offset as i64;
let tx_time = chrono::NaiveDateTime::from_timestamp_opt(utc_time, 0).unwrap();
tx_time.format("%d/%m/%Y %H:%M:%S").to_string()
}
/// Get top display inset (cutout) size. /// Get top display inset (cutout) size.
pub fn get_top_inset() -> f32 { pub fn get_top_inset() -> f32 {
TOP_DISPLAY_INSET.load(Ordering::Relaxed) as f32 TOP_DISPLAY_INSET.load(Ordering::Relaxed) as f32