-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
66 lines (51 loc) · 1.28 KB
/
types.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ots
import (
"strconv"
"strings"
"time"
)
type (
// Status API status.
Status int
// State metadata state.
State string
)
// Duration type.
type Duration time.Duration
// MarshalJSON marshal duration to json value.
func (d *Duration) MarshalJSON() ([]byte, error) {
s := int64(d.Duration().Truncate(time.Second).Seconds())
return strconv.AppendInt(nil, s, 10), nil
}
// UnmarshalJSON unmarshal duration from json value.
func (d *Duration) UnmarshalJSON(s []byte) (err error) {
q, err := strconv.ParseInt(strings.Trim(string(s), `"`), 10, 64)
if err != nil {
return err
}
*(*time.Duration)(d) = time.Duration(q) * time.Second
return
}
// Duration returns value as time.Duration.
func (d Duration) Duration() time.Duration {
return time.Duration(d)
}
// Time type.
type Time time.Time
// MarshalJSON marshal time to json value.
func (t *Time) MarshalJSON() ([]byte, error) {
return strconv.AppendInt(nil, t.Time().Unix(), 10), nil
}
// UnmarshalJSON unmarshal time from json value.
func (t *Time) UnmarshalJSON(s []byte) error {
q, err := strconv.ParseInt(strings.Trim(string(s), `"`), 10, 64)
if err != nil {
return err
}
*(*time.Time)(t) = time.Unix(q, 0)
return nil
}
// Time returns value as time.Time.
func (t Time) Time() time.Time {
return time.Time(t)
}