stkcraft-time 0.1.13

NAIF/SPICE-aligned astronomical time scales in Rust: UTC, TAI, TT, TDB/ET, GPS, UT1 and SCLK, with LSK/SCLK/EOP kernels, no_std support, WebGeocalc parity and an MCP server.
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
//! Conversion routines between the supported time scales.

use crate::calendar::{self, Calendar, GPS_EPOCH_JDN, J2000_JDN};
use crate::eop::EopTable;
use crate::epoch::{Epoch, UtcDateTime};
use crate::error::TimeError;
use crate::leap::{sofa_delta_at, LeapSecondSource, LeapSecondTable, JDN_1960, JDN_1972};
use crate::math;

/// `TAI - UTC` at the GPS epoch (`1980-01-06T00:00:00 UTC`).
const GPS_EPOCH_DELTA_AT: i64 = 19;

/// TAI seconds past the internal origin at the GPS epoch.
const GPS_EPOCH_TAI: i64 = (GPS_EPOCH_JDN - J2000_JDN) * 86_400 + GPS_EPOCH_DELTA_AT;

/// Converts a civil UTC representation to a continuous TAI epoch.
pub fn utc_to_tai(
    dt: &UtcDateTime,
    calendar: Calendar,
    lsk: &LeapSecondTable,
) -> Result<Epoch, TimeError> {
    if !(1..=12).contains(&dt.month) || dt.day < 1 {
        return Err(TimeError::InvalidDate {
            year: dt.year,
            month: dt.month,
            day: dt.day,
        });
    }

    let max_day =
        calendar::days_in_month(dt.year, dt.month, calendar).ok_or(TimeError::InvalidDate {
            year: dt.year,
            month: dt.month,
            day: dt.day,
        })?;
    if dt.day > max_day {
        return Err(TimeError::InvalidDate {
            year: dt.year,
            month: dt.month,
            day: dt.day,
        });
    }

    if dt.hour > 23 || dt.minute > 59 {
        return Err(TimeError::InvalidTime {
            hour: dt.hour,
            minute: dt.minute,
        });
    }
    if !(dt.second >= 0.0 && dt.second < 61.0) {
        return Err(TimeError::InvalidSecond);
    }

    let jdn = calendar::calendar_to_jdn(dt.year, dt.month, dt.day, calendar)?;

    if dt.second >= 60.0 {
        if jdn < JDN_1972 {
            return Err(TimeError::InvalidLeapSecond);
        }
        if dt.hour != 23 || dt.minute != 59 {
            return Err(TimeError::InvalidLeapSecond);
        }
        let current = lsk.delta_at_for_jdn(jdn).ok_or(TimeError::UtcOutOfRange)?;
        let next = lsk
            .delta_at_for_jdn(jdn + 1)
            .ok_or(TimeError::InvalidLeapSecond)?;
        if next <= current {
            return Err(TimeError::InvalidLeapSecond);
        }
    }

    // A negative leap second removes `23:59:59` from the end of the
    // preceding UTC day, which is then only 86399 seconds long. Accepting
    // that label would alias it with the following day's `00:00:00`.
    if jdn >= JDN_1972 && dt.hour == 23 && dt.minute == 59 && dt.second >= 59.0 {
        let current = lsk.delta_at_for_jdn(jdn).ok_or(TimeError::UtcOutOfRange)?;
        let next = lsk
            .delta_at_for_jdn(jdn + 1)
            .ok_or(TimeError::InvalidLeapSecond)?;
        if next < current {
            return Err(TimeError::InvalidLeapSecond);
        }
    }

    let whole_seconds = math::floor(dt.second);
    let time_of_day = dt.hour as i64 * 3_600 + dt.minute as i64 * 60 + whole_seconds as i64;
    let nominal = (jdn - J2000_JDN) * 86_400 + time_of_day;

    if jdn >= JDN_1972 {
        let delta_at = lsk.delta_at_for_jdn(jdn).ok_or(TimeError::UtcOutOfRange)?;
        let tai_sec = nominal + delta_at as i64;
        let nanos = math::round((dt.second - whole_seconds) * 1_000_000_000.0) as i64;
        return Ok(Epoch::from_parts(tai_sec, nanos));
    }

    let day_fraction = (time_of_day as f64 + (dt.second - whole_seconds)) / 86_400.0;
    let delta_at = lsk
        .delta_at_f64(jdn, day_fraction)
        .ok_or(TimeError::UtcOutOfRange)?;
    let delta_whole = math::floor(delta_at);
    let delta_fraction = delta_at - delta_whole;
    let tai_sec = nominal + delta_whole as i64;
    let nanos =
        math::round(((dt.second - whole_seconds) + delta_fraction) * 1_000_000_000.0) as i64;

    Ok(Epoch::from_parts(tai_sec, nanos))
}

