-
Notifications
You must be signed in to change notification settings - Fork 3
/
float_ms.go
44 lines (36 loc) · 994 Bytes
/
float_ms.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package epoch
import (
"encoding/json"
"fmt"
"math"
"time"
)
// FloatMS - integer part of timestamp represents seconds and fractional - milliseconds since
// the Epoch(Unix time), e.g.
//
// 1136239445.999
//
// Inherits built-in time.Time type, thus has all it methods, but has custom serializer and
// deserializer(converts float timestamp into built-in time.Time and vice versa).
type FloatMS struct {
time.Time
}
// NewFloatMS - returns FloatMS
func NewFloatMS(t time.Time) FloatMS {
return FloatMS{Time: t}
}
// MarshalJSON - implements JSON marshaling interface
func (s FloatMS) MarshalJSON() ([]byte, error) {
milli := s.Time.UnixMilli()
f := float64(milli) / float64(msPerS)
return json.Marshal(f)
}
func (s *FloatMS) UnmarshalJSON(data []byte) error {
f, err := parseFloat64(string(data))
if err != nil {
return fmt.Errorf("failed to parse epoch.FloatMS: %w", err)
}
i, frac := math.Modf(f)
s.Time = time.Unix(int64(i), int64(frac*1_000)*nsPerMs)
return nil
}