forked from igungor/go-putio
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtime.go
36 lines (31 loc) · 824 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
package putio
import (
"fmt"
"time"
)
// Time is a wrapper around time.Time that can be unmarshalled from a JSON
// string formatted as "2016-04-19T15:44:42". All methods of time.Time can be
// called on Time.
type Time struct {
time.Time
}
func (t *Time) String() string {
return t.Time.String()
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *Time) UnmarshalJSON(data []byte) error {
// put.io API has inconsistent time layouts for different endpoints, such
// as /files and /events
timeLayouts := []string{`"2006-01-02T15:04:05"`, `"2006-01-02 15:04:05"`}
s := string(data)
var err error
var tm time.Time
for _, layout := range timeLayouts {
tm, err = time.ParseInLocation(layout, s, time.UTC)
if err == nil {
t.Time = tm
return nil
}
}
return fmt.Errorf("%w", err)
}