diff --git a/_demo/gotime/time.go b/_demo/gotime/time.go new file mode 100644 index 00000000..36b2d57c --- /dev/null +++ b/_demo/gotime/time.go @@ -0,0 +1,7 @@ +package main + +import "time" + +func main() { + println(time.Now().String()) +} diff --git a/internal/lib/time/time.go b/internal/lib/time/time.go index 8f201a33..06b8262b 100644 --- a/internal/lib/time/time.go +++ b/internal/lib/time/time.go @@ -18,7 +18,11 @@ package time // llgo:skipall import ( + "unsafe" _ "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/time" ) type Time struct { @@ -383,6 +387,45 @@ func daysSinceEpoch(year int) uint64 { return d } +// Provided by package runtime. +func now() (sec int64, nsec int32, mono int64) { + tv := (*time.Timespec)(c.Alloca(unsafe.Sizeof(time.Timespec{}))) + time.ClockGettime(time.CLOCK_REALTIME, tv) + sec = int64(tv.Sec) + nsec = int32(tv.Nsec) + mono = runtimeNano() + return +} + +// runtimeNano returns the current value of the runtime clock in nanoseconds. +func runtimeNano() int64 { + tv := (*time.Timespec)(c.Alloca(unsafe.Sizeof(time.Timespec{}))) + time.ClockGettime(time.CLOCK_MONOTONIC, tv) + return int64(tv.Sec)<>33 != 0 { + // Seconds field overflowed the 33 bits available when + // storing a monotonic time. This will be true after + // March 16, 2157. + return Time{uint64(nsec), sec + minWall, Local} + } + return Time{hasMonotonic | uint64(sec)<