make epochTime available in DLLs; refs #6855

This commit is contained in:
Araq 2019-05-10 15:20:30 +02:00
commit 9aa6aa6514

View file

@ -282,7 +282,7 @@ type
dFri = "Friday" dFri = "Friday"
dSat = "Saturday" dSat = "Saturday"
dSun = "Sunday" dSun = "Sunday"
DateTimeLocale* = object DateTimeLocale* = object
MMM*: array[mJan..mDec, string] MMM*: array[mJan..mDec, string]
MMMM*: array[mJan..mDec, string] MMMM*: array[mJan..mDec, string]
@ -2484,56 +2484,55 @@ when not defined(JS):
var var
clocksPerSec {.importc: "CLOCKS_PER_SEC", nodecl, used.}: int clocksPerSec {.importc: "CLOCKS_PER_SEC", nodecl, used.}: int
when not defined(useNimRtl): proc cpuTime*(): float {.tags: [TimeEffect].} =
proc cpuTime*(): float {.rtl, extern: "nt$1", tags: [TimeEffect].} = ## gets time spent that the CPU spent to run the current process in
## gets time spent that the CPU spent to run the current process in ## seconds. This may be more useful for benchmarking than ``epochTime``.
## seconds. This may be more useful for benchmarking than ``epochTime``. ## However, it may measure the real time instead (depending on the OS).
## However, it may measure the real time instead (depending on the OS). ## The value of the result has no meaning.
## The value of the result has no meaning. ## To generate useful timing values, take the difference between
## To generate useful timing values, take the difference between ## the results of two ``cpuTime`` calls:
## the results of two ``cpuTime`` calls: runnableExamples:
runnableExamples: var t0 = cpuTime()
var t0 = cpuTime() # some useless work here (calculate fibonacci)
# some useless work here (calculate fibonacci) var fib = @[0, 1, 1]
var fib = @[0, 1, 1] for i in 1..10:
for i in 1..10: fib.add(fib[^1] + fib[^2])
fib.add(fib[^1] + fib[^2]) echo "CPU time [s] ", cpuTime() - t0
echo "CPU time [s] ", cpuTime() - t0 echo "Fib is [s] ", fib
echo "Fib is [s] ", fib when defined(posix) and not defined(osx):
when defined(posix) and not defined(osx): # 'clocksPerSec' is a compile-time constant, possibly a
# 'clocksPerSec' is a compile-time constant, possibly a # rather awful one, so use clock_gettime instead
# rather awful one, so use clock_gettime instead var ts: Timespec
var ts: Timespec discard clock_gettime(cpuClockId, ts)
discard clock_gettime(cpuClockId, ts) result = toFloat(ts.tv_sec.int) +
result = toFloat(ts.tv_sec.int) + toFloat(ts.tv_nsec.int) / 1_000_000_000
toFloat(ts.tv_nsec.int) / 1_000_000_000 else:
else: result = toFloat(int(getClock())) / toFloat(clocksPerSec)
result = toFloat(int(getClock())) / toFloat(clocksPerSec)
proc epochTime*(): float {.rtl, extern: "nt$1", tags: [TimeEffect].} = proc epochTime*(): float {.tags: [TimeEffect].} =
## gets time after the UNIX epoch (1970) in seconds. It is a float ## gets time after the UNIX epoch (1970) in seconds. It is a float
## because sub-second resolution is likely to be supported (depending ## because sub-second resolution is likely to be supported (depending
## on the hardware/OS). ## on the hardware/OS).
## ##
## ``getTime`` should generally be prefered over this proc. ## ``getTime`` should generally be prefered over this proc.
when defined(macosx): when defined(macosx):
var a: Timeval var a: Timeval
gettimeofday(a) gettimeofday(a)
result = toBiggestFloat(a.tv_sec.int64) + toFloat(a.tv_usec)*0.00_0001 result = toBiggestFloat(a.tv_sec.int64) + toFloat(a.tv_usec)*0.00_0001
elif defined(posix): elif defined(posix):
var ts: Timespec var ts: Timespec
discard clock_gettime(realTimeClockId, ts) discard clock_gettime(realTimeClockId, ts)
result = toBiggestFloat(ts.tv_sec.int64) + result = toBiggestFloat(ts.tv_sec.int64) +
toBiggestFloat(ts.tv_nsec.int64) / 1_000_000_000 toBiggestFloat(ts.tv_nsec.int64) / 1_000_000_000
elif defined(windows): elif defined(windows):
var f: winlean.FILETIME var f: winlean.FILETIME
getSystemTimeAsFileTime(f) getSystemTimeAsFileTime(f)
var i64 = rdFileTime(f) - epochDiff var i64 = rdFileTime(f) - epochDiff
var secs = i64 div rateDiff var secs = i64 div rateDiff
var subsecs = i64 mod rateDiff var subsecs = i64 mod rateDiff
result = toFloat(int(secs)) + toFloat(int(subsecs)) * 0.0000001 result = toFloat(int(secs)) + toFloat(int(subsecs)) * 0.0000001
else: else:
{.error: "unknown OS".} {.error: "unknown OS".}
when defined(JS): when defined(JS):
proc epochTime*(): float {.tags: [TimeEffect].} = proc epochTime*(): float {.tags: [TimeEffect].} =