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
235 /// [`TraitsAttos`](../trait.TraitsAttos.html))
236 /// instead of hand-counting zeros:
237 /// - `1.3` s → `sec = 1`, `attos = Dt::ms_to_attos(300)`
238 /// - `-1.3` s → `sec = -1`, `attos = Dt::ms_to_attos(-300)`
239 /// - `-0.5` s → `sec = 0`, `attos = Dt::ms_to_attos(-500)`
240 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
241 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
242 ///
243 /// ## Examples
244 ///
245 /// ```rust
246 /// use deep_time::{TraitsAttos, Dt, Scale, dt};
247 ///
248 /// // 1.3 s — convert 300 ms of remainder to attoseconds
249 /// let a = Dt::from_sec_and_frac(1, Dt::ms_to_attos(300), Scale::TAI, Scale::TAI);
250 /// // same via TraitsAttos on the integer
251 /// let b = Dt::from_sec_and_frac(1, 300_i128.ms_to_attos(), Scale::TAI, Scale::TAI);
252 /// assert_eq!(a, b);
253 /// assert_eq!(a, dt!(1_300_000_000_000_000_000));
254 ///
255 /// // -1.3 s (signed remainder)
256 /// assert_eq!(
257 /// Dt::from_sec_and_frac(-1, Dt::ms_to_attos(-300), Scale::TAI, Scale::TAI),
258 /// dt!(-1_300_000_000_000_000_000),
259 /// );
260 ///
261 /// // -0.5 s
262 /// assert_eq!(
263 /// Dt::from_sec_and_frac(0, Dt::ms_to_attos(-500), Scale::TAI, Scale::TAI),
264 /// dt!(-500_000_000_000_000_000),
265 /// );
266 /// ```
267 #[inline(always)]
268 pub const fn from_sec_and_frac(sec: i128, attos: i128, on: Scale, target: Scale) -> Dt {
269 Dt::new(
270 sec.saturating_mul(ATTOS_PER_SEC_I128).saturating_add(attos),
271 on,
272 target,
273 )
274 }
275
276 /// Builds a [`Dt`] holding the given whole seconds.
277 ///
278 /// Does **not** perform any time scale conversions. The `sec` count is stored
279 /// as-is (converted only from seconds to attoseconds); its meaning depends on
280 /// how you use the value afterward (for example as a library-epoch offset, a
281 /// Unix offset passed to [`Dt::from_unix`](#method.from_unix), a duration, etc.).
282 ///
283 /// ## Parameters
284 ///
285 /// - `sec` — whole seconds count to store.
286 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
287 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
288 #[inline(always)]
289 pub const fn from_sec(sec: i128, on: Scale, target: Scale) -> Dt {
290 Dt::new(sec.saturating_mul(ATTOS_PER_SEC_I128), on, target)
291 }
292
293 /// Builds a [`Dt`] holding the given whole milliseconds and sub-millisecond remainder.
294 ///
295 /// The remainder is in **attoseconds**, not milliseconds. Pairs with
296 /// [`to_ms`](../struct.Dt.html#method.to_ms).
297 ///
298 /// Does **not** perform any time scale conversions.
299 ///
300 /// ## Parameters
301 ///
302 /// - `ms` — whole milliseconds (truncating / signed-remainder split).
303 /// - `frac_attos` — fractional part of that split, in attoseconds.
304 /// Use a smaller-unit converter rather than counting zeros by hand:
305 /// - `1.3` ms → `ms = 1`, `frac_attos = Dt::us_to_attos(300)` (0.3 ms = 300 µs)
306 /// - `-1.3` ms → `ms = -1`, `frac_attos = Dt::us_to_attos(-300)`
307 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
308 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
309 ///
310 /// ## Examples
311 ///
312 /// ```rust
313 /// use deep_time::{TraitsAttos, Dt, Scale, us};
314 ///
315 /// // 1.3 ms
316 /// let a = Dt::from_ms(1, us!(300), Scale::TAI, Scale::TAI);
317 /// let b = Dt::from_ms(1, 300_i128.us_to_attos(), Scale::TAI, Scale::TAI);
318 /// assert_eq!(a, b);
319 /// assert_eq!(a.to_attos(), 1_300_000_000_000_000);
320 ///
321 /// // -1.3 ms
322 /// let neg = Dt::from_ms(-1, us!(-300), Scale::TAI, Scale::TAI);
323 /// assert_eq!(neg.to_attos(), -1_300_000_000_000_000);
324 ///
325 /// // or as floored -1.3 ms
326 /// let neg = Dt::from_ms(-2, us!(700), Scale::TAI, Scale::TAI);
327 /// assert_eq!(neg.to_attos(), -1_300_000_000_000_000);
328 /// ```
329 ///
330 /// ## See also
331 ///
332 /// - [`from_ms!`](../macro.from_ms.html)
333 /// - [`us!`](../macro.us.html)
334 #[inline(always)]
335 pub const fn from_ms(ms: i128, attos: i128, on: Scale, target: Scale) -> Dt {
336 let attos = Dt::unit_to_total_attos(ms, attos, ATTOS_PER_MS_I128);
337 Dt::new(attos, on, target)
338 }
339
340 /// Builds a [`Dt`] holding the given whole microseconds and sub-microsecond remainder.
341 ///
342 /// The remainder is in **attoseconds**, not microseconds. Pairs with
343 /// [`to_us`](../struct.Dt.html#method.to_us).
344 ///
345 /// Does **not** perform any time scale conversions.
346 ///
347 /// ## Parameters
348 ///
349 /// - `us` — whole microseconds (truncating / signed-remainder split).
350 /// - `frac_attos` — fractional part of that split, in attoseconds.
351 /// Use a smaller-unit converter rather than counting zeros by hand:
352 /// - `1.3` µs → `us = 1`, `frac_attos = Dt::ns_to_attos(300)` (0.3 µs = 300 ns)
353 /// - `-1.3` µs → `us = -1`, `frac_attos = Dt::ns_to_attos(-300)`
354 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
355 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
356 ///
357 /// ## Examples
358 ///
359 /// ```rust
360 /// use deep_time::{TraitsAttos, Dt, Scale, ns};
361 ///
362 /// // 1.3 µs
363 /// let a = Dt::from_us(1, ns!(300), Scale::TAI, Scale::TAI);
364 /// let b = Dt::from_us(1, 300_i128.ns_to_attos(), Scale::TAI, Scale::TAI);
365 /// assert_eq!(a, b);
366 /// assert_eq!(a.to_attos(), 1_300_000_000_000);
367 ///
368 /// // -1.3 µs
369 /// let neg = Dt::from_us(-1, ns!(-300), Scale::TAI, Scale::TAI);
370 /// assert_eq!(neg.to_attos(), -1_300_000_000_000);
371 ///
372 /// // or as floored -1.3 µs
373 /// let neg = Dt::from_us(-2, ns!(700), Scale::TAI, Scale::TAI);
374 /// assert_eq!(neg.to_attos(), -1_300_000_000_000);
375 /// ```
376 ///
377 /// ## See also
378 ///
379 /// - [`from_us!`](../macro.from_us.html)
380 /// - [`ns!`](../macro.ns.html)
381 #[inline(always)]
382 pub const fn from_us(us: i128, attos: i128, on: Scale, target: Scale) -> Dt {
383 let attos = Dt::unit_to_total_attos(us, attos, ATTOS_PER_US_I128);
384 Dt::new(attos, on, target)
385 }
386
387 /// Builds a [`Dt`] holding the given whole nanoseconds and sub-nanosecond remainder.
388 ///
389 /// The remainder is in **attoseconds**, not nanoseconds. Pairs with
390 /// [`to_ns`](../struct.Dt.html#method.to_ns).
391 ///
392 /// Does **not** perform any time scale conversions.
393 ///
394 /// ## Parameters
395 ///
396 /// - `ns` — whole nanoseconds (truncating / signed-remainder split).
397 /// - `frac_attos` — fractional part of that split, in attoseconds.
398 /// Use a smaller-unit converter rather than counting zeros by hand:
399 /// - `1.3` ns → `ns = 1`, `frac_attos = Dt::ps_to_attos(300)` (0.3 ns = 300 ps)
400 /// - `-1.3` ns → `ns = -1`, `frac_attos = Dt::ps_to_attos(-300)`
401 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
402 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
403 ///
404 /// ## Examples
405 ///
406 /// ```rust
407 /// use deep_time::{TraitsAttos, Dt, Scale};
408 ///
409 /// // 1.3 ns → whole nanoseconds + 300 ps remainder
410 /// let a = Dt::from_ns(1, Dt::ps_to_attos(300), Scale::TAI, Scale::TAI);
411 /// let b = Dt::from_ns(1, 300_i128.ps_to_attos(), Scale::TAI, Scale::TAI);
412 /// assert_eq!(a, b);
413 /// assert_eq!(a.to_attos(), 1_300_000_000);
414 ///
415 /// // -1.3 ns
416 /// let neg = Dt::from_ns(-1, Dt::ps_to_attos(-300), Scale::TAI, Scale::TAI);
417 /// assert_eq!(neg.to_attos(), -1_300_000_000);
418 /// ```
419 #[inline(always)]
420 pub const fn from_ns(ns: i128, attos: i128, on: Scale, target: Scale) -> Dt {
421 let attos = Dt::unit_to_total_attos(ns, attos, ATTOS_PER_NS_I128);
422 Dt::new(attos, on, target)
423 }
424
425 /// Builds a [`Dt`] holding the given whole picoseconds and sub-picosecond remainder.
426 ///
427 /// The remainder is in **attoseconds**, not picoseconds. Pairs with
428 /// [`to_ps`](../struct.Dt.html#method.to_ps).
429 ///
430 /// Does **not** perform any time scale conversions.
431 ///
432 /// ## Parameters
433 ///
434 /// - `ps` — whole picoseconds (truncating / signed-remainder split).
435 /// - `frac_attos` — fractional part of that split, in attoseconds.
436 /// Use a smaller-unit converter rather than counting zeros by hand:
437 /// - `1.3` ps → `ps = 1`, `frac_attos = Dt::fs_to_attos(300)` (0.3 ps = 300 fs)
438 /// - `-1.3` ps → `ps = -1`, `frac_attos = Dt::fs_to_attos(-300)`
439 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
440 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
441 ///
442 /// ## Examples
443 ///
444 /// ```rust
445 /// use deep_time::{TraitsAttos, Dt, Scale};
446 ///
447 /// // 1.3 ps
448 /// let a = Dt::from_ps(1, Dt::fs_to_attos(300), Scale::TAI, Scale::TAI);
449 /// let b = Dt::from_ps(1, 300_i128.fs_to_attos(), Scale::TAI, Scale::TAI);
450 /// assert_eq!(a, b);
451 /// assert_eq!(a.to_attos(), 1_300_000);
452 ///
453 /// // -1.3 ps
454 /// let neg = Dt::from_ps(-1, Dt::fs_to_attos(-300), Scale::TAI, Scale::TAI);
455 /// assert_eq!(neg.to_attos(), -1_300_000);
456 /// ```
457 #[inline(always)]
458 pub const fn from_ps(ps: i128, attos: i128, on: Scale, target: Scale) -> Dt {
459 let attos = Dt::unit_to_total_attos(ps, attos, ATTOS_PER_PS_I128);
460 Dt::new(attos, on, target)
461 }
462
463 /// Builds a [`Dt`] holding the given whole femtoseconds and sub-femtosecond remainder.
464 ///
465 /// The remainder is in **attoseconds**, not femtoseconds. Pairs with
466 /// [`to_fs`](../struct.Dt.html#method.to_fs).
467 ///
468 /// Does **not** perform any time scale conversions.
469 ///
470 /// ## Parameters
471 ///
472 /// - `fs` — whole femtoseconds (truncating / signed-remainder split).
473 /// - `frac_attos` — fractional part of that split, in attoseconds.
474 /// One femtosecond is 1000 attoseconds, so a fractional remainder is already
475 /// a small integer: `1.3` fs → `fs = 1`, `frac_attos = 300`.
476 /// For `-1.3` fs: `fs = -1`, `frac_attos = -300`.
477 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
478 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
479 ///
480 /// ## Examples
481 ///
482 /// ```rust
483 /// use deep_time::{Dt, Scale};
484 ///
485 /// // 1.3 fs — sub-fs remainder is already in attoseconds (×10³)
486 /// let a = Dt::from_fs(1, 300, Scale::TAI, Scale::TAI);
487 /// assert_eq!(a.to_attos(), 1_300);
488 ///
489 /// // whole fs only — still fine to use the converter for the whole part
490 /// // if you are building total attos by hand:
491 /// assert_eq!(Dt::fs_to_attos(1), 1_000);
492 ///
493 /// // -1.3 fs
494 /// let neg = Dt::from_fs(-1, -300, Scale::TAI, Scale::TAI);
495 /// assert_eq!(neg.to_attos(), -1_300);
496 /// ```
497 #[inline(always)]
498 pub const fn from_fs(fs: i128, attos: i128, on: Scale, target: Scale) -> Dt {
499 let attos = Dt::unit_to_total_attos(fs, attos, ATTOS_PER_FS_I128);
500 Dt::new(attos, on, target)
501 }
502
503 /// Builds a [`Dt`] holding the given whole minutes and sub-minute remainder.
504 ///
505 /// The remainder is in **attoseconds**, not minutes.
506 ///
507 /// Does **not** perform any time scale conversions.
508 ///
509 /// ## Parameters
510 ///
511 /// - `n` — whole minutes (truncating / signed-remainder split).
512 /// - `frac_attos` — fractional part of that split, in attoseconds.
513 /// Use a time-unit converter rather than counting zeros by hand:
514 /// - `1.5` min → `n = 1`, `frac_attos = Dt::sec_to_attos(30)` (0.5 min = 30 s)
515 /// - `-1.5` min → `n = -1`, `frac_attos = Dt::sec_to_attos(-30)`
516 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
517 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
518 ///
519 /// ## Examples
520 ///
521 /// ```rust
522 /// use deep_time::{TraitsAttos, Dt, Scale};
523 ///
524 /// // 1.5 min
525 /// let a = Dt::from_mins(1, Dt::sec_to_attos(30), Scale::TAI, Scale::TAI);
526 /// let b = Dt::from_mins(1, 30_i128.sec_to_attos(), Scale::TAI, Scale::TAI);
527 /// assert_eq!(a, b);
528 /// assert_eq!(a.to_sec(), 90);
529 ///
530 /// // -1.5 min
531 /// let neg = Dt::from_mins(-1, Dt::sec_to_attos(-30), Scale::TAI, Scale::TAI);
532 /// assert_eq!(neg.to_sec(), -90);
533 /// ```
534 #[inline(always)]
535 pub const fn from_mins(n: i128, attos: i128, on: Scale, target: Scale) -> Dt {
536 let attos = Dt::unit_to_total_attos(n, attos, ATTOS_PER_MIN);
537 Dt::new(attos, on, target)
538 }
539
540 /// Builds a [`Dt`] holding the given whole hours and sub-hour remainder.
541 ///
542 /// The remainder is in **attoseconds**, not hours.
543 ///
544 /// Does **not** perform any time scale conversions.
545 ///
546 /// ## Parameters
547 ///
548 /// - `n` — whole hours (truncating / signed-remainder split).
549 /// - `frac_attos` — fractional part of that split, in attoseconds.
550 /// Use a time-unit converter rather than counting zeros by hand:
551 /// - `1.5` h → `n = 1`, `frac_attos = Dt::mins_to_attos(30)` (0.5 h = 30 min)
552 /// - `-1.5` h → `n = -1`, `frac_attos = Dt::mins_to_attos(-30)`
553 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
554 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
555 ///
556 /// ## Examples
557 ///
558 /// ```rust
559 /// use deep_time::{TraitsAttos, Dt, Scale};
560 ///
561 /// // 1.5 h
562 /// let a = Dt::from_hours(1, Dt::mins_to_attos(30), Scale::TAI, Scale::TAI);
563 /// let b = Dt::from_hours(1, 30_i128.mins_to_attos(), Scale::TAI, Scale::TAI);
564 /// assert_eq!(a, b);
565 /// assert_eq!(a.to_sec(), 5400);
566 ///
567 /// // -1.5 h
568 /// let neg = Dt::from_hours(-1, Dt::mins_to_attos(-30), Scale::TAI, Scale::TAI);
569 /// assert_eq!(neg.to_sec(), -5400);
570 /// ```
571 #[inline(always)]
572 pub const fn from_hours(n: i128, attos: i128, on: Scale, target: Scale) -> Dt {
573 let attos = Dt::unit_to_total_attos(n, attos, ATTOS_PER_HOUR);
574 Dt::new(attos, on, target)
575 }
576
577 /// Builds a [`Dt`] holding the given whole days and sub-day remainder.
578 ///
579 /// The remainder is in **attoseconds**, not days. Uses `86400` seconds per day.
580 ///
581 /// Does **not** perform any time scale conversions.
582 ///
583 /// ## Parameters
584 ///
585 /// - `d` — whole days (truncating / signed-remainder split).
586 /// - `frac` — fractional part in attoseconds (`frac.attos` only).
587 /// - `1.25` d → `d = 1`, `frac = dt!(Dt::hours_to_attos(6))` (0.25 d = 6 h)
588 /// - `-1.25` d → `d = -1`, `frac = dt!(Dt::hours_to_attos(-6))`
589 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
590 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
591 ///
592 /// ## Examples
593 ///
594 /// ```rust
595 /// use deep_time::{TraitsAttos, Dt, Scale, dt};
596 ///
597 /// // 1.25 d
598 /// let a = Dt::from_days(1, dt!(Dt::hours_to_attos(6)), Scale::TAI, Scale::TAI);
599 /// let b = Dt::from_days(1, dt!(6_i128.hours_to_attos()), Scale::TAI, Scale::TAI);
600 /// assert_eq!(a, b);
601 /// assert_eq!(a.to_sec(), 108_000); // 1.25 * 86400
602 ///
603 /// // -1.25 d
604 /// let neg = Dt::from_days(-1, dt!(Dt::hours_to_attos(-6)), Scale::TAI, Scale::TAI);
605 /// assert_eq!(neg.to_sec(), -108_000);
606 /// ```
607 #[inline(always)]
608 pub const fn from_days(d: i128, frac: Dt, on: Scale, target: Scale) -> Dt {
609 let attos = Dt::unit_to_total_attos(d, frac.attos, ATTOS_PER_DAY);
610 Dt::new(attos, on, target)
611 }
612
613 /// Builds a [`Dt`] from a floating-point day count since the library epoch
614 /// (2000-01-01 12:00:00 TAI).
615 ///
616 /// This is the inverse of [`Dt::to_days_f`](../struct.Dt.html#method.to_days_f).
617 ///
618 /// Does **not** perform any time scale conversions.
619 ///
620 /// ## Parameters
621 ///
622 /// - `days` — day count to store (converted to attoseconds).
623 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
624 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
625 ///
626 /// ## Examples
627 ///
628 /// ```rust
629 /// use deep_time::{Dt, Scale};
630 ///
631 /// let dt = Dt::from_days_f(1.25, Scale::TAI, Scale::TAI);
632 /// assert_eq!(dt.to_days_f(), 1.25);
633 ///
634 /// let neg = Dt::from_days_f(-1.25, Scale::TAI, Scale::TAI);
635 /// assert_eq!(neg.to_days_f(), -1.25);
636 /// ```
637 ///
638 /// ## See also
639 ///
640 /// - [`Dt::from_days`](../struct.Dt.html#method.from_days)
641 /// - [`Dt::to_days_f`](../struct.Dt.html#method.to_days_f)
642 #[inline]
643 pub const fn from_days_f(days: Real, on: Scale, target: Scale) -> Dt {
644 Self::from_sec_f(days * SEC_PER_DAY_F, on, target)
645 }
646
647 /// Builds a [`Dt`] holding the given number of weeks (`604800` seconds each).
648 ///
649 /// Does **not** perform any time scale conversions.
650 ///
651 /// ## Parameters
652 ///
653 /// - `n` — whole weeks.
654 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
655 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
656 #[inline(always)]
657 pub const fn from_weeks(n: i128, on: Scale, target: Scale) -> Dt {
658 Dt::new(
659 n.saturating_mul(SEC_PER_WEEK as i128)
660 .saturating_mul(ATTOS_PER_SEC_I128),
661 on,
662 target,
663 )
664 }
665
666 /// Builds a [`Dt`] holding the given number of Julian years (`31_557_600` seconds each).
667 ///
668 /// Does **not** perform any time scale conversions.
669 ///
670 /// ## Parameters
671 ///
672 /// - `n` — whole years.
673 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
674 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
675 #[inline(always)]
676 pub const fn from_years(n: i128, on: Scale, target: Scale) -> Dt {
677 Dt::new(
678 n.saturating_mul(31_557_600)
679 .saturating_mul(ATTOS_PER_SEC_I128),
680 on,
681 target,
682 )
683 }
684
685 /// Returns an instant that is this duration **before** zero attoseconds on `scale`.
686 ///
687 /// Zero attoseconds is the library epoch **2000-01-01 12:00:00** (see
688 /// [`Dt::ZERO`](../struct.Dt.html#associatedconstant.ZERO)).
689 ///
690 /// This method does **not** read the system clock.
691 ///
692 /// For wall-clock “N units ago”, use [`Dt::ago`](../struct.Dt.html#method.ago)
693 /// (requires `std`, or WASM with `js`).
694 ///
695 /// ## Examples
696 ///
697 /// ```rust
698 /// use deep_time::{Dt, Scale, TraitsTime};
699 ///
700 /// let t = 5.sec().before_zero(Scale::TAI);
701 /// assert_eq!(t, Dt::ZERO.sub(5.sec()));
702 /// assert_eq!(t.to_sec(), -5);
703 /// ```
704 ///
705 /// ## See also
706 ///
707 /// - [`Dt::ago`](../struct.Dt.html#method.ago)
708 #[inline(always)]
709 pub const fn before_zero(self, scale: Scale) -> Dt {
710 Dt::new(0, scale, scale).to_tai().sub(self)
711 }
712
713 /// Returns the negation of this [`Dt`].
714 #[inline(always)]
715 pub const fn neg(self) -> Dt {
716 Dt::new(-self.attos, self.scale, self.target)
717 }
718
719 /// Returns the positive of this [`Dt`].
720 #[inline(always)]
721 pub const fn abs(self) -> Dt {
722 Dt::new(self.attos.saturating_abs(), self.scale, self.target)
723 }
724
725 /// Builds a [`Dt`] holding the given floating-point seconds count.
726 ///
727 /// Does **not** perform any time scale conversions. The `sec` value is
728 /// stored as attoseconds only; its meaning depends on how you use the
729 /// result afterward.
730 ///
731 /// ## Parameters
732 ///
733 /// - `sec` — seconds count to store (`NaN` → zero attoseconds;
734 /// `±∞` → [`i128::MAX`] / [`i128::MIN`]).
735 /// - `on` — value stored in the returned [`Dt`]'s `scale` field.
736 /// - `target` — value stored in the returned [`Dt`]'s `target` field.
737 ///
738 /// ## Examples
739 ///
740 /// ```rust
741 /// use deep_time::{Dt, Scale};
742 ///
743 /// let seconds = 5.5;
744 /// let duration = Dt::from_sec_f(seconds, Scale::TAI, Scale::TAI);
745 ///
746 /// assert_eq!(duration.to_sec_f(), seconds);
747 /// ```
748 pub const fn from_sec_f(sec: Real, on: Scale, target: Scale) -> Dt {
749 if sec.is_nan() {
750 return Self::new(0, on, target);
751 } else if sec.is_infinite() {
752 return if sec.is_sign_positive() {
753 Self::new(i128::MAX, on, target)
754 } else {
755 Self::new(i128::MIN, on, target)
756 };
757 }
758 Dt::new(Self::sec_f_to_attos(sec), on, target)
759 }
760
761 /// High-precision conversion from [`Real`] seconds to total attoseconds (i128).
762 ///
763 /// - Uses IEEE 754 bit extraction + exact integer multiplication by 5^18.
764 /// - Returns the rounded integer (round-to-nearest, ties away from zero).
765 pub const fn sec_f_to_attos(sec: Real) -> i128 {
766 if sec == 0.0 {
767 return 0;
768 }
769
770 let bits = sec.to_bits();
771 let is_negative = (bits >> 63) != 0;
772 let biased_exp = ((bits >> 52) & 0x7ff) as i32;
773 let mantissa = bits & 0x000f_ffff_ffff_ffff;
774
775 let (sig, exp) = if biased_exp == 0 {
776 if mantissa == 0 {
777 return 0;
778 }
779 (mantissa as u128, -1022i32 - 52)
780 } else {
781 let sig = ((1u64 << 52) | mantissa) as u128;
782 (sig, biased_exp - 1023 - 52)
783 };
784
785 const FIVE_POW_18: u128 = 3_814_697_265_625; // 5^18 exactly
786 let product = sig * FIVE_POW_18;
787 let total_exp = exp + 18;
788
789 // Safe saturation / underflow guards (prevents invalid shifts >= 128)
790 if total_exp > 120 {
791 return if is_negative { i128::MIN } else { i128::MAX };
792 }
793 if total_exp < -97 {
794 return 0;
795 }
796
797 let abs_total = if total_exp >= 0 {
798 let shift = total_exp as u32;
799 if product > (u128::MAX >> shift) {
800 if is_negative { i128::MIN } else { i128::MAX }
801 } else {
802 let shifted = product << shift;
803 if shifted > i128::MAX as u128 {
804 if is_negative { i128::MIN } else { i128::MAX }
805 } else {
806 shifted as i128
807 }
808 }
809 } else {
810 let shift = (-total_exp) as u32;
811 let int_part = (product >> shift) as i128;
812
813 // Round to nearest, half away from zero (on the absolute value)
814 let mask = (1u128 << shift) - 1;
815 let rem = product & mask;
816 if rem > (mask >> 1) {
817 int_part + 1
818 } else {
819 int_part
820 }
821 };
822
823 if is_negative { -abs_total } else { abs_total }
824 }
825
826 /// Returns the current system time as TAI from 2000-01-01 12:00:00.
827 ///
828 /// This method is only available when the `std` feature is enabled and the target
829 /// is not WASM with the `js` feature.
830 #[cfg(all(feature = "std", not(all(target_arch = "wasm32", feature = "js"))))]
831 pub fn now() -> Dt {
832 let now = std::time::SystemTime::now();
833
834 let (sec, nanos): (i64, i64) = match now.duration_since(std::time::UNIX_EPOCH) {
835 Ok(dur) => (dur.as_secs() as i64, dur.subsec_nanos() as i64),
836 Err(e) => {
837 let dur = e.duration();
838 (-(dur.as_secs() as i64), -(dur.subsec_nanos() as i64))
839 }
840 };
841
842 Dt::from_diff_and_scale(
843 Dt::new(Dt::sec_to_attos(sec as i128), Scale::TAI, Scale::UTC),
844 Dt::UNIX_EPOCH,
845 false,
846 )
847 .add(Dt::from_ns(nanos as i128, 0, Scale::TAI, Scale::TAI))
848 }
849
850 /// Returns the current system time as TAI from 2000-01-01 12:00:00.
851 /// (browser WASM version using JavaScript’s `Date.now()`).
852 #[cfg(all(target_arch = "wasm32", feature = "js"))]
853 pub fn now() -> Dt {
854 let ms: f64 = js_sys::Date::now();
855 let sec = (ms / 1000.0).floor() as i128;
856 let nanos = ((ms % 1000.0) * 1_000_000.0) as i128;
857 Dt::from_diff_and_scale(
858 Dt::new(Dt::sec_to_attos(sec), Scale::TAI, Scale::UTC),
859 Dt::UNIX_EPOCH,
860 false,
861 )
862 .add(Dt::from_ns(nanos as i128, 0, Scale::TAI, Scale::TAI))
863 }
864
865 /// Returns an instant that is this duration **before** the current system time.
866 ///
867 /// Subtracts `self` from [`Dt::now`](../struct.Dt.html#method.now). Available under
868 /// the same conditions as that method: the `std` feature (non-WASM-js), or WASM with
869 /// the `js` feature.
870 ///
871 /// For a `const` offset from the library epoch (no system clock), use
872 /// [`Dt::before_zero`](../struct.Dt.html#method.before_zero).
873 ///
874 /// ## Examples
875 ///
876 /// ```rust
877 /// # #[cfg(feature = "std")]
878 /// # {
879 /// use deep_time::{Dt, TraitsTime};
880 ///
881 /// // ~3 days in the past relative to the system clock
882 /// let past = 3.days().ago();
883 /// assert!(past < Dt::now());
884 /// # }
885 /// ```
886 ///
887 /// ## See also
888 ///
889 /// - [`Dt::from_now`](../struct.Dt.html#method.from_now)
890 /// - [`Dt::before_zero`](../struct.Dt.html#method.before_zero)
891 /// - [`Dt::now`](../struct.Dt.html#method.now)
892 #[cfg(any(
893 all(feature = "std", not(all(target_arch = "wasm32", feature = "js"))),
894 all(target_arch = "wasm32", feature = "js"),
895 ))]
896 #[inline(always)]
897 pub fn ago(self) -> Dt {
898 Dt::now().sub(self)
899 }
900
901 /// Returns an instant that is this duration **after** the current system time.
902 ///
903 /// Adds `self` to [`Dt::now`](../struct.Dt.html#method.now). Available under the same
904 /// conditions as that method: the `std` feature (non-WASM-js), or WASM with the `js`
905 /// feature.
906 ///
907 /// ## Examples
908 ///
909 /// ```rust
910 /// # #[cfg(feature = "std")]
911 /// # {
912 /// use deep_time::{Dt, TraitsTime};
913 ///
914 /// // ~3 days in the future relative to the system clock
915 /// let future = 3.days().from_now();
916 /// assert!(future > Dt::now());
917 /// # }
918 /// ```
919 ///
920 /// ## See also
921 ///
922 /// - [`Dt::ago`](../struct.Dt.html#method.ago)
923 /// - [`Dt::now`](../struct.Dt.html#method.now)
924 #[cfg(any(
925 all(feature = "std", not(all(target_arch = "wasm32", feature = "js"))),
926 all(target_arch = "wasm32", feature = "js"),
927 ))]
928 #[inline(always)]
929 pub fn from_now(self) -> Dt {
930 Dt::now().add(self)
931 }
932}