/// Converts a continuous TAI epoch to a civil UTC representation.
pub fn tai_to_utc(
    epoch: Epoch,
    calendar: Calendar,
    lsk: &LeapSecondTable,
) -> Result<UtcDateTime, TimeError> {
    let tai_sec = epoch.sec();
    let nanos = epoch.nanos() as f64 * 1e-9;

    if tai_sec < lsk.first_tai_start() {
        return tai_to_utc_pre_1972(epoch, calendar, lsk);
    }

    let index = lsk
        .interval_index(tai_sec)
        .ok_or(TimeError::UtcOutOfRange)?;
    let entries = lsk.entries();
    let delta_at = entries[index].delta_at as i64;
    let nominal = tai_sec - delta_at;

    if let Some(next) = entries.get(index + 1) {
        if lsk.is_active_index(index + 1) {
            let next_nominal = lsk.nominal_sec(index + 1);
            if nominal >= next_nominal {
                let leap_len = (next.delta_at - entries[index].delta_at) as f64;
                let fraction = (nominal - next_nominal) as f64 + nanos;
                if fraction < leap_len {
                    let (year, month, day) = calendar::jdn_to_calendar(next.jdn - 1, calendar);
                    return Ok(UtcDateTime {
                        year,
                        month,
                        day,
                        hour: 23,
                        minute: 59,
                        second: 60.0 + fraction,
                    });
                }
            }
        }
    }

    let days = nominal.div_euclid(86_400);
    let time_of_day = nominal.rem_euclid(86_400);
    let jdn = days + J2000_JDN;
    let (year, month, day) = calendar::jdn_to_calendar(jdn, calendar);

    Ok(UtcDateTime {
        year,
        month,
        day,
        hour: (time_of_day / 3_600) as u8,
        minute: ((time_of_day % 3_600) / 60) as u8,
        second: (time_of_day % 60) as f64 + nanos,
    })
}

fn tai_to_utc_pre_1972(
    epoch: Epoch,
    calendar: Calendar,
    lsk: &LeapSecondTable,
) -> Result<UtcDateTime, TimeError> {
    if lsk.source() != LeapSecondSource::Sofa {
        return Err(TimeError::UtcOutOfRange);
    }

    let tai_value = epoch.tai_seconds();
    let mut utc_nominal = tai_value;

    for _ in 0..4 {
        let mjd = 51_544.0 + utc_nominal / 86_400.0;
        let delta = sofa_delta_at(mjd).ok_or(TimeError::UtcOutOfRange)?;
        let next = tai_value - delta;
        if math::abs(next - utc_nominal) < 1e-9 {
            utc_nominal = next;
            break;
        }
        utc_nominal = next;
    }

    let start_1960 = (JDN_1960 - J2000_JDN) as f64 * 86_400.0;
    if utc_nominal < start_1960 {
        return Err(TimeError::UtcOutOfRange);
    }

    let days = math::div_euclid(utc_nominal, 86_400.0);
    let time_of_day = math::rem_euclid(utc_nominal, 86_400.0);
    let jdn = days as i64 + J2000_JDN;
    let (year, month, day) = calendar::jdn_to_calendar(jdn, calendar);

    let hour = math::floor(time_of_day / 3_600.0) as u8;
    let rest = time_of_day - hour as f64 * 3_600.0;
    let minute = math::floor(rest / 60.0) as u8;
    let second = rest - minute as f64 * 60.0;

    Ok(UtcDateTime {
        year,
        month,
        day,
        hour,
        minute,
        second,
    })
}

