Commit | Line | Data |
---|---|---|
3afbc435 | 1 | ;;; cal-iso.el --- calendar functions for the ISO calendar |
0808d911 | 2 | |
a96a5fca | 3 | ;; Copyright (C) 1995, 1997 Free Software Foundation, Inc. |
0808d911 ER |
4 | |
5 | ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu> | |
6 | ;; Keywords: calendar | |
7 | ;; Human-Keywords: ISO calendar, calendar, diary | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
0808d911 ER |
25 | |
26 | ;;; Commentary: | |
27 | ||
28 | ;; This collection of functions implements the features of calendar.el and | |
29 | ;; diary.el that deal with the ISO calendar. | |
30 | ||
a96a5fca PE |
31 | ;; Technical details of all the calendrical calculations can be found in |
32 | ;; ``Calendrical Calculations'' by Nachum Dershowitz and Edward M. Reingold, | |
33 | ;; Cambridge University Press (1997). | |
34 | ||
0808d911 ER |
35 | ;; Comments, corrections, and improvements should be sent to |
36 | ;; Edward M. Reingold Department of Computer Science | |
37 | ;; (217) 333-6733 University of Illinois at Urbana-Champaign | |
38 | ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue | |
39 | ;; Urbana, Illinois 61801 | |
40 | ||
41 | ;;; Code: | |
42 | ||
43 | (require 'calendar) | |
44 | ||
45 | (defun calendar-absolute-from-iso (date) | |
46 | "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE. | |
47 | The `ISO year' corresponds approximately to the Gregorian year, but | |
48 | weeks start on Monday and end on Sunday. The first week of the ISO year is | |
49 | the first such week in which at least 4 days are in a year. The ISO | |
50 | commercial DATE has the form (week day year) in which week is in the range | |
51 | 1..52 and day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = | |
52 | Sunday). The Gregorian date Sunday, December 31, 1 BC is imaginary." | |
53 | (let* ((week (extract-calendar-month date)) | |
54 | (day (extract-calendar-day date)) | |
55 | (year (extract-calendar-year date))) | |
56 | (+ (calendar-dayname-on-or-before | |
57 | 1 (+ 3 (calendar-absolute-from-gregorian (list 1 1 year)))) | |
58 | (* 7 (1- week)) | |
59 | (if (= day 0) 6 (1- day))))) | |
60 | ||
61 | (defun calendar-iso-from-absolute (date) | |
62 | "Compute the `ISO commercial date' corresponding to the absolute DATE. | |
63 | The ISO year corresponds approximately to the Gregorian year, but weeks | |
64 | start on Monday and end on Sunday. The first week of the ISO year is the | |
65 | first such week in which at least 4 days are in a year. The ISO commercial | |
66 | date has the form (week day year) in which week is in the range 1..52 and | |
67 | day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = Sunday). The | |
68 | absolute date is the number of days elapsed since the (imaginary) Gregorian | |
69 | date Sunday, December 31, 1 BC." | |
70 | (let* ((approx (extract-calendar-year | |
71 | (calendar-gregorian-from-absolute (- date 3)))) | |
72 | (year (+ approx | |
73 | (calendar-sum y approx | |
74 | (>= date (calendar-absolute-from-iso (list 1 1 (1+ y)))) | |
75 | 1)))) | |
76 | (list | |
77 | (1+ (/ (- date (calendar-absolute-from-iso (list 1 1 year))) 7)) | |
78 | (% date 7) | |
79 | year))) | |
80 | ||
81 | (defun calendar-iso-date-string (&optional date) | |
82 | "String of ISO date of Gregorian DATE. | |
83 | Defaults to today's date if DATE is not given." | |
a1506d29 | 84 | (let* ((d (calendar-absolute-from-gregorian |
0808d911 ER |
85 | (or date (calendar-current-date)))) |
86 | (day (% d 7)) | |
87 | (iso-date (calendar-iso-from-absolute d))) | |
88 | (format "Day %s of week %d of %d" | |
89 | (if (zerop day) 7 day) | |
90 | (extract-calendar-month iso-date) | |
91 | (extract-calendar-year iso-date)))) | |
92 | ||
93 | (defun calendar-print-iso-date () | |
94 | "Show equivalent ISO date for the date under the cursor." | |
95 | (interactive) | |
96 | (message "ISO date: %s" | |
97 | (calendar-iso-date-string (calendar-cursor-to-date t)))) | |
98 | ||
99 | (defun calendar-goto-iso-date (date &optional noecho) | |
100 | "Move cursor to ISO DATE; echo ISO date unless NOECHO is t." | |
101 | (interactive | |
102 | (let* ((today (calendar-current-date)) | |
103 | (year (calendar-read | |
104 | "ISO calendar year (>0): " | |
105 | '(lambda (x) (> x 0)) | |
106 | (int-to-string (extract-calendar-year today)))) | |
107 | (no-weeks (extract-calendar-month | |
108 | (calendar-iso-from-absolute | |
109 | (1- | |
110 | (calendar-dayname-on-or-before | |
111 | 1 (calendar-absolute-from-gregorian | |
112 | (list 1 4 (1+ year)))))))) | |
113 | (week (calendar-read | |
114 | (format "ISO calendar week (1-%d): " no-weeks) | |
115 | '(lambda (x) (and (> x 0) (<= x no-weeks))))) | |
116 | (day (calendar-read | |
117 | "ISO day (1-7): " | |
118 | '(lambda (x) (and (<= 1 x) (<= x 7)))))) | |
119 | (list (list week day year)))) | |
120 | (calendar-goto-date (calendar-gregorian-from-absolute | |
121 | (calendar-absolute-from-iso date))) | |
122 | (or noecho (calendar-print-iso-date))) | |
123 | ||
124 | (defun diary-iso-date () | |
125 | "ISO calendar equivalent of date diary entry." | |
126 | (format "ISO date: %s" (calendar-iso-date-string date))) | |
127 | ||
128 | (provide 'cal-iso) | |
129 | ||
6b61353c | 130 | ;;; arch-tag: 3c0154cc-d30f-4981-9f60-42bdf7a468f6 |
0808d911 | 131 | ;;; cal-iso.el ends here |