automatically use GetSystemTimePreciseAsFileTime if available (windows 8)
authorbeware <beware@bircd.org>
Mon, 3 Nov 2014 00:38:20 +0000 (00:38 +0000)
committerbeware <beware@bircd.org>
Mon, 3 Nov 2014 00:38:20 +0000 (00:38 +0000)
git-svn-id: file:///svnroot/lcore/trunk@140 b1de8a11-f9be-4011-bde0-cc7ace90066a

btime.pas

index 14b734e6e4c6344a742a353de42b26dca961a150..54deae594e814544942666d88a578dee925a88bd 100644 (file)
--- a/btime.pas
+++ b/btime.pas
@@ -553,12 +553,51 @@ begin
   result := mmqpctimefloat;\r
 end;\r
 \r
+\r
+\r
+var\r
+  GetSystemTimePreciseAsFileTime:procedure(var v:tfiletime); stdcall;\r
+  win8inited:boolean;\r
+\r
+procedure initwin8;\r
+var\r
+  dllhandle:thandle;\r
+\r
+begin\r
+  win8inited := true;\r
+  dllhandle := loadlibrary('kernel32.dll');\r
+  if (dllhandle <> 0) then begin\r
+    GetSystemTimePreciseAsFileTime := getprocaddress(dllhandle,'GetSystemTimePreciseAsFileTime');\r
+  end;\r
+end;\r
+\r
+\r
+function unixtimefloat_win8:float;\r
+var\r
+  ft:tfiletime;\r
+  i:int64 absolute ft;\r
+begin\r
+  GetSystemTimePreciseAsFileTime(ft);\r
+  {change from windows 1601-01-01 to unix 1970-01-01.\r
+  use integer math for this, to preserve precision}\r
+  dec(i, 116444736000000000);\r
+  result := (i / 10000000);\r
+end;\r
+\r
+\r
+\r
 function unixtimefloat:float;\r
 const\r
   margin = 0.0012;\r
 var\r
   f,g,h:float;\r
 begin\r
+  if not win8inited then initwin8;\r
+  if assigned(@GetSystemTimePreciseAsFileTime) then begin\r
+    result := unixtimefloat_win8;\r
+    exit;\r
+  end;\r
+\r
   result := monotimefloat+timefloatbias;\r
   f := result-unixtimefloat_systemtime;\r
   if ((f > ticks_freq2+margin) or (f < -margin)) or (timefloatbias = 0) then begin\r