c/time; patch: time

This commit is contained in:
xushiwei
2024-06-26 19:36:39 +08:00
parent 48a1384197
commit 137e93319e
6 changed files with 562 additions and 46 deletions

23
c/c.go
View File

@@ -17,6 +17,9 @@
package c
// typedef unsigned int uint;
// typedef unsigned long ulong;
// typedef unsigned long long ulonglong;
// typedef long long longlong;
import "C"
import "unsafe"
@@ -25,20 +28,22 @@ const (
)
type (
Char = int8
Long = int32
Ulong = uint32
LongLong = int64
UlongLong = uint64
Float = float32
Double = float64
Pointer = unsafe.Pointer
FilePtr = unsafe.Pointer
Char = int8
Float = float32
Double = float64
Pointer = unsafe.Pointer
FilePtr = unsafe.Pointer
)
type (
Int C.int
Uint C.uint
Long C.long
Ulong C.ulong
LongLong C.longlong
UlongLong C.ulonglong
)
type integer interface {

View File

@@ -143,10 +143,10 @@ func (a *CondAttr) Init(attr *CondAttr) c.Int { return 0 }
func (a *CondAttr) Destroy() {}
// llgo:link (*CondAttr).SetClock C.pthread_condattr_setclock
func (a *CondAttr) SetClock(clock time.ClockID) c.Int { return 0 }
func (a *CondAttr) SetClock(clock time.ClockidT) c.Int { return 0 }
// llgo:link (*CondAttr).GetClock C.pthread_condattr_getclock
func (a *CondAttr) GetClock(clock *time.ClockID) c.Int { return 0 }
func (a *CondAttr) GetClock(clock *time.ClockidT) c.Int { return 0 }
// -----------------------------------------------------------------------------

View File

@@ -21,6 +21,8 @@ import "C"
import (
_ "unsafe"
"github.com/goplus/llgo/c"
)
const (
@@ -29,10 +31,102 @@ const (
// -----------------------------------------------------------------------------
type Timespec C.struct_timespec
type TimeT C.time_t
//go:linkname Time C.time
func Time(timer *TimeT) TimeT
//go:linkname Mktime C.mktime
func Mktime(timer *Tm) TimeT
//go:linkname Ctime C.ctime
func Ctime(timer *TimeT) string
//go:linkname Difftime C.difftime
func Difftime(end, start TimeT) float64
// -----------------------------------------------------------------------------
type ClockID C.clockid_t
type Tm struct {
Sec c.Int
Min c.Int
Hour c.Int
Mday c.Int
Mon c.Int
Year c.Int
Wday c.Int
Yday c.Int
Isdst c.Int
Gmtoff c.Long
Zone *c.Char
}
//go:linkname Gmtime C.gmtime
func Gmtime(timer *TimeT) *Tm
//go:linkname Localtime C.localtime
func Localtime(timer *TimeT) *Tm
//go:linkname Strftime C.strftime
func Strftime(buf *c.Char, bufSize uintptr, format *c.Char, timeptr *Tm) uintptr
// -----------------------------------------------------------------------------
type ClockT C.clock_t
//go:linkname Clock C.clock
func Clock() ClockT
// -----------------------------------------------------------------------------
type ClockidT C.clockid_t
const (
// the system's real time (i.e. wall time) clock, expressed as the amount of time since the Epoch.
// This is the same as the value returned by gettimeofday
CLOCK_REALTIME = ClockidT(C.CLOCK_REALTIME)
// clock that increments monotonically, tracking the time since an arbitrary point, and will continue
// to increment while the system is asleep.
CLOCK_MONOTONIC = ClockidT(C.CLOCK_MONOTONIC)
// clock that increments monotonically, tracking the time since an arbitrary point like CLOCK_MONOTONIC.
// However, this clock is unaffected by frequency or time adjustments. It should not be compared to
// other system time sources.
CLOCK_MONOTONIC_RAW = ClockidT(C.CLOCK_MONOTONIC_RAW)
// like CLOCK_MONOTONIC_RAW, but reads a value cached by the system at context switch. This can be
// read faster, but at a loss of accuracy as it may return values that are milliseconds old.
CLOCK_MONOTONIC_RAW_APPROX = ClockidT(C.CLOCK_MONOTONIC_RAW_APPROX)
// clock that increments monotonically, in the same manner as CLOCK_MONOTONIC_RAW, but that does
// not increment while the system is asleep. The returned value is identical to the result of
// mach_absolute_time() after the appropriate mach_timebase conversion is applied.
CLOCK_UPTIME_RAW = ClockidT(C.CLOCK_UPTIME_RAW)
// like CLOCK_UPTIME_RAW, but reads a value cached by the system at context switch. This can be read
// faster, but at a loss of accuracy as it may return values that are milliseconds old.
CLOCK_UPTIME_RAW_APPROX = ClockidT(C.CLOCK_UPTIME_RAW_APPROX)
// clock that tracks the amount of CPU (in user- or kernel-mode) used by the calling process.
CLOCK_PROCESS_CPUTIME_ID = ClockidT(C.CLOCK_PROCESS_CPUTIME_ID)
// clock that tracks the amount of CPU (in user- or kernel-mode) used by the calling thread.
CLOCK_THREAD_CPUTIME_ID = ClockidT(C.CLOCK_THREAD_CPUTIME_ID)
)
type Timespec struct {
Sec TimeT // seconds
Nsec c.Long // and nanoseconds
}
//go:linkname ClockGettime C.clock_gettime
func ClockGettime(clkId ClockidT, tp *Timespec) c.Int
//go:linkname ClockSettime C.clock_settime
func ClockSettime(clkId ClockidT, tp *Timespec) c.Int
//go:linkname ClockGetres C.clock_getres
func ClockGetres(clkId ClockidT, res *Timespec) c.Int
// -----------------------------------------------------------------------------