deep-time 0.1.0-beta.21

High-precision, no-std, no-alloc date-time library, leap-seconds, time scales, relativistic time, and a powerful date & duration parser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! [`Dt`](../struct.Dt.html) impls for leap-second lookup and UTC↔TAI conversion.
//!
//! Uses the built-in [`LEAP_SECS`] list or a caller-provided [`LeapSec`] slice.
//! [`LeapInfo`] is returned by [`Dt::leap_sec`](../struct.Dt.html#method.leap_sec) and related methods.

use crate::utc::leap_seconds_list::{LEAP_SECS, LeapSec};
use crate::{Dt, Scale};

#[cfg(feature = "std")]
use std::{fs, io, path::Path};

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

/// Indicates whether a queried instant falls exactly on a leap second transition.
///
/// This is returned by [`LeapInfo::is_leap_sec`] and is only ever set to a
/// non-`None` value when the queried timestamp is *exactly* at the moment
/// a leap second is inserted or removed.
///
/// - [`IsLeapSec::Add`] is returned for the inserted leap second
///   (e.g. `23:59:60`).
/// - [`IsLeapSec::Sub`] is returned for a negative (subtracted) leap second.
/// - [`IsLeapSec::None`] is returned for all normal seconds.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum IsLeapSec {
    /// This instant is **not** a leap second.
    #[default]
    None,
    /// This instant is a positive leap second (a second is being inserted).
    ///
    /// Example: `2015-06-30 23:59:60 UTC`.
    Add,
    /// This instant is a negative leap second (a second is being removed).
    ///
    /// Perhaps this would work by having clocks skip the 59th second of a
    /// minute, e.g. `2015-06-30 23:59:58 UTC` → `2015-07-01 00:00:00 UTC`.
    Sub,
}

/// Leap-second details for an instant, returned by [`Dt::leap_sec`](../struct.Dt.html#method.leap_sec)
/// and related methods.
///
/// ## See also
///
/// - [Dt::leap_sec](../struct.Dt.html#method.leap_sec)
/// - [Dt::leap_sec_using_list](../struct.Dt.html#method.leap_sec_using_list)
/// - [Dt::leap_sec_using_sec64](../struct.Dt.html#method.leap_sec_using_sec64)
/// - [Dt::leap_sec_using_sec64_and_list](../struct.Dt.html#method.leap_sec_using_sec64_and_list)
/// - [Dt::leap_sec_list_from_str](../struct.Dt.html#method.leap_sec_list_from_str)
/// - [Dt::leap_sec_list_from_file](../struct.Dt.html#method.leap_sec_list_from_file)
/// - [Dt::to_tai_from_utc_using_list](../struct.Dt.html#method.to_tai_from_utc_using_list)
/// - [Dt::to_utc_from_tai_using_list](../struct.Dt.html#method.to_utc_from_tai_using_list)
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct LeapInfo {
    /// TAI minus UTC offset, in whole seconds.
    pub offset: i64,
    /// How many leap-second list entries come at or before the instant
    /// (`1` on 1972-01-01).
    pub n_entries_at_or_before: usize,
    /// Whether the queried instant is exactly at a leap second transition point.
    ///
    /// - [`IsLeapSec::Add`] — this is the inserted leap second (`23:59:60`).
    /// - [`IsLeapSec::Sub`] — this is a negative (removed) leap second.
    /// - [`IsLeapSec::None`] — normal second (most common).
    pub is_leap_sec: IsLeapSec,
}

