satkit 0.16.2

Satellite Toolkit
Documentation
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
use super::TimeScale;
use serde::{Deserialize, Serialize};

use anyhow::{bail, Result};

// Days in the month, neglecting leap years
const MDAYS: [u32; 12] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

/// A module for handling time and date conversions.  Time is stored natively as
/// the number of microseconds since the Unix epoch (1970-01-01 00:00:00 UTC)
/// with leap seconds accounted for.
///
/// The Instant struct provides methods for converting to and from Unix time, GPS time,
/// Julian Date, Modified Julian Date, and Gregorian calendar date.
///
/// Why do we need another structure that handles time?
///
/// This structure is necessary as it is time scale aware, i.e. it can
/// handle different time scales such as UTC, TAI, TT, UT1, GPS, etc.
/// This is necessary for high-precision coordinate transforms and orbit propagation.
///
#[derive(Copy, Clone, Serialize, Deserialize)]
pub struct Instant {
    /// The number of microseconds since
    /// Unix epoch (1970-01-01 00:00:00 UTC)
    pub raw: i64,
}

/// For conversion between Julian day and
/// Gregorian calendar date
/// See: <https://en.wikipedia.org/wiki/Julian_day>
/// or Expl. Suppl. Astron. Almanac, P. 619
mod gregorian_coefficients {
    #[allow(non_upper_case_globals)]
    pub const y: i64 = 4716;
    #[allow(non_upper_case_globals)]
    pub const j: i64 = 1401;
    #[allow(non_upper_case_globals)]
    pub const m: i64 = 2;
    #[allow(non_upper_case_globals)]
    pub const n: i64 = 12;
    #[allow(non_upper_case_globals)]
    pub const r: i64 = 4;
    #[allow(non_upper_case_globals)]
    pub const p: i64 = 1461;
    #[allow(non_upper_case_globals)]
    pub const v: i64 = 3;
    #[allow(non_upper_case_globals)]
    pub const u: i64 = 5;
    #[allow(non_upper_case_globals)]
    pub const s: i64 = 153;
    #[allow(non_upper_case_globals)]
    pub const t: i64 = 2;
    #[allow(non_upper_case_globals)]
    pub const w: i64 = 2;
    pub const A: i64 = 184;
    pub const B: i64 = 274_277;
    pub const C: i64 = -38;
}

/// Leap second table
/// The first element is the number of microseconds since unixtime epoch
/// The second element is the number of leap seconds to add as microseconds
const LEAP_SECOND_TABLE: [(i64, i64); 28] = [
    (1483228836000000, 37000000), // 2017-01-01
    (1435708835000000, 36000000), // 2015-07-01
    (1341100834000000, 35000000), // 2012-07-01
    (1230768033000000, 34000000), // 2009-01-01
    (1136073632000000, 33000000), // 2006-01-01
    (915148831000000, 32000000),  // 1999-01-01
    (867715230000000, 31000000),  // 1997-07-01
    (820454429000000, 30000000),  // 1996-01-01
    (773020828000000, 29000000),  // 1994-07-01
    (741484827000000, 28000000),  // 1993-07-01
    (709948826000000, 27000000),  // 1992-07-01
    (662688025000000, 26000000),  // 1991-01-01
    (631152024000000, 25000000),  // 1990-01-01
    (567993623000000, 24000000),  // 1988-01-01
    (489024022000000, 23000000),  // 1985-07-01
    (425865621000000, 22000000),  // 1983-07-01
    (394329620000000, 21000000),  // 1982-07-01
    (362793619000000, 20000000),  // 1981-07-01
    (315532818000000, 19000000),  // 1980-01-01
    (283996817000000, 18000000),  // 1979-01-01
    (252460816000000, 17000000),  // 1978-01-01
    (220924815000000, 16000000),  // 1977-01-01
    (189302414000000, 15000000),  // 1976-01-01
    (157766413000000, 14000000),  // 1975-01-01
    (126230412000000, 13000000),  // 1974-01-01
    (94694411000000, 12000000),   // 1973-01-01
    (78796810000000, 11000000),   // 1972-07-01
    (63072009000000, 10000000),   // 1972-01-01
];

