-
Notifications
You must be signed in to change notification settings - Fork 13
/
time.go
44 lines (33 loc) · 939 Bytes
/
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
package time
import (
"regexp"
"strconv"
"time"
)
type Time time.Time
// Return time as UTC
func (t Time) Time() time.Time {
return time.Time(t).UTC()
}
// String returns t as a formatted string
func (t Time) String() string {
return t.Time().String()
}
// MarshalJSON is used to convert the timestamp to JSON
func (t Time) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(time.Time(t).Unix(), 10)), nil
}
var replaceQuotes = regexp.MustCompile(`"`)
// UnmarshalJSON is used to convert the timestamp from JSON
func (t *Time) UnmarshalJSON(b []byte) (err error) {
data := string(b)
// Field may have already been a string so we need to remove quotes before parsing.
// https://harness.atlassian.net/browse/PL-24090
data = replaceQuotes.ReplaceAllString(data, "")
parsedInt, err := strconv.ParseInt(data, 10, 64)
if err != nil {
return err
}
*(*time.Time)(t) = time.Unix(parsedInt, 0)
return nil
}