Update copyright year to 2015
[emacs.git] / lisp / calendar / cal-iso.el
blobf3cc430590f8cf753691ee89f5fd1538a7b3fe77
1 ;;; cal-iso.el --- calendar functions for the ISO calendar
3 ;; Copyright (C) 1995, 1997, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
6 ;; Maintainer: Glenn Morris <rgm@gnu.org>
7 ;; Keywords: calendar
8 ;; Human-Keywords: ISO calendar, calendar, diary
9 ;; Package: calendar
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 ;;;###cal-autoload
51 (defun calendar-iso-from-absolute (date)
52 "Compute the `ISO commercial date' corresponding to the absolute DATE.
53 The ISO year corresponds approximately to the Gregorian year, but weeks
54 start on Monday and end on Sunday. The first week of the ISO year is the
55 first such week in which at least 4 days are in a year. The ISO commercial
56 date has the form (week day year) in which week is in the range 1..52 and
57 day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = Sunday). The
58 absolute date is the number of days elapsed since the (imaginary) Gregorian
59 date Sunday, December 31, 1 BC."
60 (let* ((approx (calendar-extract-year
61 (calendar-gregorian-from-absolute (- date 3))))
62 (year (+ approx
63 (calendar-sum y approx
64 (>= date (calendar-iso-to-absolute
65 (list 1 1 (1+ y))))
66 1))))
67 (list
68 (1+ (/ (- date (calendar-iso-to-absolute (list 1 1 year))) 7))
69 (% date 7)
70 year)))
72 ;;;###cal-autoload
73 (defun calendar-iso-date-string (&optional date)
74 "String of ISO date of Gregorian DATE, default today."
75 (let* ((d (calendar-absolute-from-gregorian
76 (or date (calendar-current-date))))
77 (day (% d 7))
78 (iso-date (calendar-iso-from-absolute d)))
79 (format "Day %s of week %d of %d"
80 (if (zerop day) 7 day)
81 (calendar-extract-month iso-date)
82 (calendar-extract-year iso-date))))
84 ;;;###cal-autoload
85 (defun calendar-iso-print-date ()
86 "Show equivalent ISO date for the date under the cursor."
87 (interactive)
88 (message "ISO date: %s"
89 (calendar-iso-date-string (calendar-cursor-to-date t))))
91 (defun calendar-iso-read-date (&optional dayflag)
92 "Interactively read the arguments for an ISO date command.
93 Reads a year and week, and if DAYFLAG is non-nil a day (otherwise
94 taken to be 1)."
95 (let* ((year (calendar-read
96 "ISO calendar year (>0): "
97 (lambda (x) (> x 0))
98 (number-to-string (calendar-extract-year
99 (calendar-current-date)))))
100 (no-weeks (calendar-extract-month
101 (calendar-iso-from-absolute
103 (calendar-dayname-on-or-before
104 1 (calendar-absolute-from-gregorian
105 (list 1 4 (1+ year))))))))
106 (week (calendar-read
107 (format "ISO calendar week (1-%d): " no-weeks)
108 (lambda (x) (and (> x 0) (<= x no-weeks)))))
109 (day (if dayflag (calendar-read
110 "ISO day (1-7): "
111 (lambda (x) (and (<= 1 x) (<= x 7))))
112 1)))
113 (list (list week day year))))
115 ;;;###cal-autoload
116 (defun calendar-iso-goto-date (date &optional noecho)
117 "Move cursor to ISO DATE; echo ISO date unless NOECHO is non-nil."
118 (interactive (calendar-iso-read-date t))
119 (calendar-goto-date (calendar-gregorian-from-absolute
120 (calendar-iso-to-absolute date)))
121 (or noecho (calendar-iso-print-date)))
123 ;;;###cal-autoload
124 (defun calendar-iso-goto-week (date &optional noecho)
125 "Move cursor to ISO DATE; echo ISO date unless NOECHO is non-nil.
126 Interactively, goes to the first day of the specified week."
127 (interactive (calendar-iso-read-date))
128 (calendar-goto-date (calendar-gregorian-from-absolute
129 (calendar-iso-to-absolute date)))
130 (or noecho (calendar-iso-print-date)))
132 (defvar date)
134 ;; To be called from diary-list-sexp-entries, where DATE is bound.
135 ;;;###diary-autoload
136 (defun diary-iso-date ()
137 "ISO calendar equivalent of date diary entry."
138 (format "ISO date: %s" (calendar-iso-date-string date)))
140 (provide 'cal-iso)
142 ;;; cal-iso.el ends here