/// Return the number of leap "micro" seconds at "raw" time,
/// which is microseconds since unixtime epoch
fn microleapseconds(raw: i64) -> i64 {
    for (t, ls) in LEAP_SECOND_TABLE.iter() {
        if raw > *t {
            return *ls;
        }
    }
    0
}

impl Instant {
    /// Construct a new Instant from raw microseconds
    ///
    /// # Arguments
    /// * `raw` - The number of microseconds since unixtime epoch
    ///
    /// # Returns
    /// A new Instant object
    ///
    /// # Example
    ///
    /// ```
    /// use satkit::Instant;
    /// let now = Instant::new(1234567890);
    /// ```
    pub const fn new(raw: i64) -> Self {
        Self { raw }
    }

    /// Construct a new Instant from GPS week and second of week
    ///
    /// # Arguments
    /// * `week` - The GPS week number
    /// * `sow` - The second of week
    ///
    /// # Returns
    /// A new Instant object
    ///
    pub fn from_gps_week_and_second(week: i32, sow: f64) -> Self {
        let week = week as i64;
        let raw = week * 604_800_000_000 + (sow * 1.0e6) as i64 + Self::GPS_EPOCH.raw;
        Self { raw }
    }

    /// Construct a new Instant from Unix time
    ///
    /// # Arguments
    /// * `unixtime` - The Unix time in seconds
    ///
    /// # Returns
    /// A new Instant object representing the input Unix time
    ///
    /// # Note:
    /// Unixtime is the number of non-leap seconds since Jan 1 1970 00:00:00 UTC
    /// (Leap seconds are ignored!!)
    pub fn from_unixtime(unixtime: f64) -> Self {
        let mut raw = (unixtime * 1.0e6) as i64 + Self::UNIX_EPOCH.raw;

        // Add leapseconds since unixtime ignores them
        let ls = microleapseconds(raw);
        raw += ls;
        // Make sure adding the leapseconds didn't cross another
        // leapsecond boundary
        raw += microleapseconds(raw) - ls;
        Self { raw }
    }

    /// Convert Instant to Unix time
    ///
    /// # Returns
    /// The Unix time in seconds (since 1970-01-01 00:00:00 UTC)
    ///
    /// # Note
    /// Unixtime is the number of non-leap seconds since
    /// 1970-01-01 00:00:00 UTC.
    pub fn as_unixtime(&self) -> f64 {
        // Subtract leap seconds since unixtime ignores them
        (self.raw - Self::UNIX_EPOCH.raw - microleapseconds(self.raw)) as f64 * 1.0e-6
    }

    /// J2000 epoch is 2000-01-01 12:00:00 TT
    /// TT (Terrestrial Time) is 32.184 seconds ahead of TAI
    pub const J2000: Self = Self {
        raw: 946727967816000,
    };

    /// Unix epoch is 1970-01-01 00:00:00 UTC
    pub const UNIX_EPOCH: Self = Self { raw: 0 };

    /// GPS epoch is 1980-01-06 00:00:00 UTC
    pub const GPS_EPOCH: Self = Self {
        raw: 315964819000000,
    };

    pub const INVALID: Self = Self { raw: i64::MIN };

    /// Modified Julian day epoch is
    /// 1858-11-17 00:00:00 UTC
    pub const MJD_EPOCH: Self = Self {
        raw: -3506716800000000,
    };

    /// Return the day of the week
    /// 0 = Sunday, 1 = Monday, ..., 6 = Saturday
    ///
    /// See: <https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week>
    pub fn day_of_week(&self) -> super::Weekday {
        let jd = self.as_jd_utc();
        super::Weekday::from(((jd + 1.5) % 7.0).floor() as i32)
    }

    /// As Modified Julian Date (UTC)
    /// Days since 1858-11-17 00:00:00 UTC
    /// where each day is 86,400 seconds
    /// (no leap seconds)
    #[deprecated(note = "Use as_mjd_utc() for explicit scale, or as_mjd_with_scale()")]
    pub fn as_mjd(&self) -> f64 {
        self.as_mjd_utc()
    }

    /// As Modified Julian Date (UTC)
    /// Days since 1858-11-17 00:00:00 UTC
    /// where each day is 86,400 seconds
    /// (no leap seconds)
    pub fn as_mjd_utc(&self) -> f64 {
        self.as_mjd_with_scale(TimeScale::UTC)
    }