/// Converts a TAI epoch to TDB seconds past the J2000 epoch (`ET`).
///
/// The periodic `TDB - TT` term depends on `ET`, so the fixed point is
/// solved by iteration. NAIF states that this model is accurate to about
/// 30 microseconds.
pub fn tai_to_et(epoch: Epoch, lsk: &LeapSecondTable) -> f64 {
    let tai = epoch.tai_seconds();
    let base = tai + lsk.delta_t_a - 43_200.0;
    let mut et = base;

    for _ in 0..4 {
        let mean_anomaly = lsk.m0 + lsk.m1 * et;
        let eccentric_anomaly = mean_anomaly + lsk.eb * math::sin(mean_anomaly);
        let next = base + lsk.k * math::sin(eccentric_anomaly);
        if math::abs(next - et) < 1e-12 {
            return next;
        }
        et = next;
    }

    et
}

/// Converts TDB seconds past J2000 (`ET`) to a TAI epoch.
pub fn et_to_tai(et: f64, lsk: &LeapSecondTable) -> Epoch {
    let mean_anomaly = lsk.m0 + lsk.m1 * et;
    let eccentric_anomaly = mean_anomaly + lsk.eb * math::sin(mean_anomaly);
    let tai = et - lsk.delta_t_a + 43_200.0 - lsk.k * math::sin(eccentric_anomaly);
    Epoch::from_tai_seconds(tai)
}

/// Converts a TAI epoch to TT seconds past J2000 TT.
pub fn tai_to_tt(epoch: Epoch, lsk: &LeapSecondTable) -> f64 {
    epoch.tai_seconds() + lsk.delta_t_a - 43_200.0
}

/// Converts TT seconds past J2000 TT to a TAI epoch.
pub fn tt_to_tai(tt: f64, lsk: &LeapSecondTable) -> Epoch {
    Epoch::from_tai_seconds(tt - lsk.delta_t_a + 43_200.0)
}

/// Converts a TAI epoch to GPS seconds past the GPS epoch.
pub fn tai_to_gps(epoch: Epoch) -> f64 {
    epoch.tai_seconds() - GPS_EPOCH_TAI as f64
}

/// Converts GPS seconds past the GPS epoch to a TAI epoch.
pub fn gps_to_tai(gps: f64) -> Epoch {
    Epoch::from_tai_seconds(gps + GPS_EPOCH_TAI as f64)
}

/// Converts a civil UTC representation directly to `ET`.
pub fn utc_to_et(
    dt: &UtcDateTime,
    calendar: Calendar,
    lsk: &LeapSecondTable,
) -> Result<f64, TimeError> {
    Ok(tai_to_et(utc_to_tai(dt, calendar, lsk)?, lsk))
}

/// Converts `ET` directly to a civil UTC representation.
pub fn et_to_utc(
    et: f64,
    calendar: Calendar,
    lsk: &LeapSecondTable,
) -> Result<UtcDateTime, TimeError> {
    tai_to_utc(et_to_tai(et, lsk), calendar, lsk)
}

/// Modified Julian Date of the UTC day containing `jdn`.
fn utc_mjd(jdn: i64) -> f64 {
    51_544.0 + (jdn - J2000_JDN) as f64
}

/// Returns `TAI - UTC` in whole seconds in force at `epoch`.
fn delta_at_at_epoch(epoch: Epoch, lsk: &LeapSecondTable) -> Option<i32> {
    let index = lsk.interval_index(epoch.sec())?;
    Some(lsk.entries()[index].delta_at)
}

/// Returns the Julian Day Number of the UTC day containing `epoch`.
///
/// During a positive leap second the value stays on the day that owns
/// `23:59:60`, matching [`tai_to_utc`].
fn utc_jdn(epoch: Epoch, lsk: &LeapSecondTable) -> Option<i64> {
    let tai_sec = epoch.sec();
    if tai_sec < lsk.first_tai_start() {
        return None;
    }

    let index = lsk.interval_index(tai_sec)?;
    let entries = lsk.entries();
    let nominal = tai_sec - entries[index].delta_at as i64;

    if let Some(next) = entries.get(index + 1) {
        if lsk.is_active_index(index + 1) && nominal >= lsk.nominal_sec(index + 1) {
            return Some(next.jdn - 1);
        }
    }

    Some(nominal.div_euclid(86_400) + J2000_JDN)
}

