deep-time 0.1.0-beta.21

High-precision, no-std, no-alloc date-time library, leap-seconds, time scales, relativistic time, and a powerful date & duration parser
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
#![allow(clippy::all, clippy::pedantic, clippy::restriction, warnings)]

/*
from astropy.time import Time

t = Time("2020-01-01 00:00:00", scale="tai")

print("galexsec", t.galexsec)
print("gps", t.gps)
print("cxcsec", t.cxcsec)
print("unix_utc", t.unix)
print("unix_tai", t.unix_tai)

print("jyear", t.jyear)
print("byear", t.byear)
print("decimalyear", t.decimalyear)
print("yday", t.yday)

scales = ["tai", "tt", "tdb", "tcg", "utc", "ut1"]

for scale in scales:
    ts = getattr(t, scale)
    print(f"jd_{scale} {ts.jd:.15f}")

print("\n-- TCB ---------")
for year in [
    1500,
    2000,
    2020,
    4000,
]:
    x = Time(f"{year}-01-01 00:00:00", scale="tai")
    print(f"{x.tcb.jd:.15f},")

print("\n-- TCG ---------")
for year in [
    1500,
    2000,
    2020,
    4000,
]:
    x = Time(f"{year}-01-01 00:00:00", scale="tai")
    print(f"{x.tcg.jd:.15f},")

print("\n-- TDB ---------")
for year in [
    1000,
    1500,
    2000,
    2001,
    2002,
    2003,
    2004,
    2005,
    2006,
    2007,
    2009,
    2010,
    2011,
    2012,
    2013,
    2014,
    2015,
    2016,
    2017,
    2018,
    2020,
    2021,
    2027,
    2030,
    2035,
    2099,
    4000,
    8000,
]:
    for month in ["01", "04"]:
        x = Time(f"{year}-{month}-01 00:00:00", scale="tai")
        print(f"{x.tdb.jd:.15f},")


"""
galexsec 1261871963.0
gps 1261871981.0
cxcsec 694224032.184
unix_utc 1577836763.0
unix_tai 1577836800.0
jyear 2019.9986310746065
byear 2020.000335739628
decimalyear 2020.0
yday 2020:001:00:00:00.000
jd_tai 2458849.500000000000000
jd_tt 2458849.500372500158846
jd_tdb 2458849.500372498761863
jd_tcg 2458849.500383445061743
jd_utc 2458849.499571759253740
jd_ut1 2458849.499569708947092

-- TCB ---------
2268923.497671171557158,
2451544.500502743292600,
2458849.500616008881480,
3182029.511829047929496,

-- TCG ---------
2268923.500251080375165,
2451544.500378353986889,
2458849.500383445061743,
3182029.500887450296432,

-- TDB ---------
2086302.500372504815459,
2086392.500372519250959,
2268923.500372501555830,
2269013.500372519250959,
2451544.500372498761863,
2451635.500372518785298,
2451910.500372499227524,
2452000.500372518785298,
2452275.500372499227524,
2452365.500372518785298,
2452640.500372499227524,
2452730.500372518785298,
2453005.500372499227524,
2453096.500372519250959,
2453371.500372499693185,
2453461.500372519250959,
2453736.500372499227524,
2453826.500372519250959,
2454101.500372499227524,
2454191.500372519250959,
2454832.500372499227524,
2454922.500372519250959,
2455197.500372498761863,
2455287.500372519250959,
2455562.500372498761863,
2455652.500372519250959,
2455927.500372498761863,
2456018.500372519250959,
2456293.500372499227524,
2456383.500372518785298,
2456658.500372499227524,
2456748.500372518785298,
2457023.500372499227524,
2457113.500372519250959,
2457388.500372499227524,
2457479.500372519250959,
2457754.500372499227524,
2457844.500372519250959,
2458119.500372499227524,
2458209.500372519250959,
2458849.500372498761863,
2458940.500372519250959,
2459215.500372498761863,
2459305.500372519250959,
2461406.500372499227524,
2461496.500372518785298,
2462502.500372499227524,
2462592.500372519250959,
2464328.500372498761863,
2464418.500372518785298,
2487704.500372499227524,
2487794.500372519250959,
3182029.500372488982975,
3182120.500372514594346,
4642999.500372484326363,
4643090.500372495502234,
"""
*/

