diff options
author | Yurii Soldak <[email protected]> | 2023-01-21 12:02:56 +0100 |
---|---|---|
committer | Ron Evans <[email protected]> | 2023-07-02 18:25:20 +0200 |
commit | dc91c96305eaef38e55ff6715597de2f62565a78 (patch) | |
tree | 5436648184d1c2f8fd769133445f3cef4506d901 | |
parent | 08b3a4576d3f235a896077cf748cd224efcab6c3 (diff) | |
download | tinygo-dc91c96305eaef38e55ff6715597de2f62565a78.tar.gz tinygo-dc91c96305eaef38e55ff6715597de2f62565a78.zip |
example: adjust time offset
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/examples/time-offset/time-offset.go | 32 |
2 files changed, 34 insertions, 0 deletions
@@ -478,6 +478,8 @@ smoketest: @$(MD5SUM) test.hex $(TINYGO) build -size short -o test.hex -target=pca10040 examples/test @$(MD5SUM) test.hex + $(TINYGO) build -size short -o test.hex -target=pca10040 examples/time-offset + @$(MD5SUM) test.hex $(TINYGO) build -size short -o test.hex -target=wioterminal examples/hid-mouse @$(MD5SUM) test.hex $(TINYGO) build -size short -o test.hex -target=wioterminal examples/hid-keyboard diff --git a/src/examples/time-offset/time-offset.go b/src/examples/time-offset/time-offset.go new file mode 100644 index 000000000..ef84997f7 --- /dev/null +++ b/src/examples/time-offset/time-offset.go @@ -0,0 +1,32 @@ +package main + +// This example demonstrates how to set the system time. +// +// Usually, boards don't keep time on power cycles and restarts, it resets to Unix epoch. +// The system time can be set by calling runtime.AdjustTimeOffset(). +// +// Possible time sources: an external RTC clock or network (WiFi access point or NTP server) + +import ( + "fmt" + "runtime" + "time" +) + +const myTime = "2006-01-02T15:04:05Z" // this is an example time you source somewhere + +func main() { + + // measure how many nanoseconds the internal clock is behind + timeOfMeasurement := time.Now() + actualTime, _ := time.Parse(time.RFC3339, myTime) + offset := actualTime.Sub(timeOfMeasurement) + + // adjust internal clock by adding the offset to the internal clock + runtime.AdjustTimeOffset(int64(offset)) + + for { + fmt.Printf("%v\r\n", time.Now().Format(time.RFC3339)) + time.Sleep(5 * time.Second) + } +} |