Mercurial > emacs
annotate lisp/calendar/cal-iso.el @ 91347:5c68d57a8dab
*** empty log message ***
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Tue, 22 Jan 2008 12:03:33 +0000 |
parents | 606f2d163a64 |
children |
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 |
79703 | 3 ;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
67465
a55ee709ec8d
Update copyright pending Emacs 22.
Glenn Morris <rgm@gnu.org>
parents:
65919
diff
changeset
|
4 ;; Free Software Foundation, Inc. |
13053 | 5 |
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu> | |
65919
5c09efcfc1d9
Update maintainer email address.
Glenn Morris <rgm@gnu.org>
parents:
65145
diff
changeset
|
7 ;; Maintainer: Glenn Morris <rgm@gnu.org> |
13053 | 8 ;; Keywords: calendar |
9 ;; Human-Keywords: ISO calendar, calendar, diary | |
10 | |
11 ;; This file is part of GNU Emacs. | |
12 | |
13 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
14 ;; it under the terms of the GNU General Public License as published by | |
78216
93e11478c954
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
77279
diff
changeset
|
15 ;; the Free Software Foundation; either version 3, or (at your option) |
13053 | 16 ;; any later version. |
17 | |
18 ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 ;; GNU General Public License for more details. | |
22 | |
23 ;; You should have received a copy of the GNU General Public License | |
14169 | 24 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
64085 | 25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
26 ;; Boston, MA 02110-1301, USA. | |
13053 | 27 |
28 ;;; Commentary: | |
29 | |
30 ;; This collection of functions implements the features of calendar.el and | |
31 ;; diary.el that deal with the ISO calendar. | |
32 | |
20462
d179de7ad92e
Add reference to new Calendrical Calculations book.
Paul Eggert <eggert@twinsun.com>
parents:
14169
diff
changeset
|
33 ;; 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
|
34 ;; ``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
|
35 ;; 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
|
36 |
13053 | 37 ;;; Code: |
38 | |
65145
ac895e21e622
(date): Add defvar.
Juanma Barranquero <lekktu@gmail.com>
parents:
64085
diff
changeset
|
39 (defvar date) |
ac895e21e622
(date): Add defvar.
Juanma Barranquero <lekktu@gmail.com>
parents:
64085
diff
changeset
|
40 |
13053 | 41 (require 'calendar) |
42 | |
43 (defun calendar-absolute-from-iso (date) | |
44 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE. | |
45 The `ISO year' corresponds approximately to the Gregorian year, but | |
46 weeks start on Monday and end on Sunday. The first week of the ISO year is | |
47 the first such week in which at least 4 days are in a year. The ISO | |
48 commercial DATE has the form (week day year) in which week is in the range | |
49 1..52 and day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = | |
50 Sunday). The Gregorian date Sunday, December 31, 1 BC is imaginary." | |
51 (let* ((week (extract-calendar-month date)) | |
52 (day (extract-calendar-day date)) | |
53 (year (extract-calendar-year date))) | |
54 (+ (calendar-dayname-on-or-before | |
55 1 (+ 3 (calendar-absolute-from-gregorian (list 1 1 year)))) | |
56 (* 7 (1- week)) | |
57 (if (= day 0) 6 (1- day))))) | |
58 | |
59 (defun calendar-iso-from-absolute (date) | |
60 "Compute the `ISO commercial date' corresponding to the absolute DATE. | |
61 The ISO year corresponds approximately to the Gregorian year, but weeks | |
62 start on Monday and end on Sunday. The first week of the ISO year is the | |
63 first such week in which at least 4 days are in a year. The ISO commercial | |
64 date has the form (week day year) in which week is in the range 1..52 and | |
65 day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = Sunday). The | |
66 absolute date is the number of days elapsed since the (imaginary) Gregorian | |
67 date Sunday, December 31, 1 BC." | |
68 (let* ((approx (extract-calendar-year | |
69 (calendar-gregorian-from-absolute (- date 3)))) | |
70 (year (+ approx | |
71 (calendar-sum y approx | |
72 (>= date (calendar-absolute-from-iso (list 1 1 (1+ y)))) | |
73 1)))) | |
74 (list | |
75 (1+ (/ (- date (calendar-absolute-from-iso (list 1 1 year))) 7)) | |
76 (% date 7) | |
77 year))) | |
78 | |
79 (defun calendar-iso-date-string (&optional date) | |
80 "String of ISO date of Gregorian DATE. | |
81 Defaults to today's date if DATE is not given." | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
38422
diff
changeset
|
82 (let* ((d (calendar-absolute-from-gregorian |
13053 | 83 (or date (calendar-current-date)))) |
84 (day (% d 7)) | |
85 (iso-date (calendar-iso-from-absolute d))) | |
86 (format "Day %s of week %d of %d" | |
87 (if (zerop day) 7 day) | |
88 (extract-calendar-month iso-date) | |
89 (extract-calendar-year iso-date)))) | |
90 | |
91 (defun calendar-print-iso-date () | |
92 "Show equivalent ISO date for the date under the cursor." | |
93 (interactive) | |
94 (message "ISO date: %s" | |
95 (calendar-iso-date-string (calendar-cursor-to-date t)))) | |
96 | |
57324
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
97 (defun calendar-iso-read-args (&optional dayflag) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
98 "Interactively read the arguments for an iso date command." |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
99 (let* ((today (calendar-current-date)) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
100 (year (calendar-read |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
101 "ISO calendar year (>0): " |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
102 '(lambda (x) (> x 0)) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
103 (int-to-string (extract-calendar-year today)))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
104 (no-weeks (extract-calendar-month |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
105 (calendar-iso-from-absolute |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
106 (1- |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
107 (calendar-dayname-on-or-before |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
108 1 (calendar-absolute-from-gregorian |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
109 (list 1 4 (1+ year)))))))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
110 (week (calendar-read |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
111 (format "ISO calendar week (1-%d): " no-weeks) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
112 '(lambda (x) (and (> x 0) (<= x no-weeks))))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
113 (day (if dayflag (calendar-read |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
114 "ISO day (1-7): " |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
115 '(lambda (x) (and (<= 1 x) (<= x 7)))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
116 1))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
117 (list (list week day year)))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
118 |
13053 | 119 (defun calendar-goto-iso-date (date &optional noecho) |
120 "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
|
121 (interactive (calendar-iso-read-args t)) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
122 (calendar-goto-date (calendar-gregorian-from-absolute |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
123 (calendar-absolute-from-iso date))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
124 (or noecho (calendar-print-iso-date))) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
125 |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
126 (defun calendar-goto-iso-week (date &optional noecho) |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
127 "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
|
128 Interactively, goes to the first day of the specified week." |
f51c087984a0
Update copyright and maintainer.
Glenn Morris <rgm@gnu.org>
parents:
52401
diff
changeset
|
129 (interactive (calendar-iso-read-args)) |
13053 | 130 (calendar-goto-date (calendar-gregorian-from-absolute |
131 (calendar-absolute-from-iso date))) | |
132 (or noecho (calendar-print-iso-date))) | |
133 | |
134 (defun diary-iso-date () | |
135 "ISO calendar equivalent of date diary entry." | |
136 (format "ISO date: %s" (calendar-iso-date-string date))) | |
137 | |
138 (provide 'cal-iso) | |
139 | |
52401 | 140 ;;; arch-tag: 3c0154cc-d30f-4981-9f60-42bdf7a468f6 |
13053 | 141 ;;; cal-iso.el ends here |