[Bugfix] Calculate the current UTC hour in a more safe way

This should now work in any time zone from -12:00 to +12:00, any time of the day.
This commit is contained in:
Gergely Polonkai 2022-05-19 08:33:29 +02:00
parent b0a943c270
commit 8a4c25bd80
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
1 changed files with 6 additions and 15 deletions

View File

@ -55,16 +55,6 @@ fn time_to_degrees(timestamp: i32, // should be time/timestamp
seconds_to_degrees(timestamp)
}
fn seconds_to_hms(seconds: i32) -> (i32, i32, i32) {
let mut secs = seconds;
let hours = secs / 3600;
secs -= hours * 3600;
let minutes = secs / 60;
secs -= minutes * 60;
(hours, minutes, secs)
}
fn hour_name_path(image_width: u32, outer_r: f32, ring_width: f32) -> Path {
let radius = outer_r - ring_width / 2.0;
let delta_x = radius * (15.0_f32.to_radians() / 2.0).sin();
@ -249,6 +239,11 @@ fn get_moon_path(image_width: u32, radius: f32, moon_phase: f64) -> Path {
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 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);
@ -408,7 +403,7 @@ fn gen_svg() -> Document {
"transform",
format!(
"rotate({}, {}, {})",
time_to_degrees(local_time) - time_to_degrees(utc_offset),
time_to_degrees(local_time) - utc_rotation,
image_width / 2,
image_width / 2
),
@ -519,10 +514,6 @@ fn gen_svg() -> Document {
image_width as f32 / 2.0 + outer_r - ring_width + hour_name_font_size,
);
let utc_now = local_time - utc_offset;
let utc_hour = utc_now / 3600;
let (local_hour, local_minute, local_second) = seconds_to_hms(local_time);
let current_box_width = (200f32 / 700f32) * image_width as f32;
let current_box_height = (100f32 / 700f32) * image_width as f32;
let current_hour_name_font_size = (40f32 / 700f32) * image_width as f32;