*** empty log message ***
[emacs.git] / lisp / timezone.el
blob66e62b1af0f69c7fdc3c80c0fb35b873f0df1c75
1 ;;; timezone.el --- time zone package for GNU Emacs
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 1996, 1999 Free Software Foundation, Inc.
5 ;; Author: Masanobu Umeda
6 ;; Maintainer: umerin@mse.kyutech.ac.jp
7 ;; Keywords: news
9 ;; This file is part of GNU Emacs.
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.
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.
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 the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Code:
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 ("UT" . +000)
40 ("GMT" . +000)
41 ("BST" . +100)
42 ("MET" . +100)
43 ("EET" . +200)
44 ("JST" . +900)
45 ("GMT+1" . +100) ("GMT+2" . +200) ("GMT+3" . +300)
46 ("GMT+4" . +400) ("GMT+5" . +500) ("GMT+6" . +600)
47 ("GMT+7" . +700) ("GMT+8" . +800) ("GMT+9" . +900)
48 ("GMT+10" . +1000) ("GMT+11" . +1100) ("GMT+12" . +1200) ("GMT+13" . +1300)
49 ("GMT-1" . -100) ("GMT-2" . -200) ("GMT-3" . -300)
50 ("GMT-4" . -400) ("GMT-5" . -500) ("GMT-6" . -600)
51 ("GMT-7" . -700) ("GMT-8" . -800) ("GMT-9" . -900)
52 ("GMT-10" . -1000) ("GMT-11" . -1100) ("GMT-12" . -1200))
53 "*Time differentials of timezone from GMT in +-HHMM form.
54 This list is obsolescent, and is present only for backwards compatibility,
55 because time zone names are ambiguous in practice.
56 Use `current-time-zone' instead.")
58 (defvar timezone-months-assoc
59 '(("JAN" . 1)("FEB" . 2)("MAR" . 3)
60 ("APR" . 4)("MAY" . 5)("JUN" . 6)
61 ("JUL" . 7)("AUG" . 8)("SEP" . 9)
62 ("OCT" . 10)("NOV" . 11)("DEC" . 12))
63 "Alist of first three letters of a month and its numerical representation.")
65 (defun timezone-make-date-arpa-standard (date &optional local timezone)
66 "Convert DATE to an arpanet standard date.
67 Optional 2nd argument LOCAL specifies the default local timezone of the DATE;
68 if nil, GMT is assumed.
69 Optional 3rd argument TIMEZONE specifies a time zone to be represented in;
70 if nil, the local time zone is assumed."
71 (let ((new (timezone-fix-time date local timezone)))
72 (timezone-make-arpa-date (aref new 0) (aref new 1) (aref new 2)
73 (timezone-make-time-string
74 (aref new 3) (aref new 4) (aref new 5))
75 (aref new 6))
78 (defun timezone-make-date-sortable (date &optional local timezone)
79 "Convert DATE to a sortable date string.
80 Optional 2nd argument LOCAL specifies the default local timezone of the DATE;
81 if nil, GMT is assumed.
82 Optional 3rd argument TIMEZONE specifies a timezone to be represented in;
83 if nil, the local time zone is assumed."
84 (let ((new (timezone-fix-time date local timezone)))
85 (timezone-make-sortable-date (aref new 0) (aref new 1) (aref new 2)
86 (timezone-make-time-string
87 (aref new 3) (aref new 4) (aref new 5)))
92 ;; Parsers and Constructors of Date and Time
95 (defun timezone-make-arpa-date (year month day time &optional timezone)
96 "Make arpanet standard date string from YEAR, MONTH, DAY, and TIME.
97 Optional argument TIMEZONE specifies a time zone."
98 (let ((zone
99 (if (listp timezone)
100 (let* ((m (timezone-zone-to-minute timezone))
101 (absm (if (< m 0) (- m) m)))
102 (format "%c%02d%02d"
103 (if (< m 0) ?- ?+) (/ absm 60) (% absm 60)))
104 timezone)))
105 (format "%02d %s %04d %s %s"
107 (capitalize (car (rassq month timezone-months-assoc)))
108 year
109 time
110 zone)))
112 (defun timezone-make-sortable-date (year month day time)
113 "Make sortable date string from YEAR, MONTH, DAY, and TIME."
114 (format "%4d%02d%02d%s"
115 year month day time))
117 (defun timezone-make-time-string (hour minute second)
118 "Make time string from HOUR, MINUTE, and SECOND."
119 (format "%02d:%02d:%02d" hour minute second))
121 (defun timezone-parse-date (date)
122 "Parse DATE and return a vector [YEAR MONTH DAY TIME TIMEZONE].
123 Two-digit dates are `windowed'. Those <69 have 2000 added; otherwise 1900
124 is added. Three-digit dates have 1900 added.
125 TIMEZONE is nil for DATEs without a zone field.
127 Understands the following styles:
128 (1) 14 Apr 89 03:20[:12] [GMT]
129 (2) Fri, 17 Mar 89 4:01[:33] [GMT]
130 (3) Mon Jan 16 16:12[:37] [GMT] 1989
131 (4) 6 May 1992 1641-JST (Wednesday)
132 (5) 22-AUG-1993 10:59:12.82
133 (6) Thu, 11 Apr 16:17:12 91 [MET]
134 (7) Mon, 6 Jul 16:47:20 T 1992 [MET]
135 (8) 1996-06-24 21:13:12 [GMT]
136 (9) 1996-06-24 21:13-ZONE"
137 ;; Get rid of any text properties.
138 (and (stringp date)
139 (or (text-properties-at 0 date)
140 (next-property-change 0 date))
141 (setq date (copy-sequence date))
142 (set-text-properties 0 (length date) nil date))
143 (let ((date (or date ""))
144 (year nil)
145 (month nil)
146 (day nil)
147 (time nil)
148 (zone nil)) ;This may be nil.
149 (cond ((string-match
150 "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
151 ;; Styles: (1) and (2) with timezone and buggy timezone
152 ;; This is most common in mail and news,
153 ;; so it is worth trying first.
154 (setq year 3 month 2 day 1 time 4 zone 5))
155 ((string-match
156 "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]*\\'" date)
157 ;; Styles: (1) and (2) without timezone
158 (setq year 3 month 2 day 1 time 4 zone nil))
159 ((string-match
160 "\\([^ \t,]+\\),[ \t]+\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\(T[ \t]+\\|\\)\\([0-9]+\\)[ \t]*\\'" date)
161 ;; Styles: (6) and (7) without timezone
162 (setq year 6 month 3 day 2 time 4 zone nil))
163 ((string-match
164 "\\([^ \t,]+\\),[ \t]+\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\(T[ \t]+\\|\\)\\([0-9]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
165 ;; Styles: (6) and (7) with timezone and buggy timezone
166 (setq year 6 month 3 day 2 time 4 zone 7))
167 ((string-match
168 "\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([0-9]+\\)" date)
169 ;; Styles: (3) without timezone
170 (setq year 4 month 1 day 2 time 3 zone nil))
171 ((string-match
172 "\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([-+a-zA-Z0-9]+\\)[ \t]+\\([0-9]+\\)" date)
173 ;; Styles: (3) with timezone
174 (setq year 5 month 1 day 2 time 3 zone 4))
175 ((string-match
176 "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
177 ;; Styles: (4) with timezone
178 (setq year 3 month 2 day 1 time 4 zone 5))
179 ((string-match
180 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)?[ \t]+\\([-+a-zA-Z0-9]+\\)" date)
181 ;; Styles: (5) with timezone.
182 (setq year 3 month 2 day 1 time 4 zone 6))
183 ((string-match
184 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)?" date)
185 ;; Styles: (5) without timezone.
186 (setq year 3 month 2 day 1 time 4 zone nil))
187 ((string-match
188 "\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+\\([-+a-zA-Z0-9]+\\)" date)
189 ;; Styles: (8) with timezone.
190 (setq year 1 month 2 day 3 time 4 zone 5))
191 ((string-match
192 "\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+\\)[ \t]+\\([-+a-zA-Z0-9:]+\\)" date)
193 ;; Styles: (8) with timezone with a colon in it.
194 (setq year 1 month 2 day 3 time 4 zone 5))
195 ((string-match
196 "\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)" date)
197 ;; Styles: (8) without timezone.
198 (setq year 1 month 2 day 3 time 4 zone nil))
200 (when year
201 (setq year (match-string year date))
202 ;; Guess ambiguous years. Assume years < 69 don't predate the
203 ;; Unix Epoch, so are 2000+. Three-digit years are assumed to
204 ;; be relative to 1900.
205 (if (< (length year) 4)
206 (let ((y (string-to-int year)))
207 (if (< y 69)
208 (setq y (+ y 100)))
209 (setq year (int-to-string (+ 1900 y)))))
210 (setq month
211 (if (= (aref date (+ (match-beginning month) 2)) ?-)
212 ;; Handle numeric months, spanning exactly two digits.
213 (substring date
214 (match-beginning month)
215 (+ (match-beginning month) 2))
216 (let* ((string (substring date
217 (match-beginning month)
218 (+ (match-beginning month) 3)))
219 (monthnum
220 (cdr (assoc (upcase string) timezone-months-assoc))))
221 (if monthnum
222 (int-to-string monthnum)))))
223 (setq day (match-string day date))
224 (setq time (match-string time date)))
225 (if zone (setq zone (match-string zone date)))
226 ;; Return a vector.
227 (if (and year month)
228 (vector year month day time zone)
229 (vector "0" "0" "0" "0" nil))))
231 (defun timezone-parse-time (time)
232 "Parse TIME (HH:MM:SS) and return a vector [hour minute second].
233 Recognize HH:MM:SS, HH:MM, HHMMSS, HHMM."
234 (let ((time (or time ""))
235 hour minute second)
236 (cond ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)\\'" time)
237 ;; HH:MM:SS
238 (setq hour 1 minute 2 second 3))
239 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\'" time)
240 ;; HH:MM
241 (setq hour 1 minute 2 second nil))
242 ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time)
243 ;; HHMMSS
244 (setq hour 1 minute 2 second 3))
245 ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time)
246 ;; HHMM
247 (setq hour 1 minute 2 second nil))
249 ;; Return [hour minute second]
250 (vector
251 (if hour (match-string hour time) "0")
252 (if minute (match-string minute time) "0")
253 (if second (match-string second time) "0"))))
256 ;; Miscellaneous
258 (defun timezone-zone-to-minute (timezone)
259 "Translate TIMEZONE to an integer minute offset from GMT.
260 TIMEZONE can be a cons cell containing the output of current-time-zone,
261 or an integer of the form +-HHMM, or a time zone name."
262 (cond
263 ((consp timezone)
264 (/ (car timezone) 60))
265 (timezone
266 (progn
267 (setq timezone
268 (or (cdr (assoc (upcase timezone) timezone-world-timezones))
269 ;; +900
270 timezone))
271 (if (stringp timezone)
272 (setq timezone (string-to-int timezone)))
273 ;; Taking account of minute in timezone.
274 ;; HHMM -> MM
275 (let* ((abszone (abs timezone))
276 (minutes (+ (* 60 (/ abszone 100)) (% abszone 100))))
277 (if (< timezone 0) (- minutes) minutes))))
278 (t 0)))
280 (defun timezone-time-from-absolute (date seconds)
281 "Compute the UTC time equivalent to DATE at time SECONDS after midnight.
282 Return a list suitable as an argument to current-time-zone,
283 or nil if the date cannot be thus represented.
284 DATE is the number of days elapsed since the (imaginary)
285 Gregorian date Sunday, December 31, 1 BC."
286 (let* ((current-time-origin 719163)
287 ;; (timezone-absolute-from-gregorian 1 1 1970)
288 (days (- date current-time-origin))
289 (seconds-per-day (float 86400))
290 (seconds (+ seconds (* days seconds-per-day)))
291 (current-time-arithmetic-base (float 65536))
292 (hi (floor (/ seconds current-time-arithmetic-base)))
293 (hibase (* hi current-time-arithmetic-base))
294 (lo (floor (- seconds hibase))))
295 (and (< (abs (- seconds (+ hibase lo))) 2) ;; Check for integer overflow.
296 (cons hi lo))))
298 (defun timezone-time-zone-from-absolute (date seconds)
299 "Compute the local time zone for DATE at time SECONDS after midnight.
300 Return a list in the same format as current-time-zone's result,
301 or nil if the local time zone could not be computed.
302 DATE is the number of days elapsed since the (imaginary)
303 Gregorian date Sunday, December 31, 1 BC."
304 (and (fboundp 'current-time-zone)
305 (let ((utc-time (timezone-time-from-absolute date seconds)))
306 (and utc-time
307 (let ((zone (current-time-zone utc-time)))
308 (and (car zone) zone))))))
310 (defun timezone-fix-time (date local timezone)
311 "Convert DATE (default timezone LOCAL) to YYYY-MM-DD-HH-MM-SS-ZONE vector.
312 If LOCAL is nil, it is assumed to be GMT.
313 If TIMEZONE is nil, use the local time zone."
314 (let* ((date (timezone-parse-date date))
315 (year (string-to-int (aref date 0)))
316 (year (cond ((< year 69)
317 (+ year 2000))
318 ((< year 100)
319 (+ year 1900))
320 ((< year 1000) ; possible 3-digit years.
321 (+ year 1900))
322 (t year)))
323 (month (string-to-int (aref date 1)))
324 (day (string-to-int (aref date 2)))
325 (time (timezone-parse-time (aref date 3)))
326 (hour (string-to-int (aref time 0)))
327 (minute (string-to-int (aref time 1)))
328 (second (string-to-int (aref time 2)))
329 (local (or (aref date 4) local)) ;Use original if defined
330 (timezone
331 (or timezone
332 (timezone-time-zone-from-absolute
333 (timezone-absolute-from-gregorian month day year)
334 (+ second (* 60 (+ minute (* 60 hour)))))))
335 (diff (- (timezone-zone-to-minute timezone)
336 (timezone-zone-to-minute local)))
337 (minute (+ minute diff))
338 (hour-fix (floor minute 60)))
339 (setq hour (+ hour hour-fix))
340 (setq minute (- minute (* 60 hour-fix)))
341 ;; HOUR may be larger than 24 or smaller than 0.
342 (cond ((<= 24 hour) ;24 -> 00
343 (setq hour (- hour 24))
344 (setq day (1+ day))
345 (when (< (timezone-last-day-of-month month year) day)
346 (setq month (1+ month))
347 (setq day 1)
348 (when (< 12 month)
349 (setq month 1)
350 (setq year (1+ year)))))
351 ((> 0 hour)
352 (setq hour (+ hour 24))
353 (setq day (1- day))
354 (when (> 1 day)
355 (setq month (1- month))
356 (when (> 1 month)
357 (setq month 12)
358 (setq year (1- year)))
359 (setq day (timezone-last-day-of-month month year)))))
360 (vector year month day hour minute second timezone)))
362 ;; Partly copied from Calendar program by Edward M. Reingold.
363 ;; Thanks a lot.
365 (defun timezone-last-day-of-month (month year)
366 "The last day in MONTH during YEAR."
367 (if (and (= month 2) (timezone-leap-year-p year))
369 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
371 (defun timezone-leap-year-p (year)
372 "Returns t if YEAR is a Gregorian leap year."
373 (or (and (zerop (% year 4))
374 (not (zerop (% year 100))))
375 (zerop (% year 400))))
377 (defun timezone-day-number (month day year)
378 "Return the day number within the year of the date month/day/year."
379 (let ((day-of-year (+ day (* 31 (1- month)))))
380 (if (> month 2)
381 (progn
382 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
383 (if (timezone-leap-year-p year)
384 (setq day-of-year (1+ day-of-year)))))
385 day-of-year))
387 (defun timezone-absolute-from-gregorian (month day year)
388 "The number of days between the Gregorian date 12/31/1 BC and month/day/year.
389 The Gregorian date Sunday, December 31, 1 BC is imaginary."
390 (+ (timezone-day-number month day year);; Days this year
391 (* 365 (1- year));; + Days in prior years
392 (/ (1- year) 4);; + Julian leap years
393 (- (/ (1- year) 100));; - century years
394 (/ (1- year) 400)));; + Gregorian leap years
396 (provide 'timezone)
398 ;;; timezone.el ends here