diff --git a/contrib/calendar/calendar.factor b/contrib/calendar/calendar.factor index a329c206d3..081b150361 100644 --- a/contrib/calendar/calendar.factor +++ b/contrib/calendar/calendar.factor @@ -1,16 +1,10 @@ +IN: calendar USING: arrays errors generic hashtables io kernel math namespaces sequences strings prettyprint inspector ; -IN: calendar TUPLE: timestamp year month day hour minute second gmt-offset ; TUPLE: dt year month day hour minute second ; -SYMBOL: gmt-offset --6 gmt-offset set-global ! central time - -LIBRARY: libc -FUNCTION: time_t time ( time_t* t ) ; - : month-names { "Not a month" "January" "February" "March" "April" "May" "June" @@ -198,7 +192,7 @@ M: number +second ( timestamp n -- timestamp ) over set-timestamp-gmt-offset ; : >local-time ( timestamp -- timestamp ) - gmt-offset get convert-timezone ; + gmt-offset convert-timezone ; : >gmt ( timestamp -- timestamp ) 0 convert-timezone ; @@ -283,3 +277,4 @@ M: number +second ( timestamp n -- timestamp ) dup timestamp-minute unparse 2 CHAR: 0 pad-left write ":" write timestamp-second >fixnum unparse 2 CHAR: 0 pad-left write " GMT" write ] string-out ; + diff --git a/contrib/calendar/load.factor b/contrib/calendar/load.factor index 6fedb3dae4..9c7b93cea4 100644 --- a/contrib/calendar/load.factor +++ b/contrib/calendar/load.factor @@ -1,13 +1,15 @@ -USING: kernel modules sequences ; -"calendar" +USING: kernel modules namespaces sequences ; -{ - { [ win32? ] [ { "os-win32.factor" } ] } - { [ t ] [ { "os-unix.factor" } ] } -} cond -{ - "calendar.factor" -} append +"calendar" +[ + { + { [ unix? macosx? not and ] [ "os-unix.factor" , "os-linux.factor" , ] } + { [ macosx? ] [ "os-unix.factor" , "os-macosx.factor" , ] } + { [ unix? ] [ "os-unix.factor" , ] } + { [ win32? ] [ "os-win32.factor" , ] } + } cond + "calendar.factor" , +] { } make { "test/calendar.factor" } provide diff --git a/contrib/calendar/os-linux.factor b/contrib/calendar/os-linux.factor new file mode 100644 index 0000000000..82d05ecb2a --- /dev/null +++ b/contrib/calendar/os-linux.factor @@ -0,0 +1,15 @@ +IN: calendar + + +BEGIN-STRUCT: tm + FIELD: int sec ! Seconds: 0-59 (K&R says 0-61?) + FIELD: int min ! Minutes: 0-59 + FIELD: int hour ! Hours since midnight: 0-23 + FIELD: int mday ! Day of the month: 1-31 + FIELD: int mon ! Months *since* january: 0-11 + FIELD: int year ! Years since 1900 + FIELD: int wday ! Days since Sunday (0-6) + FIELD: int yday ! Days since Jan. 1: 0-365 + FIELD: int isdst ! +1 Daylight Savings Time, 0 No DST, +END-STRUCT + diff --git a/contrib/calendar/os-macosx.factor b/contrib/calendar/os-macosx.factor new file mode 100644 index 0000000000..43d6c5e5e4 --- /dev/null +++ b/contrib/calendar/os-macosx.factor @@ -0,0 +1,48 @@ +IN: calendar +USING: alien arrays compiler errors kernel math ; + +TYPEDEF: uint time_t + +BEGIN-STRUCT: tm + FIELD: int sec ! Seconds: 0-59 (K&R says 0-61?) + FIELD: int min ! Minutes: 0-59 + FIELD: int hour ! Hours since midnight: 0-23 + FIELD: int mday ! Day of the month: 1-31 + FIELD: int mon ! Months *since* january: 0-11 + FIELD: int year ! Years since 1900 + FIELD: int wday ! Days since Sunday (0-6) + FIELD: int yday ! Days since Jan. 1: 0-365 + FIELD: int isdst ! +1 Daylight Savings Time, 0 No DST, + FIELD: long gmtoff + FIELD: char* zone +END-STRUCT + +FUNCTION: time_t time ( time_t* t ) ; +FUNCTION: tm* localtime ( time_t* clock ) ; +FUNCTION: size_t strftime ( char* buf, size_t maxsize, char* format, tm* timeptr ) ; + +BEGIN-STRUCT: t + FIELD: long tv_sec + FIELD: long tv_usec +END-STRUCT + +BEGIN-STRUCT: tz + FIELD: int tz_minuteswest + FIELD: int tz_dsttime +END-STRUCT + +FUNCTION: int gettimeofday ( t* timeval, tz* timezone ) ; + +: machine-gmt-offset + "t" "tz" 2dup gettimeofday + zero? [ nip tz-tz_minuteswest 60 / neg ] [ 2drop 0 ] if ; + +\ gettimeofday compile + + +: timezone-name + get-time tm-zone ; + +: gmt-offset + get-time tm-gmtoff 3600 / ; + diff --git a/contrib/calendar/os-unix.factor b/contrib/calendar/os-unix.factor index 2d37f2df92..e8334f1c75 100644 --- a/contrib/calendar/os-unix.factor +++ b/contrib/calendar/os-unix.factor @@ -1,4 +1,32 @@ IN: calendar -USING: alien ; +USING: alien arrays compiler errors kernel math ; TYPEDEF: uint time_t +BEGIN-STRUCT: t + FIELD: long tv_sec + FIELD: long tv_usec +END-STRUCT + +FUNCTION: time_t time ( time_t* t ) ; +FUNCTION: tm* localtime ( time_t* clock ) ; + +: get-time + f time localtime ; + + + + + + + +! BEGIN-STRUCT: tz + ! FIELD: int tz_minuteswest + ! FIELD: int tz_dsttime +! END-STRUCT + +! FUNCTION: int gettimeofday ( t* timeval, tz* timezone ) ; + +! : machine-gmt-offset + ! "t" "tz" 2dup gettimeofday + ! zero? [ nip tz-tz_minuteswest 60 / neg ] [ 2drop 0 ] if ; +