1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use chrono::{DateTime, NaiveDate, TimeZone, Timelike, Utc};

const UNIX_EPOCH: JulianDate = JulianDate(2440587.5);
const SECONDS_PER_DAY: u64 = 24 * 60 * 60;
const JAN_2000: JulianDate = JulianDate(2451545.0);
const LEAP_SECONDS: JulianDate = JulianDate(0.0008);
const OBLIQUITY_OF_THE_ECLIPTIC: f64 = 23.44;

#[derive(Debug, Clone, Copy)]
struct JulianDate(f64);

impl JulianDate {
    fn ceil_days(&self) -> f64 {
        self.0.ceil()
    }

    fn to_datetime(self) -> Option<DateTime<Utc>> {
        Utc.timestamp_opt(
            ((self - UNIX_EPOCH).0 * SECONDS_PER_DAY as f64).round() as i64,
            0,
        )
        .single()
    }
}

impl From<DateTime<Utc>> for JulianDate {
    fn from(date: DateTime<Utc>) -> Self {
        Self((date.timestamp() as f64 / SECONDS_PER_DAY as f64) + UNIX_EPOCH.0)
    }
}

impl std::ops::Sub<JulianDate> for JulianDate {
    type Output = Self;

    fn sub(self, rhs: JulianDate) -> Self::Output {
        Self(self.0 - rhs.0)
    }
}

impl std::ops::Add<JulianDate> for JulianDate {
    type Output = Self;

    fn add(self, rhs: JulianDate) -> Self::Output {
        Self(self.0 + rhs.0)
    }
}

/// Calculates the approximate sunset and sunrise times at a given latitude, longitude, and altitude
///
/// Note that elevation is used to correct for atmospheric refraction, so negative elevations are treated as being at
/// sea level due to having minimal difference in refraction to being at sea level
///
/// # Arguments
///
/// * `date` - The date on which to calculate the sunset and sunrise, in UTC
/// * `latitude` - The latitude at which to calculate the times. Expressed as degrees
/// * `longitude` - The longitude at which to calculate the times. Expressed as degrees
/// * `elevation` - The elevation at which to calculate the times. Expressed as meters above sea level. Negative values will be ignored
///
/// # Return value
///
/// Returns
///  - `None` if the date is not representable in chrono (~5M years from now), or sunsets/rises cannot be calculated due to long arctic/antarctic day/night (outside ~±67° of latitude)
///  - `Some((sunrise,sunset))` otherwise
///
/// # Examples
///
/// ```
/// //Calculate the sunset and sunrise times today at Sheffield university's new computer science building
/// let times = sun_times(Utc::today(),53.38,-1.48,100.0);
/// println!("Sunrise: {}, Sunset: {}",times.0,times.1);
/// ```
pub fn sun_times(
    date: NaiveDate,
    latitude: f64,
    longitude: f64,
    elevation: f64,
) -> Option<(DateTime<Utc>, DateTime<Utc>)> {
    //see https://en.wikipedia.org/wiki/Sunrise_equation

    const ARGUMENT_OF_PERIHELION: f64 = 102.9372;

    let julian_date = JulianDate::from(
        date.and_hms_opt(0, 0, 0)?
            .and_local_timezone(Utc)
            .single()?,
    );

    //elevations below sea level will have minimal atmospheric refraction + the
    //calculation is broken below sea level, so treat negative elevations as being at sea level
    let elevation = elevation.max(0.0);
    let elevation_correction = -2.076 * (elevation.sqrt()) / 60.0;

    let days_since_2000 = (julian_date - JAN_2000 + LEAP_SECONDS).ceil_days();

    let mean_solar_time = days_since_2000 - (longitude / 360.0);
    let solar_mean_anomaly = (357.5291 + 0.98560028 * mean_solar_time).rem_euclid(360.0);
    let center = 1.9148 * solar_mean_anomaly.to_radians().sin()
        + 0.0200 * (2.0 * solar_mean_anomaly).to_radians().sin()
        + 0.0003 * (3.0 * solar_mean_anomaly).to_radians().sin();
    let ecliptic_longitude =
        (solar_mean_anomaly + center + 180.0 + ARGUMENT_OF_PERIHELION).rem_euclid(360.0);

    let declination = (ecliptic_longitude.to_radians().sin()
        * OBLIQUITY_OF_THE_ECLIPTIC.to_radians().sin())
    .asin();
    let event_hour_angle = (((-0.83 + elevation_correction).to_radians().sin()
        - (latitude.to_radians().sin() * declination.sin()))
        / (latitude.to_radians().cos() * declination.cos()))
    .acos()
    .to_degrees();

    if event_hour_angle.is_nan() {
        return None;
    }

    let solar_transit =
        JAN_2000.0 + mean_solar_time + 0.0053 * solar_mean_anomaly.to_radians().sin()
            - 0.0069 * (2.0 * ecliptic_longitude).to_radians().sin();
    let solar_transit_julian = JulianDate(solar_transit);

    let julian_rise = JulianDate(solar_transit_julian.0 - event_hour_angle / 360.0);
    let julian_set = JulianDate(solar_transit_julian.0 + event_hour_angle / 360.0);
    let rise = julian_rise.to_datetime();
    let set = julian_set.to_datetime();
    if let (Some(rise), Some(set)) = (rise, set) {
        Some((rise, set))
    } else {
        None
    }
}

