From b6d6e71be260e6a9c43d773f470b0f458930df63 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 24 May 2022 10:27:46 +0200 Subject: [PATCH] =?UTF-8?q?[Refactor]=20Move=20local=20time=E2=80=99s=20se?= =?UTF-8?q?conds=20since=20midnight=20calculation=20to=20the=20clock=20mod?= =?UTF-8?q?ule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/clock.rs | 16 +++++++++++++++- src/svg_clock.rs | 4 ++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/clock.rs b/src/clock.rs index ab0f1a5..361881a 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -1,4 +1,8 @@ -use chrono::prelude::Local; +use chrono::{prelude::Local, Timelike}; + +pub enum DayPart { + LocalNow, +} pub fn get_moon_phase() -> f32 { let local_timestamp = Local::now(); @@ -7,3 +11,13 @@ pub fn get_moon_phase() -> f32 { moon_illumination.phase as f32 * 28.0 } + +pub fn get_seconds_since_midnight(which: DayPart) -> i32 { + let local_timestamp = Local::now(); + + let seconds = match which { + DayPart::LocalNow => local_timestamp.time().num_seconds_from_midnight(), + }; + + seconds as i32 +} diff --git a/src/svg_clock.rs b/src/svg_clock.rs index b5dd6ce..4bc466b 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; +use crate::clock::{get_moon_phase, get_seconds_since_midnight, DayPart}; use crate::config::Config; const HOUR_NAMES: [&str; 24] = [ @@ -349,7 +349,7 @@ pub fn gen_svg( let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis()); let utc_offset = local_timestamp.offset().local_minus_utc(); - let local_time = local_timestamp.time().num_seconds_from_midnight() as i32; + let local_time = get_seconds_since_midnight(DayPart::LocalNow); let utc_rotation = seconds_to_degrees(utc_offset); let moon_radius = IMAGE_WIDTH as f32 * 0.071428571;