]> gitweb.factorcode.org Git - factor.git/blob - extra/calendar/windows/windows.factor
Initial import
[factor.git] / extra / calendar / windows / windows.factor
1 USING: alien alien.c-types kernel math
2 windows windows.kernel32 calendar namespaces ;
3 IN: calendar.windows
4
5 TUPLE: windows-calendar ;
6
7 T{ windows-calendar } calendar-impl set-global
8
9 M: windows-calendar gmt-offset ( -- float )
10     "TIME_ZONE_INFORMATION" <c-object>
11     [ GetTimeZoneInformation win32-error=0/f ] keep
12     [ TIME_ZONE_INFORMATION-Bias ] keep
13     TIME_ZONE_INFORMATION-DaylightBias + 60 /f neg ;
14
15 : >64bit ( lo hi -- n )
16     32 shift bitor ;
17
18 : windows-1601 ( -- timestamp )
19     1601 1 1 0 0 0 0 <timestamp> ;
20
21 : FILETIME>windows-time ( FILETIME -- n )
22     [ FILETIME-dwLowDateTime ] keep
23     FILETIME-dwHighDateTime >64bit ;
24
25 : windows-time>timestamp ( n -- timestamp )
26     10000000 /i seconds windows-1601 swap +dt ;
27
28 : windows-time ( -- n )
29     "FILETIME" <c-object> [ GetSystemTimeAsFileTime ] keep
30     FILETIME>windows-time ;
31
32 : timestamp>windows-time ( timestamp -- n )
33     #! 64bit number representing # of nanoseconds since Jan 1, 1601 (UTC)
34     >gmt windows-1601 timestamp- >bignum 10000000 * ;
35
36 : windows-time>FILETIME ( n -- FILETIME )
37     "FILETIME" <c-object>
38     [
39         [ >r HEX: ffffffff bitand r> set-FILETIME-dwLowDateTime ] 2keep
40         >r -32 shift r> set-FILETIME-dwHighDateTime
41     ] keep ;
42
43 : timestamp>FILETIME ( timestamp -- FILETIME/f )
44     [ >gmt timestamp>windows-time windows-time>FILETIME ] [ f ] if* ;
45
46 : FILETIME>timestamp ( FILETIME -- timestamp/f )
47     FILETIME>windows-time windows-time>timestamp ;
48