2 go.cypherpunks.ru/tai64n -- Pure Go TAI64N implementation
3 Copyright (C) 2020 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/>.
30 Base = 0x400000000000000a
31 LocalFmt = "2006-01-02 15:04:05.000000000"
34 type TAI64N [Size]byte
36 func FromTime(t time.Time, tai *TAI64N) {
37 binary.BigEndian.PutUint64(tai[:], uint64(Base)+uint64(t.Unix()))
38 binary.BigEndian.PutUint32(tai[8:], uint32(t.Nanosecond()))
41 func ToTime(tai []byte) time.Time {
45 secs := int64(binary.BigEndian.Uint64(tai[:8]))
46 nano := int64(binary.BigEndian.Uint32(tai[8:]))
47 return time.Unix(secs-Base, nano)
50 func (tai TAI64N) Encode() string {
51 return "@" + hex.EncodeToString(tai[:])
54 func Decode(s string) (time.Time, error) {
55 tai, err := hex.DecodeString(strings.TrimPrefix(s, "@"))
57 return time.Time{}, errors.New("invalid ts length")
60 return time.Time{}, err
62 return ToTime(tai), nil