-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
time.go
140 lines (105 loc) · 2.76 KB
/
time.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package discord
import (
"encoding/json"
"strconv"
"strings"
"time"
)
// Timestamp has a valid zero-value, which can be checked using the Valid()
// method. This is useful for optional timestamps such as EditedTimestamp.
type Timestamp time.Time
const TimestampFormat = time.RFC3339 // same as ISO8601
var (
_ json.Unmarshaler = (*Timestamp)(nil)
_ json.Marshaler = (*Timestamp)(nil)
)
func NewTimestamp(t time.Time) Timestamp {
return Timestamp(t)
}
func NowTimestamp() Timestamp {
return NewTimestamp(time.Now())
}
// UnmarshalJSON parses a nullable RFC3339 string into time.
func (t *Timestamp) UnmarshalJSON(v []byte) error {
str := strings.Trim(string(v), `"`)
if str == "null" {
return nil
}
r, err := time.Parse(TimestampFormat, str)
if err != nil {
return err
}
*t = Timestamp(r)
return nil
}
// MarshalJSON returns null if Timestamp is not valid (zero). It returns the
// time formatted in RFC3339 otherwise.
func (t Timestamp) MarshalJSON() ([]byte, error) {
if !t.Valid() {
return []byte("null"), nil
}
return []byte(`"` + t.Format(TimestampFormat) + `"`), nil
}
func (t Timestamp) Valid() bool {
return !t.Time().IsZero()
}
func (t Timestamp) Format(fmt string) string {
return t.Time().Format(fmt)
}
func (t Timestamp) Time() time.Time {
return time.Time(t)
}
//
type UnixTimestamp int64
func (t UnixTimestamp) String() string {
return t.Time().String()
}
func (t UnixTimestamp) Time() time.Time {
return time.Unix(int64(t), 0)
}
//
type UnixMsTimestamp int64
func TimeToMilliseconds(t time.Time) UnixMsTimestamp {
return UnixMsTimestamp(t.UnixNano() / int64(time.Millisecond))
}
func (t UnixMsTimestamp) String() string {
return t.Time().String()
}
func (t UnixMsTimestamp) Time() time.Time {
return time.Unix(0, int64(t)*int64(time.Millisecond))
}
//
type Seconds int
// NullSecond is used in cases where null should be used instead of a number or
// omitted. This is similar to NullSnowflake.
const NullSecond = -1
func DurationToSeconds(dura time.Duration) Seconds {
return Seconds(dura.Seconds())
}
func (s Seconds) MarshalJSON() ([]byte, error) {
if s < 1 {
return []byte("null"), nil
} else {
return []byte(strconv.Itoa(int(s))), nil
}
}
func (s Seconds) String() string {
return s.Duration().String()
}
func (s Seconds) Duration() time.Duration {
return time.Duration(s) * time.Second
}
//
// Milliseconds is in float64 because some Discord events return time with a
// trailing decimal.
type Milliseconds float64
func DurationToMilliseconds(dura time.Duration) Milliseconds {
return Milliseconds(dura.Milliseconds())
}
func (ms Milliseconds) String() string {
return ms.Duration().String()
}
func (ms Milliseconds) Duration() time.Duration {
const f64ms = Milliseconds(time.Millisecond)
return time.Duration(ms * f64ms)
}