github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/core/ntpdate/ntpdate.go (about)

     1  // Copyright 2016-2017 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 !plan9
     6  // +build !plan9
     7  
     8  // ntpdate uses NTP to adjust the system clock.
     9  //
    10  // Synopsis:
    11  //
    12  //	ntpdate [--config=/etc/ntp.conf] [--rtc] [--verbose] [server ...]
    13  //
    14  // Description:
    15  //
    16  //	ntpdate queries NTP server(s) for time and update susyem time.
    17  //	If --rtc is specified, it updates the hardware clock as well.
    18  //	Servers to query are obtained from /etc/ntp.conf and/or the command line.
    19  //	By default --config is set to /etc/ntp.conf, config lookup can be disabled
    20  //	by setting --confg to an empty string.
    21  //	If servers are specified on the command line, they are tried first.
    22  //	time.google.com is used as the last resort.
    23  //
    24  // Options:
    25  //
    26  //	-w: set hwclock to system clock in UTC
    27  package main
    28  
    29  import (
    30  	"flag"
    31  	"log"
    32  
    33  	"github.com/mvdan/u-root-coreutils/pkg/ntpdate"
    34  )
    35  
    36  var (
    37  	config  = flag.String("config", ntpdate.DefaultNTPConfig, "NTP config file.")
    38  	setRTC  = flag.Bool("rtc", false, "Set RTC time as well")
    39  	verbose = flag.Bool("verbose", false, "Verbose output")
    40  )
    41  
    42  const (
    43  	fallback = "time.google.com"
    44  )
    45  
    46  func main() {
    47  	flag.Parse()
    48  	if *verbose {
    49  		ntpdate.Debug = log.Printf
    50  	}
    51  	server, offset, err := ntpdate.SetTime(flag.Args(), *config, fallback, *setRTC)
    52  	if err != nil {
    53  		log.Fatalf("Error: %v", err)
    54  	}
    55  	plus := ""
    56  	if offset > 0 {
    57  		plus = "+"
    58  	}
    59  	log.Printf("adjust time server %s offset %s%f sec", server, plus, offset)
    60  }