Commit | Line | Data |
---|---|---|
a47a5be5 RS |
1 | ;;; Timezone package for GNU Emacs |
2 | ||
aa56d805 RS |
3 | ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc. |
4 | ||
5 | ;;; Author: Masanobu Umeda | |
6 | ;;; Maintainer: umerin@mse.kyutech.ac.jp | |
a47a5be5 RS |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation; either version 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
22 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | ||
24 | ;;; Code: | |
25 | ||
26 | (provide 'timezone) | |
27 | ||
28 | (defvar timezone-world-timezones | |
29 | '(("PST" . -800) | |
30 | ("PDT" . -700) | |
31 | ("MST" . -700) | |
32 | ("MDT" . -600) | |
33 | ("CST" . -600) | |
34 | ("CDT" . -500) | |
35 | ("EST" . -500) | |
36 | ("EDT" . -400) | |
37 | ("AST" . -400) ;by <clamen@CS.CMU.EDU> | |
38 | ("NST" . -330) ;by <clamen@CS.CMU.EDU> | |
39 | ("GMT" . +000) | |
40 | ("BST" . +100) | |
41 | ("MET" . +100) | |
42 | ("EET" . +200) | |
43 | ("JST" . +900) | |
44 | ("GMT+1" . +100) ("GMT+2" . +200) ("GMT+3" . +300) | |
45 | ("GMT+4" . +400) ("GMT+5" . +500) ("GMT+6" . +600) | |
46 | ("GMT+7" . +700) ("GMT+8" . +800) ("GMT+9" . +900) | |
47 | ("GMT+10" . +1000) ("GMT+11" . +1100) ("GMT+12" . +1200) ("GMT+13" . +1300) | |
48 | ("GMT-1" . -100) ("GMT-2" . -200) ("GMT-3" . -300) | |
49 | ("GMT-4" . -400) ("GMT-5" . -500) ("GMT-6" . -600) | |
50 | ("GMT-7" . -700) ("GMT-8" . -800) ("GMT-9" . -900) | |
51 | ("GMT-10" . -1000) ("GMT-11" . -1100) ("GMT-12" . -1200)) | |
52 | "*Time differentials of timezone from GMT in hour.") | |
53 | ||
54 | (defvar timezone-months-assoc | |
55 | '(("JAN" . 1)("FEB" . 2)("MAR" . 3) | |
56 | ("APR" . 4)("MAY" . 5)("JUN" . 6) | |
57 | ("JUL" . 7)("AUG" . 8)("SEP" . 9) | |
58 | ("OCT" . 10)("NOV" . 11)("DEC" . 12)) | |
59 | "Alist of first three letters of a month and its numerical representation.") | |
60 | ||
61 | (defun timezone-make-date-arpa-standard (date &optional local timezone) | |
62 | "Convert DATE to an arpanet standard date. | |
63 | Optional 1st argumetn LOCAL specifies the default local timezone of the DATE. | |
64 | Optional 2nd argument TIMEZONE specifies a timezone to be represented in." | |
65 | (let* ((date (timezone-parse-date date)) | |
66 | (year (string-to-int (aref date 0))) | |
67 | (month (string-to-int (aref date 1))) | |
68 | (day (string-to-int (aref date 2))) | |
69 | (time (timezone-parse-time (aref date 3))) | |
70 | (hour (string-to-int (aref time 0))) | |
71 | (minute (string-to-int (aref time 1))) | |
72 | (second (string-to-int (aref time 2))) | |
73 | (local (or (aref date 4) local)) ;Use original if defined | |
74 | (timezone (or timezone local)) | |
75 | (diff (- (timezone-zone-to-minute timezone) | |
76 | (timezone-zone-to-minute local))) | |
77 | (new (timezone-fix-time year month day | |
78 | hour (+ minute diff) second))) | |
79 | (timezone-make-arpa-date (aref new 0) (aref new 1) (aref new 2) | |
80 | (timezone-make-time-string | |
81 | (aref new 3) (aref new 4) (aref new 5)) | |
82 | timezone) | |
83 | )) | |
84 | ||
85 | (defun timezone-make-date-sortable (date &optional local timezone) | |
86 | "Convert DATE to a sortable date string. | |
87 | Optional 1st argumetn LOCAL specifies the default local timezone of the DATE. | |
88 | Optional 2nd argument TIMEZONE specifies a timezone to be represented in." | |
89 | (let* ((date (timezone-parse-date date)) | |
90 | (year (string-to-int (aref date 0))) | |
91 | (month (string-to-int (aref date 1))) | |
92 | (day (string-to-int (aref date 2))) | |
93 | (time (timezone-parse-time (aref date 3))) | |
94 | (hour (string-to-int (aref time 0))) | |
95 | (minute (string-to-int (aref time 1))) | |
96 | (second (string-to-int (aref time 2))) | |
97 | (local (or (aref date 4) local)) ;Use original if defined | |
98 | (timezone (or timezone local)) | |
99 | (diff (- (timezone-zone-to-minute timezone) | |
100 | (timezone-zone-to-minute local))) | |
101 | (new (timezone-fix-time year month day | |
102 | hour (+ minute diff) second))) | |
103 | (timezone-make-sortable-date (aref new 0) (aref new 1) (aref new 2) | |
104 | (timezone-make-time-string | |
105 | (aref new 3) (aref new 4) (aref new 5))) | |
106 | )) | |
107 | ||
108 | \f | |
109 | ;; | |
110 | ;; Parsers and Constructors of Date and Time | |
111 | ;; | |
112 | ||
113 | (defun timezone-make-arpa-date (year month day time &optional timezone) | |
114 | "Make arpanet standard date string from YEAR, MONTH, DAY, and TIME. | |
115 | Optional argument TIMEZONE specifies a time zone." | |
116 | (format "%02d %s %4d %s%s" | |
117 | day | |
118 | (capitalize (car (rassq month timezone-months-assoc))) | |
119 | ;;(- year (* (/ year 100) 100)) ;1990 -> 90 | |
120 | (if (< year 100) (+ year 1900) year) ;90->1990 | |
121 | time | |
122 | (if timezone (concat " " timezone) "") | |
123 | )) | |
124 | ||
125 | (defun timezone-make-sortable-date (year month day time) | |
126 | "Make sortable date string from YEAR, MONTH, DAY, and TIME." | |
127 | (format "%4d%02d%02d%s" | |
128 | ;;(- year (* (/ year 100) 100)) ;1990 -> 90 | |
129 | (if (< year 100) (+ year 1900) year) ;90->1990 | |
130 | month day time)) | |
131 | ||
132 | (defun timezone-make-time-string (hour minute second) | |
133 | "Make time string from HOUR, MINUTE, and SECOND." | |
134 | (format "%02d:%02d:%02d" hour minute second)) | |
135 | ||
136 | (defun timezone-parse-date (date) | |
137 | "Parse DATE and return a vector [year month day time timezone]. | |
138 | 19 is prepended to year if necessary. Timezone may be NIL if nothing. | |
139 | Understand the following styles: | |
140 | (1) 14 Apr 89 03:20[:12] [GMT] | |
141 | (2) Fri, 17 Mar 89 4:01[:33] [GMT] | |
142 | (3) Mon Jan 16 16:12[:37] [GMT] 1989 | |
143 | (4) 6 May 1992 1641-JST (Wednesday)" | |
144 | (let ((date (or date "")) | |
145 | (year nil) | |
146 | (month nil) | |
147 | (day nil) | |
148 | (time nil) | |
149 | (zone nil)) ;This may be nil. | |
150 | (cond ((string-match | |
151 | "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]*\\'" date) | |
152 | ;; Styles: (1) and (2) without timezone | |
153 | (setq year 3 month 2 day 1 time 4 zone nil)) | |
154 | ((string-match | |
155 | "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date) | |
156 | ;; Styles: (1) and (2) with timezone and buggy timezone | |
157 | (setq year 3 month 2 day 1 time 4 zone 5)) | |
158 | ((string-match | |
159 | "\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([0-9]+\\)" date) | |
160 | ;; Styles: (3) without timezone | |
161 | (setq year 4 month 1 day 2 time 3 zone nil)) | |
162 | ((string-match | |
163 | "\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([-+a-zA-Z0-9]+\\)[ \t]+\\([0-9]+\\)" date) | |
164 | ;; Styles: (3) with timezoen | |
165 | (setq year 5 month 1 day 2 time 3 zone 4)) | |
166 | ((string-match | |
167 | "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date) | |
168 | ;; Styles: (4) with timezone | |
169 | (setq year 3 month 2 day 1 time 4 zone 5)) | |
170 | ) | |
171 | (if year | |
172 | (progn | |
173 | (setq year | |
174 | (substring date (match-beginning year) (match-end year))) | |
175 | ;; It is now Dec 1992. 8 years before the end of the World. | |
176 | (if (< (length year) 4) | |
177 | (setq year (concat "19" (substring year -2 nil)))) | |
178 | (setq month | |
179 | (int-to-string | |
180 | (cdr | |
181 | (assoc | |
182 | (upcase | |
183 | ;; Don't use `match-end' in order to take 3 | |
184 | ;; letters from the beginning. | |
185 | (substring date | |
186 | (match-beginning month) | |
187 | (+ (match-beginning month) 3))) | |
188 | timezone-months-assoc)))) | |
189 | (setq day | |
190 | (substring date (match-beginning day) (match-end day))) | |
191 | (setq time | |
192 | (substring date (match-beginning time) (match-end time))))) | |
193 | (if zone | |
194 | (setq zone | |
195 | (substring date (match-beginning zone) (match-end zone)))) | |
196 | ;; Return a vector. | |
197 | (if year | |
198 | (vector year month day time zone) | |
199 | (vector "0" "0" "0" "0" nil)) | |
200 | )) | |
201 | ||
202 | (defun timezone-parse-time (time) | |
203 | "Parse TIME (HH:MM:SS) and return a vector [hour minute second]. | |
204 | Recognize HH:MM:SS, HH:MM, HHMMSS, HHMM." | |
205 | (let ((time (or time "")) | |
206 | (hour nil) | |
207 | (minute nil) | |
208 | (second nil)) | |
209 | (cond ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)\\'" time) | |
210 | ;; HH:MM:SS | |
211 | (setq hour 1 minute 2 second 3)) | |
212 | ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\'" time) | |
213 | ;; HH:MM | |
214 | (setq hour 1 minute 2 second nil)) | |
215 | ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time) | |
216 | ;; HHMMSS | |
217 | (setq hour 1 minute 2 second 3)) | |
218 | ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time) | |
219 | ;; HHMM | |
220 | (setq hour 1 minute 2 second nil)) | |
221 | ) | |
222 | ;; Return [hour minute second] | |
223 | (vector | |
224 | (if hour | |
225 | (substring time (match-beginning hour) (match-end hour)) "0") | |
226 | (if minute | |
227 | (substring time (match-beginning minute) (match-end minute)) "0") | |
228 | (if second | |
229 | (substring time (match-beginning second) (match-end second)) "0")) | |
230 | )) | |
231 | ||
232 | \f | |
233 | ;; Miscellaneous | |
234 | ||
235 | (defun timezone-zone-to-minute (timezone) | |
236 | "Translate TIMEZONE (in zone name or integer) to integer minute." | |
237 | (if timezone | |
238 | (progn | |
239 | (setq timezone | |
240 | (or (cdr (assoc (upcase timezone) timezone-world-timezones)) | |
241 | ;; +900 | |
242 | timezone)) | |
243 | (if (stringp timezone) | |
244 | (setq timezone (string-to-int timezone))) | |
245 | ;; Taking account of minute in timezone. | |
246 | ;; HHMM -> MM | |
247 | ;;(+ (* 60 (/ timezone 100)) (% timezone 100)) | |
248 | ;; ANSI C compliance about truncation of integer division | |
249 | ;; by eggert@twinsun.com (Paul Eggert) | |
250 | (let* ((abszone (max timezone (- timezone))) | |
251 | (minutes (+ (* 60 (/ abszone 100)) (% abszone 100)))) | |
252 | (if (< timezone 0) (- minutes) minutes))) | |
253 | 0)) | |
254 | ||
255 | (defun timezone-fix-time (year month day hour minute second) | |
256 | "Fix date and time." | |
257 | ;; MINUTE may be larger than 60 or smaller than -60. | |
258 | (let ((hour-fix | |
259 | (if (< minute 0) | |
260 | ;;(/ (- minute 59) 60) (/ minute 60) | |
261 | ;; ANSI C compliance about truncation of integer division | |
262 | ;; by eggert@twinsun.com (Paul Eggert) | |
263 | (- (/ (- 59 minute) 60)) (/ minute 60)))) | |
264 | (setq hour (+ hour hour-fix)) | |
265 | (setq minute (- minute (* 60 hour-fix)))) | |
266 | ;; HOUR may be larger than 24 or smaller than 0. | |
267 | (cond ((<= 24 hour) ;24 -> 00 | |
268 | (setq hour (- hour 24)) | |
269 | (setq day (1+ day)) | |
270 | (if (< (timezone-last-day-of-month month year) day) | |
271 | (progn | |
272 | (setq month (1+ month)) | |
273 | (setq day 1) | |
274 | (if (< 12 month) | |
275 | (progn | |
276 | (setq month 1) | |
277 | (setq year (1+ year)) | |
278 | )) | |
279 | ))) | |
280 | ((> 0 hour) | |
281 | (setq hour (+ hour 24)) | |
282 | (setq day (1- day)) | |
283 | (if (> 1 day) | |
284 | (progn | |
285 | (setq month (1- month)) | |
286 | (if (> 1 month) | |
287 | (progn | |
288 | (setq month 12) | |
289 | (setq year (1- year)) | |
290 | )) | |
291 | (setq day (timezone-last-day-of-month month year)) | |
292 | ))) | |
293 | ) | |
294 | (vector year month day hour minute second)) | |
295 | ||
296 | ;; Partly copied from Calendar program by Edward M. Reingold. | |
297 | ;; Thanks a lot. | |
298 | ||
299 | (defun timezone-last-day-of-month (month year) | |
300 | "The last day in MONTH during YEAR." | |
301 | (if (and (= month 2) (timezone-leap-year-p year)) | |
302 | 29 | |
303 | (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month)))) | |
304 | ||
305 | (defun timezone-leap-year-p (year) | |
306 | "Returns t if YEAR is a Gregorian leap year." | |
307 | (or (and (zerop (% year 4)) | |
308 | (not (zerop (% year 100)))) | |
309 | (zerop (% year 400)))) | |
aa56d805 RS |
310 | |
311 | ;;; timezone.el ends here |