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