forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtc_linux.go
62 lines (50 loc) · 1.3 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
// 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.
//go:build linux
package rtc
import (
"time"
"golang.org/x/sys/unix"
)
type syscalls interface {
ioctlGetRTCTime(int) (*unix.RTCTime, error)
ioctlSetRTCTime(int, *unix.RTCTime) error
}
type realSyscalls struct{}
func (sc realSyscalls) ioctlGetRTCTime(fd int) (*unix.RTCTime, error) {
return unix.IoctlGetRTCTime(fd)
}
func (sc realSyscalls) ioctlSetRTCTime(fd int, time *unix.RTCTime) error {
return unix.IoctlSetRTCTime(fd, time)
}
// Read implements Read for the Linux RTC
func (r *RTC) Read() (time.Time, error) {
rt, err := r.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
}
// Set implements Set for the Linux RTC
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 r.ioctlSetRTCTime(int(r.file.Fd()), &rt)
}