duration.go raw
1 package marshaler
2
3 import (
4 "encoding/json"
5 "time"
6 )
7
8 // Duration implements a JSON Marshaler to encode a time.Duration in milliseconds.
9 type Duration int64
10
11 const milliSec = Duration(time.Millisecond)
12
13 // NewDuration converts a *time.Duration to a *Duration type.
14 func NewDuration(t *time.Duration) *Duration {
15 if t == nil {
16 return nil
17 }
18 d := Duration(t.Nanoseconds())
19 return &d
20 }
21
22 // Standard converts a *Duration to a *time.Duration type.
23 func (d *Duration) Standard() *time.Duration {
24 return (*time.Duration)(d)
25 }
26
27 // MarshalJSON encodes the Duration in milliseconds.
28 func (d Duration) MarshalJSON() ([]byte, error) {
29 return json.Marshal(int64(d / milliSec))
30 }
31
32 // UnmarshalJSON decodes milliseconds to Duration.
33 func (d *Duration) UnmarshalJSON(b []byte) error {
34 var tmp int64
35 err := json.Unmarshal(b, &tmp)
36 if err != nil {
37 return err
38 }
39 *d = Duration(tmp) * milliSec
40 return nil
41 }
42
43 // DurationSlice is a slice of *Duration
44 type DurationSlice []*Duration
45
46 // NewDurationSlice converts a []*time.Duration to a DurationSlice type.
47 func NewDurationSlice(t []*time.Duration) DurationSlice {
48 ds := make([]*Duration, len(t))
49 for i := range ds {
50 ds[i] = NewDuration(t[i])
51 }
52 return ds
53 }
54
55 // Standard converts a DurationSlice to a []*time.Duration type.
56 func (ds *DurationSlice) Standard() []*time.Duration {
57 t := make([]*time.Duration, len(*ds))
58 for i := range t {
59 t[i] = (*ds)[i].Standard()
60 }
61 return t
62 }
63
64 // Durationint32Map is a int32 map of *Duration
65 type Durationint32Map map[int32]*Duration
66
67 // NewDurationint32Map converts a map[int32]*time.Duration to a Durationint32Map type.
68 func NewDurationint32Map(t map[int32]*time.Duration) Durationint32Map {
69 dm := make(Durationint32Map, len(t))
70 for i := range t {
71 dm[i] = NewDuration(t[i])
72 }
73 return dm
74 }
75
76 // Standard converts a Durationint32Map to a map[int32]*time.Duration type.
77 func (dm *Durationint32Map) Standard() map[int32]*time.Duration {
78 t := make(map[int32]*time.Duration, len(*dm))
79 for key, value := range *dm {
80 t[key] = value.Standard()
81 }
82 return t
83 }
84
85 // LongDuration implements a JSON Marshaler to encode a time.Duration in days.
86 type LongDuration int64
87
88 const day = LongDuration(time.Hour) * 24
89
90 // NewLongDuration converts a *time.Duration to a *LongDuration type.
91 func NewLongDuration(t *time.Duration) *LongDuration {
92 if t == nil {
93 return nil
94 }
95 d := LongDuration(t.Nanoseconds())
96 return &d
97 }
98
99 // Standard converts a *LongDuration to a *time.Duration type.
100 func (d *LongDuration) Standard() *time.Duration {
101 return (*time.Duration)(d)
102 }
103
104 // MarshalJSON encodes the LongDuration in days.
105 func (d LongDuration) MarshalJSON() ([]byte, error) {
106 return json.Marshal(int64(d / day))
107 }
108
109 // UnmarshalJSON decodes days to LongDuration.
110 func (d *LongDuration) UnmarshalJSON(b []byte) error {
111 var tmp int64
112 err := json.Unmarshal(b, &tmp)
113 if err != nil {
114 return err
115 }
116 *d = LongDuration(tmp) * day
117 return nil
118 }
119
120 // LongDurationSlice is a slice of *LongDuration
121 type LongDurationSlice []*LongDuration
122
123 // NewLongDurationSlice converts a []*time.Duration to a LongDurationSlice type.
124 func NewLongDurationSlice(t []*time.Duration) LongDurationSlice {
125 ds := make([]*LongDuration, len(t))
126 for i := range ds {
127 ds[i] = NewLongDuration(t[i])
128 }
129 return ds
130 }
131
132 // Standard converts a LongDurationSlice to a []*time.Duration type.
133 func (ds *LongDurationSlice) Standard() []*time.Duration {
134 t := make([]*time.Duration, len(*ds))
135 for i := range t {
136 t[i] = (*ds)[i].Standard()
137 }
138 return t
139 }
140
141 // LongDurationint32Map is a int32 map of *LongDuration
142 type LongDurationint32Map map[int32]*LongDuration
143
144 // NewLongDurationint32Map converts a map[int32]*time.LongDuration to a LongDurationint32Map type.
145 func NewLongDurationint32Map(t map[int32]*time.Duration) LongDurationint32Map {
146 dm := make(LongDurationint32Map, len(t))
147 for i := range t {
148 dm[i] = NewLongDuration(t[i])
149 }
150 return dm
151 }
152
153 // Standard converts a LongDurationint32Map to a map[int32]*time.LongDuration type.
154 func (dm *LongDurationint32Map) Standard() map[int32]*time.Duration {
155 t := make(map[int32]*time.Duration, len(*dm))
156 for key, value := range *dm {
157 t[key] = value.Standard()
158 }
159 return t
160 }
161