deep-time 0.1.0-beta.22

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
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
use crate::{
    ATTOS_PER_DAY, ATTOS_PER_HALF_DAY, ATTOS_PER_SEC_I128, Dt, JD_2000_2_451_545_I128, Real, Scale,
    floor_f,
};

impl Dt {
    /// Splits this instant's Julian Date into whole days and a remainder in attoseconds.
    ///
    /// ## Important
    ///
    /// - Converts to this [`Dt`]'s `target` scale before splitting. Set `target` first
    ///   if you need JD on a particular scale (e.g. `Scale::TT` or `Scale::TDB`).
    /// - Assumes this [`Dt`] is on the 2000-01-01 noon epoch.
    /// - When the whole part is negative and there is a non-zero remainder, `frac_attos`
    ///   is negative too. e.g. a jd of `-1000.25` will return the whole part as `-1000`
    ///   and the remainder as `-0.25 * ATTOS_PER_DAY` as an `i128`.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Scale, consts::ATTOS_PER_DAY};
    ///
    /// let dt = Dt::from_jd_f(2_460_782.25, Scale::TAI);
    /// // 2_460_782 and 0.25 days in attoseconds
    /// assert_eq!(dt.to_jd(), (2_460_782, ATTOS_PER_DAY / 4));
    ///
    /// let dt = Dt::from_jd_f(-1_000.25, Scale::TAI);
    /// // -1_000 and -0.25 days in attoseconds
    /// assert_eq!(dt.to_jd(), (-1_000, -ATTOS_PER_DAY / 4));
    /// ```
    ///
    /// ## See also
    ///
    /// - [`Dt::to_jd_floor`](../struct.Dt.html#method.to_jd_floor)
    /// - [`Dt::to_jd_f`](../struct.Dt.html#method.to_jd_f)
    /// - [`Dt::from_jd`](../struct.Dt.html#method.from_jd)
    /// - [`Dt::to_mjd`](../struct.Dt.html#method.to_mjd)
    #[inline(always)]
    pub const fn to_jd(&self) -> (i128, i128) {
        self.to(self.target).to_jd_raw()
    }

    /// Like [`Dt::to_jd`](../struct.Dt.html#method.to_jd), but uses this [`Dt`]'s current
    /// `scale` instead of converting to `target` first.
    ///
    /// ## See also
    ///
    /// - [`Dt::to_jd`](../struct.Dt.html#method.to_jd)
    /// - [`Dt::to_jd_floor_raw`](../struct.Dt.html#method.to_jd_floor_raw)
    /// - [`Dt::to_jd_f_raw`](../struct.Dt.html#method.to_jd_f_raw)
    #[inline(always)]
    pub const fn to_jd_raw(&self) -> (i128, i128) {
        let attos = self.to_attos();
        let days_since_j2000 = attos / ATTOS_PER_DAY;
        let remaining_attos = attos % ATTOS_PER_DAY;

        let jd_int = JD_2000_2_451_545_I128.saturating_add(days_since_j2000);

        (jd_int, remaining_attos)
    }

    /// Splits this instant's Julian Date into whole days and a remainder in attoseconds.
    ///
    /// ## Important
    ///
    /// - Converts to this [`Dt`]'s `target` scale before splitting. Set `target` first
    ///   if you need JD on a particular scale (e.g. `Scale::TT` or `Scale::TDB`).
    /// - Assumes this [`Dt`] is on the 2000-01-01 noon epoch.
    /// - The remainder is always zero or positive and less than one day. e.g. a jd of
    ///   `-1000.25` will return the whole part as `-1001` and the remainder as
    ///   `0.75 * ATTOS_PER_DAY` as a `u128`.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Scale, consts::ATTOS_PER_HALF_DAY_U128};
    ///
    /// let dt = Dt::from_jd_f(2_460_782.25, Scale::TAI);
    /// // 2_460_782 and 0.25 days in attoseconds
    /// assert_eq!(dt.to_jd_floor(), (2_460_782, ATTOS_PER_HALF_DAY_U128 / 2));
    ///
    /// let dt = Dt::from_jd_f(-1_000.25, Scale::TAI);
    /// // -1_001 and effectively 0.75 days in attoseconds
    /// assert_eq!(dt.to_jd_floor(), (-1_001, 3 * ATTOS_PER_HALF_DAY_U128 / 2));
    /// ```
    ///
    /// ## See also
    ///
    /// - [`Dt::to_jd`](../struct.Dt.html#method.to_jd)
    /// - [`Dt::to_jd_f`](../struct.Dt.html#method.to_jd_f)
    /// - [`Dt::from_jd_floor`](../struct.Dt.html#method.from_jd_floor)
    #[inline(always)]
    pub const fn to_jd_floor(&self) -> (i128, u128) {
        self.to(self.target).to_jd_floor_raw()
    }

