Mercurial > emacs
annotate lisp/calendar/cal-iso.el @ 66413:4e56b3fda002
* menu-bar.el (menu-bar-help-menu): Rename "psychiatrist", in line
with 2005-10-23 change to doctor.el.
* finder.el (finder-mode-map): Add follow-link binding.
author | Chong Yidong <cyd@stupidchicken.com> |
---|---|
date | Mon, 24 Oct 2005 22:13:08 +0000 |
parents | 5c09efcfc1d9 |
children | a55ee709ec8d 5e2d3828e89f |
rev | line source |
---|---|
38422
7a94f1c588c4
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
20462
diff
changeset
|
1 ;;; cal-iso.el --- calendar functions for the ISO calendar |
13053 | 2 |
57324
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
3 ;; Copyright (C) 1995, 1997, 2004 Free Software Foundation, Inc. |
13053 | 4 |
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu> | |
65919
5c09efcfc1d9
Update maintainer email address.
Glenn Morris <rgm@gnu.org>
parents:
65145
diff
changeset
|
6 ;; Maintainer: Glenn Morris <rgm@gnu.org> |
13053 | 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 | |
14169 | 23 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
64085 | 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02110-1301, USA. | |
13053 | 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 | |
20462
d179de7ad92e
Add reference to new Calendrical Calculations book.
Paul Eggert <eggert@twinsun.com>
parents:
14169
diff
changeset
|
32 ;; Technical details of all the calendrical calculations can be found in |
61148
7f7db25577d9
Update reference to "Calendrical Calculations" book; there's a new edition.
Paul Eggert <eggert@twinsun.com>
parents:
57324
diff
changeset
|
33 ;; ``Calendrical Calculations: The Millennium Edition'' by Edward M. Reingold |
7f7db25577d9
Update reference to "Calendrical Calculations" book; there's a new edition.
Paul Eggert <eggert@twinsun.com>
parents:
57324
diff
changeset
|
34 ;; and Nachum Dershowitz, Cambridge University Press (2001). |
20462
d179de7ad92e
Add reference to new Calendrical Calculations book.
Paul Eggert <eggert@twinsun.com>
parents:
14169
diff
changeset
|
35 |
13053 | 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 | |
65145
ac895e21e622
(date): Add defvar.
Juanma Barranquero <lekktu@gmail.com>
parents:
64085
diff
changeset
|
44 (defvar date) |
ac895e21e622
(date): Add defvar.
Juanma Barranquero <lekktu@gmail.com>
parents:
64085
diff
changeset
|
45 |
13053 | 46 (require 'calendar) |
47 | |
48 (defun calendar-absolute-from-iso (date) | |
49 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE. | |
50 The `ISO year' corresponds approximately to the Gregorian year, but | |
51 weeks start on Monday and end on Sunday. The first week of the ISO year is | |
52 the first such week in which at least 4 days are in a year. The ISO | |
53 commercial DATE has the form (week day year) in which week is in the range | |
54 1..52 and day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = | |
55 Sunday). The Gregorian date Sunday, December 31, 1 BC is imaginary." | |
56 (let* ((week (extract-calendar-month date)) | |
57 (day (extract-calendar-day date)) | |
58 (year (extract-calendar-year date))) | |
59 (+ (calendar-dayname-on-or-before | |
60 1 (+ 3 (calendar-absolute-from-gregorian (list 1 1 year)))) | |
61 (* 7 (1- week)) | |
62 (if (= day 0) 6 (1- day))))) | |
63 | |
64 (defun calendar-iso-from-absolute (date) | |
65 "Compute the `ISO commercial date' corresponding to the absolute DATE. | |
66 The ISO year corresponds approximately to the Gregorian year, but weeks | |
67 start on Monday and end on Sunday. The first week of the ISO year is the | |
68 first such week in which at least 4 days are in a year. The ISO commercial | |
69 date has the form (week day year) in which week is in the range 1..52 and | |
70 day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = Sunday). The | |
71 absolute date is the number of days elapsed since the (imaginary) Gregorian | |
72 date Sunday, December 31, 1 BC." | |
73 (let* ((approx (extract-calendar-year | |
74 (calendar-gregorian-from-absolute (- date 3)))) | |
75 (year (+ approx | |
76 (calendar-sum y approx | |
77 (>= date (calendar-absolute-from-iso (list 1 1 (1+ y)))) | |
78 1)))) | |
79 (list | |
80 (1+ (/ (- date (calendar-absolute-from-iso (list 1 1 year))) 7)) | |
81 (% date 7) | |
82 year))) | |
83 | |
84 (defun calendar-iso-date-string (&optional date) | |
85 "String of ISO date of Gregorian DATE. | |
86 Defaults to today's date if DATE is not given." | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38422
diff
changeset
|
87 (let* ((d (calendar-absolute-from-gregorian |
13053 | 88 (or date (calendar-current-date)))) |
89 (day (% d 7)) | |
90 (iso-date (calendar-iso-from-absolute d))) | |
91 (format "Day %s of week %d of %d" | |
92 (if (zerop day) 7 day) | |
93 (extract-calendar-month iso-date) | |
94 (extract-calendar-year iso-date)))) | |
95 | |
96 (defun calendar-print-iso-date () | |
97 "Show equivalent ISO date for the date under the cursor." | |
98 (interactive) | |
99 (message "ISO date: %s" | |
100 (calendar-iso-date-string (calendar-cursor-to-date t)))) | |
101 | |
57324
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
102 (defun calendar-iso-read-args (&optional dayflag) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
103 "Interactively read the arguments for an iso date command." |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
104 (let* ((today (calendar-current-date)) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
105 (year (calendar-read |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
106 "ISO calendar year (>0): " |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
107 '(lambda (x) (> x 0)) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
108 (int-to-string (extract-calendar-year today)))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
109 (no-weeks (extract-calendar-month |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
110 (calendar-iso-from-absolute |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
111 (1- |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
112 (calendar-dayname-on-or-before |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
113 1 (calendar-absolute-from-gregorian |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
114 (list 1 4 (1+ year)))))))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
115 (week (calendar-read |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
116 (format "ISO calendar week (1-%d): " no-weeks) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
117 '(lambda (x) (and (> x 0) (<= x no-weeks))))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
118 (day (if dayflag (calendar-read |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
119 "ISO day (1-7): " |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
120 '(lambda (x) (and (<= 1 x) (<= x 7)))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
121 1))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
122 (list (list week day year)))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
123 |
13053 | 124 (defun calendar-goto-iso-date (date &optional noecho) |
125 "Move cursor to ISO DATE; echo ISO date unless NOECHO is t." | |
57324
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
126 (interactive (calendar-iso-read-args t)) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
127 (calendar-goto-date (calendar-gregorian-from-absolute |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
128 (calendar-absolute-from-iso date))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
129 (or noecho (calendar-print-iso-date))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
130 |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
131 (defun calendar-goto-iso-week (date &optional noecho) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
132 "Move cursor to ISO DATE; echo ISO date unless NOECHO is t. |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
133 Interactively, goes to the first day of the specified week." |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
134 (interactive (calendar-iso-read-args)) |
13053 | 135 (calendar-goto-date (calendar-gregorian-from-absolute |
136 (calendar-absolute-from-iso date))) | |
137 (or noecho (calendar-print-iso-date))) | |
138 | |
139 (defun diary-iso-date () | |
140 "ISO calendar equivalent of date diary entry." | |
141 (format "ISO date: %s" (calendar-iso-date-string date))) | |
142 | |
143 (provide 'cal-iso) | |
144 | |
52401 | 145 ;;; arch-tag: 3c0154cc-d30f-4981-9f60-42bdf7a468f6 |
13053 | 146 ;;; cal-iso.el ends here |