use deep_time::{Dt, Scale};

mod astropy_verified_conversions_tests {
    use super::*;

    #[test]
    fn galexsec() {
        let galexsec = Dt::from_ymd(2020, 1, 1, Scale::TAI, 0, 0, 0, 0)
            .target(Scale::UTC)
            .to_galexsec()
            .to_sec_f();
        // galexsec  : 1261871963.0
        assert_eq!(galexsec, 1261871963.0);
    }

    #[test]
    fn gps() {
        let gps = Dt::from_ymd(2020, 1, 1, Scale::TAI, 0, 0, 0, 0)
            .target(Scale::GPS)
            .to_gps()
            .to_sec_f();
        // gps       : 1261871981.0
        assert_eq!(gps, 1261871981.0);
    }

    #[test]
    fn unix_tai() {
        let unix_tai = Dt::from_ymd(2020, 1, 1, Scale::TAI, 0, 0, 0, 0)
            .to_unix()
            .to_sec_f();
        // unix_tai 1577836800.0
        assert_eq!(unix_tai, 1577836800.0);
    }

    #[test]
    fn unix_utc() {
        let unix = Dt::from_ymd(2020, 1, 1, Scale::TAI, 0, 0, 0, 0)
            .target(Scale::UTC)
            .to_unix()
            .to_sec_f();
        // unix_utc 1577836763.0
        assert_eq!(unix, 1577836763.0);
    }

    #[test]
    fn cxcsec() {
        let cxc = Dt::from_ymd(2020, 1, 1, Scale::TAI, 0, 0, 0, 0)
            .target(Scale::TT)
            .to_cxcsec()
            .to_sec_f();
        // cxcsec 694224032.184
        assert_eq!(cxc, 694224032.184);
    }

    #[test]
    fn decimal_year() {
        let x = Dt::from_ymd(2020, 1, 1, Scale::TAI, 0, 0, 0, 0);
        let y = Dt::from_ymd(-2000, 1, 1, Scale::TAI, 0, 0, 0, 0);
        // jyear 2019.9986310746065
        assert_eq!(x.to_jyear(), 2019.9986310746065);
        // byear 2020.000335739628
        assert!((x.to_byear() - 2020.000335739628).abs() < 1e-12);
        // decimalyear 2020.0
        assert_eq!(x.to_decimalyear(), 2020.0);
        // Negative decimal year
        assert_eq!(y.to_decimalyear(), -2000.0);
    }

    #[test]
    fn tai_jd() {
        let jd = Dt::from_ymd(2020, 1, 1, Scale::TAI, 0, 0, 0, 0).to_jd_f();
        // jd_tai 2458849.5
        assert_eq!(jd, 2458849.5);
    }

    #[test]
    fn tt_jd() {
        let jd = Dt::from_ymd(2020, 1, 1, Scale::TAI, 0, 0, 0, 0)
            .target(Scale::TT)
            .to_jd_f();
        // jd_tt 2458849.500372500158846
        assert_eq!(jd, 2458849.500372500158846);
    }

