Debian Codegraph @

putio/time.go

raw symbols SCIP
1 package putio
2
3 import "time"
4
5 // Time is a wrapper around time.Time that can be unmarshalled from a JSON
6 // string formatted as "2016-04-19T15:44:42". All methods of time.Time can be
7 // called on Time.
8 type Time struct {
9 time.Time
10 }
11
12 func (t *Time) String() string {
13 return t.Time.String()
14 }
15
16 // UnmarshalJSON implements the json.Unmarshaler interface.
17 func (t *Time) UnmarshalJSON(data []byte) error {
18 // put.io API has inconsistent time layouts for different endpoints, such
19 // as /files and /events
20 var timeLayouts = []string{`"2006-01-02T15:04:05"`, `"2006-01-02 15:04:05"`}
21
22 s := string(data)
23 var err error
24 var tm time.Time
25 for _, layout := range timeLayouts {
26 tm, err = time.ParseInLocation(layout, s, time.UTC)
27 if err == nil {
28 t.Time = tm
29 return nil
30 }
31 }
32 return err
33 }