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
//! Leap seconds table from the official IANA
//! [leap-seconds.list](https://data.iana.org/time-zones/data/leap-seconds.list)
//! Updated through IERS Bulletin C as of April 2026.
//! Last leap second: 2017-01-01 (TAI-UTC = 37 s)
//! File expires: 28 December 2026
use crate::Dt;
#[cfg(feature = "std")]
use std::{fs, io, path::Path};
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct LeapSec {
pub ntp_timestamp: i64,
pub leap_sec_after: i64,
// pub utc_sec: i64,
pub tai_sec: i64,
}
pub const LEAP_SECS: &[LeapSec] = &[
LeapSec {
ntp_timestamp: 2272060800,
leap_sec_after: 10,
// utc_sec: -883656000,
tai_sec: -883655991, // was -883655990
}, // 1 Jan 1972 (start of modern UTC definition)
LeapSec {
ntp_timestamp: 2287785600,
leap_sec_after: 11,
// utc_sec: -867931200,
tai_sec: -867931190, // was -867931189
}, // 1 Jul 1972
LeapSec {
ntp_timestamp: 2303683200,
leap_sec_after: 12,
// utc_sec: -852033600,
tai_sec: -852033589, // was -852033588
}, // 1 Jan 1973
LeapSec {
ntp_timestamp: 2335219200,
leap_sec_after: 13,
// utc_sec: -820497600,
tai_sec: -820497588, // was -820497587
}, // 1 Jan 1974
LeapSec {
ntp_timestamp: 2366755200,
leap_sec_after: 14,
// utc_sec: -788961600,
tai_sec: -788961587, // was -788961586
}, // 1 Jan 1975
LeapSec {
ntp_timestamp: 2398291200,
leap_sec_after: 15,
// utc_sec: -757425600,
tai_sec: -757425586, // was -757425585
}, // 1 Jan 1976
LeapSec {
ntp_timestamp: 2429913600,
leap_sec_after: 16,
// utc_sec: -725803200,
tai_sec: -725803185, // was -725803184
}, // 1 Jan 1977
LeapSec {
ntp_timestamp: 2461449600,
leap_sec_after: 17,
// utc_sec: -694267200,
tai_sec: -694267184, // was -694267183
}, // 1 Jan 1978
LeapSec {
ntp_timestamp: 2492985600,
leap_sec_after: 18,
// utc_sec: -662731200,
tai_sec: -662731183, // was -662731182
}, // 1 Jan 1979
LeapSec {
ntp_timestamp: 2524521600,
leap_sec_after: 19,
// utc_sec: -631195200,
tai_sec: -631195182, // was -631195181
}, // 1 Jan 1980
LeapSec {
ntp_timestamp: 2571782400,
leap_sec_after: 20,
// utc_sec: -583934400,
tai_sec: -583934381, // was -583934380
}, // 1 Jul 1981
LeapSec {
ntp_timestamp: 2603318400,
leap_sec_after: 21,
// utc_sec: -552398400,
tai_sec: -552398380, // was -552398379
}, // 1 Jul 1982
LeapSec {
ntp_timestamp: 2634854400,
leap_sec_after: 22,
// utc_sec: -520862400,
tai_sec: -520862379, // was -520862378
}, // 1 Jul 1983
LeapSec {
ntp_timestamp: 2698012800,
leap_sec_after: 23,
// utc_sec: -457704000,
tai_sec: -457703978, // was -457703977
}, // 1 Jul 1985
LeapSec {
ntp_timestamp: 2776982400,
leap_sec_after: 24,
// utc_sec: -378734400,
tai_sec: -378734377, // was -378734376
}, // 1 Jan 1988
LeapSec {
ntp_timestamp: 2840140800,
leap_sec_after: 25,
// utc_sec: -315576000,
tai_sec: -315575976, // was -315575975
}, // 1 Jan 1990
LeapSec {
ntp_timestamp: 2871676800,
leap_sec_after: 26,
// utc_sec: -284040000,
tai_sec: -284039975, // was -284039974
}, // 1 Jan 1991
LeapSec {
ntp_timestamp: 2918937600,
leap_sec_after: 27,
// utc_sec: -236779200,
tai_sec: -236779174, // was -236779173
}, // 1 Jul 1992
LeapSec {
ntp_timestamp: 2950473600,
leap_sec_after: 28,
// utc_sec: -205243200,
tai_sec: -205243173, // was -205243172
}, // 1 Jul 1993
LeapSec {
ntp_timestamp: 2982009600,
leap_sec_after: 29,
// utc_sec: -173707200,
tai_sec: -173707172, // was -173707171
}, // 1 Jul 1994
LeapSec {
ntp_timestamp: 3029443200,
leap_sec_after: 30,
// utc_sec: -126273600,
tai_sec: -126273571, // was -126273570
}, // 1 Jan 1996
LeapSec {
ntp_timestamp: 3076704000,
leap_sec_after: 31,
// utc_sec: -79012800,
tai_sec: -79012770, // was -79012769
}, // 1 Jul 1997
LeapSec {
ntp_timestamp: 3124137600,
leap_sec_after: 32,
// utc_sec: -31579200,
tai_sec: -31579169, // was -31579168
}, // 1 Jan 1999
LeapSec {
ntp_timestamp: 3345062400,
leap_sec_after: 33,
// utc_sec: 189345600,
tai_sec: 189345632, // was 189345633
}, // 1 Jan 2006
LeapSec {
ntp_timestamp: 3439756800,
leap_sec_after: 34,
// utc_sec: 284040000,
tai_sec: 284040033, // was 284040034
}, // 1 Jan 2009
LeapSec {
ntp_timestamp: 3550089600,
leap_sec_after: 35,
// utc_sec: 394372800,
tai_sec: 394372834, // was 394372835
}, // 1 Jul 2012
LeapSec {
ntp_timestamp: 3644697600,
leap_sec_after: 36,
// utc_sec: 488980800,
tai_sec: 488980835, // was 488980836
}, // 1 Jul 2015
LeapSec {
ntp_timestamp: 3692217600,
leap_sec_after: 37,
// utc_sec: 536500800,
tai_sec: 536500836, // was 536500837
}, // 1 Jan 2017
];
#[derive(Copy, Clone, Debug)]
pub struct LeapInfo {
pub offset: i64,
pub leaps_inserted: i64,
pub is_leap_sec: bool,
}
impl Dt {
#[inline]
pub const fn leap_sec(&self, is_utc: bool) -> LeapInfo {
leap_sec(self, is_utc)
}
#[inline]
pub const fn leap_sec_using(&self, is_utc: bool, table: &[LeapSec]) -> LeapInfo {
leap_sec_using(self, is_utc, table)
}
}
#[inline]
pub const fn leap_sec(dt: &Dt, is_utc: bool) -> LeapInfo {
leap_sec_using(dt, is_utc, LEAP_SECS)
}
pub const fn leap_sec_using(dt: &Dt, is_utc: bool, table: &[LeapSec]) -> LeapInfo {
let len = table.len();
if len == 0 {
return LeapInfo {
offset: 0,
leaps_inserted: 0,
is_leap_sec: false,
};
}
let target = dt.sec;
// Binary search for upper_bound: first index where entry_sec > target
let mut low = 0usize;
let mut high = len;
if is_utc {
while low < high {
let mid = low + (high - low) / 2;
let entry_sec = table[mid].tai_sec - table[mid].leap_sec_after + 1;
if entry_sec <= target {
low = mid + 1;
} else {
high = mid;
}
}
} else {
while low < high {
let mid = low + (high - low) / 2;
let entry_sec = table[mid].tai_sec;
if entry_sec <= target {
low = mid + 1;
} else {
high = mid;
}
}
}
// low == first index with entry_sec > target (or len)
if low == 0 {
return LeapInfo {
offset: 0,
leaps_inserted: 0,
is_leap_sec: false,
};
}
let idx = low - 1;
let entry = &table[idx];
let entry_sec = if is_utc {
entry.tai_sec - entry.leap_sec_after + 1
} else {
entry.tai_sec
};
let is_leap = target == entry_sec;
LeapInfo {
offset: entry.leap_sec_after,
leaps_inserted: (idx + 1) as i64,
is_leap_sec: is_leap,
}
}
#[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 data 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 |
pub fn leap_sec_data_from_file<P: AsRef<Path>>(path: P) -> io::Result<Vec<LeapSec>> {
let content = fs::read_to_string(path)?;
Ok(Self::leap_sec_data_from_str(&content))
}
}
#[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 data 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 |
///
/// ## Example:
///
/// ```ignore
/// let table = Self::leap_sec_from_str(&file_content_as_str);
/// ```
pub fn leap_sec_data_from_str(s: &str) -> Vec<LeapSec> {
use crate::Scale;
let mut table = Vec::new();
let mut prev_leap_sec_after: i64 = 0;
for line in s.lines() {
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with("#") {
continue;
}
let parts: Vec<&str> = trimmed.split_whitespace().collect();
if parts.len() < 2 {
continue;
}
let Ok(ntp_timestamp) = parts[0].parse::<i64>() else {
continue;
};
let Ok(leap_sec_after) = parts[1].parse::<i64>() else {
continue;
};
// don't use current: UTC because it would use the internal leap table
let dt = Dt::from_ntp(f!(ntp_timestamp), Scale::TAI);
let tai_sec = if prev_leap_sec_after == 0 {
dt.sec + leap_sec_after - 1
} else {
dt.sec + leap_sec_after - (leap_sec_after - prev_leap_sec_after)
};
// let utc_sec = tai_sec - leap_sec_after + 1;
table.push(LeapSec {
ntp_timestamp,
leap_sec_after,
// utc_sec,
tai_sec,
});
prev_leap_sec_after = leap_sec_after;
}
table
}
}