[Refactor] Move calculation of the day parts to the clock module
This commit is contained in:
parent
b485b01c2f
commit
eecd5c2938
62
src/clock.rs
62
src/clock.rs
@ -1,11 +1,22 @@
|
|||||||
use chrono::{
|
use chrono::{
|
||||||
prelude::{Local, Utc},
|
prelude::{Local, Utc},
|
||||||
Timelike,
|
TimeZone, Timelike,
|
||||||
};
|
};
|
||||||
|
use suncalc::SunTimes;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
|
||||||
pub enum DayPart {
|
pub enum DayPart {
|
||||||
LocalNow,
|
LocalNow,
|
||||||
UtcNow,
|
UtcNow,
|
||||||
|
UtcNoon,
|
||||||
|
UtcMidnight,
|
||||||
|
UtcMorningGoldenEnd,
|
||||||
|
UtcEveningGoldenStart,
|
||||||
|
UtcSunrise,
|
||||||
|
UtcSunset,
|
||||||
|
UtcDawnStart,
|
||||||
|
UtcDuskEnd,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_moon_phase() -> f32 {
|
pub fn get_moon_phase() -> f32 {
|
||||||
@ -16,8 +27,19 @@ 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 {
|
pub fn get_seconds_since_midnight(config: &Option<Config>, which: DayPart) -> i32 {
|
||||||
let local_timestamp = Local::now();
|
let local_timestamp = Local::now();
|
||||||
|
let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis());
|
||||||
|
let sun_times: Option<SunTimes> = if config.is_some() {
|
||||||
|
Some(suncalc::get_times(
|
||||||
|
unixtime,
|
||||||
|
config.unwrap().latitude,
|
||||||
|
config.unwrap().longitude,
|
||||||
|
None,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
let seconds = match which {
|
let seconds = match which {
|
||||||
DayPart::LocalNow => local_timestamp.time().num_seconds_from_midnight(),
|
DayPart::LocalNow => local_timestamp.time().num_seconds_from_midnight(),
|
||||||
@ -25,13 +47,45 @@ pub fn get_seconds_since_midnight(which: DayPart) -> i32 {
|
|||||||
.with_timezone(&Utc)
|
.with_timezone(&Utc)
|
||||||
.time()
|
.time()
|
||||||
.num_seconds_from_midnight(),
|
.num_seconds_from_midnight(),
|
||||||
|
DayPart::UtcNoon => Utc
|
||||||
|
.timestamp_millis(sun_times.unwrap().solar_noon.0)
|
||||||
|
.time()
|
||||||
|
.num_seconds_from_midnight(),
|
||||||
|
DayPart::UtcMidnight => Utc
|
||||||
|
.timestamp_millis(sun_times.unwrap().nadir.0)
|
||||||
|
.time()
|
||||||
|
.num_seconds_from_midnight(),
|
||||||
|
DayPart::UtcMorningGoldenEnd => Utc
|
||||||
|
.timestamp_millis(sun_times.unwrap().golden_hour_end.0)
|
||||||
|
.time()
|
||||||
|
.num_seconds_from_midnight(),
|
||||||
|
DayPart::UtcEveningGoldenStart => Utc
|
||||||
|
.timestamp_millis(sun_times.unwrap().golden_hour.0)
|
||||||
|
.time()
|
||||||
|
.num_seconds_from_midnight(),
|
||||||
|
DayPart::UtcSunrise => Utc
|
||||||
|
.timestamp_millis(sun_times.unwrap().sunrise.0)
|
||||||
|
.time()
|
||||||
|
.num_seconds_from_midnight(),
|
||||||
|
DayPart::UtcSunset => Utc
|
||||||
|
.timestamp_millis(sun_times.unwrap().sunset.0)
|
||||||
|
.time()
|
||||||
|
.num_seconds_from_midnight(),
|
||||||
|
DayPart::UtcDawnStart => Utc
|
||||||
|
.timestamp_millis(sun_times.unwrap().dawn.0)
|
||||||
|
.time()
|
||||||
|
.num_seconds_from_midnight(),
|
||||||
|
DayPart::UtcDuskEnd => Utc
|
||||||
|
.timestamp_millis(sun_times.unwrap().dusk.0)
|
||||||
|
.time()
|
||||||
|
.num_seconds_from_midnight(),
|
||||||
};
|
};
|
||||||
|
|
||||||
seconds as i32
|
seconds as i32
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_hms(which: DayPart) -> (u8, u8, u8) {
|
pub fn get_hms(config: &Option<Config>, which: DayPart) -> (u8, u8, u8) {
|
||||||
let mut seconds = get_seconds_since_midnight(which);
|
let mut seconds = get_seconds_since_midnight(config, which);
|
||||||
|
|
||||||
let hours = seconds / 3600;
|
let hours = seconds / 3600;
|
||||||
seconds -= hours * 3600;
|
seconds -= hours * 3600;
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use chrono::{
|
use chrono::prelude::Local;
|
||||||
prelude::{Local, Utc},
|
|
||||||
TimeZone, Timelike,
|
|
||||||
};
|
|
||||||
use rctree::Node;
|
use rctree::Node;
|
||||||
use svg::{
|
use svg::{
|
||||||
node::{
|
node::{
|
||||||
@ -341,13 +338,11 @@ pub fn gen_svg(
|
|||||||
config: &Option<Config>,
|
config: &Option<Config>,
|
||||||
hour_name_path_cache: &[(PathData, PathData); 24],
|
hour_name_path_cache: &[(PathData, PathData); 24],
|
||||||
) -> Document {
|
) -> Document {
|
||||||
let local_timestamp = Local::now();
|
let (utc_hour, _, _) = get_hms(config, DayPart::UtcNow);
|
||||||
let (utc_hour, _, _) = get_hms(DayPart::UtcNow);
|
let (local_hour, local_minute, local_second) = get_hms(config, DayPart::LocalNow);
|
||||||
let (local_hour, local_minute, local_second) = get_hms(DayPart::LocalNow);
|
|
||||||
let unixtime = suncalc::Timestamp(local_timestamp.timestamp_millis());
|
|
||||||
|
|
||||||
let utc_offset = local_timestamp.offset().local_minus_utc();
|
let utc_offset = Local::now().offset().local_minus_utc();
|
||||||
let local_time = get_seconds_since_midnight(DayPart::LocalNow);
|
let local_time = get_seconds_since_midnight(config, 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;
|
||||||
|
|
||||||
@ -442,44 +437,15 @@ pub fn gen_svg(
|
|||||||
.set("r", marker_radius);
|
.set("r", marker_radius);
|
||||||
|
|
||||||
let day_parts_group: Option<Group> = if config.is_some() {
|
let day_parts_group: Option<Group> = if config.is_some() {
|
||||||
let sun_times = suncalc::get_times(
|
let noon = get_seconds_since_midnight(config, DayPart::UtcNoon);
|
||||||
unixtime,
|
let midnight = get_seconds_since_midnight(config, DayPart::UtcMidnight);
|
||||||
config.unwrap().latitude,
|
let morning_golden_end = get_seconds_since_midnight(config, DayPart::UtcMorningGoldenEnd);
|
||||||
config.unwrap().longitude,
|
let evening_golden_start =
|
||||||
None,
|
get_seconds_since_midnight(config, DayPart::UtcEveningGoldenStart);
|
||||||
);
|
let sunrise = get_seconds_since_midnight(config, DayPart::UtcSunrise);
|
||||||
let noon = Utc
|
let sunset = get_seconds_since_midnight(config, DayPart::UtcSunset);
|
||||||
.timestamp_millis(sun_times.solar_noon.0)
|
let dawn = get_seconds_since_midnight(config, DayPart::UtcDawnStart);
|
||||||
.time()
|
let dusk = get_seconds_since_midnight(config, DayPart::UtcDuskEnd);
|
||||||
.num_seconds_from_midnight() as i32;
|
|
||||||
let midnight = Utc
|
|
||||||
.timestamp_millis(sun_times.nadir.0)
|
|
||||||
.time()
|
|
||||||
.num_seconds_from_midnight() as i32;
|
|
||||||
let morning_golden_end = Utc
|
|
||||||
.timestamp_millis(sun_times.golden_hour_end.0)
|
|
||||||
.time()
|
|
||||||
.num_seconds_from_midnight() as i32;
|
|
||||||
let evening_golden_start = Utc
|
|
||||||
.timestamp_millis(sun_times.golden_hour.0)
|
|
||||||
.time()
|
|
||||||
.num_seconds_from_midnight() as i32;
|
|
||||||
let sunrise = Utc
|
|
||||||
.timestamp_millis(sun_times.sunrise.0)
|
|
||||||
.time()
|
|
||||||
.num_seconds_from_midnight() as i32;
|
|
||||||
let sunset = Utc
|
|
||||||
.timestamp_millis(sun_times.sunset.0)
|
|
||||||
.time()
|
|
||||||
.num_seconds_from_midnight() as i32;
|
|
||||||
let dawn = Utc
|
|
||||||
.timestamp_millis(sun_times.dawn.0)
|
|
||||||
.time()
|
|
||||||
.num_seconds_from_midnight() as i32;
|
|
||||||
let dusk = Utc
|
|
||||||
.timestamp_millis(sun_times.dusk.0)
|
|
||||||
.time()
|
|
||||||
.num_seconds_from_midnight() as i32;
|
|
||||||
|
|
||||||
let golden_hour_path = get_range_path(
|
let golden_hour_path = get_range_path(
|
||||||
marker_radius,
|
marker_radius,
|
||||||
|
Loading…
Reference in New Issue
Block a user