Mercurial > emacs
comparison lisp/calendar/cal-iso.el @ 13053:621d48117fde
Initial revision
author | Edward M. Reingold <reingold@emr.cs.iit.edu> |
---|---|
date | Thu, 21 Sep 1995 03:11:06 +0000 |
parents | |
children | 83f275dcd93a |
comparison
equal
deleted
inserted
replaced
13052:71be832cf34a | 13053:621d48117fde |
---|---|
1 ;;; cal-iso.el --- calendar functions for the ISO calendar. | |
2 | |
3 ;; Copyright (C) 1995 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu> | |
6 ;; Keywords: calendar | |
7 ;; Human-Keywords: ISO calendar, calendar, diary | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;; This collection of functions implements the features of calendar.el and | |
28 ;; diary.el that deal with the ISO calendar. | |
29 | |
30 ;; Comments, corrections, and improvements should be sent to | |
31 ;; Edward M. Reingold Department of Computer Science | |
32 ;; (217) 333-6733 University of Illinois at Urbana-Champaign | |
33 ;; reingold@cs.uiuc.edu 1304 West Springfield Avenue | |
34 ;; Urbana, Illinois 61801 | |
35 | |
36 ;;; Code: | |
37 | |
38 (require 'calendar) | |
39 | |
40 (defun calendar-absolute-from-iso (date) | |
41 "The number of days elapsed between the Gregorian date 12/31/1 BC and DATE. | |
42 The `ISO year' corresponds approximately to the Gregorian year, but | |
43 weeks start on Monday and end on Sunday. The first week of the ISO year is | |
44 the first such week in which at least 4 days are in a year. The ISO | |
45 commercial DATE has the form (week day year) in which week is in the range | |
46 1..52 and day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = | |
47 Sunday). The Gregorian date Sunday, December 31, 1 BC is imaginary." | |
48 (let* ((week (extract-calendar-month date)) | |
49 (day (extract-calendar-day date)) | |
50 (year (extract-calendar-year date))) | |
51 (+ (calendar-dayname-on-or-before | |
52 1 (+ 3 (calendar-absolute-from-gregorian (list 1 1 year)))) | |
53 (* 7 (1- week)) | |
54 (if (= day 0) 6 (1- day))))) | |
55 | |
56 (defun calendar-iso-from-absolute (date) | |
57 "Compute the `ISO commercial date' corresponding to the absolute DATE. | |
58 The ISO year corresponds approximately to the Gregorian year, but weeks | |
59 start on Monday and end on Sunday. The first week of the ISO year is the | |
60 first such week in which at least 4 days are in a year. The ISO commercial | |
61 date has the form (week day year) in which week is in the range 1..52 and | |
62 day is in the range 0..6 (1 = Monday, 2 = Tuesday, ..., 0 = Sunday). The | |
63 absolute date is the number of days elapsed since the (imaginary) Gregorian | |
64 date Sunday, December 31, 1 BC." | |
65 (let* ((approx (extract-calendar-year | |
66 (calendar-gregorian-from-absolute (- date 3)))) | |
67 (year (+ approx | |
68 (calendar-sum y approx | |
69 (>= date (calendar-absolute-from-iso (list 1 1 (1+ y)))) | |
70 1)))) | |
71 (list | |
72 (1+ (/ (- date (calendar-absolute-from-iso (list 1 1 year))) 7)) | |
73 (% date 7) | |
74 year))) | |
75 | |
76 (defun calendar-iso-date-string (&optional date) | |
77 "String of ISO date of Gregorian DATE. | |
78 Defaults to today's date if DATE is not given." | |
79 (let* ((d (calendar-absolute-from-gregorian | |
80 (or date (calendar-current-date)))) | |
81 (day (% d 7)) | |
82 (iso-date (calendar-iso-from-absolute d))) | |
83 (format "Day %s of week %d of %d" | |
84 (if (zerop day) 7 day) | |
85 (extract-calendar-month iso-date) | |
86 (extract-calendar-year iso-date)))) | |
87 | |
88 (defun calendar-print-iso-date () | |
89 "Show equivalent ISO date for the date under the cursor." | |
90 (interactive) | |
91 (message "ISO date: %s" | |
92 (calendar-iso-date-string (calendar-cursor-to-date t)))) | |
93 | |
94 (defun calendar-goto-iso-date (date &optional noecho) | |
95 "Move cursor to ISO DATE; echo ISO date unless NOECHO is t." | |
96 (interactive | |
97 (let* ((today (calendar-current-date)) | |
98 (year (calendar-read | |
99 "ISO calendar year (>0): " | |
100 '(lambda (x) (> x 0)) | |
101 (int-to-string (extract-calendar-year today)))) | |
102 (no-weeks (extract-calendar-month | |
103 (calendar-iso-from-absolute | |
104 (1- | |
105 (calendar-dayname-on-or-before | |
106 1 (calendar-absolute-from-gregorian | |
107 (list 1 4 (1+ year)))))))) | |
108 (week (calendar-read | |
109 (format "ISO calendar week (1-%d): " no-weeks) | |
110 '(lambda (x) (and (> x 0) (<= x no-weeks))))) | |
111 (day (calendar-read | |
112 "ISO day (1-7): " | |
113 '(lambda (x) (and (<= 1 x) (<= x 7)))))) | |
114 (list (list week day year)))) | |
115 (calendar-goto-date (calendar-gregorian-from-absolute | |
116 (calendar-absolute-from-iso date))) | |
117 (or noecho (calendar-print-iso-date))) | |
118 | |
119 (defun diary-iso-date () | |
120 "ISO calendar equivalent of date diary entry." | |
121 (format "ISO date: %s" (calendar-iso-date-string date))) | |
122 | |
123 (provide 'cal-iso) | |
124 | |
125 ;;; cal-iso.el ends here |