-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime.go
More file actions
51 lines (39 loc) · 1010 Bytes
/
datetime.go
File metadata and controls
51 lines (39 loc) · 1010 Bytes
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
package bot_api_client
import (
"time"
)
const RFC3339Micro string = "2006-01-02T15:04:05.999999Z07:00"
type DateTimeRFC3339 struct {
Time time.Time `json:"time"`
}
type DateTimeRFC3339Micro struct {
Time time.Time `json:"time"`
}
func (t DateTimeRFC3339) MarshalJSON() ([]byte, error) {
return []byte(`"` + t.Time.Format(time.RFC3339) + `"`), nil
}
func (t *DateTimeRFC3339) UnmarshalJSON(data []byte) error {
if len(data) == 2 && string(data) == `""` {
return nil
}
parsedTime, err := time.Parse(`"`+time.RFC3339+`"`, string(data))
if err != nil {
return err
}
t.Time = parsedTime
return nil
}
func (t DateTimeRFC3339Micro) MarshalJSON() ([]byte, error) {
return []byte(`"` + t.Time.Format(RFC3339Micro) + `"`), nil
}
func (t *DateTimeRFC3339Micro) UnmarshalJSON(data []byte) error {
if len(data) == 2 && string(data) == `""` {
return nil
}
parsedTime, err := time.Parse(`"`+RFC3339Micro+`"`, string(data))
if err != nil {
return err
}
t.Time = parsedTime
return nil
}