Coverage report: /development/source/library/com/dydra/gitlab/dydra-cgi/ffi/lisp/dydra-ndk/time.lisp

KindCoveredAll%
expression4153 77.4
branch00nil
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 (in-package :dydra-ndk)
2
 
3
 (defctype timezone :int16) ;; dydra_timezone_t in <dydra/time.h>
4
 
5
 (defctype timestamp :int64) ;; dydra_time_t in <dydra/time.h>
6
 
7
 (defcstruct timespec ;; dydra_timespec_t in <dydra/time.h>
8
   (year :int32)
9
   (month :uint8)
10
   (date :uint8)
11
   (hour :uint8)
12
   (minute :uint8)
13
   (second :uint8)
14
   (microsecond :uint32))
15
 
16
 (defmacro timespec-slot-value (timespec-var slot-name)
17
   `(foreign-slot-value ,timespec-var '(:struct timespec) ,slot-name))
18
 
19
 (cffi:defcfun ("dydra_time_encode" %%encode-time) timestamp (timespec (:pointer (:struct timespec))) (timezone timezone) (flags :uint))
20
 
21
 (cffi:defcfun ("dydra_time_decode" %%decode-time) (:pointer (:struct timespec)) (timestamp timestamp) (timezone timezone) (flags :uint))
22
 
23
 (cffi:defcfun ("dydra_time_string" %%format-time) :string (timestamp timestamp) (timezone timezone))
24
 
25
 (defun encode-time (microsecond second minute hour date month year &optional (zone 0))
26
   "Encodes the given decoded time components into a signed integer
27
    representing microseconds since the Unix epoch."
28
   (declare (type fixnum microsecond second minute hour date month year zone))
29
   (with-foreign-object (timespec-pointer '(:struct timespec))
30
     (setf (timespec-slot-value timespec-pointer 'year) year)
31
     (setf (timespec-slot-value timespec-pointer 'month) month)
32
     (setf (timespec-slot-value timespec-pointer 'date) date)
33
     (setf (timespec-slot-value timespec-pointer 'hour) hour)
34
     (setf (timespec-slot-value timespec-pointer 'minute) minute)
35
     (setf (timespec-slot-value timespec-pointer 'second) second)
36
     (setf (timespec-slot-value timespec-pointer 'microsecond) microsecond)
37
     (%%encode-time timespec-pointer (* zone 60) 0)))
38
 
39
 (defun decode-time (timestamp &optional (zone 0))
40
   "Decodes TIME, a signed integer representing microseconds since the Unix
41
    epoch, returning (VALUES microsecond second minute hour date month year
42
    day daylight-p zone)."
43
   (declare (type integer timestamp) (type fixnum zone))
44
   (let ((timespec-pointer (%%decode-time timestamp (* zone 60) 0)))
45
     (assert (not (null-pointer-p timespec-pointer)))
46
     (with-foreign-slots ((year month date hour minute second microsecond) timespec-pointer (:struct timespec))
47
       (let ((day 0) (daylight-p nil))
48
         (values microsecond second minute hour date month year day daylight-p zone)))))
49
 
50
 (defun format-time (timestamp &optional (zone 0))
51
   (declare (type integer timestamp) (type fixnum zone))
52
   (%%format-time timestamp (* zone 60)))