From b0a943c270f78fbf7a0f423277d24fd60b20e63a Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 19 May 2022 08:14:01 +0200 Subject: [PATCH] Calculate day parts based on UTC time instead of local time --- src/main.rs | 78 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9cf987c..8e483c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ extern crate smithay_client_toolkit as sctk; use std::fmt; use std::time::SystemTime; -use chrono::prelude::Local; +use chrono::prelude::{Local, Utc}; use chrono::TimeZone; use chrono::Timelike; use sctk::reexports::client::protocol::{wl_shm, wl_surface}; @@ -251,6 +251,7 @@ fn gen_svg() -> Document { let local_timestamp = Local::now(); let utc_offset = local_timestamp.offset().local_minus_utc(); let local_time = local_timestamp.time().num_seconds_from_midnight() as i32; + let utc_rotation = seconds_to_degrees(utc_offset); // TODO: Make these configurable let lat = 47.655235; @@ -266,35 +267,35 @@ fn gen_svg() -> Document { let moon_phase = moon_illumination.phase * 28.0; let sun_times = suncalc::get_times(unixtime, lat, long, None); - let noon = Local + let noon = Utc .timestamp_millis(sun_times.solar_noon.0) .time() .num_seconds_from_midnight() as i32; - let midnight = Local + let midnight = Utc .timestamp_millis(sun_times.nadir.0) .time() .num_seconds_from_midnight() as i32; - let morning_golden_end = Local + let morning_golden_end = Utc .timestamp_millis(sun_times.golden_hour_end.0) .time() .num_seconds_from_midnight() as i32; - let evening_golden_start = Local + let evening_golden_start = Utc .timestamp_millis(sun_times.golden_hour.0) .time() .num_seconds_from_midnight() as i32; - let sunrise = Local + let sunrise = Utc .timestamp_millis(sun_times.sunrise.0) .time() .num_seconds_from_midnight() as i32; - let sunset = Local + let sunset = Utc .timestamp_millis(sun_times.sunset.0) .time() .num_seconds_from_midnight() as i32; - let dawn = Local + let dawn = Utc .timestamp_millis(sun_times.dawn.0) .time() .num_seconds_from_midnight() as i32; - let dusk = Local + let dusk = Utc .timestamp_millis(sun_times.dusk.0) .time() .num_seconds_from_midnight() as i32; @@ -367,7 +368,7 @@ fn gen_svg() -> Document { "transform", format!( "rotate({}, {}, {})", - seconds_to_degrees(utc_offset), + utc_rotation, image_width / 2, image_width / 2 ), @@ -407,7 +408,7 @@ fn gen_svg() -> Document { "transform", format!( "rotate({}, {}, {})", - time_to_degrees(local_time), + time_to_degrees(local_time) - time_to_degrees(utc_offset), image_width / 2, image_width / 2 ), @@ -429,26 +430,6 @@ fn gen_svg() -> Document { .set("cy", image_width / 2) .set("r", marker_radius); - let day_parts_group = Group::new() - .set("id", "day-parts") - .add(daytime_circle) - .add(golden_hour_path) - .add(sun_disc) - .add(blue_hour_path) - .add(nighttime_path) - .add(marker_circle); - - let moon_circle = Circle::new() - .set("class", "moon-background") - .set("cx", image_width / 2) - .set("cy", image_width / 2) - .set("r", moon_radius); - - let moon_group = Group::new() - .set("id", "moon-container") - .add(moon_circle) - .add(get_moon_path(image_width, moon_radius, moon_phase)); - let noon_marker = Line::new() .set("class", "mid-marker") .set( @@ -467,6 +448,7 @@ fn gen_svg() -> Document { ) .set("x2", image_width / 2) .set("y2", image_width as f32 / 2.0 + marker_radius); + let midnight_marker = Line::new() .set("class", "mid-marker") .set( @@ -485,6 +467,38 @@ fn gen_svg() -> Document { ) .set("x2", image_width / 2) .set("y2", image_width as f32 / 2.0 + marker_radius); + + let day_parts_group = Group::new() + .set("id", "day-parts") + .set( + "transform", + format!( + "rotate({}, {}, {})", + utc_rotation, + image_width / 2, + image_width / 2 + ), + ) + .add(daytime_circle) + .add(golden_hour_path) + .add(sun_disc) + .add(blue_hour_path) + .add(nighttime_path) + .add(marker_circle) + .add(noon_marker) + .add(midnight_marker); + + let moon_circle = Circle::new() + .set("class", "moon-background") + .set("cx", image_width / 2) + .set("cy", image_width / 2) + .set("r", moon_radius); + + let moon_group = Group::new() + .set("id", "moon-container") + .add(moon_circle) + .add(get_moon_path(image_width, moon_radius, moon_phase)); + let dial = Line::new() .set("id", "dial") .set("class", "dial") @@ -575,8 +589,6 @@ fn gen_svg() -> Document { .add(day_parts_group) .add(seasonal_clock) .add(moon_group) - .add(noon_marker) - .add(midnight_marker) .add(dial) .add(current_hour_group) }