Skip to main content

deep_time/dt/
constructors.rs

1use crate::{
2    ATTOS_PER_DAY, ATTOS_PER_FS_I128, ATTOS_PER_HOUR, ATTOS_PER_MIN, ATTOS_PER_MS_I128,
3    ATTOS_PER_NS_I128, ATTOS_PER_PS_I128, ATTOS_PER_SEC_I128, ATTOS_PER_US_I128, Dt, Real,
4    SEC_PER_DAY_F, SEC_PER_DAY_I64, SEC_PER_WEEK, Scale, TAI_SEC_1970_MIDNIGHT_TO_2000_NOON,
5};
6
7impl Dt {
8    /// The library’s internal reference epoch.
9    ///
10    /// - **2000-01-01 12:00:00 TAI**.
11    /// - 0 attoseconds
12    /// - The vast majority of conversion functions in the library expect the given
13    ///   [`Dt`] to be an attoseconds count since this epoch.
14    pub const ZERO: Self = Self::new(0, Scale::TAI, Scale::TAI);
15
16    /// UNIX epoch.
17    ///
18    /// - 1970-01-01 00:00:00 TAI.
19    /// - Stored here on the **TAI** timescale as an offset from [`Dt::ZERO`](#associatedconstant.ZERO).
20    /// - -946_728_000_000_000_000_000_000_000 attoseconds
21    /// - Does not take into account historical UTC offsets from the "rubber time" era.
22    /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
23    pub const UNIX_EPOCH: Self = Self::new(
24        -(TAI_SEC_1970_MIDNIGHT_TO_2000_NOON as i128) * ATTOS_PER_SEC_I128,
25        Scale::TAI,
26        Scale::UTC,
27    );
28
29    /// NTP epoch.
30    ///
31    /// - 1900-01-01 00:00:00 UTC.
32    /// - Stored here on the **TAI** timescale as an offset from [`Dt::ZERO`](#associatedconstant.ZERO).
33    /// - -3_155_716_800_000_000_000_000_000_000 attoseconds
34    /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
35    pub const NTP_EPOCH: Self = Self::new(
36        -3_155_716_800_000_000_000_000_000_000i128,
37        Scale::TAI,
38        Scale::TAI,
39    );
40
41    /// TT/TCG/TCB/TDB epoch.
42    ///
43    /// - 1977-01-01 00:00:00 TAI.
44    /// - Stored here on the **TAI** timescale as an offset from [`Dt::ZERO`](#associatedconstant.ZERO).
45    /// - -725_803_200_000_000_000_000_000_000 attoseconds
46    /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
47    pub const TAI_1977_EPOCH: Self = Self::new(
48        -725_803_200_000_000_000_000_000_000i128,
49        Scale::TAI,
50        Scale::TAI,
51    );
52
53    /// Chandra X-ray Center (CXC) Time epoch.
54    ///
55    /// - 1998-01-01 00:00:00 TT.
56    /// - Stored here on the **TAI** timescale as an offset from [`Dt::ZERO`](#associatedconstant.ZERO).
57    /// - -63_115_232_184_000_000_000_000_000_000 attoseconds
58    /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
59    pub const CXC_EPOCH: Self = Self::new(
60        -63_115_232_184_000_000_000_000_000i128,
61        Scale::TAI,
62        Scale::TT,
63    );
64
65    /// GPS/Galileo Experiment (GALEX) Time epoch.
66    ///
67    /// - 1980-01-06 00:00:00 UTC.
68    /// - Stored here on the **TAI** timescale as an offset from [`Dt::ZERO`](#associatedconstant.ZERO).
69    /// - -630_763_181_000_000_000_000_000_000 attoseconds
70    /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
71    pub const GPS_EPOCH: Self = Self::new(
72        -630_763_181_000_000_000_000_000_000i128,
73        Scale::TAI,
74        Scale::GPS,
75    );
76
77    /// Galileo System Time (GST) epoch.
78    ///
79    /// - 1999-08-22 00:00:00 GST.
80    /// - Stored here on the **TAI** timescale as an offset from [`Dt::ZERO`](#associatedconstant.ZERO).
81    /// - -11_447_981_000_000_000_000_000_000 attoseconds
82    /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
83    pub const GALILEO_EPOCH: Self = Self::new(
84        -11_447_981_000_000_000_000_000_000i128,
85        Scale::TAI,
86        Scale::GST,
87    );
88
89    /// BeiDou Time (BDT) epoch.
90    ///
91    /// - 2006-01-01 00:00:00 UTC.
92    /// - Stored here on the **TAI** timescale as an offset from [`Dt::ZERO`](#associatedconstant.ZERO).
93    /// - 189_345_633_000_000_000_000_000_000 attoseconds
94    /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
95    pub const BDT_EPOCH: Self = Self::new(
96        189_345_633_000_000_000_000_000_000i128,
97        Scale::TAI,
98        Scale::BDT,
99    );
100
101    /// CCSDS epoch (used in CCSDS time codes such as CUC).
102    ///
103    /// - 1958-01-01 00:00:00 TAI.
104    /// - Stored here on the **TAI** timescale as an offset from [`Dt::ZERO`](#associatedconstant.ZERO).
105    /// - -1_325_419_200_000_000_000_000_000_000 attoseconds
106    /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
107    pub const CCSDS_EPOCH: Self = Self::new(
108        -1_325_419_200_000_000_000_000_000_000i128,
109        Scale::TAI,
110        Scale::TAI,
111    );
112
113    /// JD epoch (JD 0.0).
114    ///
115    /// - -4713-11-24 12:00:00
116    /// - Stored here on the **TAI** timescale as an offset from [`Dt::ZERO`](#associatedconstant.ZERO).
117    /// - -211_813_488_000_000_000_000_000_000 attoseconds
118    /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
119    pub const JD_EPOCH: Self = Self::new(
120        -211_813_488_000_000_000_000_000_000_000i128,
121        Scale::TAI,
122        Scale::TAI,
123    );
124
125    /// MJD epoch (MJD 0.0)
126    ///
127    /// - 1858-11-17 00:00:00
128    /// - Stored here on the **TAI** timescale as an offset from [`Dt::ZERO`](#associatedconstant.ZERO).
129    /// - -4_453_444_800_000_000_000_000_000_000 attoseconds
130    /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
131    pub const MJD_EPOCH: Self = Self::new(
132        -4_453_444_800_000_000_000_000_000_000i128,
133        Scale::TAI,
134        Scale::TAI,
135    );
136
137    /// Maximum representable duration.
138    pub const MAX: Self = Self::new(i128::MAX, Scale::TAI, Scale::TAI);
139
140    /// Minimum (most negative) representable duration.
141    pub const MIN: Self = Self::new(i128::MIN, Scale::TAI, Scale::TAI);
142
143    /// 19 seconds.
144    pub const SEC_19: Self = Self::new(19i128 * ATTOS_PER_SEC_I128, Scale::TAI, Scale::TAI);
145
146    /// 33 seconds.
147    pub const SEC_33: Self = Self::new(33i128 * ATTOS_PER_SEC_I128, Scale::TAI, Scale::TAI);
148
149    /// 37 seconds.
150    pub const SEC_37: Self = Self::new(37i128 * ATTOS_PER_SEC_I128, Scale::TAI, Scale::TAI);
151
152    /// One days worth of attoseconds.
153    pub const ONE_DAY: Self = Self::new(
154        (SEC_PER_DAY_I64 as i128) * ATTOS_PER_SEC_I128,
155        Scale::TAI,
156        Scale::TAI,
157    );
158
159    /// Creates a new [`Dt`] from a total number of attoseconds since the librarys
160    /// epoch **2000-01-01 12:00:00 TAI**.
161    ///
162    /// Does **not** perform any time scale conversions.
163    ///
164    /// ## Examples
165    ///
166    /// ```rust
167    /// use deep_time::{Dt, Scale};
168    ///
169    /// // current scale TAI, target scale UTC
170    /// let a = Dt::new(0, Scale::TAI, Scale::UTC);
171    ///
172    /// // equivalent to direct construction
173    /// let b = Dt { attos: 0, scale: Scale::TAI, target: Scale::UTC };
174    ///
175    /// assert_eq!(a, b);
176    /// ```
177    ///
178    /// ## See also
179    ///
180    /// - [`dt!`](../macro.dt.html)
181    /// - [`ns!`](../macro.ns.html)
182    #[inline(always)]
183    pub const fn new(attos: i128, scale: Scale, target: Scale) -> Dt {
184        Dt {
185            attos,
186            scale,
187            target,
188        }
189    }
190
191    /// Low level constructor from total attoseconds since a given epoch.
192    ///
193    /// Simply adds the total attoseconds to the epoch. Does not perform
194    /// any time scale conversions.
195    ///
196    /// The returned [`Dt`] copies the epoch's `scale` and `target` fields.
197    ///
198    /// ## Examples
199    ///
200    /// ```rust
201    /// use deep_time::{Dt, Scale};
202    ///
203    /// // A leap second from the middle of the table (36 leap seconds accumulated)
204    /// let original = Dt::from_ymd(2015, 6, 30, Scale::UTC, 23, 59, 60, 123_456_789_000_000_000);
205    ///
206    /// // Round-trip through canonical attoseconds
207    /// let canon = original.to_diff_raw(Dt::UNIX_EPOCH).to_attos();
208    /// let roundtrip1 = Dt::from_diff_raw(canon, Dt::UNIX_EPOCH);
209    ///
210    /// assert_eq!(original, roundtrip1, "Canonical round-trip failed");
211    /// ```
212    ///
213    /// ## See also
214    ///
215    /// - [`Dt::to_diff_raw`](../struct.Dt.html#method.to_diff_raw)
216    /// - [`Dt::to_diff_raw_f`](../struct.Dt.html#method.to_diff_raw_f)
217    #[inline(always)]
218    pub const fn from_diff_raw(attos: i128, epoch: Dt) -> Dt {
219        epoch.add(Dt::new(attos, epoch.scale, epoch.target))
220    }
221
222    /// Builds a [`Dt`] holding the given whole seconds and sub-second remainder.
223    ///
224    /// The remainder is in **attoseconds**, not seconds. Pairs with
225    /// [`Dt::to_sec64`](#method.to_sec64) + [`Dt::to_sec_frac`](#method.to_sec_frac).
226    ///
227    /// Does **not** perform any time scale conversions.
228    ///
229    /// ## Parameters
230    ///
231    /// - `sec` — whole seconds (truncating / signed-remainder split).
232    /// - `attos` — fractional part of that split, in attoseconds.
233    ///   Prefer helpers such as [`Dt::ms_to_attos`](#method.ms_to_attos) /
234    ///   [`Dt::ns_to_attos`](#method.ns_to_attos) or the [`ms!`](../macro.ms.html) /
235    ///   [`ns!`](../macro.ns.html) macros instead of hand-counting zeros:
236    ///   - `1.3` s → `sec = 1`, `attos = Dt::ms_to_attos(300)`
237    ///   - `-1.3` s → `sec = -1`, `attos = Dt::ms_to_attos(-300)`
238    ///   - `-0.5` s → `sec = 0`, `attos = Dt::ms_to_attos(-500)`
239    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
240    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
241    ///
242    /// ## Examples
243    ///
244    /// ```rust
245    /// use deep_time::{Dt, Scale, dt, ms};
246    ///
247    /// // 1.3 s — convert 300 ms of remainder to attoseconds
248    /// let a = Dt::from_sec_and_frac(1, ms!(300), Scale::TAI, Scale::TAI);
249    /// assert_eq!(a, dt!(1_300_000_000_000_000_000));
250    ///
251    /// // -1.3 s (signed remainder)
252    /// assert_eq!(
253    ///     Dt::from_sec_and_frac(-1, Dt::ms_to_attos(-300), Scale::TAI, Scale::TAI),
254    ///     dt!(-1_300_000_000_000_000_000),
255    /// );
256    ///
257    /// // -0.5 s
258    /// assert_eq!(
259    ///     Dt::from_sec_and_frac(0, Dt::ms_to_attos(-500), Scale::TAI, Scale::TAI),
260    ///     dt!(-500_000_000_000_000_000),
261    /// );
262    /// ```
263    #[inline(always)]
264    pub const fn from_sec_and_frac(sec: i128, attos: i128, on: Scale, target: Scale) -> Dt {
265        Dt::new(
266            sec.saturating_mul(ATTOS_PER_SEC_I128).saturating_add(attos),
267            on,
268            target,
269        )
270    }
271
272    /// Builds a [`Dt`] holding the given whole seconds.
273    ///
274    /// Does **not** perform any time scale conversions. The `sec` count is stored
275    /// as-is (converted only from seconds to attoseconds); its meaning depends on
276    /// how you use the value afterward (for example as a library-epoch offset, a
277    /// Unix offset passed to [`Dt::from_unix`](#method.from_unix), a duration, etc.).
278    ///
279    /// ## Parameters
280    ///
281    /// - `sec` — whole seconds count to store.
282    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
283    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
284    #[inline(always)]
285    pub const fn from_sec(sec: i128, on: Scale, target: Scale) -> Dt {
286        Dt::new(sec.saturating_mul(ATTOS_PER_SEC_I128), on, target)
287    }
288
289    /// Builds a [`Dt`] holding the given whole milliseconds and sub-millisecond remainder.
290    ///
291    /// The remainder is in **attoseconds**, not milliseconds. Pairs with
292    /// [`to_ms`](../struct.Dt.html#method.to_ms).
293    ///
294    /// Does **not** perform any time scale conversions.
295    ///
296    /// ## Parameters
297    ///
298    /// - `ms` — whole milliseconds (truncating / signed-remainder split).
299    /// - `frac_attos` — fractional part of that split, in attoseconds.
300    ///   Use a smaller-unit converter rather than counting zeros by hand:
301    ///   - `1.3` ms → `ms = 1`, `frac_attos = Dt::us_to_attos(300)` (0.3 ms = 300 µs)
302    ///   - `-1.3` ms → `ms = -1`, `frac_attos = Dt::us_to_attos(-300)`
303    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
304    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
305    ///
306    /// ## Examples
307    ///
308    /// ```rust
309    /// use deep_time::{Dt, Scale};
310    /// use deep_time::macros::us;
311    ///
312    /// // 1.3 ms
313    /// let a = Dt::from_ms(1, us!(300), Scale::TAI, Scale::TAI);
314    /// assert_eq!(a.to_attos(), 1_300_000_000_000_000);
315    ///
316    /// // -1.3 ms
317    /// let neg = Dt::from_ms(-1, us!(-300), Scale::TAI, Scale::TAI);
318    /// assert_eq!(neg.to_attos(), -1_300_000_000_000_000);
319    ///
320    /// // or as floored -1.3 ms
321    /// let neg = Dt::from_ms(-2, us!(700), Scale::TAI, Scale::TAI);
322    /// assert_eq!(neg.to_attos(), -1_300_000_000_000_000);
323    /// ```
324    ///
325    /// ## See also
326    ///
327    /// - [`from_ms!`](../macros/macro.from_ms.html)
328    /// - [`us!`](../macros/macro.us.html)
329    #[inline(always)]
330    pub const fn from_ms(ms: i128, attos: i128, on: Scale, target: Scale) -> Dt {
331        let attos = Dt::unit_to_total_attos(ms, attos, ATTOS_PER_MS_I128);
332        Dt::new(attos, on, target)
333    }
334
335    /// Builds a [`Dt`] holding the given whole microseconds and sub-microsecond remainder.
336    ///
337    /// The remainder is in **attoseconds**, not microseconds. Pairs with
338    /// [`to_us`](../struct.Dt.html#method.to_us).
339    ///
340    /// Does **not** perform any time scale conversions.
341    ///
342    /// ## Parameters
343    ///
344    /// - `us` — whole microseconds (truncating / signed-remainder split).
345    /// - `frac_attos` — fractional part of that split, in attoseconds.
346    ///   Use a smaller-unit converter rather than counting zeros by hand:
347    ///   - `1.3` µs → `us = 1`, `frac_attos = Dt::ns_to_attos(300)` (0.3 µs = 300 ns)
348    ///   - `-1.3` µs → `us = -1`, `frac_attos = Dt::ns_to_attos(-300)`
349    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
350    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
351    ///
352    /// ## Examples
353    ///
354    /// ```rust
355    /// use deep_time::{Dt, Scale, ns};
356    ///
357    /// // 1.3 µs
358    /// let a = Dt::from_us(1, ns!(300), Scale::TAI, Scale::TAI);
359    /// assert_eq!(a.to_attos(), 1_300_000_000_000);
360    ///
361    /// // -1.3 µs
362    /// let neg = Dt::from_us(-1, ns!(-300), Scale::TAI, Scale::TAI);
363    /// assert_eq!(neg.to_attos(), -1_300_000_000_000);
364    ///
365    /// // or as floored -1.3 µs
366    /// let neg = Dt::from_us(-2, ns!(700), Scale::TAI, Scale::TAI);
367    /// assert_eq!(neg.to_attos(), -1_300_000_000_000);
368    /// ```
369    ///
370    /// ## See also
371    ///
372    /// - [`from_us!`](../macros/macro.from_us.html)
373    /// - [`ns!`](../macro.ns.html)
374    #[inline(always)]
375    pub const fn from_us(us: i128, attos: i128, on: Scale, target: Scale) -> Dt {
376        let attos = Dt::unit_to_total_attos(us, attos, ATTOS_PER_US_I128);
377        Dt::new(attos, on, target)
378    }
379
380    /// Builds a [`Dt`] holding the given whole nanoseconds and sub-nanosecond remainder.
381    ///
382    /// The remainder is in **attoseconds**, not nanoseconds. Pairs with
383    /// [`to_ns`](../struct.Dt.html#method.to_ns).
384    ///
385    /// Does **not** perform any time scale conversions.
386    ///
387    /// ## Parameters
388    ///
389    /// - `ns` — whole nanoseconds (truncating / signed-remainder split).
390    /// - `frac_attos` — fractional part of that split, in attoseconds.
391    ///   Use a smaller-unit converter rather than counting zeros by hand:
392    ///   - `1.3` ns → `ns = 1`, `frac_attos = Dt::ps_to_attos(300)` (0.3 ns = 300 ps)
393    ///   - `-1.3` ns → `ns = -1`, `frac_attos = Dt::ps_to_attos(-300)`
394    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
395    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
396    ///
397    /// ## Examples
398    ///
399    /// ```rust
400    /// use deep_time::{Dt, Scale};
401    /// use deep_time::macros::ps;
402    ///
403    /// // 1.3 ns → whole nanoseconds + 300 ps remainder
404    /// let a = Dt::from_ns(1, ps!(300), Scale::TAI, Scale::TAI);
405    /// assert_eq!(a.to_attos(), 1_300_000_000);
406    ///
407    /// // -1.3 ns
408    /// let neg = Dt::from_ns(-1, Dt::ps_to_attos(-300), Scale::TAI, Scale::TAI);
409    /// assert_eq!(neg.to_attos(), -1_300_000_000);
410    /// ```
411    #[inline(always)]
412    pub const fn from_ns(ns: i128, attos: i128, on: Scale, target: Scale) -> Dt {
413        let attos = Dt::unit_to_total_attos(ns, attos, ATTOS_PER_NS_I128);
414        Dt::new(attos, on, target)
415    }
416
417    /// Builds a [`Dt`] holding the given whole picoseconds and sub-picosecond remainder.
418    ///
419    /// The remainder is in **attoseconds**, not picoseconds. Pairs with
420    /// [`to_ps`](../struct.Dt.html#method.to_ps).
421    ///
422    /// Does **not** perform any time scale conversions.
423    ///
424    /// ## Parameters
425    ///
426    /// - `ps` — whole picoseconds (truncating / signed-remainder split).
427    /// - `frac_attos` — fractional part of that split, in attoseconds.
428    ///   Use a smaller-unit converter rather than counting zeros by hand:
429    ///   - `1.3` ps → `ps = 1`, `frac_attos = Dt::fs_to_attos(300)` (0.3 ps = 300 fs)
430    ///   - `-1.3` ps → `ps = -1`, `frac_attos = Dt::fs_to_attos(-300)`
431    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
432    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
433    ///
434    /// ## Examples
435    ///
436    /// ```rust
437    /// use deep_time::{Dt, Scale};
438    /// use deep_time::macros::fs;
439    ///
440    /// // 1.3 ps
441    /// let a = Dt::from_ps(1, fs!(300), Scale::TAI, Scale::TAI);
442    /// assert_eq!(a.to_attos(), 1_300_000);
443    ///
444    /// // -1.3 ps
445    /// let neg = Dt::from_ps(-1, Dt::fs_to_attos(-300), Scale::TAI, Scale::TAI);
446    /// assert_eq!(neg.to_attos(), -1_300_000);
447    /// ```
448    #[inline(always)]
449    pub const fn from_ps(ps: i128, attos: i128, on: Scale, target: Scale) -> Dt {
450        let attos = Dt::unit_to_total_attos(ps, attos, ATTOS_PER_PS_I128);
451        Dt::new(attos, on, target)
452    }
453
454    /// Builds a [`Dt`] holding the given whole femtoseconds and sub-femtosecond remainder.
455    ///
456    /// The remainder is in **attoseconds**, not femtoseconds. Pairs with
457    /// [`to_fs`](../struct.Dt.html#method.to_fs).
458    ///
459    /// Does **not** perform any time scale conversions.
460    ///
461    /// ## Parameters
462    ///
463    /// - `fs` — whole femtoseconds (truncating / signed-remainder split).
464    /// - `frac_attos` — fractional part of that split, in attoseconds.
465    ///   One femtosecond is 1000 attoseconds, so a fractional remainder is already
466    ///   a small integer: `1.3` fs → `fs = 1`, `frac_attos = 300`.
467    ///   For `-1.3` fs: `fs = -1`, `frac_attos = -300`.
468    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
469    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
470    ///
471    /// ## Examples
472    ///
473    /// ```rust
474    /// use deep_time::{Dt, Scale};
475    ///
476    /// // 1.3 fs — sub-fs remainder is already in attoseconds (×10³)
477    /// let a = Dt::from_fs(1, 300, Scale::TAI, Scale::TAI);
478    /// assert_eq!(a.to_attos(), 1_300);
479    ///
480    /// // whole fs only — still fine to use the converter for the whole part
481    /// // if you are building total attos by hand:
482    /// assert_eq!(Dt::fs_to_attos(1), 1_000);
483    ///
484    /// // -1.3 fs
485    /// let neg = Dt::from_fs(-1, -300, Scale::TAI, Scale::TAI);
486    /// assert_eq!(neg.to_attos(), -1_300);
487    /// ```
488    #[inline(always)]
489    pub const fn from_fs(fs: i128, attos: i128, on: Scale, target: Scale) -> Dt {
490        let attos = Dt::unit_to_total_attos(fs, attos, ATTOS_PER_FS_I128);
491        Dt::new(attos, on, target)
492    }
493
494    /// Builds a [`Dt`] holding the given whole minutes and sub-minute remainder.
495    ///
496    /// The remainder is in **attoseconds**, not minutes.
497    ///
498    /// Does **not** perform any time scale conversions.
499    ///
500    /// ## Parameters
501    ///
502    /// - `n` — whole minutes (truncating / signed-remainder split).
503    /// - `frac_attos` — fractional part of that split, in attoseconds.
504    ///   Use a time-unit converter rather than counting zeros by hand:
505    ///   - `1.5` min → `n = 1`, `frac_attos = Dt::sec_to_attos(30)` (0.5 min = 30 s)
506    ///   - `-1.5` min → `n = -1`, `frac_attos = Dt::sec_to_attos(-30)`
507    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
508    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
509    ///
510    /// ## Examples
511    ///
512    /// ```rust
513    /// use deep_time::{Dt, Scale};
514    /// use deep_time::macros::sec;
515    ///
516    /// // 1.5 min
517    /// let a = Dt::from_mins(1, sec!(30), Scale::TAI, Scale::TAI);
518    /// assert_eq!(a.to_sec(), 90);
519    ///
520    /// // -1.5 min
521    /// let neg = Dt::from_mins(-1, Dt::sec_to_attos(-30), Scale::TAI, Scale::TAI);
522    /// assert_eq!(neg.to_sec(), -90);
523    /// ```
524    #[inline(always)]
525    pub const fn from_mins(n: i128, attos: i128, on: Scale, target: Scale) -> Dt {
526        let attos = Dt::unit_to_total_attos(n, attos, ATTOS_PER_MIN);
527        Dt::new(attos, on, target)
528    }
529
530    /// Builds a [`Dt`] holding the given whole hours and sub-hour remainder.
531    ///
532    /// The remainder is in **attoseconds**, not hours.
533    ///
534    /// Does **not** perform any time scale conversions.
535    ///
536    /// ## Parameters
537    ///
538    /// - `n` — whole hours (truncating / signed-remainder split).
539    /// - `frac_attos` — fractional part of that split, in attoseconds.
540    ///   Use a time-unit converter rather than counting zeros by hand:
541    ///   - `1.5` h → `n = 1`, `frac_attos = Dt::mins_to_attos(30)` (0.5 h = 30 min)
542    ///   - `-1.5` h → `n = -1`, `frac_attos = Dt::mins_to_attos(-30)`
543    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
544    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
545    ///
546    /// ## Examples
547    ///
548    /// ```rust
549    /// use deep_time::{Dt, Scale};
550    /// use deep_time::macros::mins;
551    ///
552    /// // 1.5 h
553    /// let a = Dt::from_hours(1, mins!(30), Scale::TAI, Scale::TAI);
554    /// assert_eq!(a.to_sec(), 5400);
555    ///
556    /// // -1.5 h
557    /// let neg = Dt::from_hours(-1, Dt::mins_to_attos(-30), Scale::TAI, Scale::TAI);
558    /// assert_eq!(neg.to_sec(), -5400);
559    /// ```
560    #[inline(always)]
561    pub const fn from_hours(n: i128, attos: i128, on: Scale, target: Scale) -> Dt {
562        let attos = Dt::unit_to_total_attos(n, attos, ATTOS_PER_HOUR);
563        Dt::new(attos, on, target)
564    }
565
566    /// Builds a [`Dt`] holding the given whole days and sub-day remainder.
567    ///
568    /// The remainder is in **attoseconds**, not days. Uses `86400` seconds per day.
569    ///
570    /// Does **not** perform any time scale conversions.
571    ///
572    /// ## Parameters
573    ///
574    /// - `d` — whole days (truncating / signed-remainder split).
575    /// - `frac` — fractional part in attoseconds (`frac.attos` only).
576    ///   - `1.25` d → `d = 1`, `frac = dt!(Dt::hours_to_attos(6))` (0.25 d = 6 h)
577    ///   - `-1.25` d → `d = -1`, `frac = dt!(Dt::hours_to_attos(-6))`
578    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
579    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
580    ///
581    /// ## Examples
582    ///
583    /// ```rust
584    /// use deep_time::{Dt, Scale, dt};
585    /// use deep_time::macros::hours;
586    ///
587    /// // 1.25 d
588    /// let a = Dt::from_days(1, dt!(hours!(6)), Scale::TAI, Scale::TAI);
589    /// assert_eq!(a.to_sec(), 108_000); // 1.25 * 86400
590    ///
591    /// // -1.25 d
592    /// let neg = Dt::from_days(-1, dt!(Dt::hours_to_attos(-6)), Scale::TAI, Scale::TAI);
593    /// assert_eq!(neg.to_sec(), -108_000);
594    /// ```
595    #[inline(always)]
596    pub const fn from_days(d: i128, frac: Dt, on: Scale, target: Scale) -> Dt {
597        let attos = Dt::unit_to_total_attos(d, frac.attos, ATTOS_PER_DAY);
598        Dt::new(attos, on, target)
599    }
600
601    /// Builds a [`Dt`] from a floating-point day count since the library epoch
602    /// (2000-01-01 12:00:00 TAI).
603    ///
604    /// This is the inverse of [`Dt::to_days_f`](../struct.Dt.html#method.to_days_f).
605    ///
606    /// Does **not** perform any time scale conversions.
607    ///
608    /// ## Parameters
609    ///
610    /// - `days` — day count to store (converted to attoseconds).
611    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
612    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
613    ///
614    /// ## Examples
615    ///
616    /// ```rust
617    /// use deep_time::{Dt, Scale};
618    ///
619    /// let dt = Dt::from_days_f(1.25, Scale::TAI, Scale::TAI);
620    /// assert_eq!(dt.to_days_f(), 1.25);
621    ///
622    /// let neg = Dt::from_days_f(-1.25, Scale::TAI, Scale::TAI);
623    /// assert_eq!(neg.to_days_f(), -1.25);
624    /// ```
625    ///
626    /// ## See also
627    ///
628    /// - [`Dt::from_days`](../struct.Dt.html#method.from_days)
629    /// - [`Dt::to_days_f`](../struct.Dt.html#method.to_days_f)
630    #[inline]
631    pub const fn from_days_f(days: Real, on: Scale, target: Scale) -> Dt {
632        Self::from_sec_f(days * SEC_PER_DAY_F, on, target)
633    }
634
635    /// Builds a [`Dt`] holding the given number of weeks (`604800` seconds each).
636    ///
637    /// Does **not** perform any time scale conversions.
638    ///
639    /// ## Parameters
640    ///
641    /// - `n` — whole weeks.
642    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
643    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
644    #[inline(always)]
645    pub const fn from_weeks(n: i128, on: Scale, target: Scale) -> Dt {
646        Dt::new(
647            n.saturating_mul(SEC_PER_WEEK as i128)
648                .saturating_mul(ATTOS_PER_SEC_I128),
649            on,
650            target,
651        )
652    }
653
654    /// Builds a [`Dt`] holding the given number of Julian years (`31_557_600` seconds each).
655    ///
656    /// Does **not** perform any time scale conversions.
657    ///
658    /// ## Parameters
659    ///
660    /// - `n` — whole years.
661    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
662    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
663    #[inline(always)]
664    pub const fn from_years(n: i128, on: Scale, target: Scale) -> Dt {
665        Dt::new(
666            n.saturating_mul(31_557_600)
667                .saturating_mul(ATTOS_PER_SEC_I128),
668            on,
669            target,
670        )
671    }
672
673    /// Returns an instant that is this duration **before** zero attoseconds on `scale`.
674    ///
675    /// Zero attoseconds is the library epoch **2000-01-01 12:00:00** (see
676    /// [`Dt::ZERO`](../struct.Dt.html#associatedconstant.ZERO)).
677    ///
678    /// This method does **not** read the system clock.
679    ///
680    /// For wall-clock “N units ago”, use [`Dt::ago`](../struct.Dt.html#method.ago)
681    /// (requires `std`, or WASM with `js`).
682    ///
683    /// ## Examples
684    ///
685    /// ```rust
686    /// use deep_time::{Dt, Scale, TraitsTime};
687    ///
688    /// let t = 5.sec().before_zero(Scale::TAI);
689    /// assert_eq!(t, Dt::ZERO.sub(5.sec()));
690    /// assert_eq!(t.to_sec(), -5);
691    /// ```
692    ///
693    /// ## See also
694    ///
695    /// - [`Dt::ago`](../struct.Dt.html#method.ago)
696    #[inline(always)]
697    pub const fn before_zero(self, scale: Scale) -> Dt {
698        Dt::new(0, scale, scale).to_tai().sub(self)
699    }
700
701    /// Returns the negation of this [`Dt`].
702    #[inline(always)]
703    pub const fn neg(self) -> Dt {
704        Dt::new(-self.attos, self.scale, self.target)
705    }
706
707    /// Returns the positive of this [`Dt`].
708    #[inline(always)]
709    pub const fn abs(self) -> Dt {
710        Dt::new(self.attos.saturating_abs(), self.scale, self.target)
711    }
712
713    /// Builds a [`Dt`] holding the given floating-point seconds count.
714    ///
715    /// Does **not** perform any time scale conversions. The `sec` value is
716    /// stored as attoseconds only; its meaning depends on how you use the
717    /// result afterward.
718    ///
719    /// ## Parameters
720    ///
721    /// - `sec` — seconds count to store (`NaN` → zero attoseconds;
722    ///   `±∞` → [`i128::MAX`] / [`i128::MIN`]).
723    /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
724    /// - `target` — value stored in the returned [`Dt`]'s `target` field.
725    ///
726    /// ## Examples
727    ///
728    /// ```rust
729    /// use deep_time::{Dt, Scale};
730    ///
731    /// let seconds = 5.5;
732    /// let duration = Dt::from_sec_f(seconds, Scale::TAI, Scale::TAI);
733    ///
734    /// assert_eq!(duration.to_sec_f(), seconds);
735    /// ```
736    pub const fn from_sec_f(sec: Real, on: Scale, target: Scale) -> Dt {
737        if sec.is_nan() {
738            return Self::new(0, on, target);
739        } else if sec.is_infinite() {
740            return if sec.is_sign_positive() {
741                Self::new(i128::MAX, on, target)
742            } else {
743                Self::new(i128::MIN, on, target)
744            };
745        }
746        Dt::new(Self::sec_f_to_attos(sec), on, target)
747    }
748
749    /// High-precision conversion from [`Real`] seconds to total attoseconds (i128).
750    ///
751    /// - Uses IEEE 754 bit extraction + exact integer multiplication by 5^18.
752    /// - Returns the rounded integer (round-to-nearest, ties away from zero).
753    pub const fn sec_f_to_attos(sec: Real) -> i128 {
754        if sec == 0.0 {
755            return 0;
756        }
757
758        let bits = sec.to_bits();
759        let is_negative = (bits >> 63) != 0;
760        let biased_exp = ((bits >> 52) & 0x7ff) as i32;
761        let mantissa = bits & 0x000f_ffff_ffff_ffff;
762
763        let (sig, exp) = if biased_exp == 0 {
764            if mantissa == 0 {
765                return 0;
766            }
767            (mantissa as u128, -1022i32 - 52)
768        } else {
769            let sig = ((1u64 << 52) | mantissa) as u128;
770            (sig, biased_exp - 1023 - 52)
771        };
772
773        const FIVE_POW_18: u128 = 3_814_697_265_625; // 5^18 exactly
774        let product = sig * FIVE_POW_18;
775        let total_exp = exp + 18;
776
777        // Safe saturation / underflow guards (prevents invalid shifts >= 128)
778        if total_exp > 120 {
779            return if is_negative { i128::MIN } else { i128::MAX };
780        }
781        if total_exp < -97 {
782            return 0;
783        }
784
785        let abs_total = if total_exp >= 0 {
786            let shift = total_exp as u32;
787            if product > (u128::MAX >> shift) {
788                if is_negative { i128::MIN } else { i128::MAX }
789            } else {
790                let shifted = product << shift;
791                if shifted > i128::MAX as u128 {
792                    if is_negative { i128::MIN } else { i128::MAX }
793                } else {
794                    shifted as i128
795                }
796            }
797        } else {
798            let shift = (-total_exp) as u32;
799            let int_part = (product >> shift) as i128;
800
801            // Round to nearest, half away from zero (on the absolute value)
802            let mask = (1u128 << shift) - 1;
803            let rem = product & mask;
804            if rem > (mask >> 1) {
805                int_part + 1
806            } else {
807                int_part
808            }
809        };
810
811        if is_negative { -abs_total } else { abs_total }
812    }
813
814    /// Returns the current **UTC** system time as TAI from 2000-01-01 12:00:00.
815    ///
816    /// This method is only available when the `std` feature is enabled and the target
817    /// is not WASM with the `js` feature.
818    #[cfg(all(feature = "std", not(all(target_arch = "wasm32", feature = "js"))))]
819    pub fn now() -> Dt {
820        use crate::macros::{from_sec, ns};
821
822        let now = std::time::SystemTime::now();
823
824        let (sec, nanos): (i64, i64) = match now.duration_since(std::time::UNIX_EPOCH) {
825            Ok(dur) => (dur.as_secs() as i64, dur.subsec_nanos() as i64),
826            Err(e) => {
827                let dur = e.duration();
828                (-(dur.as_secs() as i64), -(dur.subsec_nanos() as i64))
829            }
830        };
831        Dt::from_diff_and_scale(
832            from_sec!(sec as i128, ns!(nanos as i128), on = Scale::UTC),
833            Dt::UNIX_EPOCH,
834            false,
835        )
836    }
837
838    /// Returns the current **UTC** system time as TAI from 2000-01-01 12:00:00.
839    /// (browser WASM version using JavaScript’s `Date.now()`).
840    #[cfg(all(target_arch = "wasm32", feature = "js"))]
841    pub fn now() -> Dt {
842        use crate::macros::{from_sec, ns};
843
844        let ms: f64 = js_sys::Date::now();
845        let sec = (ms / 1000.0).floor() as i128;
846        let nanos = ((ms % 1000.0) * 1_000_000.0) as i128;
847        Dt::from_diff_and_scale(
848            from_sec!(sec as i128, ns!(nanos as i128), on = Scale::UTC),
849            Dt::UNIX_EPOCH,
850            false,
851        )
852    }
853
854    /// Returns an instant that is this duration **before** the current system time.
855    ///
856    /// Subtracts `self` from [`Dt::now`](../struct.Dt.html#method.now). Available under
857    /// the same conditions as that method: the `std` feature (non-WASM-js), or WASM with
858    /// the `js` feature.
859    ///
860    /// For a `const` offset from the library epoch (no system clock), use
861    /// [`Dt::before_zero`](../struct.Dt.html#method.before_zero).
862    ///
863    /// ## Examples
864    ///
865    /// ```rust
866    /// # #[cfg(feature = "std")]
867    /// # {
868    /// use deep_time::{Dt, TraitsTime};
869    ///
870    /// // ~3 days in the past relative to the system clock
871    /// let past = 3.days().ago();
872    /// assert!(past < Dt::now());
873    /// # }
874    /// ```
875    ///
876    /// ## See also
877    ///
878    /// - [`Dt::from_now`](../struct.Dt.html#method.from_now)
879    /// - [`Dt::before_zero`](../struct.Dt.html#method.before_zero)
880    /// - [`Dt::now`](../struct.Dt.html#method.now)
881    #[cfg(any(
882        all(feature = "std", not(all(target_arch = "wasm32", feature = "js"))),
883        all(target_arch = "wasm32", feature = "js"),
884    ))]
885    #[inline(always)]
886    pub fn ago(self) -> Dt {
887        Dt::now().sub(self)
888    }
889
890    /// Returns an instant that is this duration **after** the current system time.
891    ///
892    /// Adds `self` to [`Dt::now`](../struct.Dt.html#method.now). Available under the same
893    /// conditions as that method: the `std` feature (non-WASM-js), or WASM with the `js`
894    /// feature.
895    ///
896    /// ## Examples
897    ///
898    /// ```rust
899    /// # #[cfg(feature = "std")]
900    /// # {
901    /// use deep_time::{Dt, TraitsTime};
902    ///
903    /// // ~3 days in the future relative to the system clock
904    /// let future = 3.days().from_now();
905    /// assert!(future > Dt::now());
906    /// # }
907    /// ```
908    ///
909    /// ## See also
910    ///
911    /// - [`Dt::ago`](../struct.Dt.html#method.ago)
912    /// - [`Dt::now`](../struct.Dt.html#method.now)
913    #[cfg(any(
914        all(feature = "std", not(all(target_arch = "wasm32", feature = "js"))),
915        all(target_arch = "wasm32", feature = "js"),
916    ))]
917    #[inline(always)]
918    pub fn from_now(self) -> Dt {
919        Dt::now().add(self)
920    }
921}