Switch to recommended form of GPLv3 permissions notice.
[emacs.git] / lisp / calendar / cal-iso.el
blobcd50074be2bad29088a43e9c7e66f037d626f6f0
1 ;;; cal-iso.el --- calendar functions for the ISO calendar
3 ;; Copyright (C) 1995, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008 Free Software Foundation, Inc.
6 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
7 ;; Maintainer: Glenn Morris <rgm@gnu.org>
8 ;; Keywords: calendar
9 ;; Human-Keywords: ISO calendar, calendar, diary
11 ;; This file is part of GNU Emacs.
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
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; See calendar.el.
30 ;;; Code:
32 (require 'calendar)
34 (defun calendar-iso-to-absolute (date)
35 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE.
36 The `ISO year' corresponds approximately to the Gregorian year, but
37 weeks start on Monday and end on Sunday. The first week of the ISO year is
38 the first such week in which at least 4 days are in a year. The ISO
39 commercial DATE has the form (week day year) in which week is in the range
40 1..52 and day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 =
41 Sunday). The Gregorian date Sunday, December 31, 1 BC is imaginary."
42 (let ((day (calendar-extract-day date)))
43 (+ (calendar-dayname-on-or-before
44 1 (+ 3 (calendar-absolute-from-gregorian
45 (list 1 1 (calendar-extract-year date)))))
46 ;; ISO date is (week day year); normally (month day year).
47 (* 7 (1- (calendar-extract-month date)))
48 (if (zerop day) 6 (1- day)))))
50 (define-obsolete-function-alias 'calendar-absolute-from-iso
51 'calendar-iso-to-absolute "23.1")
53 (defun calendar-iso-from-absolute (date)
54 "Compute the `ISO commercial date' corresponding to the absolute DATE.
55 The ISO year corresponds approximately to the Gregorian year, but weeks
56 start on Monday and end on Sunday. The first week of the ISO year is the
57 first such week in which at least 4 days are in a year. The ISO commercial
58 date has the form (week day year) in which week is in the range 1..52 and
59 day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = Sunday). The
60 absolute date is the number of days elapsed since the (imaginary) Gregorian
61 date Sunday, December 31, 1 BC."
62 (let* ((approx (calendar-extract-year
63 (calendar-gregorian-from-absolute (- date 3))))
64 (year (+ approx
65 (calendar-sum y approx
66 (>= date (calendar-iso-to-absolute
67 (list 1 1 (1+ y))))
68 1))))
69 (list
70 (1+ (/ (- date (calendar-iso-to-absolute (list 1 1 year))) 7))
71 (% date 7)
72 year)))
74 ;;;###cal-autoload
75 (defun calendar-iso-date-string (&optional date)
76 "String of ISO date of Gregorian DATE, default today."
77 (let* ((d (calendar-absolute-from-gregorian
78 (or date (calendar-current-date))))
79 (day (% d 7))
80 (iso-date (calendar-iso-from-absolute d)))
81 (format "Day %s of week %d of %d"
82 (if (zerop day) 7 day)
83 (calendar-extract-month iso-date)
84 (calendar-extract-year iso-date))))
86 ;;;###cal-autoload
87 (defun calendar-iso-print-date ()
88 "Show equivalent ISO date for the date under the cursor."
89 (interactive)
90 (message "ISO date: %s"
91 (calendar-iso-date-string (calendar-cursor-to-date t))))
93 (define-obsolete-function-alias 'calendar-print-iso-date
94 'calendar-iso-print-date "23.1")
96 (defun calendar-iso-read-date (&optional dayflag)
97 "Interactively read the arguments for an ISO date command.
98 Reads a year and week, and if DAYFLAG is non-nil a day (otherwise
99 taken to be 1)."
100 (let* ((year (calendar-read
101 "ISO calendar year (>0): "
102 (lambda (x) (> x 0))
103 (number-to-string (calendar-extract-year
104 (calendar-current-date)))))
105 (no-weeks (calendar-extract-month
106 (calendar-iso-from-absolute
108 (calendar-dayname-on-or-before
109 1 (calendar-absolute-from-gregorian
110 (list 1 4 (1+ year))))))))
111 (week (calendar-read
112 (format "ISO calendar week (1-%d): " no-weeks)
113 (lambda (x) (and (> x 0) (<= x no-weeks)))))
114 (day (if dayflag (calendar-read
115 "ISO day (1-7): "
116 (lambda (x) (and (<= 1 x) (<= x 7))))
117 1)))
118 (list (list week day year))))
120 (define-obsolete-function-alias 'calendar-iso-read-args
121 'calendar-iso-read-date "23.1")
123 ;;;###cal-autoload
124 (defun calendar-iso-goto-date (date &optional noecho)
125 "Move cursor to ISO DATE; echo ISO date unless NOECHO is non-nil."
126 (interactive (calendar-iso-read-date t))
127 (calendar-goto-date (calendar-gregorian-from-absolute
128 (calendar-iso-to-absolute date)))
129 (or noecho (calendar-iso-print-date)))
131 (define-obsolete-function-alias 'calendar-goto-iso-date
132 'calendar-iso-goto-date "23.1")
134 ;;;###cal-autoload
135 (defun calendar-iso-goto-week (date &optional noecho)
136 "Move cursor to ISO DATE; echo ISO date unless NOECHO is non-nil.
137 Interactively, goes to the first day of the specified week."
138 (interactive (calendar-iso-read-date))
139 (calendar-goto-date (calendar-gregorian-from-absolute
140 (calendar-iso-to-absolute date)))
141 (or noecho (calendar-iso-print-date)))
143 (define-obsolete-function-alias 'calendar-goto-iso-week
144 'calendar-iso-goto-week "23.1")
146 (defvar date)
148 ;; To be called from diary-list-sexp-entries, where DATE is bound.
149 ;;;###diary-autoload
150 (defun diary-iso-date ()
151 "ISO calendar equivalent of date diary entry."
152 (format "ISO date: %s" (calendar-iso-date-string date)))
154 (provide 'cal-iso)
156 ;; arch-tag: 3c0154cc-d30f-4981-9f60-42bdf7a468f6
157 ;;; cal-iso.el ends here