    #[test]
    fn tdb_jd_multi() {
        let results = [
            2086302.500372504815459,
            2086392.500372519250959,
            2268923.500372501555830,
            2269013.500372519250959,
            2451544.500372498761863,
            2451635.500372518785298,
            2451910.500372499227524,
            2452000.500372518785298,
            2452275.500372499227524,
            2452365.500372518785298,
            2452640.500372499227524,
            2452730.500372518785298,
            2453005.500372499227524,
            2453096.500372519250959,
            2453371.500372499693185,
            2453461.500372519250959,
            2453736.500372499227524,
            2453826.500372519250959,
            2454101.500372499227524,
            2454191.500372519250959,
            2454832.500372499227524,
            2454922.500372519250959,
            2455197.500372498761863,
            2455287.500372519250959,
            2455562.500372498761863,
            2455652.500372519250959,
            2455927.500372498761863,
            2456018.500372519250959,
            2456293.500372499227524,
            2456383.500372518785298,
            2456658.500372499227524,
            2456748.500372518785298,
            2457023.500372499227524,
            2457113.500372519250959,
            2457388.500372499227524,
            2457479.500372519250959,
            2457754.500372499227524,
            2457844.500372519250959,
            2458119.500372499227524,
            2458209.500372519250959,
            2458849.500372498761863,
            2458940.500372519250959,
            2459215.500372498761863,
            2459305.500372519250959,
            2461406.500372499227524,
            2461496.500372518785298,
            2462502.500372499227524,
            2462592.500372519250959,
            2464328.500372498761863,
            2464418.500372518785298,
            2487704.500372499227524,
            2487794.500372519250959,
            3182029.500372488982975,
            3182120.500372514594346,
            4642999.500372484326363,
            4643090.500372495502234,
        ];
        let mut results_idx: usize = 0;
        let mut diffs: f64 = 0.0;
        let mut max_diff: f64 = 0.0;

        #[cfg(feature = "tdb_fairhead1990")]
        const TOLERANCE: f64 = 0.0;

        #[cfg(not(feature = "tdb_fairhead1990"))]
        const TOLERANCE: f64 = 2.0e-9;

        for yr in [
            1000, 1500, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2012,
            2013, 2014, 2015, 2016, 2017, 2018, 2020, 2021, 2027, 2030, 2035, 2099, 4000, 8000,
        ]
        .iter()
        {
            for mo in [1, 4] {
                let jd = Dt::from_ymd(*yr as i64, mo, 1, Scale::TAI, 0, 0, 0, 0)
                    .target(Scale::TDB)
                    .to_jd_f();
                let expected = results[results_idx];
                let diff = (jd - expected).abs();

                assert!(
                    diff <= TOLERANCE,
                    "{yr}-{mo:02}-01: diff = {diff:.2e} JD (expected {expected:.15}, got {jd:.15})"
                );

                // eprintln!(
                //     "{}-{}-01 Mine: {} Astropy: {} Diff: {}",
                //     yr, mo, jd, results[results_idx], diff,
                // );

                diffs += diff;

                if diff > max_diff {
                    max_diff = diff;
                }

                results_idx += 1;
            }
        }

        let ave_diff = diffs / results_idx as f64;

        // eprintln!("=== Summary ===");
        // eprintln!("Average diff: {:.2e}", ave_diff);
        // eprintln!("Max diff:     {:.2e}", max_diff);
    }

    #[test]
    fn tcb_jd() {
        let results = [
            2268923.497671171557158,
            2451544.500502743292600,
            2458849.500616008881480,
            3182029.511829047929496,
        ];
        let mut result: usize = 0;

        for yr in [1500, 2000, 2020, 4000] {
            let expected = results[result];

            let jd = Dt::from_ymd(yr as i64, 1, 1, Scale::TAI, 0, 0, 0, 0)
                .target(Scale::TCB)
                .to_jd_f();

            if cfg!(feature = "tdb_fairhead1990") {
                // ERFA path should match Astropy
                assert_eq!(jd, expected);
            } else {
                // Default uses the compact DE440/LTE440 analytical model.
                // It is not identical to ERFA; differences of O(100 µs) are expected
                // for TCB.
                let diff = (jd - expected).abs();
                // eprintln!("TCB our_jd: {} diff: {}", jd, diff);
                assert!(
                    diff < 2e-9,
                    "TCB JD diff too large without tdb_fairhead1990 feature: {}",
                    diff
                );
            }

            result += 1;
        }
    }

    #[test]
    fn tcg_jd() {
        let results = [
            2268923.500251080375165,
            2451544.500378353986889,
            2458849.500383445061743,
            3182029.500887450296432,
        ];
        let mut result: usize = 0;

        for yr in [1500, 2000, 2020, 4000] {
            let expected = results[result];

            let jd = Dt::from_ymd(yr as i64, 1, 1, Scale::TAI, 0, 0, 0, 0)
                .target(Scale::TCG)
                .to_jd_f();

            assert_eq!(jd, expected);

            result += 1;
        }
    }
}