Commit 77b02c8b authored by Mart Lubbers's avatar Mart Lubbers

Merge branch 'add-subsecond-time' into 'master'

Add subsecond time

See merge request !94
parents bffe183d fa40907d
Pipeline #8839 passed with stage
in 1 minute and 27 seconds
......@@ -76,9 +76,11 @@ write :: !Int !{#Char} !Int !*w -> (!Int, !*w)
select_ :: !Int !Pointer !Pointer !Pointer !Pointer !*w -> (!Int, !*w)
kill :: !Int !Int !*w -> (!Int, !*w)
timegm :: !{#Int} -> Int
clock_gettime :: !Int !Pointer !*w -> (!Int, !*w)
//Memory (impure)
malloc :: !Int -> Pointer
mallocSt :: !Int !*w -> (!Pointer, !*w)
free :: !Pointer -> Int
freeSt :: !Pointer !*w -> *w
memcpy_string_to_pointer :: !Pointer !{#Char} !Int -> Pointer
......
......@@ -164,6 +164,10 @@ malloc :: !Int -> Pointer
malloc num = code {
ccall malloc "I:p"
}
mallocSt :: !Int !*w -> (!Pointer, !*w)
mallocSt num w = code {
ccall malloc "I:p:A"
}
free :: !Pointer -> Int
free ptr = code {
ccall free "p:I"
......@@ -176,6 +180,10 @@ memcpy_string_to_pointer :: !Pointer !{#Char} !Int -> Pointer
memcpy_string_to_pointer p s n = code {
ccall memcpy "psp:p"
}
clock_gettime :: !Int !Pointer !*w -> (!Int, !*w)
clock_gettime _ _ _ = code {
ccall clock_gettime "Ip:I:A"
}
//Mapping to/from byte arrays
unpackStat :: !{#Char} -> Stat
......
......@@ -75,9 +75,11 @@ write :: !Int !{#Char} !Int !*w -> (!Int, !*w)
select_ :: !Int !Pointer !Pointer !Pointer !Pointer !*w -> (!Int, !*w)
kill :: !Int !Int !*w -> (!Int, !*w)
timegm :: !{#Int} -> Int
clock_gettime :: !Int !Pointer !*w -> (!Int, !*w)
//Memory (impure)
malloc :: !Int -> Pointer
mallocSt :: !Int !*w -> (!Pointer, !*w)
free :: !Pointer -> Int
freeSt :: !Pointer !*w -> *w
memcpy_string_to_pointer :: !Pointer !{#Char} !Int -> Pointer
......
......@@ -165,6 +165,10 @@ malloc :: !Int -> Pointer
malloc num = code {
ccall malloc "I:p"
}
mallocSt :: !Int !*w -> (!Pointer, !*w)
mallocSt num w = code {
ccall malloc "I:p:A"
}
free :: !Pointer -> Int
free ptr = code {
ccall free "p:I"
......@@ -177,6 +181,10 @@ memcpy_string_to_pointer :: !Pointer !{#Char} !Int -> Pointer
memcpy_string_to_pointer p s n = code {
ccall memcpy "psp:p"
}
clock_gettime :: !Int !Pointer !*w -> (!Int, !*w)
clock_gettime _ _ _ = code {
ccall clock_gettime "Ip:I:A"
}
//Mapping to/from byte arrays
unpackStat :: !{#Char} -> Stat
......
......@@ -85,3 +85,8 @@ toLocalTime :: !Timestamp !*World -> (!Tm,!*World)
* Convert a timestamp to a Tm record (GMT time)
*/
toGmTime :: !Timestamp -> Tm
/**
* Get subsecond precision time
*/
nsTime :: !*World -> (!Timespec, !*World)
:: Timespec = {tv_sec :: !Int, tv_nsec :: !Int}
implementation module System.Time
import StdString, StdArray, StdClass, StdOverloaded, StdInt
import StdString, StdArray, StdClass, StdOverloaded, StdInt, StdMisc
import System._Pointer, System._Posix
import Text
......@@ -166,3 +166,13 @@ unpackTm buf off =
sizeOfTm :: Int
sizeOfTm = 36
nsTime :: !*World -> (!Timespec, !*World)
nsTime w
# (p, w) = mallocSt 16 w
# (r, w) = clock_gettime 0 p w
//For completeness sake
| r <> 0 = abort "clock_gettime error: everyone should have permission to open CLOCK_REALTIME?"
# (tv_sec, p) = readIntP p 0
# (tv_nsec, p) = readIntP p 8
= ({Timespec | tv_sec = tv_sec, tv_nsec = tv_nsec}, freeSt p w)
......@@ -14,3 +14,4 @@ PeekNamedPipe@24
SetHandleInformation@12
TerminateProcess@8
GetFullPathNameA@16
GetSystemTimeAsFileTime@4
......@@ -88,3 +88,9 @@ toGmTime :: !Timestamp -> Tm
derefTm :: !Int -> Tm
packTm :: !Tm -> {#Int}
gmTimeC :: !{#Int} -> Int
/**
* Get subsecond precision time
*/
nsTime :: !*World -> (!Timespec, !*World)
:: Timespec = {tv_sec :: !Int, tv_nsec :: !Int}
......@@ -2,6 +2,10 @@ implementation module System.Time
import StdString, StdArray, StdClass, StdOverloaded, StdInt, StdMisc
import System._Pointer
import System._WinBase
import Data.Integer
import Data.List
from Data.Func import $
import Text
import code from library "msvcrt.txt"
......@@ -159,3 +163,20 @@ packTm32 tm = { tm.sec
, tm.yday
, tm.isdst
}
//Number of ticks difference between the windows and linux epoch
TICKSDIFF :== {integer_s=0,integer_a={-1240428288,2}} * TICKSPERSEC
//Number of ticks per second on windows machines
TICKSPERSEC :== {integer_s=10000000,integer_a={}}
BIGTWO :== {integer_s=2,integer_a={}}
nsTime :: !*World -> (!Timespec, !*World)
nsTime w
# (is, w) = GetSystemTimeAsFileTime (createArray 2 0) w
# ticks = uintToInt is.[0] + foldr ($) (uintToInt is.[1]) (repeatn 32 ((*) BIGTWO)) - TICKSDIFF
= ({Timespec | tv_sec=toInt (ticks / TICKSPERSEC), tv_nsec=toInt (ticks rem TICKSPERSEC) *100}, w)
uintToInt :: Int -> Integer
uintToInt i
| i < 0 = toInteger i + {integer_s=0,integer_a={0,1}}
= toInteger i
......@@ -194,3 +194,5 @@ sleep :: !DWORD !*w -> *w
createPipe :: !PHANDLE !PHANDLE !SECURITY_ATTRIBUTES !DWORD !*w -> (!Bool, !*w)
peekNamedPipe :: !HANDLE !LPVOID !DWORD !LPDWORD !LPDWORD !LPDWORD !*w -> (!Bool, !*w)
GetSystemTimeAsFileTime :: !{#Int} !*World -> (!{#Int},!*World)
......@@ -284,3 +284,11 @@ peekNamedPipe :: !HANDLE !LPVOID !DWORD !LPDWORD !LPDWORD !LPDWORD !*w -> (!Bool
peekNamedPipe hNamedPipe lpBuffer nBufferSize lpBytesRead lpTotalBytesAvail lpBytesLeftThisMessage world = code {
ccall PeekNamedPipe@24 "PIpIppp:I:I"
}
GetSystemTimeAsFileTime :: !{#Int} !*World -> (!{#Int},!*World)
GetSystemTimeAsFileTime i w
= code {
push_a 0
ccall GetSystemTimeAsFileTime@4 "PA:I:AA"
pop_b 1
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment