2 go.cypherpunks.ru/tai64n -- Pure Go TAI64/TAI64N implementation
3 Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 const Leapsecs1972 = 10
27 // Database of Unix timestamps of the time when leap second occurred.
28 // Library contains and initializes it with leap seconds up to 2016-12-31.
29 var LeapsecsDB []int64
31 // TAI<->UTC difference for the given Unix timestamp.
32 func LeapsecsDiff(t int64) int {
33 for i, leap := range LeapsecsDB {
35 return len(LeapsecsDB) - i
41 // Add currently known (LeapsecsDB) leap seconds, not including initial
42 // 1972-01-01 10-seconds offset.
43 func LeapsecsAdd(t time.Time) time.Time {
44 return t.Add(time.Second * time.Duration(LeapsecsDiff(t.Unix())))
47 // Opposite of LeapsecsAdd().
48 func LeapsecsSub(t time.Time) time.Time {
49 return t.Add(-time.Second * time.Duration(LeapsecsDiff(t.Unix())))
54 func (a Int64s) Len() int { return len(a) }
55 func (a Int64s) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
56 func (a Int64s) Less(i, j int) bool { return a[i] > a[j] }
58 // Load "leapsecs.dat"-like database: concatenated TAI64 leap seconds.
59 // Function panics if encoding is invalid.
60 func LeapsecsDBLoad(buf []byte) {
61 db := make([]int64, 0, len(buf)/TAI64Size)
62 for i := 0; i < len(buf); i += TAI64Size {
63 db = append(db, (ToTime(buf[i:i+TAI64Size]).Unix()/86400)*86400)