-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime.go
More file actions
40 lines (30 loc) · 761 Bytes
/
Copy pathtime.go
File metadata and controls
40 lines (30 loc) · 761 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
// Copyright 2018 DataArt. All rights reserved.
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package devicehive_go
import (
"strings"
"time"
)
// Custom timestamp in ISO8601 format
type ISO8601Time struct {
time.Time
}
func (t *ISO8601Time) String() string {
return t.Time.Format(timestampLayout)
}
func (t *ISO8601Time) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" || s == "" {
t.Time = time.Time{}
return
}
t.Time, err = time.Parse(timestampLayout, s)
return
}
func (t *ISO8601Time) MarshalJSON() (b []byte, err error) {
if t.Time.UnixNano() <= 0 {
return []byte("\"\""), nil
}
return []byte("\"" + t.String() + "\""), nil
}