github.com/simpleiot/simpleiot@v0.18.3/system/time_linux.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package system
     5  
     6  import (
     7  	"log"
     8  	"os/exec"
     9  	"syscall"
    10  	"time"
    11  )
    12  
    13  // SetTime sets the system time
    14  func SetTime(t time.Time) (err error) {
    15  	tv := syscall.NsecToTimeval(t.UnixNano())
    16  	err = syscall.Settimeofday(&tv)
    17  	if err != nil {
    18  		log.Println("Error synchronizing system clock:", err)
    19  		return err
    20  	}
    21  
    22  	// Sync the real-time clock (RTC)
    23  	// Always store time in UTC on the RTC
    24  	err = exec.Command("hwclock", "-w", "-u").Run()
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	return nil
    30  }