September 26, 2008

How to retreive current date and time in Pocket Scheme

Unfortunalelly I could not find proper procedure for getting current date and time. But it is possible to use WinApi functions. In our case we will import GetLocalTime funtion from Kernel32.dll.
This funcion requires a pointer to structure SYSTEMTIME:
void WINAPI GetLocalTime(
__out LPSYSTEMTIME lpSystemTime
);
And after calling this function this structure will be filled with data:

typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME,
*PSYSTEMTIME;
Here is Pocket Scheme code that imports function GetLocalTime and usage.

(require "w32.dll")

(define *kdll*
(cond-expand
(windows-nt "kernel32.dll")
(windows-ce "coredll.dll")))

(define GetLocalTime
(w32:foreign-procedure *kdll* "GetLocalTime" '(w32api void lpvoid)))

; outputs current year
(let ((rgw (make-raw-vector 8 'u16)))
(begin
(GetLocalTime rgw)
(display (raw-vector-ref rgw 0)) ; outputs current year
)
)

No comments: