r10608: update license
[uffi.git] / examples / gettime.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          gettime
6 ;;;; Purpose:       UFFI Example file to get time, use C structures
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15
16 (in-package :cl-user)
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 ("localtime" c-localtime)
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 (defun gettime ()
43    "Returns the local time"
44    (uffi:with-foreign-object (time 'time-t)
45 ;;     (declare (type time-t time))
46      (c-time time)
47      (let ((tm-ptr (the tm-pointer (c-localtime time))))
48        (declare (type tm-pointer tm-ptr))
49        (let ((time-string (format nil "~2d/~2,'0d/~d ~2d:~2,'0d:~2,'0d" 
50                                   (1+ (uffi:get-slot-value tm-ptr 'tm 'mon))
51                                   (uffi:get-slot-value tm-ptr 'tm 'mday)
52                                   (+ 1900 (uffi:get-slot-value tm-ptr 'tm 'year))
53                                   (uffi:get-slot-value tm-ptr 'tm 'hour)
54                                   (uffi:get-slot-value tm-ptr 'tm 'min)
55                                   (uffi:get-slot-value tm-ptr 'tm 'sec)
56                                   )))
57          time-string))))
58
59
60
61
62 #+examples-uffi
63 (format t "~&~A" (gettime))
64
65 #+test-uffi
66 (progn
67   (let ((time (gettime)))
68     (util.test:test (stringp time) t :fail-info "Time is not a string")
69     (util.test:test (plusp (parse-integer time :junk-allowed t))
70                     t
71                     :fail-info "time string does not start with a number")))
72
73