gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/hwclock/hwclock_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  // hwclock reads or changes the hardware clock (RTC) in UTC format.
     6  //
     7  // Synopsis:
     8  //     hwclock [-w]
     9  //
    10  // Description:
    11  //     It prints the current hwclock time in UTC if called without any flags.
    12  //     It sets the hwclock to the system clock in UTC if called with -w.
    13  //
    14  // Options:
    15  //     -w: set hwclock to system clock in UTC
    16  package main
    17  
    18  import (
    19  	"flag"
    20  	"fmt"
    21  	"log"
    22  	"time"
    23  
    24  	"github.com/u-root/u-root/pkg/rtc"
    25  )
    26  
    27  var write = flag.Bool("w", false, "Set hwclock from system clock in UTC")
    28  
    29  func main() {
    30  	flag.Parse()
    31  
    32  	r, err := rtc.OpenRTC()
    33  	if err != nil {
    34  		log.Fatal(err)
    35  	}
    36  
    37  	if *write {
    38  		tu := time.Now().UTC()
    39  		if err := r.Set(tu); err != nil {
    40  			log.Fatal(err)
    41  		}
    42  	}
    43  
    44  	t, err := r.Read()
    45  	if err != nil {
    46  		log.Fatal(err)
    47  	}
    48  
    49  	// Print local time. Match the format of util-linux' hwclock.
    50  	fmt.Println(t.Local().Format("Mon 2 Jan 2006 15:04:05 AM MST"))
    51  }