forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hwclock_linux.go
51 lines (42 loc) · 1.01 KB
/
hwclock_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
// 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.
// Read or set the hardware clock (RTC) in UTC format.
//
// Synopsis:
// hwclock [-w]
//
// Description:
// It prints the current hwclock time in UTC if called without any flags.
// It sets the hwclock to the system clock in UTC if called with -w.
//
// Options:
// -w: set hwclock to system clock in UTC
package main
import (
"flag"
"fmt"
"log"
"time"
"github.com/u-root/u-root/pkg/rtc"
)
var write = flag.Bool("w", false, "Set hwclock from system clock in UTC")
func main() {
flag.Parse()
r, err := rtc.OpenRTC()
if err != nil {
log.Fatal(err)
}
if *write {
tu := time.Now().UTC()
if err := r.Set(tu); err != nil {
log.Fatal(err)
}
}
t, err := r.Read()
if err != nil {
log.Fatal(err)
}
// Print local time. Match the format of util-linux' hwclock.
fmt.Println(t.Local().Format("Mon 2 Jan 2006 15:04:05 AM MST"))
}