nim-chronos/asyncdispatch2/timer.nim

51 lines
1.3 KiB
Nim
Raw Normal View History

2018-05-16 08:22:34 +00:00
#
#
# nAIO
# (c) Copyright 2017 Eugene Kabanov
#
# See the file "LICENSE", included in this
# distribution, for details about the copyright.
#
## This module implements cross-platform system timer with
## milliseconds resolution.
when defined(windows):
from winlean import DWORD, getSystemTimeAsFileTime, FILETIME
proc fastEpochTime*(): uint64 {.inline.} =
var t: FILETIME
getSystemTimeAsFileTime(t)
result = ((uint64(t.dwHighDateTime) shl 32) or
uint64(t.dwLowDateTime)) div 10_000
elif defined(macosx):
from posix import Timeval
2018-05-16 08:22:34 +00:00
proc posix_gettimeofday(tp: var Timeval, unused: pointer = nil) {.
importc: "gettimeofday", header: "<sys/time.h>".}
2018-05-16 08:22:34 +00:00
proc fastEpochTime*(): uint64 {.inline.} =
var t: Timeval
posix_gettimeofday(t)
2018-05-17 15:20:58 +00:00
result = (uint64(t.tv_sec) * 1_000 + uint64(t.tv_usec) div 1_000)
2018-05-16 08:22:34 +00:00
elif defined(posix):
from posix import clock_gettime, Timespec, CLOCK_REALTIME
proc fastEpochTime*(): uint64 {.inline.} =
var t: Timespec
discard clock_gettime(CLOCK_REALTIME, t)
result = (uint64(t.tv_sec) * 1_000 + uint64(t.tv_nsec) div 1_000_000)
elif defined(nimdoc):
proc fastEpochTime*(): uint64
## Returns system's timer in milliseconds.
else:
error("Sorry, your operation system is not yet supported!")