github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/examples/time-offset/time-offset.go (about) 1 package main 2 3 // This example demonstrates how to set the system time. 4 // 5 // Usually, boards don't keep time on power cycles and restarts, it resets to Unix epoch. 6 // The system time can be set by calling runtime.AdjustTimeOffset(). 7 // 8 // Possible time sources: an external RTC clock or network (WiFi access point or NTP server) 9 10 import ( 11 "fmt" 12 "runtime" 13 "time" 14 ) 15 16 const myTime = "2006-01-02T15:04:05Z" // this is an example time you source somewhere 17 18 func main() { 19 20 // measure how many nanoseconds the internal clock is behind 21 timeOfMeasurement := time.Now() 22 actualTime, _ := time.Parse(time.RFC3339, myTime) 23 offset := actualTime.Sub(timeOfMeasurement) 24 25 // adjust internal clock by adding the offset to the internal clock 26 runtime.AdjustTimeOffset(int64(offset)) 27 28 for { 29 fmt.Printf("%v\r\n", time.Now().Format(time.RFC3339)) 30 time.Sleep(5 * time.Second) 31 } 32 }