2020-04-24 08:33:25 +03:00
|
|
|
module time
|
|
|
|
|
|
|
|
#include <mach/mach_time.h>
|
|
|
|
const (
|
|
|
|
// start_time is needed on Darwin and Windows because of potential overflows
|
|
|
|
start_time = C.mach_absolute_time()
|
2020-12-06 17:19:39 +03:00
|
|
|
time_base = init_time_base()
|
2020-04-24 08:33:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
[typedef]
|
|
|
|
struct C.mach_timebase_info_data_t {
|
2020-04-26 20:36:38 +03:00
|
|
|
numer u32
|
|
|
|
denom u32
|
2020-04-24 08:33:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn C.mach_absolute_time() u64
|
2020-12-06 17:19:39 +03:00
|
|
|
|
2020-04-24 08:33:25 +03:00
|
|
|
fn C.mach_timebase_info(&C.mach_timebase_info_data_t)
|
2020-12-06 17:19:39 +03:00
|
|
|
|
2020-04-26 20:36:38 +03:00
|
|
|
fn C.clock_gettime_nsec_np(int) u64
|
2020-04-24 08:33:25 +03:00
|
|
|
|
|
|
|
struct InternalTimeBase {
|
2020-04-26 20:36:38 +03:00
|
|
|
numer u32 = 1
|
|
|
|
denom u32 = 1
|
2020-04-24 08:33:25 +03:00
|
|
|
}
|
|
|
|
|
2020-06-07 16:19:09 +03:00
|
|
|
pub struct C.timeval {
|
|
|
|
tv_sec u64
|
|
|
|
tv_usec u64
|
|
|
|
}
|
|
|
|
|
2020-11-29 22:23:37 +03:00
|
|
|
fn init_time_base() C.mach_timebase_info_data_t {
|
2020-04-26 20:36:38 +03:00
|
|
|
tb := C.mach_timebase_info_data_t{}
|
2020-04-24 08:33:25 +03:00
|
|
|
C.mach_timebase_info(&tb)
|
2020-12-06 17:19:39 +03:00
|
|
|
return C.mach_timebase_info_data_t{
|
|
|
|
numer: tb.numer
|
|
|
|
denom: tb.denom
|
|
|
|
}
|
2020-04-24 08:33:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn sys_mono_now_darwin() u64 {
|
|
|
|
tm := C.mach_absolute_time()
|
2020-04-26 20:36:38 +03:00
|
|
|
if time_base.denom == 0 {
|
|
|
|
C.mach_timebase_info(&time_base)
|
|
|
|
}
|
|
|
|
return (tm - start_time) * time_base.numer / time_base.denom
|
2020-04-24 08:33:25 +03:00
|
|
|
}
|
2020-05-05 17:19:25 +03:00
|
|
|
|
|
|
|
// NB: vpc_now_darwin is used by `v -profile` .
|
|
|
|
// It should NOT call *any other v function*, just C functions and casts.
|
|
|
|
[inline]
|
|
|
|
fn vpc_now_darwin() u64 {
|
|
|
|
tm := C.mach_absolute_time()
|
|
|
|
if time_base.denom == 0 {
|
|
|
|
C.mach_timebase_info(&time_base)
|
|
|
|
}
|
|
|
|
return (tm - start_time) * time_base.numer / time_base.denom
|
|
|
|
}
|
2020-06-07 16:19:09 +03:00
|
|
|
|
|
|
|
// darwin_now returns a better precision current time for Darwin based operating system
|
|
|
|
// this should be implemented with native system calls eventually
|
|
|
|
// but for now a bit tweaky. It uses the deprecated gettimeofday clock to get
|
2020-06-10 12:14:55 +03:00
|
|
|
// the microseconds seconds part and converts to local time
|
|
|
|
[inline]
|
2020-06-07 16:19:09 +03:00
|
|
|
fn darwin_now() Time {
|
|
|
|
// get the high precision time as UTC clock
|
|
|
|
tv := C.timeval{}
|
|
|
|
C.gettimeofday(&tv, 0)
|
2020-06-10 12:14:55 +03:00
|
|
|
loc_tm := C.tm{}
|
|
|
|
C.localtime_r(&tv.tv_sec, &loc_tm)
|
|
|
|
return convert_ctime(loc_tm, int(tv.tv_usec))
|
|
|
|
}
|
|
|
|
|
|
|
|
// darwin_utc returns a better precision current time for Darwin based operating system
|
|
|
|
// this should be implemented with native system calls eventually
|
|
|
|
// but for now a bit tweaky. It uses the deprecated gettimeofday clock to get
|
|
|
|
// the microseconds seconds part and normal local time to get correct local time
|
|
|
|
[inline]
|
|
|
|
fn darwin_utc() Time {
|
|
|
|
// get the high precision time as UTC clock
|
|
|
|
tv := C.timeval{}
|
|
|
|
C.gettimeofday(&tv, 0)
|
|
|
|
return unix2(int(tv.tv_sec), int(tv.tv_usec))
|
2020-06-07 16:19:09 +03:00
|
|
|
}
|