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