| 1 |
package date |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"encoding/binary" |
| 20 |
"encoding/json" |
| 21 |
"time" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
var unixEpoch = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
type UnixTime time.Time |
| 30 |
|
| 31 |
|
| 32 |
func (t UnixTime) Duration() time.Duration { |
| 33 |
return time.Time(t).Sub(unixEpoch) |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func NewUnixTimeFromSeconds(seconds float64) UnixTime { |
| 38 |
return NewUnixTimeFromDuration(time.Duration(seconds * float64(time.Second))) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
func NewUnixTimeFromNanoseconds(nanoseconds int64) UnixTime { |
| 43 |
return NewUnixTimeFromDuration(time.Duration(nanoseconds)) |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func NewUnixTimeFromDuration(dur time.Duration) UnixTime { |
| 48 |
return UnixTime(unixEpoch.Add(dur)) |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
func UnixEpoch() time.Time { |
| 53 |
return unixEpoch |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
func (t UnixTime) MarshalJSON() ([]byte, error) { |
| 59 |
buffer := &bytes.Buffer{} |
| 60 |
enc := json.NewEncoder(buffer) |
| 61 |
err := enc.Encode(float64(time.Time(t).UnixNano()) / 1e9) |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
return buffer.Bytes(), nil |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
func (t *UnixTime) UnmarshalJSON(text []byte) error { |
| 71 |
dec := json.NewDecoder(bytes.NewReader(text)) |
| 72 |
|
| 73 |
var secondsSinceEpoch float64 |
| 74 |
if err := dec.Decode(&secondsSinceEpoch); err != nil { |
| 75 |
return err |
| 76 |
} |
| 77 |
|
| 78 |
*t = NewUnixTimeFromSeconds(secondsSinceEpoch) |
| 79 |
|
| 80 |
return nil |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func (t UnixTime) MarshalText() ([]byte, error) { |
| 85 |
cast := time.Time(t) |
| 86 |
return cast.MarshalText() |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
func (t *UnixTime) UnmarshalText(raw []byte) error { |
| 91 |
var unmarshaled time.Time |
| 92 |
|
| 93 |
if err := unmarshaled.UnmarshalText(raw); err != nil { |
| 94 |
return err |
| 95 |
} |
| 96 |
|
| 97 |
*t = UnixTime(unmarshaled) |
| 98 |
return nil |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
func (t UnixTime) MarshalBinary() ([]byte, error) { |
| 103 |
buf := &bytes.Buffer{} |
| 104 |
|
| 105 |
payload := int64(t.Duration()) |
| 106 |
|
| 107 |
if err := binary.Write(buf, binary.LittleEndian, &payload); err != nil { |
| 108 |
return nil, err |
| 109 |
} |
| 110 |
|
| 111 |
return buf.Bytes(), nil |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
func (t *UnixTime) UnmarshalBinary(raw []byte) error { |
| 116 |
var nanosecondsSinceEpoch int64 |
| 117 |
|
| 118 |
if err := binary.Read(bytes.NewReader(raw), binary.LittleEndian, &nanosecondsSinceEpoch); err != nil { |
| 119 |
return err |
| 120 |
} |
| 121 |
*t = NewUnixTimeFromNanoseconds(nanosecondsSinceEpoch) |
| 122 |
return nil |
| 123 |
} |