]> gitweb.factorcode.org Git - factor.git/blob - basis/calendar/unix/unix.factor
Move <ref>, deref and little-endian? from alien.c-types to alien.data, remove <c...
[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: alien alien.c-types alien.data alien.syntax arrays
4 calendar kernel math unix unix.time unix.types namespaces system
5 accessors classes.struct ;
6 IN: calendar.unix
7
8 : timeval>duration ( timeval -- duration )
9     [ sec>> seconds ] [ usec>> microseconds ] bi time+ ;
10
11 : timeval>unix-time ( timeval -- timestamp )
12     timeval>duration since-1970 ;
13
14 : timespec>duration ( timespec -- seconds )
15     [ sec>> seconds ] [ nsec>> nanoseconds ] bi time+ ;
16
17 : timespec>nanoseconds ( timespec -- seconds )
18     [ sec>> 1000000000 * ] [ nsec>> ] bi + ;
19
20 : timespec>unix-time ( timespec -- timestamp )
21     timespec>duration since-1970 ;
22
23 : get-time ( -- alien )
24     f time time_t <ref> localtime ;
25
26 : timezone-name ( -- string )
27     get-time zone>> ;
28
29 M: unix gmt-offset ( -- hours minutes seconds )
30     get-time gmtoff>> 3600 /mod 60 /mod ;
31
32 : current-timeval ( -- timeval )
33     timeval <struct> f [ gettimeofday io-error ] 2keep drop ;
34
35 : system-micros ( -- n )
36     current-timeval
37     [ sec>> 1,000,000 * ] [ usec>> ] bi + ;
38
39 M: unix gmt
40     current-timeval timeval>unix-time ;