forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtc_linux.go
66 lines (55 loc) · 1.19 KB
/
rtc_linux.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
// Copyright 2019 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rtc
import (
"errors"
"os"
"time"
"golang.org/x/sys/unix"
)
type RTC struct {
file *os.File
}
func OpenRTC() (*RTC, error) {
devs := []string{
"/dev/rtc",
"/dev/rtc0",
"/dev/misc/rtc0",
}
for _, dev := range devs {
f, err := os.Open(dev)
if err == nil {
return &RTC{f}, err
} else if !os.IsNotExist(err) {
return nil, err
}
}
return nil, errors.New("No RTC device found")
}
func (r *RTC) Read() (time.Time, error) {
rt, err := unix.IoctlGetRTCTime(int(r.file.Fd()))
if err != nil {
return time.Time{}, err
}
return time.Date(int(rt.Year)+1900,
time.Month(rt.Mon+1),
int(rt.Mday),
int(rt.Hour),
int(rt.Min),
int(rt.Sec),
0,
time.UTC), nil
}
func (r *RTC) Set(tu time.Time) error {
rt := unix.RTCTime{Sec: int32(tu.Second()),
Min: int32(tu.Minute()),
Hour: int32(tu.Hour()),
Mday: int32(tu.Day()),
Mon: int32(tu.Month() - 1),
Year: int32(tu.Year() - 1900),
Wday: int32(0),
Yday: int32(0),
Isdst: int32(0)}
return unix.IoctlSetRTCTime(int(r.file.Fd()), &rt)
}