/// Calculates the altitude (angle from the horizon) of the sun at a given place and moment
/// # Arguments
///
/// * `date_time` - The date and time on which to calculate the sunset and sunrise
/// * `latitude` - The latitude at which to calculate the times. Expressed as degrees
/// * `longitude` - The longitude at which to calculate the times. Expressed as degrees
///
/// # Return value
///
/// Returns the altitude in degrees
///
/// # Notes
///
/// This currently does not include an altitude correction like the [sun_times] function does, but *is* usable within arctic regions
///
/// # Examples
///
/// ```
/// //Calculate the sunset and sunrise times today at Sheffield university's new computer science building
/// let altitude = altitude(Utc::now(),53.38,-1.48);
/// println!("Altitude: {}",altitude);
/// ```
pub fn altitude(date_time: DateTime<Utc>, latitude: f64, longitude: f64) -> f64 {
    //see https://en.wikipedia.org/wiki/Sunrise_equation
    //see https://en.wikipedia.org/wiki/Astronomical_coordinate_systems
    //see http://www.stargazing.net/kepler/altaz.html

    const ARGUMENT_OF_PERIHELION: f64 = 102.9372;

    let julian_date = JulianDate::from(date_time);

    let days_since_2000 = (julian_date - JAN_2000 + LEAP_SECONDS).ceil_days();

    let mean_solar_time = days_since_2000 - (longitude / 360.0);
    let solar_mean_anomaly = (357.5291 + 0.98560028 * mean_solar_time).rem_euclid(360.0);
    let center = 1.9148 * solar_mean_anomaly.to_radians().sin()
        + 0.0200 * (2.0 * solar_mean_anomaly).to_radians().sin()
        + 0.0003 * (3.0 * solar_mean_anomaly).to_radians().sin();
    let ecliptic_longitude =
        (solar_mean_anomaly + center + 180.0 + ARGUMENT_OF_PERIHELION).rem_euclid(360.0);

    let sin_declination =
        ecliptic_longitude.to_radians().sin() * OBLIQUITY_OF_THE_ECLIPTIC.to_radians().sin();
    let declination = sin_declination.asin();

    let right_ascension = (ecliptic_longitude.to_radians().sin()
        * OBLIQUITY_OF_THE_ECLIPTIC.to_radians().cos())
    .atan2(ecliptic_longitude.to_radians().cos())
    .to_degrees();

    let greenwich_sidereal_time = mean_solar_time + 0.0;
    let local_sideral_time = greenwich_sidereal_time
        + (date_time.time().hour() as f64
            + (date_time.time().minute() as f64 / 60.0)
            + (date_time.time().second() as f64 / 60.0 * 60.0))
            * 15.0
        + longitude.to_degrees();
    let local_hour_angle = local_sideral_time - right_ascension;

    let sin_altitude = (latitude.to_radians().sin() * declination.sin())
        + (latitude.to_radians().cos() * declination.cos() * local_hour_angle.to_radians().cos());
    sin_altitude.asin().to_degrees()
}

#[cfg(test)]
mod tests {
    use chrono::{Duration, NaiveDate};

    #[test]
    ///Test for https://github.com/Eroc33/sun-times/issues/1
    fn sunrise_and_sunset_land_on_requested_day() {
        let date_range =
            std::iter::successors(Some(NaiveDate::from_ymd_opt(2022, 1, 1).unwrap()), |date| {
                let next = *date + Duration::days(1);
                if next > NaiveDate::from_ymd_opt(2022, 12, 31).unwrap() {
                    None
                } else {
                    Some(next)
                }
            });
        for date in date_range {
            let times = super::sun_times(date, 53.38, -1.48, 0.0);
            assert!(times.is_some());
            let times = times.unwrap();
            assert_eq!(date, times.0.naive_utc().date());
            assert_eq!(date, times.1.naive_utc().date());
        }
    }
}