From 6c2050912166a4960b40c3825afb1a31cfdde0eb Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Fri, 28 May 2010 23:32:46 +0200 Subject: explicit types for generic routines --- lib/pure/times.nim | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'lib/pure/times.nim') diff --git a/lib/pure/times.nim b/lib/pure/times.nim index 70cb038a7..da712263d 100755 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -26,6 +26,19 @@ type when defined(posix): type TTime* = distinct int ## distinct type that represents a time + + Ttimeval {.importc: "struct timeval", header: "", + final, pure.} = object ## struct timeval + tv_sec: int ## Seconds. + tv_usec: int ## Microseconds. + + # we cannot import posix.nim here, because posix.nim depends on times.nim. + # Ok, we could, but I don't want circular dependencies. + # And gettimeofday() is not defined in the posix module anyway. Sigh. + + proc posix_gettimeofday(tp: var Ttimeval, unused: pointer = nil) {. + importc: "gettimeofday", header: "".} + elif defined(windows): when defined(vcc): # newest version of Visual C++ defines time_t to be of 64 bits @@ -147,7 +160,7 @@ when not defined(ECMAScript): PTimeInfo = ptr structTM PTime = ptr TTime - TClock {.importc: "clock_t".} = range[low(int)..high(int)] + TClock {.importc: "clock_t".} = distinct int #range[low(int)..high(int)] proc localtime(timer: PTime): PTimeInfo {. importc: "localtime", header: "".} @@ -197,9 +210,17 @@ when not defined(ECMAScript): return toBiggestInt(difftime(a, b)) proc getStartMilsecs(): int = - #echo "clocks per sec: ", clocksPerSec + #echo "clocks per sec: ", clocksPerSec, "clock: ", int(clock()) #return clock() div (clocksPerSec div 1000) - result = toInt(toFloat(clock()) / (toFloat(clocksPerSec) / 1000.0)) + when defined(posix): + var a: Ttimeval + posix_gettimeofday(a) + result = a.tv_sec * 1000 + a.tv_usec + else: + result = int(clock()) div (clocksPerSec div 1000) + when false: + when defined(macosx): + result = toInt(toFloat(clock()) / (toFloat(clocksPerSec) / 1000.0)) proc getTime(): TTime = return timec(nil) proc getLocalTime(t: TTime): TTimeInfo = -- cgit 1.4.1-2-gfad0