deep_time/dt/constructors.rs
1use crate::{
2 ATTOS_PER_FS, ATTOS_PER_MS, ATTOS_PER_NS, ATTOS_PER_PS, ATTOS_PER_SEC_I128, ATTOS_PER_US, Dt,
3 Real, SEC_PER_DAYI64, SEC_PER_DAYI128, SEC_PER_WEEK, Scale,
4 TAI_SECS_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 [`Self::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_SECS_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 [`Self::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 =
36 Self::new(-3155716800000000000000000000i128, Scale::TAI, Scale::TAI);
37
38 /// TT/TCG/TCB/TDB epoch.
39 ///
40 /// - 1977-01-01 00:00:00 TAI.
41 /// - Stored here on the **TAI** timescale as an offset from [`Self::ZERO`].
42 /// - -725_803_200_000_000_000_000_000_000 attoseconds
43 /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
44 pub const TAI_1977_EPOCH: Self =
45 Self::new(-725803200000000000000000000i128, Scale::TAI, Scale::TAI);
46
47 /// Chandra X-ray Center (CXC) Time epoch.
48 ///
49 /// - 1998-01-01 00:00:00 TT.
50 /// - Stored here on the **TAI** timescale as an offset from [`Self::ZERO`].
51 /// - -63_115_232_184_000_000_000_000_000_000 attoseconds
52 /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
53 pub const CXC_EPOCH: Self = Self::new(-63115232184000000000000000i128, Scale::TAI, Scale::TT);
54
55 /// GPS/Galileo Experiment (GALEX) Time epoch.
56 ///
57 /// - 1980-01-06 00:00:00 UTC.
58 /// - Stored here on the **TAI** timescale as an offset from [`Self::ZERO`].
59 /// - -630_763_181_000_000_000_000_000_000 attoseconds
60 /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
61 pub const GPS_EPOCH: Self = Self::new(-630763181000000000000000000i128, Scale::TAI, Scale::GPS);
62
63 /// Galileo System Time (GST) epoch.
64 ///
65 /// - 1999-08-22 00:00:00 GST.
66 /// - Stored here on the **TAI** timescale as an offset from [`Self::ZERO`].
67 /// - -11_447_981_000_000_000_000_000_000 attoseconds
68 /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
69 pub const GALILEO_EPOCH: Self =
70 Self::new(-11447981000000000000000000i128, Scale::TAI, Scale::GST);
71
72 /// BeiDou Time (BDT) epoch.
73 ///
74 /// - 2006-01-01 00:00:00 UTC.
75 /// - Stored here on the **TAI** timescale as an offset from [`Self::ZERO`].
76 /// - 189_345_633_000_000_000_000_000_000 attoseconds
77 /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
78 pub const BDT_EPOCH: Self = Self::new(189345633000000000000000000i128, Scale::TAI, Scale::BDT);
79
80 /// CCSDS epoch (used in CCSDS time codes such as CUC).
81 ///
82 /// - 1958-01-01 00:00:00 TAI.
83 /// - Stored here on the **TAI** timescale as an offset from [`Self::ZERO`].
84 /// - -1_325_419_200_000_000_000_000_000_000 attoseconds
85 /// - The library's epoch for time scales during conversions is 2000-01-01 12:00:00.
86 pub const CCSDS_EPOCH: Self = Self::new(
87 -1_325_419_200_000_000_000_000_000_000i128,
88 Scale::TAI,
89 Scale::TAI,
90 );
91
92 /// Maximum representable duration.
93 pub const MAX: Self = Self::new(i128::MAX, Scale::TAI, Scale::TAI);
94
95 /// Minimum (most negative) representable duration.
96 pub const MIN: Self = Self::new(i128::MIN, Scale::TAI, Scale::TAI);
97
98 /// 19 seconds.
99 pub const SEC_19: Self = Self::new(19i128 * ATTOS_PER_SEC_I128, Scale::TAI, Scale::TAI);
100
101 /// 33 seconds.
102 pub const SEC_33: Self = Self::new(33i128 * ATTOS_PER_SEC_I128, Scale::TAI, Scale::TAI);
103
104 /// 37 seconds.
105 pub const SEC_37: Self = Self::new(37i128 * ATTOS_PER_SEC_I128, Scale::TAI, Scale::TAI);
106
107 /// One days worth of attoseconds.
108 pub const ONE_DAY: Self = Self::new(
109 (SEC_PER_DAYI64 as i128) * ATTOS_PER_SEC_I128,
110 Scale::TAI,
111 Scale::TAI,
112 );
113
114 /// Creates a new [`Dt`] from a total number of attoseconds since the librarys
115 /// epoch **2000-01-01 12:00:00 TAI**.
116 ///
117 /// Does **not** perform any time scale conversions.
118 ///
119 /// ## Examples
120 ///
121 /// ```rust
122 /// use deep_time::{Dt, Scale};
123 ///
124 /// // current scale TAI, target scale UTC
125 /// let a = Dt::new(0, Scale::TAI, Scale::UTC);
126 ///
127 /// // equivalent to direct construction
128 /// let b = Dt { attos: 0, scale: Scale::TAI, target: Scale::UTC };
129 ///
130 /// assert_eq!(a, b);
131 /// ```
132 #[inline(always)]
133 pub const fn new(attos: i128, scale: Scale, target: Scale) -> Dt {
134 Self {
135 attos,
136 scale,
137 target,
138 }
139 }
140
141 /// Creates a new [`Dt`] from a total number of seconds since the librarys
142 /// epoch **2000-01-01 12:00:00 TAI**.
143 ///
144 /// Does **not** perform any time scale conversions.
145 #[inline(always)]
146 pub const fn new_sec(sec: i128, scale: Scale, target: Scale) -> Dt {
147 Self {
148 attos: Dt::sec_to_attos(sec),
149 scale,
150 target,
151 }
152 }
153
154 /// Creates a new [`Dt`] from a total number of seconds as a float
155 /// since the librarys epoch **2000-01-01 12:00:00 TAI**.
156 ///
157 /// - Does **not** perform any time scale conversions.
158 /// - Fractional seconds represented by any decimals.
159 #[inline(always)]
160 pub const fn new_f(sec: Real, scale: Scale, target: Scale) -> Dt {
161 Self {
162 attos: Dt::sec_f_to_attos(sec),
163 scale,
164 target,
165 }
166 }
167
168 /// Creates a new [`Dt`] from a total number of attoseconds (signed i128) without
169 /// performing any time scale conversions.
170 ///
171 /// - This is an easy way to create a duration.
172 /// - The returned [`Dt`] has its `scale` and `target` fields set to
173 /// `Scale::TAI`.
174 #[inline(always)]
175 pub const fn span(attos: i128) -> Dt {
176 Dt::new(attos, Scale::TAI, Scale::TAI)
177 }
178
179 /// Creates a [`Dt`] from a floating-point number of seconds without performing
180 /// any time scale conversions.
181 ///
182 /// - This is an easy way to create a duration or a seconds count that doesn't
183 /// include any time scale conversions, just holds the seconds count as is.
184 /// - The returned [`Dt`] has its `scale` and `target` fields set to
185 /// `Scale::TAI`.
186 #[inline(always)]
187 pub const fn span_f(sec: Real) -> Dt {
188 Self::from_sec_f(sec, Scale::TAI)
189 }
190
191 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
192 /// the given `scale`.
193 ///
194 /// - Requires a seconds and attoseconds count such that would be returned from the
195 /// functions [`Dt::to_sec64`](../struct.Dt.html#method.to_sec64) and
196 /// [`Dt::to_sec_ufrac`](../struct.Dt.html#method.to_sec_ufrac).
197 /// - The returned object's `scale` field is set to TAI and its `target` field is set to
198 /// the given `scale` arg.
199 /// - The `sec` should be from the epoch TAI 2000-01-01 12:00:00.
200 ///
201 /// This function performs a time scale conversion from the given `scale` to **TAI**,
202 /// if you don't want any time scale conversion to take place then either use
203 /// `Scale::TAI` as an arg or use any of the following constructors:
204 ///
205 /// - [`Dt::new`](../struct.Dt.html#method.new)
206 /// - [`Dt::new_sec`](../struct.Dt.html#method.new_sec)
207 /// - [`Dt::new_f`](../struct.Dt.html#method.new_f)
208 /// - [`Dt::span`](../struct.Dt.html#method.span)
209 /// - [`Dt::span_f`](../struct.Dt.html#method.span_f)
210 /// - [`Dt::from_tai_sec`](../struct.Dt.html#method.from_tai_sec)
211 #[inline(always)]
212 pub(crate) fn from_sec_and_attos(sec: i64, attos: u64, scale: Scale) -> Dt {
213 if attos == 0 {
214 Dt::from_attos((sec as i128) * ATTOS_PER_SEC_I128, scale)
215 } else {
216 Dt::from_attos((sec as i128) * ATTOS_PER_SEC_I128 + (attos as i128), scale)
217 }
218 }
219
220 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
221 /// the given `scale`.
222 ///
223 /// - Requires a total attoseconds value.
224 /// - The value should be from the epoch TAI 2000-01-01 12:00:00.
225 /// - The returned object's `scale` field is set to TAI and its `target` field is set to
226 /// the given `scale` arg.
227 ///
228 /// This function performs a time scale conversion from the given `scale` to **TAI**,
229 /// if you don't want any time scale conversion to take place then either use
230 /// `Scale::TAI` as an arg or use any of the following constructors:
231 ///
232 /// - [`Dt::new`](../struct.Dt.html#method.new)
233 /// - [`Dt::new_sec`](../struct.Dt.html#method.new_sec)
234 /// - [`Dt::new_f`](../struct.Dt.html#method.new_f)
235 /// - [`Dt::span`](../struct.Dt.html#method.span)
236 /// - [`Dt::span_f`](../struct.Dt.html#method.span_f)
237 /// - [`Dt::from_tai_sec`](../struct.Dt.html#method.from_tai_sec)
238 #[inline(always)]
239 pub const fn from_attos(attos: i128, current: Scale) -> Dt {
240 Dt::new(attos, current, current).to_tai()
241 }
242
243 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
244 /// the given `scale`.
245 ///
246 /// - Requires a total attoseconds value.
247 /// - The value should be from the epoch TAI 2000-01-01 12:00:00.
248 /// - The returned object's `scale` field is set to TAI and its `target` field is set to
249 /// the given `scale` arg.
250 ///
251 /// This function performs a time scale conversion from the given `scale` to **TAI**,
252 /// if you don't want any time scale conversion to take place then either use
253 /// `Scale::TAI` as an arg or use any of the following constructors:
254 ///
255 /// - [`Dt::new`](../struct.Dt.html#method.new)
256 /// - [`Dt::new_sec`](../struct.Dt.html#method.new_sec)
257 /// - [`Dt::new_f`](../struct.Dt.html#method.new_f)
258 /// - [`Dt::span`](../struct.Dt.html#method.span)
259 /// - [`Dt::span_f`](../struct.Dt.html#method.span_f)
260 /// - [`Dt::from_tai_sec`](../struct.Dt.html#method.from_tai_sec)
261 #[inline(always)]
262 pub const fn from_attos_with_target(attos: i128, current: Scale, target: Scale) -> Dt {
263 Dt::new(attos, current, target).to_tai()
264 }
265
266 /// Creates a new [`Dt`] from a total number of seconds (signed i128) without
267 /// performing any time scale conversions.
268 #[inline(always)]
269 pub const fn from_tai_sec(sec: i128) -> Dt {
270 Self::from_attos(sec.saturating_mul(ATTOS_PER_SEC_I128), Scale::TAI)
271 }
272
273 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
274 /// the given `scale`.
275 ///
276 /// - Requires a total seconds value.
277 /// - The value should be from the epoch TAI 2000-01-01 12:00:00.
278 /// - The returned object's `scale` field is set to TAI and its `target` field is set to
279 /// the given `scale` arg.
280 ///
281 /// This function performs a time scale conversion from the given `scale` to **TAI**,
282 /// if you don't want any time scale conversion to take place then either use
283 /// `Scale::TAI` as an arg or use any of the following constructors:
284 ///
285 /// - [`Dt::new`](../struct.Dt.html#method.new)
286 /// - [`Dt::new_sec`](../struct.Dt.html#method.new_sec)
287 /// - [`Dt::new_f`](../struct.Dt.html#method.new_f)
288 /// - [`Dt::span`](../struct.Dt.html#method.span)
289 /// - [`Dt::span_f`](../struct.Dt.html#method.span_f)
290 /// - [`Dt::from_tai_sec`](../struct.Dt.html#method.from_tai_sec)
291 #[inline(always)]
292 pub const fn from_sec(sec: i128, scale: Scale) -> Dt {
293 Self::from_attos(sec.saturating_mul(ATTOS_PER_SEC_I128), scale)
294 }
295
296 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
297 /// the given `scale`.
298 ///
299 /// - Requires a total milliseconds value.
300 /// - The value should be from the epoch TAI 2000-01-01 12:00:00.
301 /// - The returned object's `scale` field is set to TAI and its `target` field is set to
302 /// the given `scale` arg.
303 #[inline(always)]
304 pub const fn from_ms(ms: i128, scale: Scale) -> Dt {
305 let attos = ms.saturating_mul(ATTOS_PER_MS as i128);
306 Self::from_attos(attos, scale)
307 }
308
309 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
310 /// the given `scale`.
311 ///
312 /// - Requires a total microseconds value.
313 /// - The value should be from the epoch TAI 2000-01-01 12:00:00.
314 /// - The returned object's `scale` field is set to TAI and its `target` field is set to
315 /// the given `scale` arg.
316 #[inline(always)]
317 pub const fn from_us(us: i128, scale: Scale) -> Dt {
318 let attos = us.saturating_mul(ATTOS_PER_US as i128);
319 Self::from_attos(attos, scale)
320 }
321
322 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
323 /// the given `scale`.
324 ///
325 /// - Requires a total nanoseconds value.
326 /// - The value should be from the epoch TAI 2000-01-01 12:00:00.
327 /// - The returned object's `scale` field is set to TAI and its `target` field is set to
328 /// the given `scale` arg.
329 #[inline(always)]
330 pub const fn from_ns(ns: i128, scale: Scale) -> Dt {
331 let attos = ns.saturating_mul(ATTOS_PER_NS as i128);
332 Self::from_attos(attos, scale)
333 }
334
335 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
336 /// the given `scale`.
337 ///
338 /// - Requires a total picoseconds value.
339 /// - The value should be from the epoch TAI 2000-01-01 12:00:00.
340 /// - The returned object's `scale` field is set to TAI and its `target` field is set to
341 /// the given `scale` arg.
342 #[inline(always)]
343 pub const fn from_ps(ps: i128, scale: Scale) -> Dt {
344 let attos = ps.saturating_mul(ATTOS_PER_PS as i128);
345 Self::from_attos(attos, scale)
346 }
347
348 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
349 /// the given `scale`.
350 ///
351 /// - Requires a total femtoseconds value.
352 /// - The value should be from the epoch TAI 2000-01-01 12:00:00.
353 /// - The returned object's `scale` field is set to TAI and its `target` field is set to
354 /// the given `scale` arg.
355 #[inline(always)]
356 pub const fn from_fs(fs: i128, scale: Scale) -> Dt {
357 let attos = fs.saturating_mul(ATTOS_PER_FS as i128);
358 Self::from_attos(attos, scale)
359 }
360
361 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
362 /// the given `scale`.
363 ///
364 /// Convenience wrapper around
365 /// [`Dt::from_sec`](../struct.Dt.html#method.from_sec).
366 #[inline(always)]
367 pub const fn from_min(m: i64, scale: Scale) -> Dt {
368 Self::from_sec((m as i128) * 60, scale)
369 }
370
371 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
372 /// the given `scale`.
373 ///
374 /// Convenience wrapper around
375 /// [`Dt::from_sec`](../struct.Dt.html#method.from_sec).
376 #[inline(always)]
377 pub const fn from_hr(h: i64, scale: Scale) -> Dt {
378 Self::from_sec((h as i128) * 3600, scale)
379 }
380
381 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
382 /// the given `scale`.
383 ///
384 /// - Params are hours, minutes, seconds, milliseconds, microseconds, and nanoseconds.
385 /// - All values are essentially optional (you can use 0 for ones you want to leave out).
386 /// - Negative values are handled.
387 /// - Uses saturating arithmetic.
388 pub const fn from_hms(
389 hr: i64,
390 min: i64,
391 sec: i64,
392 ms: i128,
393 us: i128,
394 ns: i128,
395 scale: Scale,
396 ) -> Dt {
397 // Combine hours/minutes/seconds with saturating arithmetic
398 let total_sec: i128 = (hr as i128)
399 .saturating_mul(3600)
400 .saturating_add((min as i128).saturating_mul(60))
401 .saturating_add(sec as i128);
402
403 // Combine sub-second parts (nanoseconds) with saturating arithmetic
404 let sub_ns: i128 = ms
405 .saturating_mul(1_000_000)
406 .saturating_add(us.saturating_mul(1_000))
407 .saturating_add(ns);
408
409 if sub_ns == 0 {
410 return Self::from_sec(total_sec, scale);
411 }
412
413 // Handle carry/borrow from sub-second component
414 let abs_ns: u128 = sub_ns.unsigned_abs();
415 let extra_sec: i128 = (abs_ns / 1_000_000_000) as i128;
416 let rem_ns: u64 = (abs_ns % 1_000_000_000) as u64;
417 let frac_attos: u128 = (rem_ns as u128) * (ATTOS_PER_NS as u128);
418
419 let attos = if sub_ns >= 0 {
420 total_sec
421 .saturating_add(extra_sec)
422 .saturating_mul(ATTOS_PER_SEC_I128)
423 .saturating_add(frac_attos as i128)
424 } else if frac_attos == 0 {
425 total_sec
426 .saturating_sub(extra_sec)
427 .saturating_mul(ATTOS_PER_SEC_I128)
428 } else {
429 total_sec
430 .saturating_sub(extra_sec)
431 .saturating_sub(1)
432 .saturating_mul(ATTOS_PER_SEC_I128)
433 .saturating_add(ATTOS_PER_SEC_I128 - frac_attos as i128)
434 };
435
436 Self::from_attos(attos, scale)
437 }
438
439 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
440 /// the given `scale`.
441 ///
442 /// - Convenience wrapper around
443 /// [`Dt::from_sec`](../struct.Dt.html#method.from_sec).
444 /// - Uses `86400` seconds per day in the calculation.
445 #[inline(always)]
446 pub const fn from_days(d: i64, scale: Scale) -> Dt {
447 Self::from_sec((d as i128).saturating_mul(SEC_PER_DAYI128), scale)
448 }
449
450 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
451 /// the given `scale`.
452 ///
453 /// - Convenience wrapper around
454 /// [`Dt::from_sec`](../struct.Dt.html#method.from_sec).
455 /// - Uses `604800` seconds per week in the calculation.
456 #[inline(always)]
457 pub const fn from_wk(wk: i64, scale: Scale) -> Dt {
458 Dt::from_sec((wk as i128).saturating_mul(SEC_PER_WEEK as i128), scale)
459 }
460
461 /// Returns a [`Dt`] on the TAI time scale, after having been **converted** to TAI from
462 /// the given `scale`.
463 ///
464 /// - Convenience wrapper around
465 /// [`Dt::from_sec`](../struct.Dt.html#method.from_sec).
466 /// - Uses `31_557_600` in the calculation.
467 #[inline(always)]
468 pub const fn from_yr(yr: i64, scale: Scale) -> Dt {
469 Dt::from_sec((yr as i128).saturating_mul(31_557_600), scale)
470 }
471
472 /// Returns a [`Dt`] that is this duration ago from the given scale.
473 #[inline(always)]
474 pub const fn ago(self, scale: Scale) -> Dt {
475 Dt::from_attos(0, scale).sub(self)
476 }
477
478 /// Returns a [`Dt`] that is this duration from now in the given scale.
479 #[inline(always)]
480 pub const fn from_now(self, scale: Scale) -> Dt {
481 Dt::from_attos(0, scale).add(self)
482 }
483
484 /// Returns the negation of this [`Dt`].
485 #[inline(always)]
486 pub const fn neg(self) -> Dt {
487 Dt::new(-self.attos, self.scale, self.target)
488 }
489
490 /// Returns the positive of this [`Dt`].
491 #[inline(always)]
492 pub const fn abs(self) -> Dt {
493 Dt::new(self.attos.saturating_abs(), self.scale, self.target)
494 }
495
496 /// Creates a [`Dt`] from a floating-point number of seconds.
497 ///
498 /// - Assumes the value is on the given scale.
499 /// - Converts the values to TAI from the given `scale`.
500 /// - The returned [`Dt`] is on the TAI time scale.
501 #[inline]
502 pub const fn from_sec_f(sec: Real, scale: Scale) -> Dt {
503 if sec.is_nan() {
504 return Self::ZERO;
505 } else if sec.is_infinite() {
506 return if sec.is_sign_positive() {
507 Self::MAX
508 } else {
509 Self::MIN
510 };
511 }
512 Self::from_attos(Self::sec_f_to_attos(sec), scale)
513 }
514
515 /// High-precision conversion from [`Real`] seconds to total attoseconds (i128).
516 ///
517 /// - Uses IEEE 754 bit extraction + exact integer multiplication by 5^18.
518 /// - Returns the rounded integer (round-to-nearest, ties away from zero).
519 pub const fn sec_f_to_attos(sec: Real) -> i128 {
520 if sec == 0.0 {
521 return 0;
522 }
523
524 let bits = sec.to_bits();
525 let is_negative = (bits >> 63) != 0;
526 let biased_exp = ((bits >> 52) & 0x7ff) as i32;
527 let mantissa = bits & 0x000f_ffff_ffff_ffff;
528
529 let (sig, exp) = if biased_exp == 0 {
530 if mantissa == 0 {
531 return 0;
532 }
533 (mantissa as u128, -1022i32 - 52)
534 } else {
535 let sig = ((1u64 << 52) | mantissa) as u128;
536 (sig, biased_exp - 1023 - 52)
537 };
538
539 const FIVE_POW_18: u128 = 3_814_697_265_625; // 5^18 exactly
540 let product = sig * FIVE_POW_18;
541 let total_exp = exp + 18;
542
543 // Safe saturation / underflow guards (prevents invalid shifts >= 128)
544 if total_exp > 120 {
545 return if is_negative { i128::MIN } else { i128::MAX };
546 }
547 if total_exp < -97 {
548 return 0;
549 }
550
551 let abs_total = if total_exp >= 0 {
552 let shift = total_exp as u32;
553 if product > (u128::MAX >> shift) {
554 if is_negative { i128::MIN } else { i128::MAX }
555 } else {
556 let shifted = product << shift;
557 if shifted > i128::MAX as u128 {
558 if is_negative { i128::MIN } else { i128::MAX }
559 } else {
560 shifted as i128
561 }
562 }
563 } else {
564 let shift = (-total_exp) as u32;
565 let int_part = (product >> shift) as i128;
566
567 // Round to nearest, half away from zero (on the absolute value)
568 let mask = (1u128 << shift) - 1;
569 let rem = product & mask;
570 if rem > (mask >> 1) {
571 int_part + 1
572 } else {
573 int_part
574 }
575 };
576
577 if is_negative { -abs_total } else { abs_total }
578 }
579
580 /// Returns the current system time as TAI from 2000-01-01 12:00:00.
581 ///
582 /// This method is only available when the `std` feature is enabled and the target
583 /// is not WASM with the `js` feature.
584 #[cfg(all(feature = "std", not(all(target_arch = "wasm32", feature = "js"))))]
585 pub fn now() -> Dt {
586 let now = std::time::SystemTime::now();
587
588 let (secs, nanos): (i64, i64) = match now.duration_since(std::time::UNIX_EPOCH) {
589 Ok(dur) => (dur.as_secs() as i64, dur.subsec_nanos() as i64),
590 Err(e) => {
591 let dur = e.duration();
592 (-(dur.as_secs() as i64), -(dur.subsec_nanos() as i64))
593 }
594 };
595
596 Dt::from_diff_and_scale(
597 Dt::new(Dt::sec_to_attos(secs as i128), Scale::TAI, Scale::UTC),
598 Dt::UNIX_EPOCH,
599 false,
600 )
601 .add(Dt::from_ns(nanos as i128, Scale::TAI))
602 }
603
604 /// Returns the current system time as TAI from 2000-01-01 12:00:00.
605 /// (browser WASM version using JavaScript’s `Date.now()`).
606 #[cfg(all(target_arch = "wasm32", feature = "js"))]
607 pub fn now() -> Dt {
608 let ms: f64 = js_sys::Date::now();
609 let secs = (ms / 1000.0).floor() as i128;
610 let nanos = ((ms % 1000.0) * 1_000_000.0) as i128;
611 Dt::from_diff_and_scale(
612 Dt::new(Dt::sec_to_attos(secs), Scale::TAI, Scale::UTC),
613 Dt::UNIX_EPOCH,
614 false,
615 )
616 .add(Dt::from_ns(nanos as i128, Scale::TAI))
617 }
618}