    /// Like [`Dt::to_jd_floor`](../struct.Dt.html#method.to_jd_floor), but uses this [`Dt`]'s
    /// current `scale` instead of converting to `target` first.
    ///
    /// ## See also
    ///
    /// - [`Dt::to_jd_floor`](../struct.Dt.html#method.to_jd_floor)
    /// - [`Dt::to_jd_raw`](../struct.Dt.html#method.to_jd_raw)
    #[inline(always)]
    pub const fn to_jd_floor_raw(&self) -> (i128, u128) {
        let attos = self.to_attos();
        let days_since_j2000 = attos.div_euclid(ATTOS_PER_DAY);
        let remaining_attos = attos.rem_euclid(ATTOS_PER_DAY);

        let jd_int = JD_2000_2_451_545_I128.saturating_add(days_since_j2000);

        (jd_int, remaining_attos as u128)
    }

    /// Returns this instant's Julian Date as an `f64`.
    ///
    /// ## Important
    ///
    /// - Converts to this [`Dt`]'s `target` scale first. Set `target` first if you need
    ///   JD on a particular scale (e.g. `Scale::TT` or `Scale::TDB`).
    /// - Assumes this [`Dt`] is on the 2000-01-01 noon epoch.
    /// - Same value as [`Dt::to_jd`](../struct.Dt.html#method.to_jd), expressed as a single
    ///   `f64` instead of a `(days, frac_attos)` pair.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Scale};
    ///
    /// let dt = Dt::from_jd_f(2_460_782.25, Scale::TAI);
    /// assert_eq!(dt.to_jd_f(), 2_460_782.25);
    ///
    /// let dt = Dt::from_jd_f(-1_000.25, Scale::TAI);
    /// assert_eq!(dt.to_jd_f(), -1_000.25);
    /// ```
    ///
    /// ## See also
    ///
    /// - [`Dt::to_jd`](../struct.Dt.html#method.to_jd)
    /// - [`Dt::to_jd_f_raw`](../struct.Dt.html#method.to_jd_f_raw)
    /// - [`Dt::from_jd_f`](../struct.Dt.html#method.from_jd_f)
    #[inline]
    pub const fn to_jd_f(&self) -> Real {
        let (days, attos) = self.to_jd();
        f!(days) + f!(attos) / f!(ATTOS_PER_DAY)
    }

    /// Like [`Dt::to_jd_f`](../struct.Dt.html#method.to_jd_f), but uses this [`Dt`]'s current
    /// `scale` instead of converting to `target` first.
    ///
    /// ## See also
    ///
    /// - [`Dt::to_jd_f`](../struct.Dt.html#method.to_jd_f)
    /// - [`Dt::to_jd_raw`](../struct.Dt.html#method.to_jd_raw)
    #[inline]
    pub const fn to_jd_f_raw(&self) -> Real {
        let (days, attos) = self.to_jd_raw();
        f!(days) + f!(attos) / f!(ATTOS_PER_DAY)
    }

    /// Splits this instant's Modified Julian Date into whole days and a remainder in attoseconds.
    ///
    /// ## Important
    ///
    /// - Converts to this [`Dt`]'s `target` scale before splitting. Set `target` first
    ///   if you need MJD on a particular scale.
    /// - Assumes this [`Dt`] is on the 2000-01-01 noon epoch.
    /// - MJD and JD relate by `JD = MJD + 2_400_000.5`.
    /// - e.g. an mjd of `-1000.25` will return the whole part as `-1001` and the
    ///   remainder as `0.75 * ATTOS_PER_DAY` as an `i128`.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Scale, consts::ATTOS_PER_DAY};
    ///
    /// let dt = Dt::from_mjd_f(60_961.25, Scale::TAI);
    /// // 60_961 and 0.25 days in attoseconds
    /// assert_eq!(dt.to_mjd(), (60_961, ATTOS_PER_DAY / 4));
    ///
    /// let dt = Dt::from_mjd_f(-1_000.25, Scale::TAI);
    /// // -1_001 and effectively 0.75 days in attoseconds
    /// assert_eq!(dt.to_mjd(), (-1_001, 3 * ATTOS_PER_DAY / 4));
    /// ```
    ///
    /// ## See also
    ///
    /// - [`Dt::to_mjd_floor`](../struct.Dt.html#method.to_mjd_floor)
    /// - [`Dt::to_mjd_f`](../struct.Dt.html#method.to_mjd_f)
    /// - [`Dt::from_mjd`](../struct.Dt.html#method.from_mjd)
    /// - [`Dt::to_jd`](../struct.Dt.html#method.to_jd)
    #[inline(always)]
    pub const fn to_mjd(&self) -> (i128, i128) {
        self.to(self.target).to_mjd_raw()
    }