/// TAI seconds past the internal origin at `00:00:00 UTC` of `jdn`.
fn tai_at_utc_day_start(jdn: i64, lsk: &LeapSecondTable) -> Option<i64> {
    let delta_at = lsk.delta_at_for_jdn(jdn)?;
    Some((jdn - J2000_JDN) * 86_400 + delta_at as i64)
}

/// Returns `UT1 - UTC` in seconds at `epoch`, using an IERS EOP table.
///
/// The EOP samples are converted to continuous `UT1 - TAI` using the
/// leap-second table before interpolation. This keeps a leap-second step
/// from being smeared when the table has missing days around the step.
///
/// Returns [`TimeError::Ut1OutOfRange`] when the epoch or the bracketing
/// EOP samples lie outside the table.
pub fn ut1_minus_utc(
    epoch: Epoch,
    lsk: &LeapSecondTable,
    eop: &EopTable,
) -> Result<f64, TimeError> {
    let jdn = utc_jdn(epoch, lsk).ok_or(TimeError::Ut1OutOfRange)?;
    let mjd_start = utc_mjd(jdn);
    let samples = eop.samples();

    if samples.len() < 2 {
        return Err(TimeError::Ut1OutOfRange);
    }

    let index = samples.partition_point(|sample| sample.mjd <= mjd_start);
    if index == 0 || index >= samples.len() {
        return Err(TimeError::Ut1OutOfRange);
    }

    let before = samples[index - 1];
    let after = samples[index];
    let before_jdn = mjd_to_jdn(before.mjd);
    let after_jdn = mjd_to_jdn(after.mjd);

    let before_tai = tai_at_utc_day_start(before_jdn, lsk).ok_or(TimeError::Ut1OutOfRange)? as f64;
    let after_tai = tai_at_utc_day_start(after_jdn, lsk).ok_or(TimeError::Ut1OutOfRange)? as f64;
    let before_delta = lsk
        .delta_at_for_jdn(before_jdn)
        .ok_or(TimeError::Ut1OutOfRange)? as f64;
    let after_delta = lsk
        .delta_at_for_jdn(after_jdn)
        .ok_or(TimeError::Ut1OutOfRange)? as f64;

    let before_ut1_tai = before.ut1_minus_utc - before_delta;
    let after_ut1_tai = after.ut1_minus_utc - after_delta;
    let span = after_tai - before_tai;
    let tai = epoch.tai_seconds();

    let ut1_tai = if span <= 0.0 {
        before_ut1_tai
    } else {
        let fraction = (tai - before_tai) / span;
        if !(0.0..=1.0).contains(&fraction) {
            return Err(TimeError::Ut1OutOfRange);
        }
        before_ut1_tai + fraction * (after_ut1_tai - before_ut1_tai)
    };

    let delta_at = delta_at_at_epoch(epoch, lsk).ok_or(TimeError::Ut1OutOfRange)? as f64;
    Ok(ut1_tai + delta_at)
}

/// Converts a TAI epoch to UT1 seconds past `2000-01-01T00:00:00 UT1`.
///
/// The result is the continuous UT1 time coordinate. It is defined as
///
/// ```text
/// UT1 = TAI + (UT1 - UTC) - (TAI - UTC)
/// ```
///
/// so it is zero at the UT1 instant labelled `2000-01-01T00:00:00`.
/// UT1 is an Earth-rotation scale; it is intentionally not folded into
/// ET or TDB.
pub fn tai_to_ut1(epoch: Epoch, lsk: &LeapSecondTable, eop: &EopTable) -> Result<f64, TimeError> {
    let delta_at = delta_at_at_epoch(epoch, lsk).ok_or(TimeError::Ut1OutOfRange)? as f64;
    let ut1_minus_tai = ut1_minus_utc(epoch, lsk, eop)? - delta_at;
    Ok(epoch.tai_seconds() + ut1_minus_tai)
}

