-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtime.go
More file actions
80 lines (68 loc) · 2.03 KB
/
time.go
File metadata and controls
80 lines (68 loc) · 2.03 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package dexcom
import (
"time"
)
const (
// JSONTimeLayout specifies the format for JSON time values.
JSONTimeLayout = time.RFC3339
// UserTimeLayout specifies a consistent, human-readable format for local time.
UserTimeLayout = "2006-01-02 15:04:05"
)
var (
dexcomEpoch = time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC)
)
func toTime(t int64) time.Time {
u := dexcomEpoch.Add(time.Duration(t) * time.Second)
// Construct the corresponding value in the local timezone.
year, month, day := u.Date()
hour, min, sec := u.Clock()
return time.Date(year, month, day, hour, min, sec, 0, time.Local)
}
func fromTime(t time.Time) int64 {
// Construct the corresponding value in UTC.
year, month, day := t.Date()
hour, min, sec := t.Clock()
u := time.Date(year, month, day, hour, min, sec, 0, time.UTC)
return int64(u.Sub(dexcomEpoch) / time.Second)
}
func unmarshalTime(v []byte) time.Time {
return toTime(int64(unmarshalUint32(v)))
}
// A Timestamp contains system and display time values.
type Timestamp struct {
SystemTime time.Time
DisplayTime time.Time
}
func (r *Timestamp) unmarshal(v []byte) {
r.SystemTime = unmarshalTime(v[0:4])
r.DisplayTime = unmarshalTime(v[4:8])
}
func displayTime(sys uint32, offset int32) time.Time {
return toTime(int64(sys) + int64(offset))
}
// ReadDisplayTime returns the Dexcom receiver's display time.
// SystemTime = RTC + SystemTimeOffset
// DisplayTime = SystemTime + DisplayTimeOffset
func (cgm *CGM) ReadDisplayTime() time.Time {
v := cgm.Cmd(ReadDisplayTimeOffset)
if cgm.Error() != nil {
return time.Time{}
}
displayOffset := unmarshalInt32(v)
v = cgm.Cmd(ReadSystemTime)
if cgm.Error() != nil {
return time.Time{}
}
sysTime := unmarshalUint32(v)
return displayTime(sysTime, displayOffset)
}
// SetDisplayTime sets the Dexcom receiver's display time.
func (cgm *CGM) SetDisplayTime(t time.Time) {
v := cgm.Cmd(ReadSystemTime)
if cgm.Error() != nil {
return
}
sysTime := unmarshalUint32(v)
offset := int32(fromTime(t) - int64(sysTime))
cgm.Cmd(WriteDisplayTimeOffset, marshalInt32(offset)...)
}