    /// Like [`Dt::to_mjd`](../struct.Dt.html#method.to_mjd), but uses this [`Dt`]'s current
    /// `scale` instead of converting to `target` first.
    ///
    /// ## See also
    ///
    /// - [`Dt::to_mjd`](../struct.Dt.html#method.to_mjd)
    /// - [`Dt::to_mjd_floor_raw`](../struct.Dt.html#method.to_mjd_floor_raw)
    /// - [`Dt::to_mjd_f_raw`](../struct.Dt.html#method.to_mjd_f_raw)
    pub const fn to_mjd_raw(&self) -> (i128, i128) {
        let (jd_days, frac_attos) = self.to_jd_raw();

        let mut mjd_days = jd_days.saturating_sub(2_400_001);
        let mut mjd_attos = frac_attos.saturating_add(ATTOS_PER_HALF_DAY);

        if mjd_attos >= ATTOS_PER_DAY {
            mjd_days = mjd_days.saturating_add(1);
            mjd_attos = mjd_attos.saturating_sub(ATTOS_PER_DAY);
        } else if mjd_attos < 0 {
            mjd_days = mjd_days.saturating_sub(1);
            mjd_attos = mjd_attos.saturating_add(ATTOS_PER_DAY);
        }

        (mjd_days, mjd_attos)
    }

    /// Splits this instant's Modified Julian Date into whole days and a remainder in attoseconds.
    ///
    /// ## Important
    ///
    /// - Converts to this [`Dt`]'s `target` scale before splitting. Set `target` first
    ///   if you need MJD on a particular scale.
    /// - Assumes this [`Dt`] is on the 2000-01-01 noon epoch.
    /// - MJD and JD relate by `JD = MJD + 2_400_000.5`.
    /// - The remainder is always zero or positive and less than one day. e.g. an mjd of
    ///   `-1000.25` will return the whole part as `-1001` and the remainder as
    ///   `0.75 * ATTOS_PER_DAY` as a `u128`.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Scale, consts::ATTOS_PER_HALF_DAY_U128};
    ///
    /// let dt = Dt::from_mjd_f(60_961.25, Scale::TAI);
    /// // 60_961 and 0.25 days in attoseconds
    /// assert_eq!(dt.to_mjd_floor(), (60_961, ATTOS_PER_HALF_DAY_U128 / 2));
    ///
    /// let dt = Dt::from_mjd_f(-1_000.25, Scale::TAI);
    /// // -1_001 and effectively 0.75 days in attoseconds
    /// assert_eq!(dt.to_mjd_floor(), (-1_001, 3 * ATTOS_PER_HALF_DAY_U128 / 2));
    /// ```
    ///
    /// ## See also
    ///
    /// - [`Dt::to_mjd`](../struct.Dt.html#method.to_mjd)
    /// - [`Dt::to_mjd_f`](../struct.Dt.html#method.to_mjd_f)
    /// - [`Dt::from_mjd_floor`](../struct.Dt.html#method.from_mjd_floor)
    #[inline(always)]
    pub const fn to_mjd_floor(&self) -> (i128, u128) {
        self.to(self.target).to_mjd_floor_raw()
    }