    /// Create Instant from Modified Julian Date (UTC)
    ///
    /// # Arguments
    /// * `mjd` - Modified Julian Date (UTC)
    ///
    /// # Returns
    /// A new Instant object representing the given MJD
    #[deprecated(note = "Use from_mjd_utc() for explicit scale, or from_mjd_with_scale()")]
    pub fn from_mjd(mjd: f64) -> Self {
        Self::from_mjd_utc(mjd)
    }

    /// Create Instant from Modified Julian Date (UTC)
    ///
    /// # Arguments
    /// * `mjd` - Modified Julian Date (UTC)
    ///
    /// # Returns
    /// A new Instant object representing the given MJD
    pub fn from_mjd_utc(mjd: f64) -> Self {
        Self::from_mjd_with_scale(mjd, TimeScale::UTC)
    }

    /// Create Instant from Julian Date (UTC)
    ///
    /// # Arguments
    /// * `jd` - Julian Date (UTC)
    ///
    /// # Returns
    /// A new Instant object representing the given JD
    #[deprecated(note = "Use from_jd_utc() for explicit scale, or from_jd_with_scale()")]
    pub fn from_jd(jd: f64) -> Self {
        Self::from_jd_utc(jd)
    }

    /// Create Instant from Julian Date (UTC)
    ///
    /// # Arguments
    /// * `jd` - Julian Date (UTC)
    ///
    /// # Returns
    /// A new Instant object representing the given JD
    pub fn from_jd_utc(jd: f64) -> Self {
        Self::from_mjd_utc(jd - 2400000.5)
    }

    /// Create Instant from Julian Date with given time scale
    /// (UTC, TAI, TT, UT1, GPS)
    /// Days since 4713 BC January 1, 12:00 UTC
    ///
    /// # Arguments
    /// * `jd` - Julian Date
    /// * `scale` - The time scale to use
    ///
    /// # Returns
    /// A new Instant object representing the given JD at given time scale
    pub fn from_jd_with_scale(jd: f64, scale: TimeScale) -> Self {
        Self::from_mjd_with_scale(jd - 2400000.5, scale)
    }

    /// Construct an instant from a given Modified Julian Date
    /// and time scale
    ///
    /// # Arguments
    /// * `mjd` - The Modified Julian Date
    /// * `scale` - The time scale to use
    ///
    /// # Returns
    /// A new Instant object representing the given MJD at given time scale
    pub fn from_mjd_with_scale(mjd: f64, scale: TimeScale) -> Self {
        match scale {
            TimeScale::UTC => {
                let raw = (mjd * 86_400_000_000.0) as i64 + Self::MJD_EPOCH.raw;
                let ls = microleapseconds(raw);
                let raw = raw + ls;
                // Make sure adding the leapseconds didn't cross another
                // leapsecond boundary
                let raw = raw + microleapseconds(raw) - ls;
                Self { raw }
            }
            TimeScale::TAI => {
                let raw = (mjd * 86_400_000_000.0) as i64 + Self::MJD_EPOCH.raw;
                Self { raw }
            }
            TimeScale::TT => {
                let raw = (mjd * 86_400_000_000.0) as i64 + Self::MJD_EPOCH.raw - 32_184_000;
                Self { raw }
            }
            TimeScale::UT1 => {
                // This will be approximately correct for computing ut1
                let eop =
                    crate::earth_orientation_params::eop_from_mjd_utc(mjd).unwrap_or([0.0; 6]);
                let dut1 = eop[0] as f64;
                Self::from_mjd_with_scale(mjd - dut1 / 86_400.0, TimeScale::UTC)
            }
            TimeScale::GPS => {
                // GPS = TAI - 19 seconds
                let raw = (mjd * 86_400_000_000.0) as i64 + Self::MJD_EPOCH.raw + 19_000_000;
                Self { raw }
            }
            TimeScale::Invalid => Self::INVALID,
            TimeScale::TDB => {
                let ttc: f64 = (mjd - (2451545.0 - 2400000.5)) / 36525.0;
                let mjd = (0.001657f64 / 86400.0f64).mul_add(
                    -(std::f64::consts::PI / 180.0 * 628.3076f64.mul_add(ttc, 6.2401)).sin(),
                    mjd,
                ) - 32.184 / 86400.0;
                Self::from_mjd_with_scale(mjd, TimeScale::TAI)
            }
        }
    }

