Fix clock_gettime() is not available on old MacOS.

This commit is contained in:
cheatfate 2019-09-03 17:30:28 +03:00
parent 7029f8bc1e
commit a039011f5b
No known key found for this signature in database
GPG Key ID: 46ADD633A7201F95
2 changed files with 24 additions and 10 deletions

View File

@ -1,5 +1,5 @@
packageName = "chronos"
version = "2.2.9"
version = "2.3.0"
author = "Status Research & Development GmbH"
description = "Chronos"
license = "Apache License 2.0 or MIT"

View File

@ -101,22 +101,36 @@ elif defined(macosx):
result = (cast[uint64](t.tv_sec) * 1_000_000_000 +
cast[uint64](t.tv_usec) * 1_000)
else:
from posix import clock_gettime, Timespec, CLOCK_MONOTONIC
type
MachTimebaseInfo {.importc: "struct mach_timebase_info",
header: "<mach/mach_time.h>", pure, final.} = object
numer: uint32
denom: uint32
proc mach_timebase_info(info: var MachTimebaseInfo) {.importc,
header: "<mach/mach_time.h>".}
proc mach_absolute_time(): uint64 {.importc, header: "<mach/mach_time.h>".}
var queryFrequencyN: uint64
var queryFrequencyD: uint64
proc setupQueryFrequence() =
var info: MachTimebaseInfo
mach_timebase_info(info)
queryFrequencyN = info.numer
queryFrequencyD = info.denom
proc fastEpochTime*(): uint64 {.
inline, deprecated: "Use Moment.now()".} =
## Procedure's resolution is millisecond.
var t: Timespec
discard clock_gettime(CLOCK_MONOTONIC, t)
result = ((cast[uint64](t.tv_sec) * 1_000) +
(cast[uint64](t.tv_nsec) div 1_000_000))
result = (mach_absolute_time() * queryFrequencyN) div queryFrequencyD
result = result div 1_000_000
proc fastEpochTimeNano(): uint64 {.inline.} =
## Procedure's resolution is nanosecond.
var t: Timespec
discard clock_gettime(CLOCK_MONOTONIC, t)
result = cast[uint64](t.tv_sec) * 1_000_000_000'u64 +
cast[uint64](t.tv_nsec)
result = (mach_absolute_time() * queryFrequencyN) div queryFrequencyD
setupQueryFrequence()
elif defined(posix):
from posix import clock_gettime, Timespec, CLOCK_REALTIME, CLOCK_MONOTONIC