Remove old CVS $Id$ keyword
[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 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
11 ;;;;
12 ;;;; *************************************************************************
13
14 (in-package :cl-user)
15
16 (uffi:def-foreign-type time-t :unsigned-long)
17
18 (uffi:def-struct tm
19     (sec :int)
20   (min :int)
21   (hour :int)
22   (mday :int)
23   (mon :int)
24   (year :int)
25   (wday :int)
26   (yday :int)
27   (isdst :int))
28
29 (uffi:def-function ("time" c-time)
30     ((time (* time-t)))
31   :returning time-t)
32
33 (uffi:def-function ("localtime" c-localtime)
34     ((time (* time-t)))
35   :returning (* tm))
36
37 (uffi:def-type time-t :unsigned-long)
38 (uffi:def-type tm-pointer (* tm))
39
40 (defun gettime ()
41    "Returns the local time"
42    (uffi:with-foreign-object (time 'time-t)
43 ;;     (declare (type time-t time))
44      (c-time time)
45      (let ((tm-ptr (the tm-pointer (c-localtime time))))
46        (declare (type tm-pointer tm-ptr))
47        (let ((time-string (format nil "~2d/~2,'0d/~d ~2d:~2,'0d:~2,'0d"
48                                   (1+ (uffi:get-slot-value tm-ptr 'tm 'mon))
49                                   (uffi:get-slot-value tm-ptr 'tm 'mday)
50                                   (+ 1900 (uffi:get-slot-value tm-ptr 'tm 'year))
51                                   (uffi:get-slot-value tm-ptr 'tm 'hour)
52                                   (uffi:get-slot-value tm-ptr 'tm 'min)
53                                   (uffi:get-slot-value tm-ptr 'tm 'sec)
54                                   )))
55          time-string))))
56
57
58
59
60 #+examples-uffi
61 (format t "~&~A" (gettime))
62
63 #+test-uffi
64 (progn
65   (let ((time (gettime)))
66     (util.test:test (stringp time) t :fail-info "Time is not a string")
67     (util.test:test (plusp (parse-integer time :junk-allowed t))
68                     t
69                     :fail-info "time string does not start with a number")))
70
71