    /// As Julian Date (UTC)
    /// Days since 4713 BC January 1, 12:00 UTC
    /// where each day is 86,400 seconds
    /// (no leap seconds)
    #[deprecated(note = "Use as_jd_utc() for explicit scale, or as_jd_with_scale()")]
    #[allow(deprecated)]
    pub fn as_jd(&self) -> f64 {
        self.as_mjd() + 2400000.5
    }

    /// As Julian Date (UTC)
    /// Days since 4713 BC January 1, 12:00 UTC
    /// where each day is 86,400 seconds
    /// (no leap seconds)
    pub fn as_jd_utc(&self) -> f64 {
        self.as_mjd_utc() + 2400000.5
    }

    /// As Julian Date with given time scale
    /// Days since 4713 BC January 1, 12:00 UTC
    ///
    /// # Arguments
    /// * `scale` - The time scale to use
    ///
    /// # Returns
    /// The Julian Date in the given time scale
    ///
    pub fn as_jd_with_scale(&self, scale: TimeScale) -> f64 {
        self.as_mjd_with_scale(scale) + 2400000.5
    }

    /// Add given floating-point number of days to Instant instance,
    /// and return new instance representing new time.
    ///
    /// Days are defined in this case to have exactly 86400.0 seconds
    /// In other words, this will ignore leap seconds and the integer
    /// part of the floating point will increment the number of days and
    /// the decimal part will increment the fractions of a day.
    ///
    /// So, for example, adding 1.0 to a day with a leap second will
    /// increment by a full day
    ///
    /// # Arguments
    /// * `days` - The number of days to add
    ///
    /// # Returns
    /// A new Instant object representing the new time
    ///
    pub fn add_utc_days(&self, days: f64) -> Self {
        let mut utc = self.as_mjd_with_scale(TimeScale::UTC);
        utc += days;
        Self::from_mjd_with_scale(utc, TimeScale::UTC)
    }

    /// As Modified Julian Date with given time scale
    /// Days since 1858-11-17 00:00:00 UTC
    ///
    /// # Arguments
    /// * `scale` - The time scale to use
    ///
    /// # Returns
    /// The Modified Julian Date in the given time scale
    ///
    pub fn as_mjd_with_scale(&self, scale: TimeScale) -> f64 {
        match scale {
            TimeScale::UTC => {
                (self.raw - Self::MJD_EPOCH.raw - microleapseconds(self.raw)) as f64
                    / 86_400_000_000.0
            }
            TimeScale::TT => {
                (self.raw - Self::MJD_EPOCH.raw + 32_184_000) as f64 / 86_400_000_000.0
            }
            TimeScale::UT1 => {
                let mjd_utc = self.as_mjd_utc();
                let eop =
                    crate::earth_orientation_params::eop_from_mjd_utc(mjd_utc).unwrap_or([0.0; 6]);
                let dut1 = eop[0] as f64;
                mjd_utc + dut1 / 86_400.0
            }
            TimeScale::TAI => (self.raw - Self::MJD_EPOCH.raw) as f64 / 86_400_000_000.0,
            TimeScale::GPS => {
                // GPS = TAI - 19 seconds
                (self.raw - Self::MJD_EPOCH.raw - 19_000_000) as f64 / 86_400_000_000.0
            }
            TimeScale::TDB => {
                let tt: f64 = self.as_mjd_with_scale(TimeScale::TT);
                let ttc: f64 = (tt - (2451545.0f64 - 2400000.5f64)) / 36525.0;
                (0.001657f64 / 86400.0f64).mul_add(
                    (std::f64::consts::PI / 180.0 * 628.3076f64.mul_add(ttc, 6.2401)).sin(),
                    tt,
                )
            }
            TimeScale::Invalid => 0.0,
        }
    }