impl Dt {
    /// Get [`LeapInfo`] for a particular library timestamp (seconds from 2000-01-01 noon TAI)
    /// using a provided leap seconds list.
    ///
    /// - If the timestamp is currently on the UTC time scale then use **`true`** for the `is_utc`
    ///   parameter.
    /// - If the timestamp is currently on the TAI time scale then use **`false`** for the `is_utc`
    ///   parameter.
    /// - `sec64` should be such that it was produced using euclid division, see
    ///   [`Dt::to_sec64`](../struct.Dt.html#method.to_sec64) for more info. This only applies to
    ///   negative `sec64` values.
    ///
    /// ## See also
    ///
    /// For more information on how to make a leap seconds list, see the following functions:
    ///
    /// - [Dt::leap_sec_list_from_str](../struct.Dt.html#method.leap_sec_list_from_str)
    /// - [Dt::leap_sec_list_from_file](../struct.Dt.html#method.leap_sec_list_from_file)
    /// - [Dt::to_tai_from_utc_using_list](../struct.Dt.html#method.to_tai_from_utc_using_list)
    /// - [Dt::to_utc_from_tai_using_list](../struct.Dt.html#method.to_utc_from_tai_using_list)
    pub const fn leap_sec_using_sec64_and_list(
        sec64: i64,
        is_utc: bool,
        list: &[LeapSec],
    ) -> Option<LeapInfo> {
        let len = list.len();
        if len == 0 {
            return None;
        }

        // Binary search for upper_bound: first index where entry_sec > sec64
        let mut low = 0usize;
        let mut high = len;
        if is_utc {
            while low < high {
                let mid = low + (high - low) / 2;
                if list[mid].utc_sec <= sec64 {
                    low = mid + 1;
                } else {
                    high = mid;
                }
            }
        } else {
            while low < high {
                let mid = low + (high - low) / 2;
                if list[mid].tai_sec <= sec64 {
                    low = mid + 1;
                } else {
                    high = mid;
                }
            }
        }

        // low == first index with entry_sec > sec64 (or len)
        if low == 0 {
            return None;
        }

        let idx = low - 1;
        let entry = &list[idx];
        let is_leap = {
            if sec64 != if is_utc { entry.utc_sec } else { entry.tai_sec } {
                IsLeapSec::None
            } else if idx != 0 {
                let prev_leap_sec_after = list[idx - 1].leap_sec_after;
                if entry.leap_sec_after > prev_leap_sec_after {
                    IsLeapSec::Add
                } else if entry.leap_sec_after < prev_leap_sec_after {
                    IsLeapSec::Sub
                } else {
                    IsLeapSec::None
                }
            } else if entry.leap_sec_after > 0 {
                IsLeapSec::Add
            } else if entry.leap_sec_after < 0 {
                IsLeapSec::Sub
            } else {
                IsLeapSec::None
            }
        };

        Some(LeapInfo {
            offset: entry.leap_sec_after,
            n_entries_at_or_before: low,
            is_leap_sec: is_leap,
        })
    }

    /// Get [`LeapInfo`] for a particular library timestamp (seconds from 2000-01-01 noon TAI)
    /// using the library's in-built leap seconds list.
    ///
    /// - If the timestamp is currently on the UTC time scale then use **`true`** for the `is_utc`
    ///   parameter.
    /// - If the timestamp is currently on the TAI time scale then use **`false`** for the `is_utc`
    ///   parameter.
    /// - `sec64` should be such that it was produced using euclid division, see
    ///   [`Dt::to_sec64`](../struct.Dt.html#method.to_sec64) for more info. This only applies to
    ///   negative `sec64` values.
    #[inline(always)]
    pub const fn leap_sec_using_sec64(sec64: i64, is_utc: bool) -> Option<LeapInfo> {
        Self::leap_sec_using_sec64_and_list(sec64, is_utc, LEAP_SECS)
    }

    /// Get the leap seconds info for this instant.
    ///
    /// Uses the library's in-built leap seconds list.
    #[inline(always)]
    pub const fn leap_sec(&self, is_utc: bool) -> Option<LeapInfo> {
        Self::leap_sec_using_sec64_and_list(self.to_sec64(), is_utc, LEAP_SECS)
    }

    /// Get the leap seconds info for this instant with a given list.
    #[inline(always)]
    pub const fn leap_sec_using_list(&self, is_utc: bool, list: &[LeapSec]) -> Option<LeapInfo> {
        Self::leap_sec_using_sec64_and_list(self.to_sec64(), is_utc, list)
    }

