practical_astronomy_rust/
util.rs

1/// Determine if year is a leap year.
2///
3/// ## Arguments
4/// year
5///
6/// ## Returns
7/// true or false
8pub fn is_leap_year(input_year: u32) -> bool {
9    let year = input_year as f64;
10
11    if year % 4.0 == 0.0 {
12        if year % 100.0 == 0.0 {
13            return if year % 400.0 == 0.0 { true } else { false };
14        } else {
15            return true;
16        }
17    } else {
18        return false;
19    }
20}
21
22/// Round an f64 primitive to the specified number of decimal places.
23pub fn round_f64(input_value: f64, places: usize) -> f64 {
24    return format!("{:.width$}", input_value, width = places)
25        .parse::<f64>()
26        .unwrap();
27}
28
29/// Convert a Universal Time hour to Local Time
30pub fn get_local_hour_from_ut(
31    input_hour: f64,
32    is_daylight_saving: bool,
33    zone_correction_hours: i32,
34) -> f64 {
35    let adjustment_value: f64 = if is_daylight_saving {
36        (zone_correction_hours as f64) - 1.0
37    } else {
38        zone_correction_hours as f64
39    };
40
41    let local_hour: f64 = input_hour - adjustment_value;
42
43    return local_hour;
44}