]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/unix/unix.factor
f913ef0ee9da158bc3f70392035e62cb6dfe2a03
[factor.git] / basis / calendar / unix / unix.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.data calendar calendar.private
4 classes.struct kernel math system unix unix.time unix.types ;
5 IN: calendar.unix
6
7 : timeval>seconds ( timeval -- seconds )
8     [ sec>> ] [ usec>> 1000000 / ] bi + ; inline
9
10 : timeval>duration ( timeval -- duration )
11     timeval>seconds seconds ;
12
13 : timeval>unix-time ( timeval -- timestamp )
14     [ unix-1970 ] dip timeval>seconds +second ;
15
16 : timespec>seconds ( timespec -- seconds )
17     [ sec>> ] [ nsec>> 1000000000 / ] bi + ; inline
18
19 : timespec>duration ( timespec -- duration )
20     timespec>seconds seconds ;
21
22 : timespec>unix-time ( timespec -- timestamp )
23     [ unix-1970 ] dip timespec>seconds +second ;
24
25 : get-time ( -- alien )
26     f time time_t <ref> localtime ;
27
28 : timezone-name ( -- string )
29     get-time zone>> ;
30
31 M: unix gmt-offset ( -- hours minutes seconds )
32     get-time gmtoff>> 3600 /mod 60 /mod ;
33
34 : current-timeval ( -- timeval )
35     timeval <struct> f [ gettimeofday io-error ] 2keep drop ;
36
37 : system-micros ( -- n )
38     current-timeval
39     [ sec>> 1,000,000 * ] [ usec>> ] bi + ;
40
41 M: unix gmt
42     current-timeval timeval>unix-time ;