Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
48 lines (47 sloc) 1.32 KB
#require "ctypes.foreign" ;;
#require "ctypes.top" ;;
open Ctypes ;;
open PosixTypes ;;
open Foreign ;;
let time = foreign "time" (ptr time_t @-> returning time_t) ;;
#part 1
let cur_time = time (from_voidp time_t null) ;;
#part 2
let time' () = time (from_voidp time_t null) ;;
#part 3
let difftime =
foreign "difftime" (time_t @-> time_t @-> returning double) ;;
let t1 =
time' () in
Unix.sleep 2;
let t2 = time' () in
difftime t2 t1 ;;
#part 4
let ctime = foreign "ctime" (ptr time_t @-> returning string) ;;
#part 5
ctime (time' ()) ;;
#part 6
let t_ptr = allocate time_t (time' ()) ;;
#part 7
ctime t_ptr ;;
#part 8
type timeval ;;
let timeval : timeval structure typ = structure "timeval" ;;
#part 9
let tv_sec = field timeval "tv_sec" long ;;
let tv_usec = field timeval "tv_usec" long ;;
seal timeval ;;
#part 10
type timezone ;;
let timezone : timezone structure typ = structure "timezone" ;;
#part 11
let gettimeofday = foreign "gettimeofday"
(ptr timeval @-> ptr timezone @-> returning_checking_errno int) ;;
#part 12
let gettimeofday' () =
let tv = make timeval in
ignore(gettimeofday (addr tv) (from_voidp timezone null));
let secs = Signed.Long.(to_int (getf tv tv_sec)) in
let usecs = Signed.Long.(to_int (getf tv tv_usec)) in
Pervasives.(float secs +. float usecs /. 1000000.0) ;;
gettimeofday' () ;;