factor/basis/calendar/unix/unix.factor

45 lines
1.3 KiB
Factor
Raw Normal View History

2008-10-06 19:18:33 -04:00
! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.data calendar calendar.private
classes.struct kernel math system unix unix.time unix.types ;
2007-09-20 18:09:08 -04:00
IN: calendar.unix
: timeval>seconds ( timeval -- seconds )
[ sec>> ] [ usec>> 1,000,000 / ] bi + ; inline
2012-08-15 11:29:00 -04:00
: timeval>micros ( timeval -- micros )
[ sec>> 1,000,000 * ] [ usec>> ] bi + ; inline
2010-06-13 19:24:48 -04:00
: timeval>duration ( timeval -- duration )
2012-08-15 11:29:00 -04:00
timeval>seconds seconds ; inline
2008-10-21 05:19:50 -04:00
: timeval>unix-time ( timeval -- timestamp )
2012-08-15 11:29:00 -04:00
[ unix-1970 ] dip timeval>seconds +second ; inline
: timespec>seconds ( timespec -- seconds )
[ sec>> ] [ nsec>> 1,000,000,000 / ] bi + ; inline
2008-10-21 05:19:50 -04:00
: timespec>duration ( timespec -- duration )
2012-08-15 11:29:00 -04:00
timespec>seconds seconds ; inline
2008-10-21 05:19:50 -04:00
: timespec>unix-time ( timespec -- timestamp )
2012-08-15 11:29:00 -04:00
[ unix-1970 ] dip timespec>seconds +second ; inline
: get-time ( -- alien )
2012-08-15 11:29:00 -04:00
f time time_t <ref> localtime ; inline
2007-09-20 18:09:08 -04:00
: timezone-name ( -- string )
get-time zone>> ;
2007-09-20 18:09:08 -04:00
2008-04-02 19:28:55 -04:00
M: unix gmt-offset ( -- hours minutes seconds )
get-time gmtoff>> 3600 /mod 60 /mod ;
2010-06-17 01:04:53 -04:00
: current-timeval ( -- timeval )
2012-08-15 11:29:00 -04:00
timeval <struct> f [ gettimeofday io-error ] 2keep drop ; inline
: system-micros ( -- n )
2012-08-15 11:29:00 -04:00
current-timeval timeval>micros ;
2010-06-17 01:04:53 -04:00
M: unix gmt
current-timeval timeval>unix-time ;