Calculate day parts based on UTC time instead of local time

This commit is contained in:
Gergely Polonkai 2022-05-19 08:14:01 +02:00
parent 95557c839b
commit b0a943c270
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4

View File

@ -3,7 +3,7 @@ extern crate smithay_client_toolkit as sctk;
use std::fmt; use std::fmt;
use std::time::SystemTime; use std::time::SystemTime;
use chrono::prelude::Local; use chrono::prelude::{Local, Utc};
use chrono::TimeZone; use chrono::TimeZone;
use chrono::Timelike; use chrono::Timelike;
use sctk::reexports::client::protocol::{wl_shm, wl_surface}; use sctk::reexports::client::protocol::{wl_shm, wl_surface};
@ -251,6 +251,7 @@ fn gen_svg() -> Document {
let local_timestamp = Local::now(); let local_timestamp = Local::now();
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 = local_timestamp.time().num_seconds_from_midnight() as i32;
let utc_rotation = seconds_to_degrees(utc_offset);
// TODO: Make these configurable // TODO: Make these configurable
let lat = 47.655235; let lat = 47.655235;
@ -266,35 +267,35 @@ fn gen_svg() -> Document {
let moon_phase = moon_illumination.phase * 28.0; let moon_phase = moon_illumination.phase * 28.0;
let sun_times = suncalc::get_times(unixtime, lat, long, None); let sun_times = suncalc::get_times(unixtime, lat, long, None);
let noon = Local let noon = Utc
.timestamp_millis(sun_times.solar_noon.0) .timestamp_millis(sun_times.solar_noon.0)
.time() .time()
.num_seconds_from_midnight() as i32; .num_seconds_from_midnight() as i32;
let midnight = Local let midnight = Utc
.timestamp_millis(sun_times.nadir.0) .timestamp_millis(sun_times.nadir.0)
.time() .time()
.num_seconds_from_midnight() as i32; .num_seconds_from_midnight() as i32;
let morning_golden_end = Local let morning_golden_end = Utc
.timestamp_millis(sun_times.golden_hour_end.0) .timestamp_millis(sun_times.golden_hour_end.0)
.time() .time()
.num_seconds_from_midnight() as i32; .num_seconds_from_midnight() as i32;
let evening_golden_start = Local let evening_golden_start = Utc
.timestamp_millis(sun_times.golden_hour.0) .timestamp_millis(sun_times.golden_hour.0)
.time() .time()
.num_seconds_from_midnight() as i32; .num_seconds_from_midnight() as i32;
let sunrise = Local let sunrise = Utc
.timestamp_millis(sun_times.sunrise.0) .timestamp_millis(sun_times.sunrise.0)
.time() .time()
.num_seconds_from_midnight() as i32; .num_seconds_from_midnight() as i32;
let sunset = Local let sunset = Utc
.timestamp_millis(sun_times.sunset.0) .timestamp_millis(sun_times.sunset.0)
.time() .time()
.num_seconds_from_midnight() as i32; .num_seconds_from_midnight() as i32;
let dawn = Local let dawn = Utc
.timestamp_millis(sun_times.dawn.0) .timestamp_millis(sun_times.dawn.0)
.time() .time()
.num_seconds_from_midnight() as i32; .num_seconds_from_midnight() as i32;
let dusk = Local let dusk = Utc
.timestamp_millis(sun_times.dusk.0) .timestamp_millis(sun_times.dusk.0)
.time() .time()
.num_seconds_from_midnight() as i32; .num_seconds_from_midnight() as i32;
@ -367,7 +368,7 @@ fn gen_svg() -> Document {
"transform", "transform",
format!( format!(
"rotate({}, {}, {})", "rotate({}, {}, {})",
seconds_to_degrees(utc_offset), utc_rotation,
image_width / 2, image_width / 2,
image_width / 2 image_width / 2
), ),
@ -407,7 +408,7 @@ fn gen_svg() -> Document {
"transform", "transform",
format!( format!(
"rotate({}, {}, {})", "rotate({}, {}, {})",
time_to_degrees(local_time), time_to_degrees(local_time) - time_to_degrees(utc_offset),
image_width / 2, image_width / 2,
image_width / 2 image_width / 2
), ),
@ -429,26 +430,6 @@ fn gen_svg() -> Document {
.set("cy", image_width / 2) .set("cy", image_width / 2)
.set("r", marker_radius); .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() let noon_marker = Line::new()
.set("class", "mid-marker") .set("class", "mid-marker")
.set( .set(
@ -467,6 +448,7 @@ fn gen_svg() -> Document {
) )
.set("x2", image_width / 2) .set("x2", image_width / 2)
.set("y2", image_width as f32 / 2.0 + marker_radius); .set("y2", image_width as f32 / 2.0 + marker_radius);
let midnight_marker = Line::new() let midnight_marker = Line::new()
.set("class", "mid-marker") .set("class", "mid-marker")
.set( .set(
@ -485,6 +467,38 @@ fn gen_svg() -> Document {
) )
.set("x2", image_width / 2) .set("x2", image_width / 2)
.set("y2", image_width as f32 / 2.0 + marker_radius); .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() let dial = Line::new()
.set("id", "dial") .set("id", "dial")
.set("class", "dial") .set("class", "dial")
@ -575,8 +589,6 @@ fn gen_svg() -> Document {
.add(day_parts_group) .add(day_parts_group)
.add(seasonal_clock) .add(seasonal_clock)
.add(moon_group) .add(moon_group)
.add(noon_marker)
.add(midnight_marker)
.add(dial) .add(dial)
.add(current_hour_group) .add(current_hour_group)
} }