]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/unix/unix.factor
classes.struct: moving to new/boa instead of <struct>/<struct-boa>
[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 libc unix unix.time unix.types ;
5 IN: calendar.unix
6
7 : timeval>seconds ( timeval -- seconds )
8     [ sec>> ] [ usec>> 1,000,000 / ] bi + ; inline
9
10 : timeval>micros ( timeval -- micros )
11     [ sec>> 1,000,000 * ] [ usec>> ] bi + ; inline
12
13 : timeval>duration ( timeval -- duration )
14     timeval>seconds seconds ; inline
15
16 : timeval>unix-time ( timeval -- timestamp )
17     [ unix-1970 ] dip timeval>seconds +second ; inline
18
19 : timespec>seconds ( timespec -- seconds )
20     [ sec>> ] [ nsec>> 1,000,000,000 / ] bi + ; inline
21
22 : timespec>duration ( timespec -- duration )
23     timespec>seconds seconds ; inline
24
25 : timespec>unix-time ( timespec -- timestamp )
26     [ unix-1970 ] dip timespec>seconds +second ; inline
27
28 : get-time ( -- alien )
29     f time time_t <ref> localtime ; inline
30
31 : timezone-name ( -- string )
32     get-time zone>> ;
33
34 M: unix gmt-offset
35     get-time gmtoff>> 3600 /mod 60 /mod ;
36
37 : current-timeval ( -- timeval )
38     timeval new [ f gettimeofday io-error ] keep ; inline
39
40 : system-micros ( -- n )
41     current-timeval timeval>micros ;
42
43 M: unix now-gmt
44     current-timeval timeval>unix-time ;