Move DECODE-TIMEOUT and NORMALIZE-TIMEOUT to base package.
[iolib/alendvai.git] / base / time.lisp
blob76919375007619804a8e19176aea56a2ffbabb5f
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; --- Time utils.
4 ;;;
6 (in-package :iolib.base)
8 (deftype timeout ()
9 'double-float)
11 ;;; Break a real timeout into seconds and microseconds.
12 (defun decode-timeout (timeout)
13 (assert (or (not timeout)
14 (and (typep timeout 'real)
15 (not (minusp timeout))))
16 (timeout)
17 "The timeout must be a non-negative real or NIL: ~S" timeout)
18 (typecase timeout
19 (null nil)
20 (integer (values timeout 0))
21 (real
22 (multiple-value-bind (q r) (truncate (coerce timeout 'timeout))
23 (declare (type unsigned-byte q)
24 (type timeout r))
25 (values q (the (values unsigned-byte t) (truncate (* r 1d6))))))))
27 (defun normalize-timeout (timeout)
28 (assert (and (typep timeout 'real)
29 (not (minusp timeout)))
30 (timeout)
31 "The timeout must be non-negative: ~A" timeout)
32 (coerce timeout 'timeout))