diff --git a/src/runtime/runtime.go b/src/runtime/runtime.go index c316bf36..13f3ab3c 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() {