forked from guitmz/n26
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp.go
More file actions
36 lines (30 loc) · 784 Bytes
/
Copy pathtimestamp.go
File metadata and controls
36 lines (30 loc) · 784 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
package n26
import (
"strconv"
"strings"
"time"
)
type TimeStamp struct {
time.Time
}
const millisAsNanos = int64(time.Millisecond)
var loc, _ = time.LoadLocation("Europe/Berlin")
func (ts *TimeStamp) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
return
}
value, err := strconv.ParseInt(s, 0, 64)
if err != nil {
return
}
//Adjust the timezone information as timestamp is given in local TZ
unadjusted := time.Unix(value/1000, (value%1000)*millisAsNanos).In(loc)
_, offset := unadjusted.Zone()
ts.Time = unadjusted.Add(time.Duration(-offset) * time.Second)
return
}
// convert the timestamp to an integer - millis since epoch
func (ts *TimeStamp) AsMillis() int64 {
return ts.UnixNano() / int64(time.Millisecond)
}