    /// Return the Gregorian date and time
    ///
    /// # Returns
    /// (year, month, day, hour, minute, second), UTC
    pub fn as_datetime(&self) -> (i32, i32, i32, i32, i32, f64) {
        // Fractional part of UTC day, accounting for leapseconds and TT - TAI
        let utc_usec_of_day = (self.raw - microleapseconds(self.raw)) % 86_400_000_000;
        let mut jdadd: i64 = 0;

        let mut hour = utc_usec_of_day / 3_600_000_000;
        if hour < 12 {
            jdadd += 1
        }
        let mut minute = (utc_usec_of_day - (hour * 3_600_000_000)) / 60_000_000;
        let mut second =
            (utc_usec_of_day - (hour * 3_600_000_000) - (minute * 60_000_000)) as f64 * 1.0e-6;

        // Rare case where we are in the middle of a leap-second
        for (t, _) in LEAP_SECOND_TABLE.iter() {
            if self.raw >= *t && self.raw - *t < 1_000_000 {
                hour = 23;
                minute = 59;
                if second == 0.0 {
                    second = 60.0;
                    jdadd -= 1;
                } else {
                    second += 1.0;
                }
            }
        }

        /// See: https://en.wikipedia.org/wiki/Julian_day
        /// or Expl. Suppl. Astron. Almanac, P. 619
        use gregorian_coefficients as gc;
        let mut jd = self.as_jd_utc().floor() as i64;
        jd += jdadd;
        let f = jd + gc::j + (((4 * jd + gc::B) / 146097) * 3) / 4 + gc::C;
        let e = gc::r * f + gc::v;
        let g = (e % gc::p) / gc::r;
        let h = gc::u * g + gc::w;
        let day = ((h % gc::s) / gc::u) + 1;
        let month = ((h / gc::s + gc::m) % gc::n) + 1;
        let year = (e / gc::p) - gc::y + (gc::n + gc::m - month) / gc::n;

        (
            year as i32,
            month as i32,
            day as i32,
            hour as i32,
            minute as i32,
            second,
        )
    }

    /// Construct an instant from a given UTC date
    ///
    /// # Arguments
    /// * `year` - The year
    /// * `month` - The month
    /// * `day` - The day
    ///
    /// # Returns
    /// A new Instant object representing the given date
    pub fn from_date(year: i32, month: i32, day: i32) -> Result<Self> {
        Self::from_datetime(year, month, day, 0, 0, 0.0)
    }

    /// Return the day of the year (1-based, Gregorian); leap-year aware.
    ///
    /// # Returns
    /// The day of the year (1-based, Gregorian); leap-year aware.
    ///
    /// # Notes:
    ///
    /// * Gregorian Jan 1 = 1
    ///
    /// # Example
    ///
    /// ```rust
    /// // Examples checked against google query
    ///
    /// let thedate = satkit::Instant::from_date(2023, 1, 1).unwrap();
    /// assert_eq!(thedate.day_of_year(), 1);
    ///
    /// let thedate = satkit::Instant::from_date(2025, 8, 16).unwrap();
    /// assert_eq!(thedate.day_of_year(), 228);
    /// ```
    pub fn day_of_year(&self) -> u32 {
        let (y, m, d, _, _, _) = self.as_datetime();
        let leap = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
        let mut doy = d;
        for mm in 1..m {
            doy += if mm == 2 && leap {
                29i32
            } else {
                MDAYS[(mm - 1) as usize] as i32
            };
        }
        doy as u32
    }

    /// Convenience alias for `from_datetime`
    ///
    /// Construct an instant from a given Gregorian UTC date and time
    ///
    /// # Arguments
    /// * `year` - The year
    /// * `month` - The month (1-12)
    /// * `day` - The day (1-31)
    /// * `hour` - The hour (0-23)
    /// * `minute` - The minute (0-59)
    /// * `second` - The second (0.0-60.0)
    ///
    /// # Returns
    /// A new Instant object representing the given date and time, or error if invalid
    pub fn utc(
        year: i32,
        month: i32,
        day: i32,
        hour: i32,
        minute: i32,
        second: f64,
    ) -> Result<Self> {
        Self::from_datetime(year, month, day, hour, minute, second)
    }

