gettimeofday.ss
#lang scheme

(require (planet synx/sizeof))

(require scheme/foreign)
(unsafe!)

(define _time (pick-an-integer "time_t" #f '("sys/time")))
(define _microtime (pick-an-integer "suseconds_t" #f '("sys/time")))

(define-cstruct _timeval ([seconds _time] [microseconds _microtime]))

(define inner-get-time-of-day
  (get-ffi-obj 
   "gettimeofday" (ffi-lib #f)
   (_fun _timeval-pointer _pointer -> _bool)))

(define (get-time-of-day)
  (let ([timeval (make-timeval 0 0)])
    (when (inner-get-time-of-day timeval #f)
        (error "gettimeofday returned an error"))
    (values (timeval-seconds timeval) (timeval-microseconds timeval))))
    
(provide get-time-of-day)