1 ;;; timezone.el --- time zone package for GNU Emacs
3 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
5 ;; Author: Masanobu Umeda
6 ;; Maintainer: umerin@mse.kyutech.ac.jp
9 ;; This file is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This file is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; Modified 1 February 1994 by Kyle Jones to fix broken
26 ;; timezone-floor function.
28 ;; Modified 25 January 1994 by Kyle Jones so that it will
29 ;; work under version 18 of Emacs. Provided timezone-floor
30 ;; and timezone-abs functions.
32 ;; Modified 4 October 1999 by Yuuichi Teranishi so that it will
33 ;; work with old GNUS 3.14.4 under version 18 of Emacs.
37 (defvar timezone-world-timezones
46 ("AST" . -400) ;by <clamen@CS.CMU.EDU>
47 ("NST" . -330) ;by <clamen@CS.CMU.EDU>
54 ("GMT+1" . +100) ("GMT+2" . +200) ("GMT+3" . +300)
55 ("GMT+4" . +400) ("GMT+5" . +500) ("GMT+6" . +600)
56 ("GMT+7" . +700) ("GMT+8" . +800) ("GMT+9" . +900)
57 ("GMT+10" . +1000) ("GMT+11" . +1100) ("GMT+12" . +1200) ("GMT+13" . +1300)
58 ("GMT-1" . -100) ("GMT-2" . -200) ("GMT-3" . -300)
59 ("GMT-4" . -400) ("GMT-5" . -500) ("GMT-6" . -600)
60 ("GMT-7" . -700) ("GMT-8" . -800) ("GMT-9" . -900)
61 ("GMT-10" . -1000) ("GMT-11" . -1100) ("GMT-12" . -1200))
62 "*Time differentials of timezone from GMT in +-HHMM form.
63 This list is obsolescent, and is present only for backwards compatibility,
64 because time zone names are ambiguous in practice.
65 Use `current-time-zone' instead.")
67 (defvar timezone-months-assoc
68 '(("JAN" . 1)("FEB" . 2)("MAR" . 3)
69 ("APR" . 4)("MAY" . 5)("JUN" . 6)
70 ("JUL" . 7)("AUG" . 8)("SEP" . 9)
71 ("OCT" . 10)("NOV" . 11)("DEC" . 12))
72 "Alist of first three letters of a month and its numerical representation.")
74 (defun timezone-make-date-arpa-standard (date &optional local timezone)
75 "Convert DATE to an arpanet standard date.
76 Optional 2nd argument LOCAL specifies the default local timezone of the DATE;
77 if nil, GMT is assumed.
78 Optional 3rd argument TIMEZONE specifies a time zone to be represented in;
79 if nil, the local time zone is assumed."
80 (let ((new (timezone-fix-time date local timezone)))
81 (timezone-make-arpa-date (aref new 0) (aref new 1) (aref new 2)
82 (timezone-make-time-string
83 (aref new 3) (aref new 4) (aref new 5))
87 (defun timezone-make-date-sortable (date &optional local timezone)
88 "Convert DATE to a sortable date string.
89 Optional 2nd argument LOCAL specifies the default local timezone of the DATE;
90 if nil, GMT is assumed.
91 Optional 3rd argument TIMEZONE specifies a timezone to be represented in;
92 if nil, the local time zone is assumed."
93 (let ((new (timezone-fix-time date local timezone)))
94 (timezone-make-sortable-date (aref new 0) (aref new 1) (aref new 2)
95 (timezone-make-time-string
96 (aref new 3) (aref new 4) (aref new 5)))
101 ;; Parsers and Constructors of Date and Time
104 (defun timezone-make-arpa-date (year month day time &optional timezone)
105 "Make arpanet standard date string from YEAR, MONTH, DAY, and TIME.
106 Optional argument TIMEZONE specifies a time zone."
109 (let* ((m (timezone-zone-to-minute timezone))
110 (absm (if (< m 0) (- m) m)))
112 (if (< m 0) ?- ?+) (/ absm 60) (% absm 60)))
114 (format "%02d %s %04d %s %s"
116 (capitalize (car (rassq month timezone-months-assoc)))
121 (defun timezone-make-sortable-date (year month day time)
122 "Make sortable date string from YEAR, MONTH, DAY, and TIME."
123 (format "%4d%02d%02d%s"
124 year month day time))
126 (defun timezone-make-time-string (hour minute second)
127 "Make time string from HOUR, MINUTE, and SECOND."
128 (format "%02d:%02d:%02d" hour minute second))
130 (defun timezone-parse-date (date)
131 "Parse DATE and return a vector [YEAR MONTH DAY TIME TIMEZONE].
132 19 is prepended to year if necessary. Timezone may be nil if nothing.
133 Understands the following styles:
134 (1) 14 Apr 89 03:20[:12] [GMT]
135 (2) Fri, 17 Mar 89 4:01[:33] [GMT]
136 (3) Mon Jan 16 16:12[:37] [GMT] 1989
137 (4) 6 May 1992 1641-JST (Wednesday)
138 (5) 22-AUG-1993 10:59:12.82
139 (6) Thu, 11 Apr 16:17:12 91 [MET]
140 (7) Mon, 6 Jul 16:47:20 T 1992 [MET]
141 (8) 1996-06-24 21:13:12 [GMT]
142 (9) 1996-06-24 21:13-ZONE"
143 ;; Get rid of any text properties.
145 (or (text-properties-at 0 date)
146 (next-property-change 0 date))
147 (setq date (copy-sequence date))
148 (set-text-properties 0 (length date) nil date))
149 (let ((date (or date ""))
154 (zone nil)) ;This may be nil.
156 "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
157 ;; Styles: (1) and (2) with timezone and buggy timezone
158 ;; This is most common in mail and news,
159 ;; so it is worth trying first.
160 (setq year 3 month 2 day 1 time 4 zone 5))
162 "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]*\\'" date)
163 ;; Styles: (1) and (2) without timezone
164 (setq year 3 month 2 day 1 time 4 zone nil))
166 "\\([^ \t,]+\\),[ \t]+\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\(T[ \t]+\\|\\)\\([0-9]+\\)[ \t]*\\'" date)
167 ;; Styles: (6) and (7) without timezone
168 (setq year 6 month 3 day 2 time 4 zone nil))
170 "\\([^ \t,]+\\),[ \t]+\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\(T[ \t]+\\|\\)\\([0-9]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
171 ;; Styles: (6) and (7) with timezone and buggy timezone
172 (setq year 6 month 3 day 2 time 4 zone 7))
174 "\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([0-9]+\\)" date)
175 ;; Styles: (3) without timezone
176 (setq year 4 month 1 day 2 time 3 zone nil))
178 "\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([-+a-zA-Z0-9]+\\)[ \t]+\\([0-9]+\\)" date)
179 ;; Styles: (3) with timezone
180 (setq year 5 month 1 day 2 time 3 zone 4))
182 "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
183 ;; Styles: (4) with timezone
184 (setq year 3 month 2 day 1 time 4 zone 5))
186 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)?[ \t]+\\([-+a-zA-Z0-9]+\\)" date)
187 ;; Styles: (5) with timezone.
188 (setq year 3 month 2 day 1 time 4 zone 6))
190 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)?" date)
191 ;; Styles: (5) without timezone.
192 (setq year 3 month 2 day 1 time 4 zone nil))
194 "\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
195 ;; Styles: (8) with timezone.
196 (setq year 1 month 2 day 3 time 4 zone 5))
198 "\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+\\)[ \t]*\\([-+a-zA-Z0-9:]+\\)" date)
199 ;; Styles: (8) with timezone with a colon in it.
200 (setq year 1 month 2 day 3 time 4 zone 5))
202 "\\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)" date)
203 ;; Styles: (8) without timezone.
204 (setq year 1 month 2 day 3 time 4 zone nil))
207 (setq year (match-string year date))
208 ;; Guess ambiguous years. Assume years < 69 don't predate the
209 ;; Unix Epoch, so are 2000+. Three-digit years are assumed to
210 ;; be relative to 1900.
211 (if (< (length year) 4)
212 (let ((y (string-to-int year)))
215 (setq year (int-to-string (+ 1900 y)))))
217 (if (= (aref date (+ (match-beginning month) 2)) ?-)
218 ;; Handle numeric months, spanning exactly two digits.
220 (match-beginning month)
221 (+ (match-beginning month) 2))
222 (let* ((string (substring date
223 (match-beginning month)
224 (+ (match-beginning month) 3)))
226 (cdr (assoc (upcase string) timezone-months-assoc))))
228 (int-to-string monthnum)))))
229 (setq day (match-string day date))
230 (setq time (match-string time date)))
231 (if zone (setq zone (match-string zone date)))
234 (vector year month day time zone)
235 (vector "0" "0" "0" "0" nil))))
237 (defun timezone-parse-time (time)
238 "Parse TIME (HH:MM:SS) and return a vector [hour minute second].
239 Recognize HH:MM:SS, HH:MM, HHMMSS, HHMM."
240 (let ((time (or time ""))
244 (cond ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)\\'" time)
246 (setq hour 1 minute 2 second 3))
247 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\'" time)
249 (setq hour 1 minute 2 second nil))
250 ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time)
252 (setq hour 1 minute 2 second 3))
253 ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time)
255 (setq hour 1 minute 2 second nil))
257 ;; Return [hour minute second]
260 (substring time (match-beginning hour) (match-end hour)) "0")
262 (substring time (match-beginning minute) (match-end minute)) "0")
264 (substring time (match-beginning second) (match-end second)) "0"))
270 (defun timezone-zone-to-minute (timezone)
271 "Translate TIMEZONE to an integer minute offset from GMT.
272 TIMEZONE can be a cons cell containing the output of `current-time-zone',
273 or an integer of the form +-HHMM, or a time zone name."
276 (/ (car timezone) 60))
280 (or (and (stringp timezone) (cdr (assoc (upcase timezone) timezone-world-timezones)))
283 (if (stringp timezone)
284 (setq timezone (string-to-int timezone)))
285 ;; Taking account of minute in timezone.
287 (let* ((abszone (abs timezone))
288 (minutes (+ (* 60 (/ abszone 100)) (% abszone 100))))
289 (if (< timezone 0) (- minutes) minutes))))
292 (defun timezone-floor (arg &optional divisor)
293 "Return the largest integer no grater than ARG.
294 With optional DIVISOR, return the largest integer no greater than ARG/DIVISOR."
298 (- (/ (- divisor 1 arg) divisor))
301 (defun timezone-time-from-absolute (date seconds)
302 "Compute the UTC time equivalent to DATE at time SECONDS after midnight.
303 Return a list suitable as an argument to `current-time-zone',
304 or nil if the date cannot be thus represented.
305 DATE is the number of days elapsed since the (imaginary)
306 Gregorian date Sunday, December 31, 1 BC."
307 (let* ((current-time-origin 719163)
308 ;; (timezone-absolute-from-gregorian 1 1 1970)
309 (days (- date current-time-origin))
310 (days-1 (/ days 65536))
311 (days-2 (% (/ days 256) 256))
312 (days-3 (% days 256))
313 ;; (seconds-per-day (float 86400))
314 (seconds-per-day-1 1)
315 (seconds-per-day-2 81)
316 (seconds-per-day-3 128)
317 ;; (seconds (+ seconds (* days seconds-per-day)))
318 ;; (current-time-arithmetic-base (float 65536))
319 ;; (hi (timezone-floor (/ seconds current-time-arithmetic-base)))
320 ;; (hibase (* hi current-time-arithmetic-base))
321 ;; (lo (timezone-floor (- seconds hibase)))
322 (seconds-1 (/ seconds 65536))
323 (seconds-2 (% (/ seconds 256) 256))
324 (seconds-3 (% seconds 256))
327 seconds-per-day*days-1
328 seconds-per-day*days-2
329 seconds-per-day*days-3)
330 (setq r (* days-3 seconds-per-day-3)
331 seconds-per-day*days-3 (% r 256))
333 (* days-2 seconds-per-day-3)
334 (* days-3 seconds-per-day-2))
335 seconds-per-day*days-2 (% r 256))
336 (setq seconds-per-day*days-1 (+ (/ r 256)
337 (* days-1 seconds-per-day-3)
338 (* (/ days 256) seconds-per-day-2)
339 (* days seconds-per-day-1)))
340 (setq r (+ seconds-2 seconds-per-day*days-2)
342 seconds-1 (+ seconds-1 (/ r 256)))
343 (setq lo (+ (* seconds-2 256)
344 seconds-3 seconds-per-day*days-3))
345 (setq hi (+ seconds-1 seconds-per-day*days-1))
346 ;; (and (< (abs (- seconds (+ hibase lo))) 2) ; Check for integer overflow.
351 (defun timezone-time-zone-from-absolute (date seconds)
352 "Compute the local time zone for DATE at time SECONDS after midnight.
353 Return a list in the same format as current-time-zone's result,
354 or nil if the local time zone could not be computed.
355 DATE is the number of days elapsed since the (imaginary)
356 Gregorian date Sunday, December 31, 1 BC."
357 (and (fboundp 'current-time-zone)
358 (let ((utc-time (timezone-time-from-absolute date seconds)))
360 (let ((zone (current-time-zone utc-time)))
361 (and (car zone) zone))))))
363 (defsubst timezone-fix-time-1 (year month day hour minute second)
365 For old `timezone-fix-time' function.
366 Arguments are YEAR, MONTH, DAY, HOUR, MINUTE and SECOND."
367 ;; MINUTE may be larger than 60 or smaller than -60.
370 ;;(/ (- minute 59) 60) (/ minute 60)
371 ;; ANSI C compliance about truncation of integer division
372 ;; by eggert@twinsun.com (Paul Eggert)
373 (- (/ (- 59 minute) 60)) (/ minute 60))))
374 (setq hour (+ hour hour-fix))
375 (setq minute (- minute (* 60 hour-fix))))
376 ;; HOUR may be larger than 24 or smaller than 0.
377 (cond ((<= 24 hour) ;24 -> 00
378 (setq hour (- hour 24))
380 (if (< (timezone-last-day-of-month month year) day)
382 (setq month (1+ month))
387 (setq year (1+ year))
391 (setq hour (+ hour 24))
395 (setq month (1- month))
399 (setq year (1- year))
401 (setq day (timezone-last-day-of-month month year))
404 (vector year month day hour minute second))
406 (defsubst timezone-fix-time-2 (date local timezone)
407 "Convert DATE (default timezone LOCAL) to YYYY-MM-DD-HH-MM-SS-ZONE vector.
408 If LOCAL is nil, it is assumed to be GMT.
409 If TIMEZONE is nil, use the local time zone."
410 (let* ((date (timezone-parse-date date))
411 (year (string-to-int (aref date 0)))
412 (year (cond ((< year 50)
417 (month (string-to-int (aref date 1)))
418 (day (string-to-int (aref date 2)))
419 (time (timezone-parse-time (aref date 3)))
420 (hour (string-to-int (aref time 0)))
421 (minute (string-to-int (aref time 1)))
422 (second (string-to-int (aref time 2)))
423 (local (or (aref date 4) local)) ;Use original if defined
426 (timezone-time-zone-from-absolute
427 (timezone-absolute-from-gregorian month day year)
428 (+ second (* 60 (+ minute (* 60 hour)))))))
429 (diff (- (timezone-zone-to-minute timezone)
430 (timezone-zone-to-minute local)))
431 (minute (+ minute diff))
432 (hour-fix (timezone-floor minute 60)))
433 (setq hour (+ hour hour-fix))
434 (setq minute (- minute (* 60 hour-fix)))
435 ;; HOUR may be larger than 24 or smaller than 0.
436 (cond ((<= 24 hour) ;24 -> 00
437 (setq hour (- hour 24))
439 (if (< (timezone-last-day-of-month month year) day)
441 (setq month (1+ month))
446 (setq year (1+ year))
450 (setq hour (+ hour 24))
454 (setq month (1- month))
458 (setq year (1- year))
460 (setq day (timezone-last-day-of-month month year))
463 (vector year month day hour minute second timezone)))
465 (defun timezone-fix-time (a1 a2 a3 &optional a4 a5 a6)
467 (Old API: A1=YEAR A2=MONTH A3=DAY A4=HOUR A5=MINUTE A6=SECOND).
468 Convert DATE (default timezone LOCAL) to YYYY-MM-DD-HH-MM-SS-ZONE vector.
469 If LOCAL is nil, it is assumed to be GMT.
470 If TIMEZONE is nil, use the local time zone.
471 (New API: A1=DATE A2=LOCAL A3=TIMEZONE)"
473 (timezone-fix-time-1 a1 a2 a3 a4 a5 a6)
474 (timezone-fix-time-2 a1 a2 a3)))
476 ;; Partly copied from Calendar program by Edward M. Reingold.
479 (defun timezone-last-day-of-month (month year)
480 "The last day in MONTH during YEAR."
481 (if (and (= month 2) (timezone-leap-year-p year))
483 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
485 (defun timezone-leap-year-p (year)
486 "Return t if YEAR is a Gregorian leap year."
487 (or (and (zerop (% year 4))
488 (not (zerop (% year 100))))
489 (zerop (% year 400))))
491 (defun timezone-day-number (month day year)
492 "Return the day number within the year of the date MONTH/DAY/YEAR."
493 (let ((day-of-year (+ day (* 31 (1- month)))))
496 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
497 (if (timezone-leap-year-p year)
498 (setq day-of-year (1+ day-of-year)))))
501 (defun timezone-absolute-from-gregorian (month day year)
502 "The number of days between the Gregorian date 12/31/1 BC and MONTH/DAY/YEAR.
503 The Gregorian date Sunday, December 31, 1 BC is imaginary."
504 (+ (timezone-day-number month day year);; Days this year
505 (* 365 (1- year));; + Days in prior years
506 (/ (1- year) 4);; + Julian leap years
507 (- (/ (1- year) 100));; - century years
508 (/ (1- year) 400)));; + Gregorian leap years
514 (product-provide (provide 'timezone) (require 'apel-ver))
516 ;;; timezone.el ends here