    /// Construct an instant from a given Gregorian UTC date and time
    ///
    /// # Arguments
    /// * `year` - The year
    /// * `month` - The month
    /// * `day` - The day
    /// * `hour` - The hour
    /// * `minute` - The minute
    /// * `second` - The second
    ///
    /// # Returns
    /// A new Instant object representing the given date and time, or error if invalid
    /// or error if the month, day, hour, minute, or second are out of bounds
    pub fn from_datetime(
        year: i32,
        month: i32,
        day: i32,
        hour: i32,
        minute: i32,
        second: f64,
    ) -> Result<Self> {
        let mut check_leapsecond: bool = false;

        // Bounds checking on input
        if !(1..=12).contains(&month) {
            bail!("Invalid month: {}", month);
        }
        let max_day = if month == 2 {
            if (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) {
                29
            } else {
                28
            }
        } else {
            MDAYS[(month - 1) as usize]
        };
        if day < 1 || day > max_day as i32 {
            bail!("Invalid day: {}", day);
        }
        if !(0..=23).contains(&hour) {
            bail!("Invalid hour: {}", hour);
        }
        if !(0..=59).contains(&minute) {
            bail!("Invalid minute: {}", minute);
        }
        if !(0.0..60.0).contains(&second) {
            // Check for rare case of leap second
            if (60.0..61.0).contains(&second) {
                // Are we in a leap second?
                check_leapsecond = true;
            } else {
                bail!("Invalid second: {}", second);
            }
        }

        use gregorian_coefficients as gc;
        let h = month as i64 - gc::m;
        let g = year as i64 + gc::y - (gc::n - h) / gc::n;
        let f = (h - 1 + gc::n) % gc::n;
        let e = (gc::p * g) / gc::r + day as i64 - 1 - gc::j;
        let mut jd = e + (gc::s * f + gc::t) / gc::u;
        jd = jd - (3 * ((g + gc::A) / 100)) / 4 - gc::C;

        // Note, JD is the given julian day at noon on given date,
        // so we subtract an additional 0.5 to get midnight
        let jd = jd as f64 - 0.5;
        let mjd = jd - 2400000.5;

        let mut raw = mjd as i64 * 86_400_000_000
            + (hour as i64 * 3_600_000_000)
            + (minute as i64 * 60_000_000)
            + (second * 1_000_000.0) as i64
            + Self::MJD_EPOCH.raw;
        // Account for additional leap seconds if needed
        let ls = microleapseconds(raw);
        raw += ls;
        // Make sure adding the leapseconds didn't cross another
        // leapsecond boundary
        raw = raw + microleapseconds(raw) - ls;

        // Very rare -- check if second is in range [60, 61).  If so, make sure
        // this is in the middle of a leap second
        // I can't believe I'm even doing this...
        if check_leapsecond {
            let mut valid_leap_second = false;
            // Check if raw is within a leap second
            for (t, _) in LEAP_SECOND_TABLE.iter() {
                // The table holds the first of the "second" that happens twice, so
                // we are in leap second if raw is between [t + 1_000_000, t + 2_000_000)
                if raw >= *t + 1_000_000 && raw < *t + 2_000_000 {
                    // We are in a leap second
                    valid_leap_second = true;
                    break;
                }
            }
            if !valid_leap_second {
                bail!("Invalid second");
            }
        }

        Ok(Self { raw })
    }

    /// Current time
    ///
    /// # Returns
    /// The current time as an Instant object
    ///
    /// # Example
    ///
    /// ```
    /// use satkit::Instant;
    /// let now = Instant::now();
    /// ```
    ///
    pub fn now() -> Self {
        let now = std::time::SystemTime::now();
        let since_epoch = now.duration_since(std::time::UNIX_EPOCH).unwrap();
        let mut raw = since_epoch.as_micros() as i64;
        let ls = microleapseconds(raw);
        raw += ls;
        // Make sure adding the leapseconds didn't cross another
        // leapsecond boundary
        raw += microleapseconds(raw) - ls;
        Self { raw }
    }
}

impl std::fmt::Display for Instant {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let (year, month, day, hour, minute, second) = self.as_datetime();
        write!(
            f,
            "{:04}-{:02}-{:02}T{:02}:{:02}:{:09.6}Z",
            year, month, day, hour, minute, second
        )
    }
}

impl std::fmt::Debug for Instant {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let (year, month, day, hour, minute, second) = self.as_datetime();
        write!(
            f,
            "Instant {{ year: {}, month: {}, day: {}, hour: {}, minute: {}, second: {:06.3} }}",
            year, month, day, hour, minute, second
        )
    }
}