solar_positioning/
time.rs

1//! Time-related calculations for solar positioning.
2//!
3//! This module provides Julian date calculations and ΔT (Delta T) estimation
4//! following the algorithms from NREL SPA and Espenak & Meeus.
5
6#![allow(clippy::unreadable_literal)]
7#![allow(clippy::many_single_char_names)]
8
9use crate::math::{floor, polynomial};
10use crate::{Error, Result};
11#[cfg(feature = "chrono")]
12use chrono::{Datelike, TimeZone, Timelike};
13
14/// Seconds per day (86,400)
15const SECONDS_PER_DAY: f64 = 86_400.0;
16
17/// Julian Day Number for J2000.0 epoch (2000-01-01 12:00:00 UTC)
18const J2000_JDN: f64 = 2_451_545.0;
19
20/// Days per Julian century
21const DAYS_PER_CENTURY: f64 = 36_525.0;
22
23/// Julian date representation for astronomical calculations.
24///
25/// Follows the SPA algorithm described in Reda & Andreas (2003).
26/// Supports both Julian Date (JD) and Julian Ephemeris Date (JDE) calculations.
27#[derive(Debug, Clone, Copy, PartialEq)]
28pub struct JulianDate {
29    /// Julian Date (JD) - referenced to UT1
30    jd: f64,
31    /// Delta T in seconds - difference between TT and UT1
32    delta_t: f64,
33}
34
35impl JulianDate {
36    /// Creates a new Julian date from a timezone-aware chrono `DateTime`.
37    ///
38    /// Converts datetime to UTC for proper Julian Date calculation.
39    ///
40    /// # Arguments
41    /// * `datetime` - Timezone-aware date and time
42    /// * `delta_t` - ΔT in seconds (difference between TT and UT1)
43    ///
44    /// # Returns
45    /// Returns `Ok(JulianDate)` on success.
46    ///
47    /// # Errors
48    /// Returns error if the date/time components are invalid (e.g., invalid month, day, hour).
49    #[cfg(feature = "chrono")]
50    #[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
51    pub fn from_datetime<Tz: TimeZone>(
52        datetime: &chrono::DateTime<Tz>,
53        delta_t: f64,
54    ) -> Result<Self> {
55        // Convert the entire datetime to UTC for proper Julian Date calculation
56        let utc_datetime = datetime.with_timezone(&chrono::Utc);
57        Self::from_utc(
58            utc_datetime.year(),
59            utc_datetime.month(),
60            utc_datetime.day(),
61            utc_datetime.hour(),
62            utc_datetime.minute(),
63            f64::from(utc_datetime.second()) + f64::from(utc_datetime.nanosecond()) / 1e9,
64            delta_t,
65        )
66    }
67
68    /// Creates a new Julian date from year, month, day, hour, minute, and second in UTC.
69    ///
70    /// # Arguments
71    /// * `year` - Year (can be negative for BCE years)
72    /// * `month` - Month (1-12)
73    /// * `day` - Day of month (1-31)
74    /// * `hour` - Hour (0-23)
75    /// * `minute` - Minute (0-59)
76    /// * `second` - Second (0-59, can include fractional seconds)
77    /// * `delta_t` - ΔT in seconds (difference between TT and UT1)
78    ///
79    /// # Returns
80    /// Julian date or error if the date is invalid
81    ///
82    /// # Errors
83    /// Returns error if any date/time component is outside valid ranges (month 1-12, day 1-31, hour 0-23, minute 0-59, second 0-59.999).
84    ///
85    /// # Example
86    /// ```
87    /// # use solar_positioning::time::JulianDate;
88    /// let jd = JulianDate::from_utc(2023, 6, 21, 12, 0, 0.0, 69.0).unwrap();
89    /// assert!(jd.julian_date() > 2_460_000.0);
90    /// ```
91    pub fn from_utc(
92        year: i32,
93        month: u32,
94        day: u32,
95        hour: u32,
96        minute: u32,
97        second: f64,
98        delta_t: f64,
99    ) -> Result<Self> {
100        // Validate input ranges
101        if !(1..=12).contains(&month) {
102            return Err(Error::invalid_datetime("month must be between 1 and 12"));
103        }
104        if !(1..=31).contains(&day) {
105            return Err(Error::invalid_datetime("day must be between 1 and 31"));
106        }
107        if hour > 23 {
108            return Err(Error::invalid_datetime("hour must be between 0 and 23"));
109        }
110        if minute > 59 {
111            return Err(Error::invalid_datetime("minute must be between 0 and 59"));
112        }
113        if !(0.0..60.0).contains(&second) {
114            return Err(Error::invalid_datetime(
115                "second must be between 0 and 59.999...",
116            ));
117        }
118
119        if day > days_in_month(year, month, day)? {
120            return Err(Error::invalid_datetime("day is out of range for month"));
121        }
122
123        let jd = calculate_julian_date(year, month, day, hour, minute, second);
124        Ok(Self { jd, delta_t })
125    }
126
127    /// Creates a Julian date assuming ΔT = 0.
128    ///
129    /// # Arguments
130    /// * `year` - Year (can be negative for BCE years)
131    /// * `month` - Month (1-12)
132    /// * `day` - Day of month (1-31)
133    /// * `hour` - Hour (0-23)
134    /// * `minute` - Minute (0-59)
135    /// * `second` - Second (0-59, can include fractional seconds)
136    ///
137    /// # Returns
138    /// Returns `Ok(JulianDate)` with ΔT = 0 on success.
139    ///
140    /// # Errors
141    /// Returns error if the date/time components are outside valid ranges.
142    pub fn from_utc_simple(
143        year: i32,
144        month: u32,
145        day: u32,
146        hour: u32,
147        minute: u32,
148        second: f64,
149    ) -> Result<Self> {
150        Self::from_utc(year, month, day, hour, minute, second, 0.0)
151    }
152
153    /// Gets the Julian Date (JD) value.
154    ///
155    /// # Returns
156    /// Julian Date referenced to UT1
157    #[must_use]
158    pub const fn julian_date(&self) -> f64 {
159        self.jd
160    }
161
162    /// Gets the ΔT value in seconds.
163    ///
164    /// # Returns
165    /// ΔT (Delta T) in seconds
166    #[must_use]
167    pub const fn delta_t(&self) -> f64 {
168        self.delta_t
169    }
170
171    /// Calculates the Julian Ephemeris Day (JDE).
172    ///
173    /// JDE = JD + ΔT/86400
174    ///
175    /// # Returns
176    /// Julian Ephemeris Day
177    #[must_use]
178    pub fn julian_ephemeris_day(&self) -> f64 {
179        self.jd + self.delta_t / SECONDS_PER_DAY
180    }
181
182    /// Calculates the Julian Century (JC) from J2000.0.
183    ///
184    /// JC = (JD - 2451545.0) / 36525
185    ///
186    /// # Returns
187    /// Julian centuries since J2000.0 epoch
188    #[must_use]
189    pub fn julian_century(&self) -> f64 {
190        (self.jd - J2000_JDN) / DAYS_PER_CENTURY
191    }
192
193    /// Calculates the Julian Ephemeris Century (JCE) from J2000.0.
194    ///
195    /// JCE = (JDE - 2451545.0) / 36525
196    ///
197    /// # Returns
198    /// Julian ephemeris centuries since J2000.0 epoch
199    #[must_use]
200    pub fn julian_ephemeris_century(&self) -> f64 {
201        (self.julian_ephemeris_day() - J2000_JDN) / DAYS_PER_CENTURY
202    }
203
204    /// Calculates the Julian Ephemeris Millennium (JME) from J2000.0.
205    ///
206    /// JME = JCE / 10
207    ///
208    /// # Returns
209    /// Julian ephemeris millennia since J2000.0 epoch
210    #[must_use]
211    pub fn julian_ephemeris_millennium(&self) -> f64 {
212        self.julian_ephemeris_century() / 10.0
213    }
214
215    /// Add days to the Julian date (like Java constructor: new `JulianDate(jd.julianDate()` + i - 1, 0))
216    pub(crate) fn add_days(self, days: f64) -> Self {
217        Self {
218            jd: self.jd + days,
219            delta_t: self.delta_t,
220        }
221    }
222}
223
224/// Calculates Julian Date from UTC date/time components.
225///
226/// This follows the algorithm from Reda & Andreas (2003), which is based on
227/// Meeus, "Astronomical Algorithms", 2nd edition.
228fn calculate_julian_date(
229    year: i32,
230    month: u32,
231    day: u32,
232    hour: u32,
233    minute: u32,
234    second: f64,
235) -> f64 {
236    let mut y = year;
237    let mut m = i32::try_from(month).expect("month should be valid i32");
238
239    // Adjust for January and February being treated as months 13 and 14 of previous year
240    if m < 3 {
241        y -= 1;
242        m += 12;
243    }
244
245    // Calculate fractional day
246    let d = f64::from(day) + (f64::from(hour) + (f64::from(minute) + second / 60.0) / 60.0) / 24.0;
247
248    // Basic Julian Date calculation
249    let mut jd =
250        floor(365.25 * (f64::from(y) + 4716.0)) + floor(30.6001 * f64::from(m + 1)) + d - 1524.5;
251
252    // Gregorian calendar correction (after October 15, 1582)
253    // JDN 2299161 corresponds to October 15, 1582
254    if jd >= 2_299_161.0 {
255        let a = floor(f64::from(y) / 100.0);
256        let b = 2.0 - a + floor(a / 4.0);
257        jd += b;
258    }
259
260    jd
261}
262
263const fn is_gregorian_date(year: i32, month: u32, day: u32) -> bool {
264    year > 1582 || (year == 1582 && (month > 10 || (month == 10 && day >= 15)))
265}
266
267const fn is_leap_year(year: i32, is_gregorian: bool) -> bool {
268    if is_gregorian {
269        (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
270    } else {
271        year % 4 == 0
272    }
273}
274
275fn days_in_month(year: i32, month: u32, day: u32) -> Result<u32> {
276    if year == 1582 && month == 10 && (5..=14).contains(&day) {
277        return Err(Error::invalid_datetime(
278            "dates 1582-10-05 through 1582-10-14 do not exist in Gregorian calendar",
279        ));
280    }
281
282    let is_gregorian = is_gregorian_date(year, month, day);
283    let days = match month {
284        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
285        4 | 6 | 9 | 11 => 30,
286        2 => {
287            if is_leap_year(year, is_gregorian) {
288                29
289            } else {
290                28
291            }
292        }
293        _ => unreachable!("month already validated"),
294    };
295    Ok(days)
296}
297
298/// ΔT (Delta T) estimation functions.
299///
300/// ΔT represents the difference between Terrestrial Time (TT) and Universal Time (UT1).
301/// These estimates are based on Espenak and Meeus polynomial fits updated in 2014.
302pub struct DeltaT;
303
304impl DeltaT {
305    /// Estimates ΔT for a given decimal year.
306    ///
307    /// Based on polynomial fits from Espenak & Meeus, updated 2014.
308    /// See: <https://www.eclipsewise.com/help/deltatpoly2014.html>
309    ///
310    /// # Arguments
311    /// * `decimal_year` - Year with fractional part (e.g., 2024.5 for mid-2024)
312    ///
313    /// # Returns
314    /// Estimated ΔT in seconds
315    ///
316    /// # Errors
317    /// Returns error for years outside the valid range (-500 to 3000 CE)
318    ///
319    /// # Example
320    /// ```
321    /// # use solar_positioning::time::DeltaT;
322    /// let delta_t = DeltaT::estimate(2024.0).unwrap();
323    /// assert!(delta_t > 60.0 && delta_t < 80.0); // Reasonable range for 2024
324    /// ```
325    #[allow(clippy::too_many_lines)] // Comprehensive polynomial fit across historical periods
326    pub fn estimate(decimal_year: f64) -> Result<f64> {
327        let year = decimal_year;
328
329        if !year.is_finite() {
330            return Err(Error::invalid_datetime("year must be finite"));
331        }
332
333        let delta_t = if year < -500.0 {
334            let u = (year - 1820.0) / 100.0;
335            polynomial(&[-20.0, 0.0, 32.0], u)
336        } else if year < 500.0 {
337            let u = year / 100.0;
338            polynomial(
339                &[
340                    10583.6,
341                    -1014.41,
342                    33.78311,
343                    -5.952053,
344                    -0.1798452,
345                    0.022174192,
346                    0.0090316521,
347                ],
348                u,
349            )
350        } else if year < 1600.0 {
351            let u = (year - 1000.0) / 100.0;
352            polynomial(
353                &[
354                    1574.2,
355                    -556.01,
356                    71.23472,
357                    0.319781,
358                    -0.8503463,
359                    -0.005050998,
360                    0.0083572073,
361                ],
362                u,
363            )
364        } else if year < 1700.0 {
365            let t = year - 1600.0;
366            polynomial(&[120.0, -0.9808, -0.01532, 1.0 / 7129.0], t)
367        } else if year < 1800.0 {
368            let t = year - 1700.0;
369            polynomial(
370                &[8.83, 0.1603, -0.0059285, 0.00013336, -1.0 / 1_174_000.0],
371                t,
372            )
373        } else if year < 1860.0 {
374            let t = year - 1800.0;
375            polynomial(
376                &[
377                    13.72,
378                    -0.332447,
379                    0.0068612,
380                    0.0041116,
381                    -0.00037436,
382                    0.0000121272,
383                    -0.0000001699,
384                    0.000000000875,
385                ],
386                t,
387            )
388        } else if year < 1900.0 {
389            let t = year - 1860.0;
390            polynomial(
391                &[
392                    7.62,
393                    0.5737,
394                    -0.251754,
395                    0.01680668,
396                    -0.0004473624,
397                    1.0 / 233_174.0,
398                ],
399                t,
400            )
401        } else if year < 1920.0 {
402            let t = year - 1900.0;
403            polynomial(&[-2.79, 1.494119, -0.0598939, 0.0061966, -0.000197], t)
404        } else if year < 1941.0 {
405            let t = year - 1920.0;
406            polynomial(&[21.20, 0.84493, -0.076100, 0.0020936], t)
407        } else if year < 1961.0 {
408            let t = year - 1950.0;
409            polynomial(&[29.07, 0.407, -1.0 / 233.0, 1.0 / 2547.0], t)
410        } else if year < 1986.0 {
411            let t = year - 1975.0;
412            polynomial(&[45.45, 1.067, -1.0 / 260.0, -1.0 / 718.0], t)
413        } else if year < 2005.0 {
414            let t = year - 2000.0;
415            polynomial(
416                &[
417                    63.86,
418                    0.3345,
419                    -0.060374,
420                    0.0017275,
421                    0.000651814,
422                    0.00002373599,
423                ],
424                t,
425            )
426        } else if year < 2015.0 {
427            let t = year - 2005.0;
428            polynomial(&[64.69, 0.2930], t)
429        } else if year <= 3000.0 {
430            let t = year - 2015.0;
431            polynomial(&[67.62, 0.3645, 0.0039755], t)
432        } else {
433            return Err(Error::invalid_datetime(
434                "ΔT estimates not available beyond year 3000",
435            ));
436        };
437
438        Ok(delta_t)
439    }
440
441    /// Estimates ΔT from year and month.
442    ///
443    /// Calculates decimal year as: year + (month - 0.5) / 12
444    ///
445    /// # Arguments
446    /// * `year` - Year
447    /// * `month` - Month (1-12)
448    ///
449    /// # Returns
450    /// Returns estimated ΔT in seconds.
451    ///
452    /// # Errors
453    /// Returns error if month is outside the range 1-12.
454    ///
455    /// # Panics
456    /// This function does not panic.
457    pub fn estimate_from_date(year: i32, month: u32) -> Result<f64> {
458        if !(1..=12).contains(&month) {
459            return Err(Error::invalid_datetime("month must be between 1 and 12"));
460        }
461
462        let decimal_year = f64::from(year) + (f64::from(month) - 0.5) / 12.0;
463        Self::estimate(decimal_year)
464    }
465
466    /// Estimates ΔT from any date-like type.
467    ///
468    /// Convenience method that extracts the year and month from any chrono type
469    /// that implements `Datelike` (`DateTime`, `NaiveDateTime`, `NaiveDate`, etc.).
470    ///
471    /// # Arguments
472    /// * `date` - Any date-like type
473    ///
474    /// # Returns
475    /// Returns estimated ΔT in seconds.
476    ///
477    /// # Errors
478    /// Returns error if the date components are invalid.
479    ///
480    /// # Example
481    /// ```
482    /// # use solar_positioning::time::DeltaT;
483    /// # use chrono::{DateTime, FixedOffset, NaiveDate};
484    ///
485    /// // Works with DateTime
486    /// let datetime = "2024-06-21T12:00:00-07:00".parse::<DateTime<FixedOffset>>().unwrap();
487    /// let delta_t = DeltaT::estimate_from_date_like(datetime).unwrap();
488    /// assert!(delta_t > 60.0 && delta_t < 80.0);
489    ///
490    /// // Also works with NaiveDate
491    /// let date = NaiveDate::from_ymd_opt(2024, 6, 21).unwrap();
492    /// let delta_t2 = DeltaT::estimate_from_date_like(date).unwrap();
493    /// assert_eq!(delta_t, delta_t2);
494    #[cfg(feature = "chrono")]
495    #[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
496    #[allow(clippy::needless_pass_by_value)]
497    pub fn estimate_from_date_like<D: Datelike>(date: D) -> Result<f64> {
498        Self::estimate_from_date(date.year(), date.month())
499    }
500}
501
502#[cfg(test)]
503mod tests {
504    use super::*;
505
506    const EPSILON: f64 = 1e-10;
507
508    #[test]
509    fn test_julian_date_creation() {
510        let jd = JulianDate::from_utc(2000, 1, 1, 12, 0, 0.0, 0.0).unwrap();
511
512        // J2000.0 epoch should be exactly 2451545.0
513        assert!((jd.julian_date() - J2000_JDN).abs() < EPSILON);
514        assert_eq!(jd.delta_t(), 0.0);
515    }
516
517    #[test]
518    fn test_julian_date_invalid_day_validation() {
519        assert!(JulianDate::from_utc(2024, 2, 30, 0, 0, 0.0, 0.0).is_err());
520        assert!(JulianDate::from_utc(2024, 2, 29, 0, 0, 0.0, 0.0).is_ok());
521        assert!(JulianDate::from_utc(1900, 2, 29, 0, 0, 0.0, 0.0).is_err());
522        assert!(JulianDate::from_utc(1500, 2, 29, 0, 0, 0.0, 0.0).is_ok());
523        assert!(JulianDate::from_utc(1582, 10, 10, 0, 0, 0.0, 0.0).is_err());
524        assert!(JulianDate::from_utc(1582, 10, 4, 0, 0, 0.0, 0.0).is_ok());
525        assert!(JulianDate::from_utc(1582, 10, 15, 0, 0, 0.0, 0.0).is_ok());
526    }
527
528    #[test]
529    fn test_julian_date_validation() {
530        assert!(JulianDate::from_utc(2024, 13, 1, 0, 0, 0.0, 0.0).is_err()); // Invalid month
531        assert!(JulianDate::from_utc(2024, 1, 32, 0, 0, 0.0, 0.0).is_err()); // Invalid day
532        assert!(JulianDate::from_utc(2024, 1, 1, 24, 0, 0.0, 0.0).is_err()); // Invalid hour
533        assert!(JulianDate::from_utc(2024, 1, 1, 0, 60, 0.0, 0.0).is_err()); // Invalid minute
534        assert!(JulianDate::from_utc(2024, 1, 1, 0, 0, 60.0, 0.0).is_err()); // Invalid second
535    }
536
537    #[test]
538    fn test_julian_centuries() {
539        let jd = JulianDate::from_utc(2000, 1, 1, 12, 0, 0.0, 0.0).unwrap();
540
541        // J2000.0 should give JC = 0
542        assert!(jd.julian_century().abs() < EPSILON);
543        assert!(jd.julian_ephemeris_century().abs() < EPSILON);
544        assert!(jd.julian_ephemeris_millennium().abs() < EPSILON);
545    }
546
547    #[test]
548    fn test_julian_ephemeris_day() {
549        let delta_t = 69.0; // seconds
550        let jd = JulianDate::from_utc(2023, 6, 21, 12, 0, 0.0, delta_t).unwrap();
551
552        let jde = jd.julian_ephemeris_day();
553        let expected = jd.julian_date() + delta_t / SECONDS_PER_DAY;
554
555        assert!((jde - expected).abs() < EPSILON);
556    }
557
558    #[test]
559    fn test_gregorian_calendar_correction() {
560        // Test dates before and after Gregorian calendar adoption
561        // October 4, 1582 was followed by October 15, 1582
562        let julian_date = JulianDate::from_utc(1582, 10, 4, 12, 0, 0.0, 0.0).unwrap();
563        let gregorian_date = JulianDate::from_utc(1582, 10, 15, 12, 0, 0.0, 0.0).unwrap();
564
565        // The calendar dates are 11 days apart, but in Julian Day Numbers they should be 1 day apart
566        // because the 10-day gap was artificial
567        let diff = gregorian_date.julian_date() - julian_date.julian_date();
568        assert!(
569            (diff - 1.0).abs() < 1e-6,
570            "Expected 1 day difference in JD, got {diff}"
571        );
572
573        // Test that the Gregorian correction is applied correctly
574        // Dates after October 15, 1582 should have the correction
575        let pre_gregorian = JulianDate::from_utc(1582, 10, 1, 12, 0, 0.0, 0.0).unwrap();
576        let post_gregorian = JulianDate::from_utc(1583, 1, 1, 12, 0, 0.0, 0.0).unwrap();
577
578        // Verify that both exist and the calculation doesn't panic
579        assert!(pre_gregorian.julian_date() > 2_000_000.0);
580        assert!(post_gregorian.julian_date() > pre_gregorian.julian_date());
581    }
582
583    #[test]
584    fn test_delta_t_modern_estimates() {
585        // Test some known ranges
586        let delta_t_2000 = DeltaT::estimate(2000.0).unwrap();
587        let delta_t_2020 = DeltaT::estimate(2020.0).unwrap();
588
589        assert!(delta_t_2000 > 60.0 && delta_t_2000 < 70.0);
590        assert!(delta_t_2020 > 65.0 && delta_t_2020 < 75.0);
591        assert!(delta_t_2020 > delta_t_2000); // ΔT is generally increasing
592    }
593
594    #[test]
595    fn test_delta_t_historical_estimates() {
596        let delta_t_1900 = DeltaT::estimate(1900.0).unwrap();
597        let delta_t_1950 = DeltaT::estimate(1950.0).unwrap();
598
599        assert!(delta_t_1900 < 0.0); // Negative in early 20th century
600        assert!(delta_t_1950 > 25.0 && delta_t_1950 < 35.0);
601    }
602
603    #[test]
604    fn test_delta_t_boundary_conditions() {
605        // Test edge cases
606        assert!(DeltaT::estimate(-500.0).is_ok());
607        assert!(DeltaT::estimate(3000.0).is_ok());
608        assert!(DeltaT::estimate(-501.0).is_ok()); // Should work for ancient dates
609        assert!(DeltaT::estimate(3001.0).is_err()); // Should fail beyond 3000
610    }
611
612    #[test]
613    fn test_delta_t_from_date() {
614        let delta_t = DeltaT::estimate_from_date(2024, 6).unwrap();
615        let delta_t_decimal = DeltaT::estimate(2024.5 - 1.0 / 24.0).unwrap(); // June = month 6, so (6-0.5)/12 ≈ 0.458
616
617        // Should be very close
618        assert!((delta_t - delta_t_decimal).abs() < 0.01);
619
620        // Test invalid month
621        assert!(DeltaT::estimate_from_date(2024, 13).is_err());
622        assert!(DeltaT::estimate_from_date(2024, 0).is_err());
623    }
624
625    #[test]
626    fn test_delta_t_from_date_like() {
627        use chrono::{DateTime, FixedOffset, NaiveDate, Utc};
628
629        // Test with DateTime<FixedOffset>
630        let datetime_fixed = "2024-06-15T12:00:00-07:00"
631            .parse::<DateTime<FixedOffset>>()
632            .unwrap();
633        let delta_t_fixed = DeltaT::estimate_from_date_like(datetime_fixed).unwrap();
634
635        // Test with DateTime<Utc>
636        let datetime_utc = "2024-06-15T19:00:00Z".parse::<DateTime<Utc>>().unwrap();
637        let delta_t_utc = DeltaT::estimate_from_date_like(datetime_utc).unwrap();
638
639        // Test with NaiveDate
640        let naive_date = NaiveDate::from_ymd_opt(2024, 6, 15).unwrap();
641        let delta_t_naive_date = DeltaT::estimate_from_date_like(naive_date).unwrap();
642
643        // Test with NaiveDateTime
644        let naive_datetime = naive_date.and_hms_opt(12, 0, 0).unwrap();
645        let delta_t_naive_datetime = DeltaT::estimate_from_date_like(naive_datetime).unwrap();
646
647        // Should all be identical since we only use year/month
648        assert_eq!(delta_t_fixed, delta_t_utc);
649        assert_eq!(delta_t_fixed, delta_t_naive_date);
650        assert_eq!(delta_t_fixed, delta_t_naive_datetime);
651
652        // Should match estimate_from_date
653        let delta_t_date = DeltaT::estimate_from_date(2024, 6).unwrap();
654        assert_eq!(delta_t_fixed, delta_t_date);
655
656        // Verify reasonable range for 2024
657        assert!(delta_t_fixed > 60.0 && delta_t_fixed < 80.0);
658    }
659
660    #[test]
661    fn test_specific_julian_dates() {
662        // Test some well-known dates
663
664        // Unix epoch: 1970-01-01 00:00:00 UTC
665        let unix_epoch = JulianDate::from_utc(1970, 1, 1, 0, 0, 0.0, 0.0).unwrap();
666        assert!((unix_epoch.julian_date() - 2_440_587.5).abs() < 1e-6);
667
668        // Y2K: 2000-01-01 00:00:00 UTC
669        let y2k = JulianDate::from_utc(2000, 1, 1, 0, 0, 0.0, 0.0).unwrap();
670        assert!((y2k.julian_date() - 2_451_544.5).abs() < 1e-6);
671    }
672}