3 // Copyright 2017 Microsoft Corporation
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
24 // unixEpoch is the moment in time that should be treated as timestamp 0.
25 var unixEpoch = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC)
27 // UnixTime marshals and unmarshals a time that is represented as the number
28 // of seconds (ignoring skip-seconds) since the Unix Epoch.
29 type UnixTime time.Time
31 // Duration returns the time as a Duration since the UnixEpoch.
32 func (t UnixTime) Duration() time.Duration {
33 return time.Time(t).Sub(unixEpoch)
36 // NewUnixTimeFromSeconds creates a UnixTime as a number of seconds from the UnixEpoch.
37 func NewUnixTimeFromSeconds(seconds float64) UnixTime {
38 return NewUnixTimeFromDuration(time.Duration(seconds * float64(time.Second)))
41 // NewUnixTimeFromNanoseconds creates a UnixTime as a number of nanoseconds from the UnixEpoch.
42 func NewUnixTimeFromNanoseconds(nanoseconds int64) UnixTime {
43 return NewUnixTimeFromDuration(time.Duration(nanoseconds))
46 // NewUnixTimeFromDuration creates a UnixTime as a duration of time since the UnixEpoch.
47 func NewUnixTimeFromDuration(dur time.Duration) UnixTime {
48 return UnixTime(unixEpoch.Add(dur))
51 // UnixEpoch retreives the moment considered the Unix Epoch. I.e. The time represented by '0'
52 func UnixEpoch() time.Time {
56 // MarshalJSON preserves the UnixTime as a JSON number conforming to Unix Timestamp requirements.
57 // (i.e. the number of seconds since midnight January 1st, 1970 not considering leap seconds.)
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)
65 return buffer.Bytes(), nil
68 // UnmarshalJSON reconstitures a UnixTime saved as a JSON number of the number of seconds since
69 // midnight January 1st, 1970.
70 func (t *UnixTime) UnmarshalJSON(text []byte) error {
71 dec := json.NewDecoder(bytes.NewReader(text))
73 var secondsSinceEpoch float64
74 if err := dec.Decode(&secondsSinceEpoch); err != nil {
78 *t = NewUnixTimeFromSeconds(secondsSinceEpoch)
83 // MarshalText stores the number of seconds since the Unix Epoch as a textual floating point number.
84 func (t UnixTime) MarshalText() ([]byte, error) {
86 return cast.MarshalText()
89 // UnmarshalText populates a UnixTime with a value stored textually as a floating point number of seconds since the Unix Epoch.
90 func (t *UnixTime) UnmarshalText(raw []byte) error {
91 var unmarshaled time.Time
93 if err := unmarshaled.UnmarshalText(raw); err != nil {
97 *t = UnixTime(unmarshaled)
101 // MarshalBinary converts a UnixTime into a binary.LittleEndian float64 of nanoseconds since the epoch.
102 func (t UnixTime) MarshalBinary() ([]byte, error) {
103 buf := &bytes.Buffer{}
105 payload := int64(t.Duration())
107 if err := binary.Write(buf, binary.LittleEndian, &payload); err != nil {
111 return buf.Bytes(), nil
114 // UnmarshalBinary converts a from a binary.LittleEndian float64 of nanoseconds since the epoch into a UnixTime.
115 func (t *UnixTime) UnmarshalBinary(raw []byte) error {
116 var nanosecondsSinceEpoch int64
118 if err := binary.Read(bytes.NewReader(raw), binary.LittleEndian, &nanosecondsSinceEpoch); err != nil {
121 *t = NewUnixTimeFromNanoseconds(nanosecondsSinceEpoch)