    /// Like [`Dt::to_mjd_floor`](../struct.Dt.html#method.to_mjd_floor), but uses this [`Dt`]'s
    /// current `scale` instead of converting to `target` first.
    ///
    /// ## See also
    ///
    /// - [`Dt::to_mjd_floor`](../struct.Dt.html#method.to_mjd_floor)
    /// - [`Dt::to_mjd_raw`](../struct.Dt.html#method.to_mjd_raw)
    pub const fn to_mjd_floor_raw(&self) -> (i128, u128) {
        let (jd_days, frac_attos) = self.to_jd_floor_raw();

        let mjd_days = jd_days.saturating_sub(2_400_001);
        let mjd_attos = frac_attos.saturating_add(ATTOS_PER_HALF_DAY as u128);

        if mjd_attos >= ATTOS_PER_DAY as u128 {
            (
                mjd_days.saturating_add(1),
                mjd_attos.saturating_sub(ATTOS_PER_DAY as u128),
            )
        } else {
            (mjd_days, mjd_attos)
        }
    }

    /// Returns this instant's Modified Julian Date as an `f64`.
    ///
    /// ## Important
    ///
    /// - Converts to this [`Dt`]'s `target` scale first. Set `target` first if you need
    ///   MJD on a particular scale.
    /// - Assumes this [`Dt`] is on the 2000-01-01 noon epoch.
    /// - Same value as [`Dt::to_mjd`](../struct.Dt.html#method.to_mjd), expressed as a single
    ///   `f64` instead of a `(days, frac_attos)` pair.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Scale};
    ///
    /// let dt = Dt::from_mjd_f(60_961.25, Scale::TAI);
    /// assert_eq!(dt.to_mjd_f(), 60_961.25);
    ///
    /// let dt = Dt::from_mjd_f(-1_000.25, Scale::TAI);
    /// assert_eq!(dt.to_mjd_f(), -1_000.25);
    /// ```
    ///
    /// ## See also
    ///
    /// - [`Dt::to_mjd`](../struct.Dt.html#method.to_mjd)
    /// - [`Dt::to_mjd_f_raw`](../struct.Dt.html#method.to_mjd_f_raw)
    /// - [`Dt::from_mjd_f`](../struct.Dt.html#method.from_mjd_f)
    #[inline]
    pub const fn to_mjd_f(&self) -> Real {
        let (days, attos) = self.to_mjd();
        f!(days) + f!(attos) / f!(ATTOS_PER_DAY)
    }

    /// Like [`Dt::to_mjd_f`](../struct.Dt.html#method.to_mjd_f), but uses this [`Dt`]'s current
    /// `scale` instead of converting to `target` first.
    ///
    /// ## See also
    ///
    /// - [`Dt::to_mjd_f`](../struct.Dt.html#method.to_mjd_f)
    /// - [`Dt::to_mjd_raw`](../struct.Dt.html#method.to_mjd_raw)
    #[inline]
    pub const fn to_mjd_f_raw(&self) -> Real {
        let (days, attos) = self.to_mjd_raw();
        f!(days) + f!(attos) / f!(ATTOS_PER_DAY)
    }

    /// Builds a **TAI** [`Dt`] from a Julian Date given as whole days plus attoseconds.
    ///
    /// ## Important
    ///
    /// This converts from the `on` time scale to `TAI` so for example, an
    /// `on` scale of `Scale::UTC` will add leap seconds to the end result.
    ///
    /// To avoid a time scale conversion use `Scale::TAI` for the `on` argument.
    ///
    /// ## Returns
    ///
    /// A [`Dt`] counting attoseconds since the library epoch
    /// [`Dt::ZERO`](../constant.ZERO.html) with its `scale` field set to
    /// `TAI` and its `target` field set to the `on` arg.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Scale, consts::ATTOS_PER_DAY};
    ///
    /// // 2_460_782.25 as whole days plus 0.25 days in attoseconds
    /// let dt = Dt::from_jd(2_460_782, ATTOS_PER_DAY / 4, Scale::TAI);
    /// assert_eq!(dt.to_jd(), (2_460_782, ATTOS_PER_DAY / 4));
    ///
    /// // -1_000.25 as whole days plus -0.25 days in attoseconds
    /// let dt = Dt::from_jd(-1_000, -ATTOS_PER_DAY / 4, Scale::TAI);
    /// assert_eq!(dt.to_jd(), (-1_000, -ATTOS_PER_DAY / 4));
    /// ```
    ///
    /// ## See also
    ///
    /// - [`Dt::from_jd_floor`](../struct.Dt.html#method.from_jd_floor)
    /// - [`Dt::from_jd_f`](../struct.Dt.html#method.from_jd_f)
    /// - [`Dt::to_jd`](../struct.Dt.html#method.to_jd)
    /// - [`Dt::from_mjd`](../struct.Dt.html#method.from_mjd)
    pub const fn from_jd(jd_days: i128, frac_attos: i128, on: Scale) -> Dt {
        let days_since_j2000 = jd_days.saturating_sub(JD_2000_2_451_545_I128);
        let attos_from_days = days_since_j2000.saturating_mul(ATTOS_PER_DAY);
        let total_attos = attos_from_days.saturating_add(frac_attos);

        Self::from_attos(total_attos, on)
    }

