forked from brutella/canopen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
45 lines (36 loc) · 956 Bytes
/
time.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
package canopen
import (
"bytes"
"encoding/binary"
"fmt"
"time"
)
// RefDate is the reference date of CAN timestamp messages.
var RefDate = time.Date(
1984, // year
1, // month
1, // day
0, // hour
0, // min
0, // sec
0, // nsec
time.UTC, // location
)
// Timestamp returns the time encoded in the frame.
func (frm Frame) Timestamp() (*time.Time, error) {
if t := frm.MessageType(); t != MessageTypeTimestamp {
return nil, fmt.Errorf("Invalid message type % X", t)
}
if n := len(frm.Data); n != 8 {
return nil, fmt.Errorf("Invalid data length %d", n)
}
var msec int32
var day int16
msecBuf := bytes.NewBuffer(frm.Data[:6])
dayBuf := bytes.NewBuffer(frm.Data[6:8])
binary.Read(msecBuf, binary.LittleEndian, &msec)
binary.Read(dayBuf, binary.LittleEndian, &dayBuf)
daySec := int64(day) * 24 * 60 * 60
t := time.Unix(RefDate.Unix()+daySec, int64(msec)*1000)
return &t, nil
}