r9236: fix library loading, function name
[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
31 (uffi:def-function ("time" c-time) 
32     ((time (* time-t)))
33   :returning time-t)
34
35 (uffi:def-function ("gmtime" c-gmtime)
36     ((time (* time-t)))
37   :returning (* tm))
38
39 (uffi:def-type time-t :unsigned-long)
40 (uffi:def-type tm-pointer (* tm))
41
42 (deftest time.1
43    (uffi:with-foreign-object (time 'time-t)
44      (setf (uffi:deref-pointer time :unsigned-long) 7381)
45      (uffi:deref-pointer time :unsigned-long))
46   7381)
47
48 (deftest time.2
49    (uffi:with-foreign-object (time 'time-t)
50      (setf (uffi:deref-pointer time :unsigned-long) 7381)
51      (let ((tm-ptr (the tm-pointer (c-gmtime time))))
52        (values (1+ (uffi:get-slot-value tm-ptr 'tm 'mon))
53                (uffi:get-slot-value tm-ptr 'tm 'mday)
54                (+ 1900 (uffi:get-slot-value tm-ptr 'tm 'year))
55                (uffi:get-slot-value tm-ptr 'tm 'hour)
56                (uffi:get-slot-value tm-ptr 'tm 'min)
57                (uffi:get-slot-value tm-ptr 'tm 'sec)
58                )))
59   1 1 1970 2 3 1)
60
61
62 (uffi:def-struct timeval 
63     (secs :long)
64   (usecs :long))
65
66 (uffi:def-struct timezone
67     (minutes-west :int)
68   (dsttime :int))
69
70 (uffi:def-function ("gettimeofday" c-gettimeofday) 
71     ((tv (* timeval))
72      (tz (* timezone)))
73   :returning :int)
74                     
75 (defun get-utime ()
76   (uffi:with-foreign-object (tv 'timeval)
77     (let ((res (c-gettimeofday tv (uffi:make-null-pointer 'timezone))))
78       (values
79        (+ (* 1000000 (uffi:get-slot-value tv 'timeval 'secs))
80           (uffi:get-slot-value tv 'timeval 'usecs))
81        res))))
82
83 (deftest timeofday.1
84     (multiple-value-bind (t1 res1) (get-utime)
85       (multiple-value-bind (t2 res2) (get-utime)
86         (and (or (= t2 t1) (> t2 t1))
87              (> t1 1000000000)
88              (> t2 1000000000)
89              (zerop res1)
90              (zerop res2))))
91   t)
92              
93 (defun posix-time-to-localtime-string (secs)
94   "Converts number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC)"
95   (string-right-trim
96    '(#\newline #\return)
97    (uffi:convert-from-cstring
98     (uffi:with-foreign-object (time 'time-t)
99                               (setf (uffi:deref-pointer time :unsigned-long)
100                                     secs)
101                               (c-time time)))))
102