forked from zerodha/gokiteconnect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
51 lines (42 loc) · 1.02 KB
/
utils.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
45
46
47
48
49
50
51
package kiteapi
import (
"strings"
"time"
)
// Time is custom time format used in all responses
type Time struct {
time.Time
}
// List of known time formats
var ctLayouts = []string{"2006-01-02", "2006-01-02 15:04:05", "2006-01-02T15:04:05-0700", "2006-01-02T15:04:05-07:00"}
// UnmarshalJSON parses JSON time string with custom time formats
func (t *Time) UnmarshalJSON(b []byte) (err error) {
var pTime time.Time
s := strings.TrimSpace(strings.Trim(string(b), "\""))
if len(s) == 0 || s == "null" {
t.Time = pTime
return nil
}
// Iterate through known layouts and parse time
for _, l := range ctLayouts {
pTime, err = time.Parse(l, s)
if err == nil && !pTime.IsZero() {
break
}
}
t.Time = pTime
return nil
}
// UnmarshalCSV converts CSV string field internal date
func (t *Time) UnmarshalCSV(s string) (err error) {
var pTime time.Time
s = strings.TrimSpace(s)
for _, l := range ctLayouts {
pTime, err = time.Parse(l, s)
if err == nil && !pTime.IsZero() {
break
}
}
t.Time = pTime
return nil
}