github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/rtc/rtc_linux.go (about) 1 // Copyright 2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build linux 6 7 package rtc 8 9 import ( 10 "time" 11 12 "golang.org/x/sys/unix" 13 ) 14 15 type syscalls interface { 16 ioctlGetRTCTime(int) (*unix.RTCTime, error) 17 ioctlSetRTCTime(int, *unix.RTCTime) error 18 } 19 20 type realSyscalls struct{} 21 22 func (sc realSyscalls) ioctlGetRTCTime(fd int) (*unix.RTCTime, error) { 23 return unix.IoctlGetRTCTime(fd) 24 } 25 26 func (sc realSyscalls) ioctlSetRTCTime(fd int, time *unix.RTCTime) error { 27 return unix.IoctlSetRTCTime(fd, time) 28 } 29 30 // Read implements Read for the Linux RTC 31 func (r *RTC) Read() (time.Time, error) { 32 rt, err := r.ioctlGetRTCTime(int(r.file.Fd())) 33 if err != nil { 34 return time.Time{}, err 35 } 36 37 return time.Date(int(rt.Year)+1900, 38 time.Month(rt.Mon+1), 39 int(rt.Mday), 40 int(rt.Hour), 41 int(rt.Min), 42 int(rt.Sec), 43 0, 44 time.UTC), nil 45 } 46 47 // Set implements Set for the Linux RTC 48 func (r *RTC) Set(tu time.Time) error { 49 rt := unix.RTCTime{ 50 Sec: int32(tu.Second()), 51 Min: int32(tu.Minute()), 52 Hour: int32(tu.Hour()), 53 Mday: int32(tu.Day()), 54 Mon: int32(tu.Month() - 1), 55 Year: int32(tu.Year() - 1900), 56 Wday: int32(0), 57 Yday: int32(0), 58 Isdst: int32(0), 59 } 60 61 return r.ioctlSetRTCTime(int(r.file.Fd()), &rt) 62 }