]> Cypherpunks repositories - gotai64n.git/commitdiff
TAI64NA storage support v3.1.0
authorSergey Matveev <stargrave@stargrave.org>
Thu, 3 Oct 2024 09:25:18 +0000 (12:25 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 3 Oct 2024 10:36:39 +0000 (13:36 +0300)
tai64n.go

index 4760142b6c90ca8742a2b273800e0ac4a15d877d..4f9c3e821243e369f34b4dc12fb299fc6c117092 100644 (file)
--- a/tai64n.go
+++ b/tai64n.go
@@ -36,56 +36,63 @@ import (
 )
 
 const (
-       TAI64Size  = 8
-       TAI64NSize = TAI64Size + 4
-       LocalFmt   = "2006-01-02 15:04:05.000000000"
-       Base       = 0x4000000000000000 + Leapsecs1972
+       TAI64Size   = 8
+       TAI64NSize  = TAI64Size + 4
+       TAI64NASize = TAI64Size + 4 + 4
+       LocalFmt    = "2006-01-02 15:04:05.000000000"
+       Base        = 0x4000000000000000 + Leapsecs1972
 )
 
 type (
-       TAI64  [TAI64Size]byte
-       TAI64N [TAI64NSize]byte
+       TAI64   [TAI64Size]byte
+       TAI64N  [TAI64NSize]byte
+       TAI64NA [TAI64NASize]byte
 )
 
-func (dst *TAI64) FromTime(src time.Time) {
-       binary.BigEndian.PutUint64(dst[:], uint64(Base)+uint64(src.Unix()))
+func (tai *TAI64) FromTime(src time.Time) {
+       binary.BigEndian.PutUint64(tai[:], uint64(Base)+uint64(src.Unix()))
 }
 
-func (dst *TAI64N) FromTime(src time.Time) {
-       binary.BigEndian.PutUint64(dst[:], uint64(Base)+uint64(src.Unix()))
-       binary.BigEndian.PutUint32(dst[8:], uint32(src.Nanosecond()))
+func (tai *TAI64N) FromTime(src time.Time) {
+       binary.BigEndian.PutUint64(tai[:], uint64(Base)+uint64(src.Unix()))
+       binary.BigEndian.PutUint32(tai[8:], uint32(src.Nanosecond()))
 }
 
 func ToTime(tai []byte) time.Time {
        var secs, nano int64
        switch len(tai) {
+       case TAI64NASize:
+               panic("TAI64NA can not be converted to time.Time")
        case TAI64NSize:
                nano = int64(binary.BigEndian.Uint32(tai[8:]))
                fallthrough
        case TAI64Size:
                secs = int64(binary.BigEndian.Uint64(tai[:8])) - Base
        default:
-               panic("invalid tai size")
+               panic("invalid TAI64* size")
        }
        return time.Unix(secs, nano)
 }
 
-// Convert TAI64/TAI64N to "@HEX(TAI64)" format.
+// Convert TAI64* to "@HEX(TAI64)" format.
 func Encode(tai []byte) string {
        raw := make([]byte, 1+hex.EncodedLen(len(tai)))
-       raw[0] = byte('@')
+       raw[0] = '@'
        hex.Encode(raw[1:], tai)
        return string(raw)
 }
 
-// Convert TAI64/TAI64N "@HEX(TAI64)" format to Time.
-func Decode(s string) (time.Time, error) {
+// Convert TAI64* "@HEX(TAI64)" format to Time.
+func Decode(s string) (t time.Time, err error) {
        raw, err := hex.DecodeString(strings.TrimPrefix(s, "@"))
-       if !(len(raw) == TAI64Size || len(raw) == TAI64NSize) {
+       switch len(raw) {
+       case TAI64NASize:
+       case TAI64NSize:
+       case TAI64Size:
+       default:
                err = errors.New("invalid length")
+               return
        }
-       if err == nil {
-               return ToTime(raw), nil
-       }
-       return time.Time{}, err
+       t = ToTime(raw)
+       return
 }