r11723: new release
[kmrcl.git] / datetime.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          datetime.lisp
6 ;;;; Purpose:       Date & Time functions for KMRCL package
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
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 ;;;; *************************************************************************
18
19 (in-package #:kmrcl)
20
21
22 ;;; Formatting functions
23
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")
30                  wkday)
31             (elt '("January" "February" "March" "April" "May" "June"
32                    "July" "August" "September" "October" "November"
33                    "December")
34                  (1- mn))
35             (format nil "~A" dy)
36             (format nil "~A" yr)
37             (format nil "~2,'0D:~2,'0D:~2,'0D" hr min sec))))
38
39 (defun pretty-date-ut (&optional (tm (get-universal-time)))
40   (multiple-value-bind (sec min hr dy mn yr) (decode-universal-time tm)
41     (pretty-date yr mn dy hr min sec)))
42
43 (defun date-string (ut)
44   (if (typep ut 'integer)
45       (multiple-value-bind (sec min hr day mon year dow daylight-p zone)
46           (decode-universal-time ut)
47         (declare (ignore daylight-p zone))
48         (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"
49                 dow
50                 day
51                 (1- mon)
52                 year
53                 hr min sec))))
54
55 (defun print-seconds (secs)
56   (print-float-units secs "sec"))
57
58 (defun print-float-units (val unit)
59   (cond
60     ((< val 1d-6)
61      (format t "~,2,9F nano~A" val unit))
62     ((< val 1d-3)
63      (format t "~,2,6F micro~A" val unit))
64     ((< val 1)
65      (format t "~,2,3F milli~A" val unit))
66     ((> val 1d9)
67      (format t "~,2,-9F giga~A" val unit))
68     ((> val 1d6)
69      (format t "~,2,-6F mega~A" val unit))
70     ((> val 1d3)
71      (format t "~,2,-3F kilo~A" val unit))
72     (t
73      (format t "~,2F ~A" val unit))))
74
75 (defconstant +posix-epoch+
76   (encode-universal-time 0 0 0 1 1 1970 0))
77
78 (defun posix-time-to-utime (time)
79   (+ time +posix-epoch+))
80
81 (defun utime-to-posix-time (utime)
82   (- utime +posix-epoch+))
83
84 ;; Monthnames taken from net-telent-date to support lml2
85
86 (defvar *monthnames*
87   '((1 . "January")
88     (2 . "February")
89     (3 . "March")
90     (4 . "April")
91     (5 . "May")
92     (6 . "June")
93     (7 . "July")
94     (8 . "August")
95     (9 . "September")
96     (10 . "October")
97     (11 . "November")
98     (12 . "December")))
99
100 (defun monthname (stream arg colon-p at-p &optional width (mincol 0) (colinc 1) (minpad 0) (padchar #\Space))
101   "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"
102   (declare (ignore colon-p))
103   (let ((monthstring (cdr (assoc arg *monthnames*))))
104     (if (not monthstring) (return-from monthname nil))
105     (let ((truncate (if width (min width (length monthstring)) nil)))
106       (format stream
107               (if at-p "~V,V,V,V@A" "~V,V,V,VA")
108               mincol colinc minpad padchar
109               (subseq monthstring 0 truncate)))))
110
111 (defconstant* +zellers-adj+ #(0 3 2 5 0 3 5 1 4 6 2 4))
112
113 (defun day-of-week (year month day)
114   "Day of week calculation using Zeller's Congruence.
115 Input: The year y, month m (1 ≤ m ≤ 12) and day d (1 ≤ d ≤ 31).
116 Output: n - the day of the week (Sunday = 0, Saturday = 6)."
117
118   (when (< month 3)
119     (decf year))
120   (mod
121    (+ year (floor year 4) (- (floor year 100)) (floor year 400)
122       (aref +zellers-adj+ (1- month)) day)
123    7))
124
125 ;;;; Daylight Saving Time calculations
126
127 ;; Daylight Saving Time begins for most of the United States at 2
128 ;; a.m. on the first Sunday of April. Time reverts to standard time at
129 ;; 2 a.m. on the last Sunday of October. In the U.S., each time zone
130 ;; switches at a different time.
131
132 ;; In the European Union, Summer Time begins and ends at 1 am
133 ;; Universal Time (Greenwich Mean Time). It starts the last Sunday in
134 ;; March, and ends the last Sunday in October. In the EU, all time
135 ;; zones change at the same moment.
136
137 ;; Spring forward, Fall back
138 ;; During DST, clocks are turned forward an hour, effectively moving
139 ;; an hour of daylight from the morning to the evening.
140
141 ;; United States                  European Union
142
143 ;; Year  DST Begins DST Ends     Summertime     Summertime
144 ;;       at 2 a.m.  at 2 a.m.    period begins  period ends
145 ;;                               at 1 a.m. UT   at 1 a.m. UT
146 ;; ----------------------------------------------------------
147 ;; 2000  April 2   October 29    March 26       October 29
148 ;; 2001  April 1   October 28    March 25       October 28
149 ;; 2002  April 7   October 27    March 31       October 27
150 ;; 2003  April 6   October 26    March 30       October 26
151 ;; 2004  April 4   October 31    March 28       October 31
152 ;; 2005  April 3   October 30    March 27       October 30
153 ;; 2006  April 2   October 29    March 26       October 29
154 ;; 2007  April 1   October 28    March 25       October 28
155 ;; 2008  April 6   October 26    March 30       October 26
156
157