    /// Builds a **TAI** [`Dt`] from a Julian Date given as whole days plus attoseconds.
    ///
    /// ## Important
    ///
    /// This converts from the `on` time scale to `TAI` so for example, an
    /// `on` scale of `Scale::UTC` will add leap seconds to the end result.
    ///
    /// To avoid a time scale conversion use `Scale::TAI` for the `on` argument.
    ///
    /// `frac_attos` must be zero or positive and less than one day. e.g. whole `-1001`
    /// and remainder `0.75 * ATTOS_PER_DAY` builds jd `-1000.25`.
    ///
    /// ## Returns
    ///
    /// A [`Dt`] counting attoseconds since the library epoch
    /// [`Dt::ZERO`](../constant.ZERO.html) with its `scale` field set to
    /// `TAI` and its `target` field set to the `on` arg.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Scale, consts::ATTOS_PER_HALF_DAY_U128};
    ///
    /// // 2_460_782.25 as whole days plus 0.25 days in attoseconds
    /// let dt = Dt::from_jd_floor(2_460_782, ATTOS_PER_HALF_DAY_U128 / 2, Scale::TAI);
    /// assert_eq!(dt.to_jd_floor(), (2_460_782, ATTOS_PER_HALF_DAY_U128 / 2));
    ///
    /// // -1_000.25 as -1_001 plus 0.75 days in attoseconds
    /// let dt = Dt::from_jd_floor(-1_001, 3 * ATTOS_PER_HALF_DAY_U128 / 2, Scale::TAI);
    /// assert_eq!(dt.to_jd_floor(), (-1_001, 3 * ATTOS_PER_HALF_DAY_U128 / 2));
    /// ```
    ///
    /// ## See also
    ///
    /// - [`Dt::from_jd`](../struct.Dt.html#method.from_jd)
    /// - [`Dt::from_jd_f`](../struct.Dt.html#method.from_jd_f)
    /// - [`Dt::to_jd_floor`](../struct.Dt.html#method.to_jd_floor)
    pub const fn from_jd_floor(jd_days: i128, frac_attos: u128, on: Scale) -> Dt {
        let days_since_j2000 = jd_days.saturating_sub(JD_2000_2_451_545_I128);
        let frac_attos_i128 = if frac_attos > i128::MAX as u128 {
            i128::MAX
        } else {
            frac_attos as i128
        };
        let attos_from_days = days_since_j2000.saturating_mul(ATTOS_PER_DAY);
        let total_attos = attos_from_days.saturating_add(frac_attos_i128);

        Self::from_attos(total_attos, on)
    }

