-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add timezone caching and use faster time primitives.
- Loading branch information
Showing
2 changed files
with
65 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
{.push raises: [].} | ||
|
||
import std/times | ||
export times | ||
|
||
when defined(macos) or defined(macosx) or defined(osx): | ||
from posix import gettimeofday, Timeval | ||
elif defined(windows): | ||
type | ||
FILETIME {.final, pure, completeStruct.} = object | ||
dwLowDateTime: uint32 | ||
dwHighDateTime: uint32 | ||
proc getSystemTimeAsFileTime*(lpSystemTimeAsFileTime: var FILETIME) {. | ||
importc: "GetSystemTimeAsFileTime", dynlib: "kernel32", stdcall, | ||
sideEffect.} | ||
else: | ||
const CLOCK_REALTIME_COARSE = 5 | ||
from std/posix import Timespec, Time, clock_gettime | ||
|
||
proc getFastTime*(): Time = | ||
when defined(js): | ||
let | ||
millis = newDate().getTime() | ||
seconds = millis div 1_000 | ||
nanos = (millis mod 1_000) * 1_000_000 | ||
initTime(seconds, nanos) | ||
elif defined(macosx): | ||
var a {.noinit.}: Timeval | ||
gettimeofday(a) | ||
initTime(a.tv_sec.int64, int(a.tv_usec) * 1_000) | ||
elif defined(windows): | ||
var f {.noinit.}: FILETIME | ||
getSystemTimeAsFileTime(f) | ||
let tmp = uint64(f.dwLowDateTime) or (uint64(f.dwHighDateTime) shl 32) | ||
fromWinTime(cast[int64](tmp)) | ||
else: | ||
var ts {.noinit.}: Timespec | ||
discard clock_gettime(CLOCK_REALTIME_COARSE, ts) | ||
initTime(int64(ts.tv_sec), int(ts.tv_nsec)) |