r10563: add module keyword to def-function
[uffi.git] / tests / time.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          time.lisp
6 ;;;; Purpose:       UFFI test file, time, use C structures
7 ;;;; Author:        Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15
16 (in-package #:uffi-tests)
17
18 (uffi:def-foreign-type time-t :unsigned-long)
19
20 (uffi:def-struct tm
21     (sec :int)
22   (min :int)
23   (hour :int)
24   (mday :int)
25   (mon :int)
26   (year :int)
27   (wday :int)
28   (yday :int)
29   (isdst :int)
30   ;; gmoffset present on SusE SLES9
31   (gmoffset :long))
32
33 (uffi:def-function ("time" c-time) 
34     ((time (* time-t)))
35   :module "c"
36   :returning time-t)
37
38 (uffi:def-function "gmtime"
39     ((time (* time-t)))
40   :module "c"
41   :returning (* tm))
42
43 (uffi:def-function "asctime"
44     ((time (* tm)))
45   :module "c"
46   :returning :cstring)
47
48 (uffi:def-type time-t :unsigned-long)
49 (uffi:def-type tm-pointer (* tm))
50
51 (deftest time.1
52    (uffi:with-foreign-object (time 'time-t)
53      (setf (uffi:deref-pointer time :unsigned-long) 7381)
54      (uffi:deref-pointer time :unsigned-long))
55   7381)
56
57 (deftest time.2
58   (uffi:with-foreign-object (time 'time-t)
59     (setf (uffi:deref-pointer time :unsigned-long) 7381)
60     (let ((tm-ptr (the tm-pointer (gmtime time))))
61       (values (1+ (uffi:get-slot-value tm-ptr 'tm 'mon))
62               (uffi:get-slot-value tm-ptr 'tm 'mday)
63               (+ 1900 (uffi:get-slot-value tm-ptr 'tm 'year))
64               (uffi:get-slot-value tm-ptr 'tm 'hour)
65               (uffi:get-slot-value tm-ptr 'tm 'min)
66               (uffi:get-slot-value tm-ptr 'tm 'sec)
67               )))
68   1 1 1970 2 3 1)
69
70
71 (uffi:def-struct timeval 
72     (secs :long)
73   (usecs :long))
74
75 (uffi:def-struct timezone
76     (minutes-west :int)
77   (dsttime :int))
78
79 (uffi:def-function ("gettimeofday" c-gettimeofday) 
80     ((tv (* timeval))
81      (tz (* timezone)))
82   :module "c"
83   :returning :int)
84                     
85 (defun get-utime ()
86   (uffi:with-foreign-object (tv 'timeval)
87     (let ((res (c-gettimeofday tv (uffi:make-null-pointer 'timezone))))
88       (values
89        (+ (* 1000000 (uffi:get-slot-value tv 'timeval 'secs))
90           (uffi:get-slot-value tv 'timeval 'usecs))
91        res))))
92
93 (deftest timeofday.1
94     (multiple-value-bind (t1 res1) (get-utime)
95       (multiple-value-bind (t2 res2) (get-utime)
96         (and (or (= t2 t1) (> t2 t1))
97              (> t1 1000000000)
98              (> t2 1000000000)
99              (zerop res1)
100              (zerop res2))))
101   t)
102              
103 (defun posix-time-to-asctime (secs)
104   "Converts number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC)"
105   (string-right-trim
106    '(#\newline #\return)
107    (uffi:convert-from-cstring
108     (uffi:with-foreign-object (time 'time-t)
109       (setf (uffi:deref-pointer time :unsigned-long) secs)
110       (asctime (gmtime time))))))
111
112 (deftest time.3
113     (posix-time-to-asctime 0)
114   "Thu Jan  1 00:00:00 1970")