    /// Builds a **TAI** [`Dt`] from a Modified Julian Date given as whole days plus attoseconds.
    ///
    /// ## Important
    ///
    /// This converts from the `on` time scale to `TAI` so for example, an
    /// `on` scale of `Scale::UTC` will add leap seconds to the end result.
    ///
    /// To avoid a time scale conversion use `Scale::TAI` for the `on` argument.
    ///
    /// ## Returns
    ///
    /// A [`Dt`] counting attoseconds since the library epoch
    /// [`Dt::ZERO`](../constant.ZERO.html) with its `scale` field set to
    /// `TAI` and its `target` field set to the `on` arg.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Scale, consts::ATTOS_PER_DAY};
    ///
    /// // 60_961.25 as whole days plus 0.25 days in attoseconds
    /// let dt = Dt::from_mjd(60_961, ATTOS_PER_DAY / 4, Scale::TAI);
    /// assert_eq!(dt.to_mjd(), (60_961, ATTOS_PER_DAY / 4));
    ///
    /// // -1_000.25 as -1_001 plus 0.75 days in attoseconds
    /// let dt = Dt::from_mjd(-1_001, 3 * ATTOS_PER_DAY / 4, Scale::TAI);
    /// assert_eq!(dt.to_mjd(), (-1_001, 3 * ATTOS_PER_DAY / 4));
    /// ```
    ///
    /// ## See also
    ///
    /// - [`Dt::from_mjd_floor`](../struct.Dt.html#method.from_mjd_floor)
    /// - [`Dt::from_mjd_f`](../struct.Dt.html#method.from_mjd_f)
    /// - [`Dt::to_mjd`](../struct.Dt.html#method.to_mjd)
    /// - [`Dt::from_jd`](../struct.Dt.html#method.from_jd)
    pub const fn from_mjd(mjd_days: i128, frac_attos: i128, on: Scale) -> Dt {
        // Inverse of `to_mjd_raw`: that subtracts 2_400_001 from the JD integer part and
        // adds half a day to the fraction. Here we undo both steps.
        let jd_days = mjd_days.saturating_add(2_400_001);
        let jd_attos = frac_attos.saturating_sub(ATTOS_PER_HALF_DAY);

        if jd_attos < 0 {
            Self::from_jd(
                jd_days.saturating_sub(1),
                jd_attos.saturating_add(ATTOS_PER_DAY),
                on,
            )
        } else if jd_attos >= ATTOS_PER_DAY {
            Self::from_jd(
                jd_days.saturating_add(1),
                jd_attos.saturating_sub(ATTOS_PER_DAY),
                on,
            )
        } else {
            Self::from_jd(jd_days, jd_attos, on)
        }
    }

    /// Builds a **TAI** [`Dt`] from a Modified Julian Date given as whole days plus attoseconds.
    ///
    /// ## Important
    ///
    /// This converts from the `on` time scale to `TAI` so for example, an
    /// `on` scale of `Scale::UTC` will add leap seconds to the end result.
    ///
    /// To avoid a time scale conversion use `Scale::TAI` for the `on` argument.
    ///
    /// MJD and JD relate by `JD = MJD + 2_400_000.5`.
    ///
    /// `frac_attos` must be zero or positive and less than one day. e.g. whole `-1001`
    /// and remainder `0.75 * ATTOS_PER_DAY` builds mjd `-1000.25`.
    ///
    /// ## Returns
    ///
    /// A [`Dt`] counting attoseconds since the library epoch
    /// [`Dt::ZERO`](../constant.ZERO.html) with its `scale` field set to
    /// `TAI` and its `target` field set to the `on` arg.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Scale, consts::ATTOS_PER_HALF_DAY_U128};
    ///
    /// // 60_961.25 as whole days plus 0.25 days in attoseconds
    /// let dt = Dt::from_mjd_floor(60_961, ATTOS_PER_HALF_DAY_U128 / 2, Scale::TAI);
    /// assert_eq!(dt.to_mjd_floor(), (60_961, ATTOS_PER_HALF_DAY_U128 / 2));
    ///
    /// // -1_000.25 as -1_001 plus 0.75 days in attoseconds
    /// let dt = Dt::from_mjd_floor(-1_001, 3 * ATTOS_PER_HALF_DAY_U128 / 2, Scale::TAI);
    /// assert_eq!(dt.to_mjd_floor(), (-1_001, 3 * ATTOS_PER_HALF_DAY_U128 / 2));
    /// ```
    ///
    /// ## See also
    ///
    /// - [`Dt::from_mjd`](../struct.Dt.html#method.from_mjd)
    /// - [`Dt::from_mjd_f`](../struct.Dt.html#method.from_mjd_f)
    /// - [`Dt::to_mjd_floor`](../struct.Dt.html#method.to_mjd_floor)
    pub const fn from_mjd_floor(mjd_days: i128, frac_attos: u128, on: Scale) -> Dt {
        let jd_days = mjd_days.saturating_add(2_400_000);
        let jd_attos = frac_attos.saturating_add(ATTOS_PER_HALF_DAY as u128);

        if jd_attos >= ATTOS_PER_DAY as u128 {
            Self::from_jd_floor(
                jd_days.saturating_add(1),
                jd_attos.saturating_sub(ATTOS_PER_DAY as u128),
                on,
            )
        } else {
            Self::from_jd_floor(jd_days, jd_attos, on)
        }
    }

