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