mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-10 22:13:39 +00:00
runtime: fix time base for time.Now()
This function previously returned the atomic time, that isn't affected by system time changes but also has a time base at some arbitrary time in the past. This makes sense for baremetal platforms (which typically don't know the wall time) but it gives surprising results on Linux and macOS: time.Now() usually returns a time somewhere near the start of 1970. This commit fixes this by obtaining both time values: the monotonic time and the wall clock time. This is also how the Go runtime implements the time.now function.
This commit is contained in:
committed by
Ron Evans
parent
73cf187552
commit
03481789b0
@@ -38,8 +38,6 @@ type timespec struct {
|
||||
tv_nsec int // long: on Linux and macOS, follows the platform bitness
|
||||
}
|
||||
|
||||
const CLOCK_MONOTONIC_RAW = 4
|
||||
|
||||
var stackTop uintptr
|
||||
|
||||
func postinit() {}
|
||||
@@ -138,19 +136,31 @@ func sleepTicks(d timeUnit) {
|
||||
usleep(uint(d) / 1000)
|
||||
}
|
||||
|
||||
// Return monotonic time in nanoseconds.
|
||||
//
|
||||
// TODO: noescape
|
||||
func monotime() uint64 {
|
||||
func getTime(clock int32) uint64 {
|
||||
ts := timespec{}
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &ts)
|
||||
clock_gettime(clock, &ts)
|
||||
return uint64(ts.tv_sec)*1000*1000*1000 + uint64(ts.tv_nsec)
|
||||
}
|
||||
|
||||
// Return monotonic time in nanoseconds.
|
||||
func monotime() uint64 {
|
||||
return getTime(clock_MONOTONIC_RAW)
|
||||
}
|
||||
|
||||
func ticks() timeUnit {
|
||||
return timeUnit(monotime())
|
||||
}
|
||||
|
||||
//go:linkname now time.now
|
||||
func now() (sec int64, nsec int32, mono int64) {
|
||||
ts := timespec{}
|
||||
clock_gettime(clock_REALTIME, &ts)
|
||||
sec = int64(ts.tv_sec)
|
||||
nsec = int32(ts.tv_nsec)
|
||||
mono = nanotime()
|
||||
return
|
||||
}
|
||||
|
||||
//go:linkname syscall_Exit syscall.Exit
|
||||
func syscall_Exit(code int) {
|
||||
exit(code)
|
||||
|
||||
Reference in New Issue
Block a user