    /// Builds a **TAI** [`Dt`] from a floating-point Julian Date.
    ///
    /// ## Important
    ///
    /// - The `on` scale becomes this [`Dt`]'s `target`; its `scale` is always `TAI`.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Scale, consts::ATTOS_PER_DAY};
    ///
    /// let dt = Dt::from_jd_f(2_460_782.25, Scale::TAI);
    /// // 2_460_782 and 0.25 days in attoseconds
    /// assert_eq!(dt.to_jd(), (2_460_782, ATTOS_PER_DAY / 4));
    ///
    /// let dt = Dt::from_jd_f(-1_000.25, Scale::TAI);
    /// // -1_000 and -0.25 days in attoseconds
    /// assert_eq!(dt.to_jd(), (-1_000, -ATTOS_PER_DAY / 4));
    /// ```
    ///
    /// ## See also
    ///
    /// - [`Dt::from_jd`](../struct.Dt.html#method.from_jd)
    /// - [`Dt::from_jd_floor`](../struct.Dt.html#method.from_jd_floor)
    /// - [`Dt::to_jd_f`](../struct.Dt.html#method.to_jd_f)
    /// - [`Dt::from_mjd_f`](../struct.Dt.html#method.from_mjd_f)
    pub const fn from_jd_f(jd: Real, on: Scale) -> Dt {
        let jd_days_f = floor_f(jd);
        let jd_days = jd_days_f as i128;

        let mut frac_day = jd - jd_days_f;
        if frac_day < 0.0 {
            frac_day = 0.0;
        } else if frac_day >= 1.0 {
            frac_day = 1.0 - f64::EPSILON;
        }

        let total_sec_f = frac_day * 86_400.0;
        let whole_sec = floor_f(total_sec_f) as i64;
        let frac_sec = total_sec_f - (whole_sec as Real);

        let attos_whole: i128 = (whole_sec as i128).saturating_mul(ATTOS_PER_SEC_I128);

        let attos_frac_f = frac_sec * 1_000_000_000_000_000_000.0;
        let attos_frac: i128 = floor_f(attos_frac_f + 0.5) as i128;

        let mut total_attos: i128 = attos_whole.saturating_add(attos_frac);

        let mut extra_days: i128 = 0;
        if total_attos >= ATTOS_PER_DAY {
            extra_days = 1;
            total_attos = total_attos.saturating_sub(ATTOS_PER_DAY);
        } else if total_attos < 0 {
            extra_days = -1;
            total_attos = total_attos.saturating_add(ATTOS_PER_DAY);
        }

        let final_jd_days = jd_days.saturating_add(extra_days);
        let frac_attos = total_attos as u128;

        Self::from_jd_floor(final_jd_days, frac_attos, on)
    }

    /// Builds a **TAI** [`Dt`] from a floating-point Modified Julian Date.
    ///
    /// ## Important
    ///
    /// This converts from the `on` time scale to `TAI` so for example, an
    /// `on` scale of `Scale::UTC` will add leap seconds to the end result.
    ///
    /// To avoid a time scale conversion use `Scale::TAI` for the `on` argument.
    ///
    /// ## Returns
    ///
    /// A [`Dt`] counting attoseconds since the library epoch
    /// [`Dt::ZERO`](../constant.ZERO.html) with its `scale` field set to
    /// `TAI` and its `target` field set to the `on` arg.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use deep_time::{Dt, Scale, consts::ATTOS_PER_DAY};
    ///
    /// let dt = Dt::from_mjd_f(60_961.25, Scale::TAI);
    /// // 60_961 and 0.25 days in attoseconds
    /// assert_eq!(dt.to_mjd(), (60_961, ATTOS_PER_DAY / 4));
    ///
    /// let dt = Dt::from_mjd_f(-1_000.25, Scale::TAI);
    /// // -1_001 and effectively 0.75 days in attoseconds
    /// assert_eq!(dt.to_mjd(), (-1_001, 3 * ATTOS_PER_DAY / 4));
    /// ```
    ///
    /// ## See also
    ///
    /// - [`Dt::from_mjd`](../struct.Dt.html#method.from_mjd)
    /// - [`Dt::from_mjd_floor`](../struct.Dt.html#method.from_mjd_floor)
    /// - [`Dt::from_jd_f`](../struct.Dt.html#method.from_jd_f)
    /// - [`Dt::to_mjd_f`](../struct.Dt.html#method.to_mjd_f)
    #[inline]
    pub const fn from_mjd_f(mjd: Real, on: Scale) -> Dt {
        let jd = mjd + f!(2_400_000.5);
        Self::from_jd_f(jd, on)
    }
}