fixed angle percentage

This commit is contained in:
Nickiel12 2023-11-08 18:44:48 -08:00
parent 6cbd5a48ca
commit ca4b5ffcda

View file

@ -16,11 +16,11 @@ pub fn run_calendar(args: &CliArgs) {
} }
let (mut moonrise, mut moonset, mut moonday, mut sunrise, mut sunset): ( let (mut moonrise, mut moonset, mut moonday, mut sunrise, mut sunset): (
Option<(u32, u32)>, Option<i32>,
Option<(u32, u32)>, Option<i32>,
Option<(u32, u32)>, Option<i32>,
Option<(u32, u32)>, Option<i32>,
Option<(u32, u32)>, Option<i32>,
) = (None, None, None, None, None); ) = (None, None, None, None, None);
let moonphase: Option<u32> = None; let moonphase: Option<u32> = None;
@ -79,55 +79,51 @@ pub fn run_calendar(args: &CliArgs) {
let mut output_state = CalandarState { let mut output_state = CalandarState {
has_bg: false, has_bg: false,
is_sun: false, is_sun: false,
gradient_angle: 0, gradient_angle_percentage: 0.0,
}; };
let (now_hr, now_min) = (Local::now().hour(), Local::now().minute()); let now_mins = (Local::now().hour() * 60 +Local::now().minute()) as i32;
if sunrise.is_none() { if sunrise.is_none() {
sunrise = Some((8, 0)); sunrise = Some(8 * 60 + 0);
} }
if sunset.is_none() { if sunset.is_none() {
sunset = Some((18, 0)); sunset = Some(18 * 60 + 0);
} }
let sun_rise = sunrise.clone().unwrap(); let sun_rise = sunrise.clone().unwrap();
let sun_set = sunset.clone().unwrap(); let sun_set = sunset.clone().unwrap();
if now_hr > sun_rise.0 && now_min > sun_rise.1 && now_hr < sun_set.0 && now_min < sun_set.1 { if now_mins > sun_rise && now_mins < sun_set {
// it is after sunrise, and before sunset // it is after sunrise, and before sunset
output_state.has_bg = true; output_state.has_bg = true;
output_state.is_sun = true; output_state.is_sun = true;
output_state.gradient_angle = { output_state.gradient_angle_percentage = {
let sunrise_mins = sun_rise.0 * 60 + sun_rise.1; (now_mins - sun_rise) as f32
(((now_hr * 60 + now_min) - sunrise_mins) as f32 / (sun_set - sun_rise) as f32
/ ((sun_set.0 * 60 + sun_set.1) - sunrise_mins) as f32)
.floor() as i32
}; };
} else { } else {
// Check if the moon is up // Check if the moon is up
if moonrise.is_some() { if moonrise.is_some() {
let (moonrise_hr, moonrise_min) = moonrise.clone().unwrap(); let moon_rise = moonrise.clone().unwrap();
if moonset.is_some() { if moonset.is_some() {
let (moonset_hr, moonset_min) = moonset.clone().unwrap(); let moon_set = moonset.clone().unwrap();
if now_hr > moonrise_hr if now_mins > moon_rise
&& now_min > moonrise_min && now_mins < moon_set
&& now_hr < moonset_hr
&& now_min < moonset_min
{ {
// moon is up // moon is up
output_state.has_bg = true; output_state.has_bg = true;
output_state.is_sun = false; output_state.is_sun = false;
} }
} else { } else {
if now_hr > moonrise_hr && now_min > moonrise_hr { if now_mins > moon_rise {
output_state.has_bg = true; output_state.has_bg = true;
output_state.is_sun = false; output_state.is_sun = false;
} }
} }
} else { } else {
if moonset.is_some() { if moonset.is_some() {
let (moonset_hr, moonset_min) = moonset.clone().unwrap(); let moon_set = moonset.clone().unwrap();
if now_hr < moonset_hr && now_min < moonset_min { if now_mins < moon_set {
// moon is up // moon is up
output_state.has_bg = true; output_state.has_bg = true;
output_state.is_sun = false; output_state.is_sun = false;
@ -138,19 +134,19 @@ pub fn run_calendar(args: &CliArgs) {
if output_state.has_bg == true && output_state.is_sun == false { if output_state.has_bg == true && output_state.is_sun == false {
// moon is up, get the position // moon is up, get the position
if moonrise.is_some() { if moonrise.is_some() {
let (moonrise_hr, moonrise_min) = moonrise.clone().unwrap(); let moon_rise = moonrise.clone().unwrap();
let (moonday_hrs, moonday_mins) = moonday.clone().unwrap(); let moon_day = moonday.clone().unwrap();
output_state.gradient_angle = { output_state.gradient_angle_percentage = {
(((now_hr * 60 + now_min) - (moonrise_hr * 60 + moonrise_min)) as f32 (now_mins - moon_rise) as f32
/ (moonday_hrs * 60 + moonday_mins) as f32) as i32 / moon_day as f32
}; };
} else { } else {
let (moonset_hr, moonset_min) = moonset.clone().unwrap(); let moon_set = moonset.clone().unwrap();
let (moonday_hrs, moonday_mins) = moonday.clone().unwrap(); let moon_day = moonday.clone().unwrap();
let begin_mins: i32 = (moonset_hr as i32 - moonday_hrs as i32) * 60 + (moonset_min as i32 - moonday_mins as i32); let begin_mins: i32 = moon_set - moon_day;
let time_since_rise = (now_hr * 60 + now_min) as i32 + (begin_mins.abs()); let time_since_rise = now_mins + (begin_mins.abs());
output_state.gradient_angle = (time_since_rise as f32 / (moonset_hr * 60 + moonset_min) as f32) as i32; output_state.gradient_angle_percentage = time_since_rise as f32 / moon_set as f32;
} }
} }
@ -159,34 +155,28 @@ pub fn run_calendar(args: &CliArgs) {
println!("{}", serde_json::to_string(&output_state).unwrap()); println!("{}", serde_json::to_string(&output_state).unwrap());
} }
fn parse_time(i: &str, do_debug: bool) -> Option<(u32, u32)> { fn parse_time(i: &str, do_debug: bool) -> Option<i32> {
if do_debug {
debug!("parsing: {}", i); debug!("parsing: {}", i);
}
let hr = match i[0..2].parse::<u32>() { let hr = match i[0..2].parse::<u32>() {
Ok(val) => val, Ok(val) => val as i32,
Err(e) => { Err(e) => {
if do_debug {
debug!("Error parsing: {e}"); debug!("Error parsing: {e}");
}
return None; return None;
} }
}; };
let min = match i[3..5].parse::<u32>() { let min = match i[3..5].parse::<u32>() {
Ok(val) => val, Ok(val) => val as i32,
Err(e) => { Err(e) => {
if do_debug {
debug!("Error parsing: {e}"); debug!("Error parsing: {e}");
}
return None; return None;
} }
}; };
Some((hr, min)) Some(hr * 60 + min)
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct CalandarState { struct CalandarState {
pub has_bg: bool, pub has_bg: bool,
pub is_sun: bool, pub is_sun: bool,
pub gradient_angle: i32, pub gradient_angle_percentage: f32,
} }