[Refactor] Move local time’s seconds since midnight calculation to the clock module

This commit is contained in:
Gergely Polonkai 2022-05-24 10:27:46 +02:00
parent 1d485c824b
commit b6d6e71be2
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
2 changed files with 17 additions and 3 deletions

View File

@ -1,4 +1,8 @@
use chrono::prelude::Local; use chrono::{prelude::Local, Timelike};
pub enum DayPart {
LocalNow,
}
pub fn get_moon_phase() -> f32 { pub fn get_moon_phase() -> f32 {
let local_timestamp = Local::now(); let local_timestamp = Local::now();
@ -7,3 +11,13 @@ pub fn get_moon_phase() -> f32 {
moon_illumination.phase as f32 * 28.0 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
}

View File

@ -17,7 +17,7 @@ use svg::{
}; };
use usvg::Tree; use usvg::Tree;
use crate::clock::get_moon_phase; use crate::clock::{get_moon_phase, get_seconds_since_midnight, DayPart};
use crate::config::Config; use crate::config::Config;
const HOUR_NAMES: [&str; 24] = [ const HOUR_NAMES: [&str; 24] = [
@ -349,7 +349,7 @@ pub fn gen_svg(
let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis()); let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis());
let utc_offset = local_timestamp.offset().local_minus_utc(); 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 utc_rotation = seconds_to_degrees(utc_offset);
let moon_radius = IMAGE_WIDTH as f32 * 0.071428571; let moon_radius = IMAGE_WIDTH as f32 * 0.071428571;