    #[inline(always)]
    pub(crate) const fn utc_to_tai_using_list(&self, list: &[LeapSec]) -> Option<Dt> {
        match self.leap_sec_using_list(true, list) {
            Some(info) => Some(self.add_sec(info.offset as i128)),
            None => None,
        }
    }

    #[inline(always)]
    pub(crate) const fn tai_to_utc_using_list(&self, list: &[LeapSec]) -> Option<Dt> {
        match self.leap_sec_using_list(false, list) {
            Some(info) => Some(self.add_sec(-info.offset as i128)),
            None => None,
        }
    }

    /// Converts **UTC -> TAI** using a provided Leap seconds list.
    ///
    /// - If the
    ///   [`Dt`](../struct.Dt.html) is before the provided leap second list's
    ///   first entry then the library's own conversion is used to convert to
    ///   [`Scale::TAI`](../enum.Scale.html#variant.TAI)
    ///
    /// ## Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "std")] {
    /// use deep_time::utc::{IsLeapSec, LEAP_SECS};
    /// use deep_time::{Dt, Scale};
    ///
    /// let leap_seconds_list =
    ///     Dt::leap_sec_list_from_file("tests/assets/leap-seconds.list.txt").unwrap();
    /// assert_eq!(leap_seconds_list[1], LEAP_SECS[1]);
    ///
    /// let x = Dt::from_ymd(2015, 6, 30, Scale::UTC, 23, 59, 60, 0);
    /// let leap_sec = x.leap_sec_using_list(false, &leap_seconds_list).unwrap();
    /// assert!(leap_sec.is_leap_sec == IsLeapSec::Add);
    ///
    /// let dt = Dt::from_ymd(2000, 1, 1, Scale::TAI, 12, 0, 0, 0);
    ///
    /// let utc1 = dt.to(Scale::UTC);
    /// let utc2 = dt.to_utc_from_tai_using_list(Scale::UTC, &leap_seconds_list);
    /// assert_eq!(utc1, utc2);
    ///
    /// let tai1 = utc1.to_tai();
    /// let tai2 = utc2.to_tai_from_utc_using_list(&leap_seconds_list);
    /// assert_eq!(tai1, tai2);
    /// # }
    /// ```
    ///
    /// ## See also
    ///
    /// - [Dt::leap_sec_list_from_str](../struct.Dt.html#method.leap_sec_list_from_str)
    /// - [Dt::leap_sec_list_from_file](../struct.Dt.html#method.leap_sec_list_from_file)
    /// - [Dt::to_utc_from_tai_using_list](../struct.Dt.html#method.to_utc_from_tai_using_list)
    pub const fn to_tai_from_utc_using_list(&self, list: &[LeapSec]) -> Dt {
        match self.scale {
            // we're going utc -> tai, check if it's
            // post start of list using the provided leap seconds list
            Scale::UTC | Scale::UtcHist | Scale::UtcSpice => {
                match self.utc_to_tai_using_list(list) {
                    // leap seconds list returned an offset, so use that
                    Some(dt) => dt.with(Scale::TAI),
                    // leap seconds list returned None so it must be pre 1972
                    None => match self.scale {
                        Scale::UtcHist => match self.historical_utc_offset() {
                            Some(offset) => self.add(Dt::span_f(offset)).with(Scale::TAI),
                            None => self.with(Scale::TAI),
                        },
                        Scale::UtcSpice => self.add_sec(9).with(Scale::TAI),
                        _ => self.with(Scale::TAI),
                    },
                }
            }
            // defer to library conversion function
            _ => self.to(Scale::TAI),
        }
    }

