-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_feat.go
42 lines (35 loc) · 1.03 KB
/
time_feat.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
package goraven
import (
"encoding/xml"
"strconv"
"time"
)
// Send the GET_TIME command to get the current time. The RAVEn will send
// a TimeCluster notification in response
func (r *Raven) GetTime() error {
return r.simpleCommand("get_time", false)
}
// GetUTCTime() is a convenience function to get the UTC time contained
// in a TimeCluster notify
func (t *TimeCluster) GetUTCTime() (time.Time, error) {
return getTime(t.UTCTime)
}
// GetLocalTime() is a convenience function to get the Local time contained
// in a TimeCluster notify
func (t *TimeCluster) GetLocalTime() (time.Time, error) {
return getTime(t.LocalTime)
}
func getTime(hex string) (time.Time, error) {
sec, err := strconv.ParseInt(hex, 0, 0)
if err != nil {
return time.Unix(0, 0), err
}
return time.Unix(sec+946684800, 0), nil
}
type TimeCluster struct {
XMLName xml.Name `xml:"TimeCluster"`
DeviceMacId string `xml:"DeviceMacId"`
MeterMacId string `xml:"MeterMacId"`
UTCTime string `xml:"UTCTime"`
LocalTime string `xml:"LocalTime"`
}