1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.syntax arrays calendar
4 kernel math unix unix.time unix.types namespaces system
5 accessors classes.struct ;
8 : timeval>duration ( timeval -- duration )
9 [ sec>> seconds ] [ usec>> microseconds ] bi time+ ;
11 : timeval>unix-time ( timeval -- timestamp )
12 timeval>duration since-1970 ;
14 : timespec>duration ( timespec -- seconds )
15 [ sec>> seconds ] [ nsec>> nanoseconds ] bi time+ ;
17 : timespec>nanoseconds ( timespec -- seconds )
18 [ sec>> 1000000000 * ] [ nsec>> ] bi + ;
20 : timespec>unix-time ( timespec -- timestamp )
21 timespec>duration since-1970 ;
23 : get-time ( -- alien )
24 f time time_t <ref> localtime ;
26 : timezone-name ( -- string )
29 M: unix gmt-offset ( -- hours minutes seconds )
30 get-time gmtoff>> 3600 /mod 60 /mod ;
32 : current-timeval ( -- timeval )
33 timeval <struct> f [ gettimeofday io-error ] 2keep drop ;
35 : system-micros ( -- n )
37 [ sec>> 1,000,000 * ] [ usec>> ] bi + ;
40 current-timeval timeval>unix-time ;