    /// Converts **TAI -> UTC** using a provided Leap seconds list.
    ///
    /// - If `new` is
    ///   [`Scale::UtcHist`](../enum.Scale.html#variant.UtcHist) or
    ///   [`Scale::UtcSpice`](../enum.Scale.html#variant.UtcSpice) and the
    ///   [`Dt`](../struct.Dt.html) is before the provided leap second list's
    ///   first entry then the library's own conversion is used to convert to
    ///   `new`.
    /// - If `new` is not one of the scales that uses leap seconds then the library's
    ///   own conversion is used to convert to `new`.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "std")] {
    /// use deep_time::utc::{IsLeapSec, LEAP_SECS};
    /// use deep_time::{Dt, Scale};
    ///
    /// let leap_seconds_list =
    ///     Dt::leap_sec_list_from_file("tests/assets/leap-seconds.list.txt").unwrap();
    /// assert_eq!(leap_seconds_list[1], LEAP_SECS[1]);
    ///
    /// let x = Dt::from_ymd(2015, 6, 30, Scale::UTC, 23, 59, 60, 0);
    /// let leap_sec = x.leap_sec_using_list(false, &leap_seconds_list).unwrap();
    /// assert!(leap_sec.is_leap_sec == IsLeapSec::Add);
    ///
    /// let dt = Dt::from_ymd(2000, 1, 1, Scale::TAI, 12, 0, 0, 0);
    ///
    /// let utc1 = dt.to(Scale::UTC);
    /// let utc2 = dt.to_utc_from_tai_using_list(Scale::UTC, &leap_seconds_list);
    /// assert_eq!(utc1, utc2);
    ///
    /// let tai1 = utc1.to_tai();
    /// let tai2 = utc2.to_tai_from_utc_using_list(&leap_seconds_list);
    /// assert_eq!(tai1, tai2);
    /// # }
    /// ```
    ///
    /// ## See also
    ///
    /// - [Dt::leap_sec_list_from_str](../struct.Dt.html#method.leap_sec_list_from_str)
    /// - [Dt::leap_sec_list_from_file](../struct.Dt.html#method.leap_sec_list_from_file)
    /// - [Dt::to_tai_from_utc_using_list](../struct.Dt.html#method.to_tai_from_utc_using_list)
    pub const fn to_utc_from_tai_using_list(&self, new: Scale, list: &[LeapSec]) -> Dt {
        match new {
            Scale::UTC | Scale::UtcHist | Scale::UtcSpice => {
                match self.tai_to_utc_using_list(list) {
                    // leap seconds list returned an offset, so use that
                    Some(dt) => dt.with(new),
                    // leap seconds list returned None so it must be pre 1972
                    None => match new {
                        Scale::UtcHist => match self.historical_utc_offset() {
                            Some(offset) => self.sub(Dt::span_f(offset)).with(new),
                            None => self.with(new),
                        },
                        Scale::UtcSpice => self.add_sec(-9).with(new),
                        _ => self.with(new),
                    },
                }
            }
            // defer to library conversion function
            _ => self.to(new),
        }
    }
}

#[cfg(feature = "alloc")]
impl Dt {
    /// Load directly from a str (e.g. the official IANA `leap-seconds.list`).
    ///
    /// Format should be the same as the file available at:
    /// <https://data.iana.org/time-zones/data/leap-seconds.list>
    ///
    /// For rows that don't start with # (the list rows) the first column
    /// should be the NTP timestamp, the second column (separated by whitespace)
    /// should be the offset against TAI in seconds (the number of leap seconds at
    /// that point).
    ///
    /// e.g.
    ///
    /// | #NTP Time  |    DTAI  |
    /// |------------|----------|
    /// | #          |          |
    /// | 2272060800 |     10   |
    /// | 2287785600 |     11   |
    /// | 2303683200 |     12   |
    ///
    /// ## See also
    ///
    /// - [Dt::leap_sec_list_from_file](../struct.Dt.html#method.leap_sec_list_from_file)
    /// - [Dt::to_utc_from_tai_using_list](../struct.Dt.html#method.to_utc_from_tai_using_list)
    /// - [Dt::to_tai_from_utc_using_list](../struct.Dt.html#method.to_tai_from_utc_using_list)
    pub fn leap_sec_list_from_str(s: &str) -> Vec<LeapSec> {
        use crate::Scale;

        let mut list = Vec::new();
        let mut prev_leap_sec_after: i64 = 0;
        let mut entries_pushed: usize = 0;

        for line in s.lines() {
            let trimmed = line.trim();

            if trimmed.is_empty() || trimmed.starts_with("#") {
                continue;
            }

            let mut parts = trimmed.split_whitespace();

            let ntp_timestamp = if let Some(num) = parts.next() {
                match num.parse::<i64>() {
                    Ok(n) => n,
                    Err(_) => continue,
                }
            } else {
                continue;
            };
            let leap_sec_after = if let Some(num) = parts.next() {
                match num.parse::<i64>() {
                    Ok(n) => n,
                    Err(_) => continue,
                }
            } else {
                continue;
            };

            // don't use current: UTC because it would use the internal leap list
            let utc_sec = Dt::from_ntp(Dt::from_sec(ntp_timestamp as i128, Scale::TAI)).to_sec64();

            let tai_sec = if entries_pushed == 0 {
                if leap_sec_after > 0 {
                    utc_sec + leap_sec_after - 1
                } else {
                    // hypothetical negative first entry
                    utc_sec + leap_sec_after
                }
            } else {
                utc_sec + prev_leap_sec_after
            };

            list.push(LeapSec {
                ntp_timestamp,
                leap_sec_after,
                utc_sec,
                tai_sec,
            });

            prev_leap_sec_after = leap_sec_after;
            entries_pushed += 1;
        }

        list
    }
}