/// Converts UT1 seconds past `2000-01-01T00:00:00 UT1` to a TAI epoch.
///
/// The inverse is a fixed-point solve, because `UT1 - UTC` depends on
/// the epoch itself. It converges in a few iterations because the offset
/// changes slowly.
pub fn ut1_to_tai(ut1: f64, lsk: &LeapSecondTable, eop: &EopTable) -> Result<Epoch, TimeError> {
    if !ut1.is_finite() {
        return Err(TimeError::Ut1OutOfRange);
    }

    let mut tai = initial_tai_guess(ut1, lsk, eop).ok_or(TimeError::Ut1OutOfRange)?;

    for _ in 0..10 {
        let epoch = Epoch::from_tai_seconds(tai);
        let delta_at = delta_at_at_epoch(epoch, lsk).ok_or(TimeError::Ut1OutOfRange)? as f64;
        let ut1_minus_tai = ut1_minus_utc(epoch, lsk, eop)? - delta_at;
        let next = ut1 - ut1_minus_tai;
        if math::abs(next - tai) < 1e-10 {
            tai = next;
            break;
        }
        tai = next;
    }

    let epoch = Epoch::from_tai_seconds(tai);
    let actual = tai_to_ut1(epoch, lsk, eop)?;
    if math::abs(actual - ut1) > 1e-6 {
        return Err(TimeError::Ut1OutOfRange);
    }

    Ok(epoch)
}

/// Clamps the first fixed-point guess to the EOP-supported TAI interval.
///
/// `ut1_minus_utc` interpolates between two consecutive samples, so the
/// last usable instant is the start of the final sample day: that day is
/// only reachable through the interval that ends there. The guess also
/// uses the measured `UT1 - UTC` offset instead of a fixed 32 s fudge, so
/// a target near either end of the table is not clamped onto an instant
/// the interpolation rejects.
fn initial_tai_guess(ut1: f64, lsk: &LeapSecondTable, eop: &EopTable) -> Option<f64> {
    let samples = eop.samples();
    if samples.len() < 2 {
        return None;
    }

    let first = samples[0];
    let last = samples[samples.len() - 1];
    let first_tai = tai_at_utc_day_start(mjd_to_jdn(first.mjd), lsk)? as f64;
    let last_tai = tai_at_utc_day_start(mjd_to_jdn(last.mjd), lsk)? as f64;
    if !last_tai.is_finite() || !first_tai.is_finite() || last_tai <= first_tai {
        return None;
    }

    // UT1 stays within a second of UTC, so this selects the nearby sample
    // without solving for the epoch itself.
    let approx_mjd = 51_544.5 + ut1 / 86_400.0;
    let index = samples.partition_point(|sample| sample.mjd < approx_mjd);
    let nearest = if index == 0 {
        first
    } else if index >= samples.len() {
        last
    } else {
        let before = samples[index - 1];
        let after = samples[index];
        if approx_mjd - before.mjd <= after.mjd - approx_mjd {
            before
        } else {
            after
        }
    };

    let delta_at = lsk.delta_at_for_jdn(mjd_to_jdn(nearest.mjd))? as f64;
    let guess = ut1 - nearest.ut1_minus_utc + delta_at;

    // Stay strictly inside the interpolated interval: the final sample
    // date only closes the preceding day.
    let lower = first_tai;
    let upper = last_tai - 1e-3;
    if upper < lower {
        return None;
    }
    Some(guess.clamp(lower, upper))
}

/// Converts a daily EOP MJD to its Julian Day Number.
fn mjd_to_jdn(mjd: f64) -> i64 {
    J2000_JDN + math::round(mjd - 51_544.0) as i64
}

/// Converts a civil UTC representation to UT1 seconds past
/// `2000-01-01T00:00:00 UT1`.
pub fn utc_to_ut1(
    dt: &UtcDateTime,
    calendar: Calendar,
    lsk: &LeapSecondTable,
    eop: &EopTable,
) -> Result<f64, TimeError> {
    tai_to_ut1(utc_to_tai(dt, calendar, lsk)?, lsk, eop)
}

/// Converts UT1 seconds past `2000-01-01T00:00:00 UT1` to a civil UTC
/// representation.
pub fn ut1_to_utc(
    ut1: f64,
    calendar: Calendar,
    lsk: &LeapSecondTable,
    eop: &EopTable,
) -> Result<UtcDateTime, TimeError> {
    tai_to_utc(ut1_to_tai(ut1, lsk, eop)?, calendar, lsk)
}