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