1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: datetime.lisp
6 ;;;; Purpose: Date & Time functions for KMRCL package
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
22 ;;; Formatting functions
24 (defun pretty-date (year month day &optional (hour 12) (m 0) (s 0))
25 (multiple-value-bind (sec min hr dy mn yr wkday)
26 (decode-universal-time
27 (encode-universal-time s m hour day month year))
28 (values (elt '("Monday" "Tuesday" "Wednesday" "Thursday"
29 "Friday" "Saturday" "Sunday")
31 (elt '("January" "February" "March" "April" "May" "June"
32 "July" "August" "September" "October" "November"
35 (format nil "~A" dy) (format nil "~A" yr)
36 (format nil "~2,'0D:~2,'0D:~2,'0D" hr min sec))))
39 (defun date-string (ut)
40 (if (typep ut 'integer)
41 (multiple-value-bind (sec min hr day mon year dow daylight-p zone)
42 (decode-universal-time ut)
43 (declare (ignore daylight-p zone))
44 (format nil "~[Mon~;Tue~;Wed~;Thu~;Fri~;Sat~;Sun~], ~d ~[Jan~;Feb~;Mar~;Apr~;May~;Jun~;Jul~;Aug~;Sep~;Oct~;Nov~;Dec~] ~d ~2,'0d:~2,'0d:~2,'0d"
51 (defun print-seconds (secs)
52 (print-float-units secs "sec"))
54 (defun print-float-units (val unit)
57 (format t "~,2,9F nano~A" val unit))
59 (format t "~,2,6F micro~A" val unit))
61 (format t "~,2,3F milli~A" val unit))
63 (format t "~,2,-9F giga~A" val unit))
65 (format t "~,2,-6F mega~A" val unit))
67 (format t "~,2,-3F kilo~A" val unit))
69 (format t "~,2F ~A" val unit))))
71 (defconstant +posix-epoch+
72 (encode-universal-time 0 0 0 1 1 1970 0))
74 (defun posix-time-to-utime (time)
75 (+ time +posix-epoch+))
77 ;; Monthnames taken from net-telent-date to support lml2
93 (defun monthname (stream arg colon-p at-p &optional width (mincol 0) (colinc 1) (minpad 0) (padchar #\Space))
94 "Print the name of the month (1=January) corresponding to ARG on STREAM. This is intended for embedding in a FORMAT directive: WIDTH governs the number of characters of text printed, MINCOL, COLINC, MINPAD, PADCHAR work as for ~A"
95 (let ((monthstring (cdr (assoc arg *monthnames*))))
96 (if (not monthstring) (return-from monthname nil))
97 (let ((truncate (if width (min width (length monthstring)) nil)))
99 (if at-p "~V,V,V,V@A" "~V,V,V,VA")
100 mincol colinc minpad padchar
101 (subseq monthstring 0 truncate)))))
103 ;;;; Daylight Saving Time calculations
105 ;; Daylight Saving Time begins for most of the United States at 2
106 ;; a.m. on the first Sunday of April. Time reverts to standard time at
107 ;; 2 a.m. on the last Sunday of October. In the U.S., each time zone
108 ;; switches at a different time.
110 ;; In the European Union, Summer Time begins and ends at 1 am
111 ;; Universal Time (Greenwich Mean Time). It starts the last Sunday in
112 ;; March, and ends the last Sunday in October. In the EU, all time
113 ;; zones change at the same moment.
115 ;; Spring forward, Fall back
116 ;; During DST, clocks are turned forward an hour, effectively moving
117 ;; an hour of daylight from the morning to the evening.
119 ;; United States European Union
121 ;; Year DST Begins DST Ends Summertime Summertime
122 ;; at 2 a.m. at 2 a.m. period begins period ends
123 ;; at 1 a.m. UT at 1 a.m. UT
124 ;; ----------------------------------------------------------
125 ;; 2000 April 2 October 29 March 26 October 29
126 ;; 2001 April 1 October 28 March 25 October 28
127 ;; 2002 April 7 October 27 March 31 October 27
128 ;; 2003 April 6 October 26 March 30 October 26
129 ;; 2004 April 4 October 31 March 28 October 31
130 ;; 2005 April 3 October 30 March 27 October 30
131 ;; 2006 April 2 October 29 March 26 October 29
132 ;; 2007 April 1 October 28 March 25 October 28
133 ;; 2008 April 6 October 26 March 30 October 26