#[cfg(feature = "std")]
impl Dt {
    /// Load directly from a file (e.g. the official IANA `leap-seconds.list`).
    ///
    /// Format should be the same as the file available at:
    /// <https://data.iana.org/time-zones/data/leap-seconds.list>
    ///
    /// For rows that don't start with # (the list rows) the first column
    /// should be the NTP timestamp, the second column (separated by whitespace)
    /// should be the offset against TAI in seconds (the number of leap seconds at
    /// that point).
    ///
    /// e.g.
    ///
    /// | #NTP Time  |    DTAI  |
    /// |------------|----------|
    /// | #          |          |
    /// | 2272060800 |     10   |
    /// | 2287785600 |     11   |
    /// | 2303683200 |     12   |
    ///
    /// ## Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "std")] {
    /// use deep_time::utc::{IsLeapSec, LEAP_SECS};
    /// use deep_time::{Dt, Scale};
    ///
    /// let leap_seconds_list =
    ///     Dt::leap_sec_list_from_file("tests/assets/leap-seconds.list.txt").unwrap();
    /// assert_eq!(leap_seconds_list[1], LEAP_SECS[1]);
    ///
    /// let x = Dt::from_ymd(2015, 6, 30, Scale::UTC, 23, 59, 60, 0);
    /// let leap_sec = x.leap_sec_using_list(false, &leap_seconds_list).unwrap();
    /// assert!(leap_sec.is_leap_sec == IsLeapSec::Add);
    ///
    /// let dt = Dt::from_ymd(2000, 1, 1, Scale::TAI, 12, 0, 0, 0);
    ///
    /// let utc1 = dt.to(Scale::UTC);
    /// let utc2 = dt.to_utc_from_tai_using_list(Scale::UTC, &leap_seconds_list);
    /// assert_eq!(utc1, utc2);
    ///
    /// let tai1 = utc1.to_tai();
    /// let tai2 = utc2.to_tai_from_utc_using_list(&leap_seconds_list);
    /// assert_eq!(tai1, tai2);
    /// # }
    /// ```
    ///
    /// ## See also
    ///
    /// - [Dt::leap_sec_list_from_str](../struct.Dt.html#method.leap_sec_list_from_str)
    /// - [Dt::to_utc_from_tai_using_list](../struct.Dt.html#method.to_utc_from_tai_using_list)
    /// - [Dt::to_tai_from_utc_using_list](../struct.Dt.html#method.to_tai_from_utc_using_list)
    #[inline]
    pub fn leap_sec_list_from_file<P: AsRef<Path>>(path: P) -> io::Result<Vec<LeapSec>> {
        let content = fs::read_to_string(path)?;
        Ok(Self::leap_sec_list_from_str(&content))
    }
}