-
Notifications
You must be signed in to change notification settings - Fork 2
/
type_time.go
100 lines (90 loc) · 2.84 KB
/
type_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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package zcl
import (
"fmt"
"time"
)
// UnixtimeOffset specifies the offset between zigbee time (epoch = 2000-01-01 UTC) and unixtime in seconds
const UnixtimeOffset = 946684800
// Zdate is Date (0xffffffff = invalid). A/D = A
type Zdate struct {
Year int
Month time.Month
Day int
Weekday time.Weekday
}
// Ztime is Time of day (0xffffffff = invalid). A/D = A
type Ztime uint32
// Zutc is UTCTime (0xffffffff = invalid). A/D = A
type Zutc uint32
func (t *Ztime) UnmarshalZcl(buf []byte) ([]byte, error) {
// (uint32) H(0) M(1) S(2) Centi(3)
val, buf, err := uintLEUnmarshalZcl(4, buf)
*t = Ztime(val)
return buf, err
}
func (t Ztime) MarshalZcl() ([]byte, error) { return uintLEMarshalZcl(4, uint64(t)) }
func (t *Ztime) Values() []Val { return []Val{t} }
func (t Ztime) TypeID() TypeID { return 224 }
func (t Ztime) String() string {
h := (t & 0xFF000000) >> 24
m := (t & 0x00FF0000) >> 16
s := (t & 0x0000FF00) >> 8
c := t & 0x000000FF
return fmt.Sprintf("%02d:%02d:%02d.%02d", h, m, s, c)
}
func (t *Ztime) SetTime(hr, min, sec, hundreths int) {
*t = Ztime(((hr & 0xFF) << 24) | ((min & 0xFF) << 16) | ((sec & 0xFF) << 8) | (hundreths & 0xFF))
}
func (t Ztime) Valid() bool { return t != 0xFFFFFFFF }
func (d *Zdate) UnmarshalZcl(buf []byte) ([]byte, error) {
// (uint32) Y=(b[0]+1900) M=b[1] D=b[2] DoW=b[3] (1-7 mon-sun)
val, buf, err := uintLEUnmarshalZcl(4, buf)
if err != nil {
return buf, err
}
if val == 0xffffffff {
return buf, err
}
d.Year = int((val&0xFF000000)>>24 + 1900)
d.Month = time.Month((val & 0xFF0000) >> 16)
d.Day = int((val & 0xFF00) >> 8)
d.Weekday = time.Weekday(val & 0xFF)
if d.Weekday == 7 {
d.Weekday = time.Sunday
}
return buf, err
}
func (d Zdate) MarshalZcl() ([]byte, error) {
var val uint64
if d.Year == 0 {
val = 0xffffffff
} else {
wd := int(d.Weekday)
if wd == 0 {
wd = 7
}
val = uint64(((d.Year-1900)&0xFF)<<24) |
uint64((d.Month&0xFF)<<16) |
uint64((d.Day&0xFF)<<8) |
uint64(wd&0xFF)
}
return uintLEMarshalZcl(4, val)
}
func (d *Zdate) Values() []Val { return []Val{d} }
func (d Zdate) TypeID() TypeID { return 225 }
func (d Zdate) String() string { return fmt.Sprintf("%04d-%02d-%02d", d.Year, d.Month, d.Day) }
func (d Zdate) Valid() bool { return d.Year > 0 }
func (u *Zutc) UnmarshalZcl(buf []byte) ([]byte, error) {
val, buf, err := uintLEUnmarshalZcl(4, buf)
*u = Zutc(val)
return buf, err
}
func (u Zutc) MarshalZcl() ([]byte, error) { return uintLEMarshalZcl(4, uint64(u)) }
func (u *Zutc) Values() []Val { return []Val{u} }
func (u Zutc) TypeID() TypeID { return 226 }
func (u Zutc) String() string { return u.Time().String() }
func (u Zutc) Time() time.Time {
// (uint32) Seconds since midnight 2000-01-01
return time.Unix(int64(u)+UnixtimeOffset, 0)
}
func (u Zutc) Valid() bool { return u != 0xFFFFFFFF }