aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/runtime/runtime.go
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2019-11-09 20:47:13 +0100
committerRon Evans <[email protected]>2019-11-10 08:54:06 +0100
commit405c0263b078a0335c7f013d48600497ac9ca4d3 (patch)
tree0711f471ecbc31275b9184c661c2bce88ee269da /src/runtime/runtime.go
parent45b5decb4edc6dee271ca544ea144736a447a9c4 (diff)
downloadtinygo-405c0263b078a0335c7f013d48600497ac9ca4d3.tar.gz
tinygo-405c0263b078a0335c7f013d48600497ac9ca4d3.zip
runtime: add AdjustTimeOffset to update current time
This function adjusts the time returned by time.Now() and similar functions. This is necessary on bare metal systems, where there would not be a way to adjust the time otherwise.
Diffstat (limited to 'src/runtime/runtime.go')
-rw-r--r--src/runtime/runtime.go17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/runtime/runtime.go b/src/runtime/runtime.go
index c316bf364..13f3ab3c9 100644
--- a/src/runtime/runtime.go
+++ b/src/runtime/runtime.go
@@ -83,14 +83,27 @@ func nanotime() int64 {
return int64(ticks()) * tickMicros
}
+// timeOffset is how long the monotonic clock started after the Unix epoch. It
+// should be a positive integer under normal operation or zero when it has not
+// been set.
+var timeOffset int64
+
//go:linkname now time.now
func now() (sec int64, nsec int32, mono int64) {
mono = nanotime()
- sec = mono / (1000 * 1000 * 1000)
- nsec = int32(mono - sec*(1000*1000*1000))
+ sec = (mono + timeOffset) / (1000 * 1000 * 1000)
+ nsec = int32((mono + timeOffset) - sec*(1000*1000*1000))
return
}
+// AdjustTimeOffset adds the given offset to the built-in time offset. A
+// positive value adds to the time (skipping some time), a negative value moves
+// the clock into the past.
+func AdjustTimeOffset(offset int64) {
+ // TODO: do this atomically?
+ timeOffset += offset
+}
+
// Copied from the Go runtime source code.
//go:linkname os_sigpipe os.sigpipe
func os_sigpipe() {