From d56c6ef08794cad55ca567355076904495a2a50f Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 24 May 2022 10:34:21 +0200 Subject: [PATCH] [Refactor] Move local time calculation to the clock module --- src/clock.rs | 11 +++++++++++ src/svg_clock.rs | 6 ++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/clock.rs b/src/clock.rs index 361881a..5a79b59 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -21,3 +21,14 @@ pub fn get_seconds_since_midnight(which: DayPart) -> i32 { seconds as i32 } + +pub fn get_hms(which: DayPart) -> (u8, u8, u8) { + let mut seconds = get_seconds_since_midnight(which); + + let hours = seconds / 3600; + seconds -= hours * 3600; + let minutes = seconds / 60; + seconds -= hours * 60; + + (hours as u8, minutes as u8, seconds as u8) +} diff --git a/src/svg_clock.rs b/src/svg_clock.rs index 4bc466b..9c7d1fe 100644 --- a/src/svg_clock.rs +++ b/src/svg_clock.rs @@ -17,7 +17,7 @@ use svg::{ }; use usvg::Tree; -use crate::clock::{get_moon_phase, get_seconds_since_midnight, DayPart}; +use crate::clock::{get_hms, get_moon_phase, get_seconds_since_midnight, DayPart}; use crate::config::Config; const HOUR_NAMES: [&str; 24] = [ @@ -343,9 +343,7 @@ pub fn gen_svg( ) -> Document { let local_timestamp = Local::now(); let utc_hour = local_timestamp.with_timezone(&Utc).time().hour(); - let local_hour = local_timestamp.time().hour(); - let local_minute = local_timestamp.time().minute(); - let local_second = local_timestamp.time().second(); + let (local_hour, local_minute, local_second) = get_hms(DayPart::LocalNow); let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis()); let utc_offset = local_timestamp.offset().local_minus_utc();