Skip to content

Commit ff89fc4

Browse files
ysoldakdeadprogram
authored andcommitted
example: adjust time offset
1 parent a3eafa6 commit ff89fc4

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ smoketest:
478478
@$(MD5SUM) test.hex
479479
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/test
480480
@$(MD5SUM) test.hex
481+
$(TINYGO) build -size short -o test.hex -target=pca10040 examples/time-offset
482+
@$(MD5SUM) test.hex
481483
$(TINYGO) build -size short -o test.hex -target=wioterminal examples/hid-mouse
482484
@$(MD5SUM) test.hex
483485
$(TINYGO) build -size short -o test.hex -target=wioterminal examples/hid-keyboard
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

0 commit comments

Comments
 (0)