Skip to main content

deep_time/
leap_seconds.rs

1//! Leap seconds table from the official IANA
2//! [leap-seconds.list](https://data.iana.org/time-zones/data/leap-seconds.list)
3//! Updated through IERS Bulletin C as of April 2026.
4//! Last leap second: 2017-01-01 (TAI-UTC = 37 s)
5//! File expires: 28 December 2026
6
7use crate::Dt;
8
9#[cfg(feature = "std")]
10use std::{fs, io, path::Path};
11
12#[cfg(feature = "alloc")]
13use alloc::vec::Vec;
14
15#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
16pub struct LeapSecond {
17    pub ntp_timestamp: i64,
18    pub leap_sec_after: i64,
19    pub utc_sec: i64,
20    pub tai_sec: i64,
21}
22
23pub const LEAP_SECS: &[LeapSecond] = &[
24    LeapSecond {
25        ntp_timestamp: 2272060800,
26        leap_sec_after: 10,
27        utc_sec: -883656000,
28        tai_sec: -883655991, // was -883655990
29    }, // 1 Jan 1972 (start of modern UTC definition)
30    LeapSecond {
31        ntp_timestamp: 2287785600,
32        leap_sec_after: 11,
33        utc_sec: -867931200,
34        tai_sec: -867931190, // was -867931189
35    }, // 1 Jul 1972
36    LeapSecond {
37        ntp_timestamp: 2303683200,
38        leap_sec_after: 12,
39        utc_sec: -852033600,
40        tai_sec: -852033589, // was -852033588
41    }, // 1 Jan 1973
42    LeapSecond {
43        ntp_timestamp: 2335219200,
44        leap_sec_after: 13,
45        utc_sec: -820497600,
46        tai_sec: -820497588, // was -820497587
47    }, // 1 Jan 1974
48    LeapSecond {
49        ntp_timestamp: 2366755200,
50        leap_sec_after: 14,
51        utc_sec: -788961600,
52        tai_sec: -788961587, // was -788961586
53    }, // 1 Jan 1975
54    LeapSecond {
55        ntp_timestamp: 2398291200,
56        leap_sec_after: 15,
57        utc_sec: -757425600,
58        tai_sec: -757425586, // was -757425585
59    }, // 1 Jan 1976
60    LeapSecond {
61        ntp_timestamp: 2429913600,
62        leap_sec_after: 16,
63        utc_sec: -725803200,
64        tai_sec: -725803185, // was -725803184
65    }, // 1 Jan 1977
66    LeapSecond {
67        ntp_timestamp: 2461449600,
68        leap_sec_after: 17,
69        utc_sec: -694267200,
70        tai_sec: -694267184, // was -694267183
71    }, // 1 Jan 1978
72    LeapSecond {
73        ntp_timestamp: 2492985600,
74        leap_sec_after: 18,
75        utc_sec: -662731200,
76        tai_sec: -662731183, // was -662731182
77    }, // 1 Jan 1979
78    LeapSecond {
79        ntp_timestamp: 2524521600,
80        leap_sec_after: 19,
81        utc_sec: -631195200,
82        tai_sec: -631195182, // was -631195181
83    }, // 1 Jan 1980
84    LeapSecond {
85        ntp_timestamp: 2571782400,
86        leap_sec_after: 20,
87        utc_sec: -583934400,
88        tai_sec: -583934381, // was -583934380
89    }, // 1 Jul 1981
90    LeapSecond {
91        ntp_timestamp: 2603318400,
92        leap_sec_after: 21,
93        utc_sec: -552398400,
94        tai_sec: -552398380, // was -552398379
95    }, // 1 Jul 1982
96    LeapSecond {
97        ntp_timestamp: 2634854400,
98        leap_sec_after: 22,
99        utc_sec: -520862400,
100        tai_sec: -520862379, // was -520862378
101    }, // 1 Jul 1983
102    LeapSecond {
103        ntp_timestamp: 2698012800,
104        leap_sec_after: 23,
105        utc_sec: -457704000,
106        tai_sec: -457703978, // was -457703977
107    }, // 1 Jul 1985
108    LeapSecond {
109        ntp_timestamp: 2776982400,
110        leap_sec_after: 24,
111        utc_sec: -378734400,
112        tai_sec: -378734377, // was -378734376
113    }, // 1 Jan 1988
114    LeapSecond {
115        ntp_timestamp: 2840140800,
116        leap_sec_after: 25,
117        utc_sec: -315576000,
118        tai_sec: -315575976, // was -315575975
119    }, // 1 Jan 1990
120    LeapSecond {
121        ntp_timestamp: 2871676800,
122        leap_sec_after: 26,
123        utc_sec: -284040000,
124        tai_sec: -284039975, // was -284039974
125    }, // 1 Jan 1991
126    LeapSecond {
127        ntp_timestamp: 2918937600,
128        leap_sec_after: 27,
129        utc_sec: -236779200,
130        tai_sec: -236779174, // was -236779173
131    }, // 1 Jul 1992
132    LeapSecond {
133        ntp_timestamp: 2950473600,
134        leap_sec_after: 28,
135        utc_sec: -205243200,
136        tai_sec: -205243173, // was -205243172
137    }, // 1 Jul 1993
138    LeapSecond {
139        ntp_timestamp: 2982009600,
140        leap_sec_after: 29,
141        utc_sec: -173707200,
142        tai_sec: -173707172, // was -173707171
143    }, // 1 Jul 1994
144    LeapSecond {
145        ntp_timestamp: 3029443200,
146        leap_sec_after: 30,
147        utc_sec: -126273600,
148        tai_sec: -126273571, // was -126273570
149    }, // 1 Jan 1996
150    LeapSecond {
151        ntp_timestamp: 3076704000,
152        leap_sec_after: 31,
153        utc_sec: -79012800,
154        tai_sec: -79012770, // was -79012769
155    }, // 1 Jul 1997
156    LeapSecond {
157        ntp_timestamp: 3124137600,
158        leap_sec_after: 32,
159        utc_sec: -31579200,
160        tai_sec: -31579169, // was -31579168
161    }, // 1 Jan 1999
162    LeapSecond {
163        ntp_timestamp: 3345062400,
164        leap_sec_after: 33,
165        utc_sec: 189345600,
166        tai_sec: 189345632, // was 189345633
167    }, // 1 Jan 2006
168    LeapSecond {
169        ntp_timestamp: 3439756800,
170        leap_sec_after: 34,
171        utc_sec: 284040000,
172        tai_sec: 284040033, // was 284040034
173    }, // 1 Jan 2009
174    LeapSecond {
175        ntp_timestamp: 3550089600,
176        leap_sec_after: 35,
177        utc_sec: 394372800,
178        tai_sec: 394372834, // was 394372835
179    }, // 1 Jul 2012
180    LeapSecond {
181        ntp_timestamp: 3644697600,
182        leap_sec_after: 36,
183        utc_sec: 488980800,
184        tai_sec: 488980835, // was 488980836
185    }, // 1 Jul 2015
186    LeapSecond {
187        ntp_timestamp: 3692217600,
188        leap_sec_after: 37,
189        utc_sec: 536500800,
190        tai_sec: 536500836, // was 536500837
191    }, // 1 Jan 2017
192];
193
194#[derive(Copy, Clone, Debug)]
195pub struct LeapInfo {
196    pub offset: i64,
197    pub leaps_inserted: i64,
198    pub is_leap_sec: bool,
199}
200
201impl Dt {
202    #[inline]
203    pub const fn leap_sec(&self, is_utc: bool) -> LeapInfo {
204        get_leap_sec(self, is_utc)
205    }
206
207    #[inline]
208    pub const fn leap_sec_using(&self, is_utc: bool, table: &[LeapSecond]) -> LeapInfo {
209        get_leap_sec_with_table(self, is_utc, table)
210    }
211}
212
213#[inline]
214pub const fn get_leap_sec(dt: &Dt, is_utc: bool) -> LeapInfo {
215    get_leap_sec_with_table(dt, is_utc, LEAP_SECS)
216}
217
218pub const fn get_leap_sec_with_table(dt: &Dt, is_utc: bool, table: &[LeapSecond]) -> LeapInfo {
219    let len = table.len();
220    if len == 0 {
221        return LeapInfo {
222            offset: 0,
223            leaps_inserted: 0,
224            is_leap_sec: false,
225        };
226    }
227
228    let target = dt.sec;
229
230    // Binary search for upper_bound: first index where entry_sec > target
231    let mut low = 0usize;
232    let mut high = len;
233
234    while low < high {
235        let mid = low + (high - low) / 2;
236        let entry_sec = if is_utc {
237            table[mid].utc_sec
238        } else {
239            table[mid].tai_sec
240        };
241
242        if entry_sec <= target {
243            low = mid + 1;
244        } else {
245            high = mid;
246        }
247    }
248
249    // low == first index with entry_sec > target (or len)
250    if low == 0 {
251        return LeapInfo {
252            offset: 0,
253            leaps_inserted: 0,
254            is_leap_sec: false,
255        };
256    }
257
258    let idx = low - 1;
259    let entry = &table[idx];
260    let entry_sec = if is_utc { entry.utc_sec } else { entry.tai_sec };
261
262    let is_leap = target == entry_sec;
263
264    LeapInfo {
265        offset: entry.leap_sec_after,
266        leaps_inserted: (idx + 1) as i64,
267        is_leap_sec: is_leap,
268    }
269}
270
271#[cfg(feature = "std")]
272impl Dt {
273    /// Load directly from a file (e.g. the official IANA `leap-seconds.list`).
274    ///
275    /// Format should be the same as the file available at:
276    /// https://data.iana.org/time-zones/data/leap-seconds.list
277    ///
278    /// For rows that don't start with # (the data rows) the first column
279    /// should be the NTP timestamp, the second column (separated by whitespace)
280    /// should be the offset against TAI in seconds (the number of leap seconds at
281    /// that point).
282    ///
283    /// e.g.
284    ///
285    /// | #NTP Time  |    DTAI  |
286    /// |------------|----------|
287    /// | #          |          |
288    /// | 2272060800 |     10   |
289    /// | 2287785600 |     11   |
290    /// | 2303683200 |     12   |
291    pub fn leap_sec_from_file<P: AsRef<Path>>(path: P) -> io::Result<Vec<LeapSecond>> {
292        let content = fs::read_to_string(path)?;
293        Ok(Self::leap_sec_from_str(&content))
294    }
295}
296
297#[cfg(feature = "alloc")]
298impl Dt {
299    /// Load directly from a str (e.g. the official IANA `leap-seconds.list`).
300    ///
301    /// Format should be the same as the file available at:
302    /// https://data.iana.org/time-zones/data/leap-seconds.list
303    ///
304    /// For rows that don't start with # (the data rows) the first column
305    /// should be the NTP timestamp, the second column (separated by whitespace)
306    /// should be the offset against TAI in seconds (the number of leap seconds at
307    /// that point).
308    ///
309    /// e.g.
310    ///
311    /// | #NTP Time  |    DTAI  |
312    /// |------------|----------|
313    /// | #          |          |
314    /// | 2272060800 |     10   |
315    /// | 2287785600 |     11   |
316    /// | 2303683200 |     12   |
317    ///
318    /// # Example:
319    ///
320    /// ```ignore
321    /// let table = Self::leap_sec_from_str(&file_content_as_str);
322    /// ```
323    pub fn leap_sec_from_str(s: &str) -> Vec<LeapSecond> {
324        use crate::Scale;
325
326        let mut table = Vec::new();
327        for line in s.lines() {
328            let trimmed = line.trim();
329
330            if trimmed.is_empty() || trimmed.starts_with("#") {
331                continue;
332            }
333
334            let parts: Vec<&str> = trimmed.split_whitespace().collect();
335
336            if parts.len() < 2 {
337                continue;
338            }
339            let Ok(ntp_timestamp) = parts[0].parse::<i64>() else {
340                continue;
341            };
342            let Ok(leap_sec_after) = parts[1].parse::<i64>() else {
343                continue;
344            };
345
346            let dt = Dt::from_ntp(f!(ntp_timestamp), Scale::UTC);
347            let tai_sec = dt.sec - 1;
348            let utc_sec = dt.to(Scale::TAI, Scale::UTC).sec;
349
350            table.push(LeapSecond {
351                ntp_timestamp,
352                leap_sec_after,
353                utc_sec,
354                tai_sec,
355            });
356        }
357
358        table
359    }
360}