Skip to content

Commit a1559ab

Browse files
committed
dates and time: rm custom, buggy day-of-week function (use local-time instead)
1 parent 7a39863 commit a1559ab

File tree

1 file changed

+0
-44
lines changed

1 file changed

+0
-44
lines changed

dates_and_times.md

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -182,50 +182,6 @@ CL-USER> (let ((numbers (loop for i from 1 to 100 collect (random 1.0))))
182182
; 3,586 cons cells, 11,704 other bytes, 0 static bytes
183183
~~~
184184

185-
<a name="weekday"></a>
186-
187-
### Computing the day of the week
188-
189-
In the section about [Universal Time](#univ) we've learned enough to write a small function that computes the day of the week. Unfortunately, by definition, this function won't work for dates before January 1, 1900.
190-
191-
~~~lisp
192-
CL-USER> (defun day-of-week (day month year)
193-
"Returns the day of the week as an integer. Monday is 0."
194-
(nth-value
195-
6
196-
(decode-universal-time
197-
(encode-universal-time 0 0 0 day month year 0)
198-
0)))
199-
DAY-OF-WEEK
200-
CL-USER> (day-of-week 23 12 1965)
201-
3
202-
CL-USER> (day-of-week 1 1 1900)
203-
0
204-
CL-USER> (day-of-week 31 12 1899)
205-
206-
Type-error in KERNEL::OBJECT-NOT-TYPE-ERROR-HANDLER:
207-
1899 is not of type (OR (MOD 100) (INTEGER 1900))
208-
~~~
209-
210-
If this is a problem for you, here's a small function by Gerald Doussot (adapted from the comp.lang.c FAQ) that will help you:
211-
212-
~~~lisp
213-
(defun day-of-week (day month year)
214-
"Returns the day of the week as an integer. Sunday is 0. Works for years after 1752."
215-
(let ((offset '(0 3 2 5 0 3 5 1 4 6 2 4)))
216-
(when (< month 3)
217-
(decf year 1))
218-
(mod
219-
(truncate (+ year
220-
(/ year 4)
221-
(/ (- year)
222-
100)
223-
(/ year 400)
224-
(nth (1- month) offset)
225-
day
226-
-1))
227-
7)))
228-
~~~
229185

230186
## The `local-time` library
231187

0 commit comments

Comments
 (0)