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.
35 ;; Modified 5 October 1999 by Yuuichi Teranishi to fix y2k problem
36 ;; of timezone-parse-date function.
40 (defvar timezone-world-timezones
49 ("AST" . -400) ;by <clamen@CS.CMU.EDU>
50 ("NST" . -330) ;by <clamen@CS.CMU.EDU>
57 ("GMT+1" . +100) ("GMT+2" . +200) ("GMT+3" . +300)
58 ("GMT+4" . +400) ("GMT+5" . +500) ("GMT+6" . +600)
59 ("GMT+7" . +700) ("GMT+8" . +800) ("GMT+9" . +900)
60 ("GMT+10" . +1000) ("GMT+11" . +1100) ("GMT+12" . +1200) ("GMT+13" . +1300)
61 ("GMT-1" . -100) ("GMT-2" . -200) ("GMT-3" . -300)
62 ("GMT-4" . -400) ("GMT-5" . -500) ("GMT-6" . -600)
63 ("GMT-7" . -700) ("GMT-8" . -800) ("GMT-9" . -900)
64 ("GMT-10" . -1000) ("GMT-11" . -1100) ("GMT-12" . -1200))
65 "*Time differentials of timezone from GMT in +-HHMM form.
66 This list is obsolescent, and is present only for backwards compatibility,
67 because time zone names are ambiguous in practice.
68 Use `current-time-zone' instead.")
70 (defvar timezone-months-assoc
71 '(("JAN" . 1)("FEB" . 2)("MAR" . 3)
72 ("APR" . 4)("MAY" . 5)("JUN" . 6)
73 ("JUL" . 7)("AUG" . 8)("SEP" . 9)
74 ("OCT" . 10)("NOV" . 11)("DEC" . 12))
75 "Alist of first three letters of a month and its numerical representation.")
77 (defun timezone-make-date-arpa-standard (date &optional local timezone)
78 "Convert DATE to an arpanet standard date.
79 Optional 2nd argument LOCAL specifies the default local timezone of the DATE;
80 if nil, GMT is assumed.
81 Optional 3rd argument TIMEZONE specifies a time zone to be represented in;
82 if nil, the local time zone is assumed."
83 (let ((new (timezone-fix-time date local timezone)))
84 (timezone-make-arpa-date (aref new 0) (aref new 1) (aref new 2)
85 (timezone-make-time-string
86 (aref new 3) (aref new 4) (aref new 5))
90 (defun timezone-make-date-sortable (date &optional local timezone)
91 "Convert DATE to a sortable date string.
92 Optional 2nd argument LOCAL specifies the default local timezone of the DATE;
93 if nil, GMT is assumed.
94 Optional 3rd argument TIMEZONE specifies a timezone to be represented in;
95 if nil, the local time zone is assumed."
96 (let ((new (timezone-fix-time date local timezone)))
97 (timezone-make-sortable-date (aref new 0) (aref new 1) (aref new 2)
98 (timezone-make-time-string
99 (aref new 3) (aref new 4) (aref new 5)))
104 ;; Parsers and Constructors of Date and Time
107 (defun timezone-make-arpa-date (year month day time &optional timezone)
108 "Make arpanet standard date string from YEAR, MONTH, DAY, and TIME.
109 Optional argument TIMEZONE specifies a time zone."
112 (let* ((m (timezone-zone-to-minute timezone))
113 (absm (if (< m 0) (- m) m)))
115 (if (< m 0) ?- ?+) (/ absm 60) (% absm 60)))
117 (format "%02d %s %04d %s %s"
119 (capitalize (car (rassq month timezone-months-assoc)))
124 (defun timezone-make-sortable-date (year month day time)
125 "Make sortable date string from YEAR, MONTH, DAY, and TIME."
126 (format "%4d%02d%02d%s"
127 year month day time))
129 (defun timezone-make-time-string (hour minute second)
130 "Make time string from HOUR, MINUTE, and SECOND."
131 (format "%02d:%02d:%02d" hour minute second))
133 (defun timezone-parse-date (date)
134 "Parse DATE and return a vector [YEAR MONTH DAY TIME TIMEZONE].
135 19 is prepended to year if necessary. Timezone may be nil if nothing.
136 Understands the following styles:
137 (1) 14 Apr 89 03:20[:12] [GMT]
138 (2) Fri, 17 Mar 89 4:01[:33] [GMT]
139 (3) Mon Jan 16 16:12[:37] [GMT] 1989
140 (4) 6 May 1992 1641-JST (Wednesday)
141 (5) 22-AUG-1993 10:59:12.82
142 (6) Thu, 11 Apr 16:17:12 91 [MET]
143 (7) Mon, 6 Jul 16:47:20 T 1992 [MET]"
146 ;; Get rid of any text properties.
148 (or (text-properties-at 0 date)
149 (next-property-change 0 date))
150 (setq date (copy-sequence date))
151 (set-text-properties 0 (length date) nil date))
152 (let ((date (or date ""))
157 (zone nil)) ;This may be nil.
159 "\\([^ \t,]+\\),[ \t]+\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\(T[ \t]+\\|\\)\\([0-9]+\\)[ \t]*\\'" date)
160 ;; Styles: (6) and (7) without timezone
161 (setq year 6 month 3 day 2 time 4 zone nil))
163 "\\([^ \t,]+\\),[ \t]+\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\(T[ \t]+\\|\\)\\([0-9]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
164 ;; Styles: (6) and (7) with timezone and buggy timezone
165 (setq year 6 month 3 day 2 time 4 zone 7))
167 "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]*\\'" date)
168 ;; Styles: (1) and (2) without timezone
169 (setq year 3 month 2 day 1 time 4 zone nil))
171 "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
172 ;; Styles: (1) and (2) with timezone and buggy timezone
173 (setq year 3 month 2 day 1 time 4 zone 5))
175 "\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([0-9]+\\)" date)
176 ;; Styles: (3) without timezone
177 (setq year 4 month 1 day 2 time 3 zone nil))
179 "\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9:]+\\)[ \t]+\\([-+a-zA-Z0-9]+\\)[ \t]+\\([0-9]+\\)" date)
180 ;; Styles: (3) with timezone
181 (setq year 5 month 1 day 2 time 3 zone 4))
183 "\\([0-9]+\\)[ \t]+\\([^ \t,]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]*\\([-+a-zA-Z0-9]+\\)" date)
184 ;; Styles: (4) with timezone
185 (setq year 3 month 2 day 1 time 4 zone 5))
187 "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\.[0-9]+" date)
188 ;; Styles: (5) without timezone.
189 (setq year 3 month 2 day 1 time 4 zone nil))
194 (substring date (match-beginning year) (match-end year)))
195 ;; It is now Dec 1992. 8 years before the end of the World.
196 (if (< (length year) 4)
197 ;; 2 digit years are bogus, so guess the century
198 (let ((yr (string-to-int year)))
200 ;; What does a three digit year mean?
201 (setq yr (- yr 100)))
202 (setq year (format "%d%02d"
207 (let ((string (substring date
208 (match-beginning month)
209 (+ (match-beginning month) 3))))
212 (cdr (assoc (upcase string) timezone-months-assoc)))))
215 (substring date (match-beginning day) (match-end day)))
217 (substring date (match-beginning time) (match-end time)))))
220 (substring date (match-beginning zone) (match-end zone))))
223 (vector year month day time zone)
224 (vector "0" "0" "0" "0" nil))
227 (t (signal 'error (list "Invalid date string" date)))))
229 (defun timezone-parse-time (time)
230 "Parse TIME (HH:MM:SS) and return a vector [hour minute second].
231 Recognize HH:MM:SS, HH:MM, HHMMSS, HHMM."
232 (let ((time (or time ""))
236 (cond ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)\\'" time)
238 (setq hour 1 minute 2 second 3))
239 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\'" time)
241 (setq hour 1 minute 2 second nil))
242 ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time)
244 (setq hour 1 minute 2 second 3))
245 ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time)
247 (setq hour 1 minute 2 second nil))
249 ;; Return [hour minute second]
252 (substring time (match-beginning hour) (match-end hour)) "0")
254 (substring time (match-beginning minute) (match-end minute)) "0")
256 (substring time (match-beginning second) (match-end second)) "0"))
262 (defun timezone-zone-to-minute (timezone)
263 "Translate TIMEZONE to an integer minute offset from GMT.
264 TIMEZONE can be a cons cell containing the output of `current-time-zone',
265 or an integer of the form +-HHMM, or a time zone name."
268 (/ (car timezone) 60))
272 (or (and (stringp timezone) (cdr (assoc (upcase timezone) timezone-world-timezones)))
275 (if (stringp timezone)
276 (setq timezone (string-to-int timezone)))
277 ;; Taking account of minute in timezone.
279 (let* ((abszone (timezone-abs timezone))
280 (minutes (+ (* 60 (/ abszone 100)) (% abszone 100))))
281 (if (< timezone 0) (- minutes) minutes))))
284 (defun timezone-time-from-absolute (date seconds)
285 "Compute the UTC time equivalent to DATE at time SECONDS after midnight.
286 Return a list suitable as an argument to `current-time-zone',
287 or nil if the date cannot be thus represented.
288 DATE is the number of days elapsed since the (imaginary)
289 Gregorian date Sunday, December 31, 1 BC."
290 (let* ((current-time-origin 719163)
291 ;; (timezone-absolute-from-gregorian 1 1 1970)
292 (days (- date current-time-origin))
293 (days-1 (/ days 65536))
294 (days-2 (mod (/ days 256) 256))
295 (days-3 (mod days 256))
296 ;; (seconds-per-day (float 86400))
297 (seconds-per-day-1 1)
298 (seconds-per-day-2 81)
299 (seconds-per-day-3 128)
300 ;; (seconds (+ seconds (* days seconds-per-day)))
301 ;; (current-time-arithmetic-base (float 65536))
302 ;; (hi (floor (/ seconds current-time-arithmetic-base)))
303 ;; (hibase (* hi current-time-arithmetic-base))
304 ;; (lo (floor (- seconds hibase)))
305 (seconds-1 (/ seconds 65536))
306 (seconds-2 (mod (/ seconds 256) 256))
307 (seconds-3 (mod seconds 256))
310 seconds-per-day*days-1
311 seconds-per-day*days-2
312 seconds-per-day*days-3)
313 (setq r (* days-3 seconds-per-day-3)
314 seconds-per-day*days-3 (mod r 256))
316 (* days-2 seconds-per-day-3)
317 (* days-3 seconds-per-day-2))
318 seconds-per-day*days-2 (mod r 256))
319 (setq seconds-per-day*days-1 (+ (/ r 256)
320 (* days-1 seconds-per-day-3)
321 (* (/ days 256) seconds-per-day-2)
322 (* days seconds-per-day-1)))
323 (setq r (+ seconds-2 seconds-per-day*days-2)
324 seconds-2 (mod r 256)
325 seconds-1 (+ seconds-1 (/ r 256)))
326 (setq lo (+ (* seconds-2 256)
327 seconds-3 seconds-per-day*days-3))
328 (setq hi (+ seconds-1 seconds-per-day*days-1))
329 ;; (and (< (abs (- seconds (+ hibase lo))) 2) ; Check for integer overflow.
334 (defun timezone-time-zone-from-absolute (date seconds)
335 "Compute the local time zone for DATE at time SECONDS after midnight.
336 Return a list in the same format as current-time-zone's result,
337 or nil if the local time zone could not be computed.
338 DATE is the number of days elapsed since the (imaginary)
339 Gregorian date Sunday, December 31, 1 BC."
340 (and (fboundp 'current-time-zone)
341 (let ((utc-time (timezone-time-from-absolute date seconds)))
343 (let ((zone (current-time-zone utc-time)))
344 (and (car zone) zone))))))
346 (defsubst timezone-fix-time-1 (year month day hour minute second)
348 For old `timezone-fix-time' function.
349 Arguments are YEAR, MONTH, DAY, HOUR, MINUTE and SECOND."
350 ;; MINUTE may be larger than 60 or smaller than -60.
353 ;;(/ (- minute 59) 60) (/ minute 60)
354 ;; ANSI C compliance about truncation of integer division
355 ;; by eggert@twinsun.com (Paul Eggert)
356 (- (/ (- 59 minute) 60)) (/ minute 60))))
357 (setq hour (+ hour hour-fix))
358 (setq minute (- minute (* 60 hour-fix))))
359 ;; HOUR may be larger than 24 or smaller than 0.
360 (cond ((<= 24 hour) ;24 -> 00
361 (setq hour (- hour 24))
363 (if (< (timezone-last-day-of-month month year) day)
365 (setq month (1+ month))
370 (setq year (1+ year))
374 (setq hour (+ hour 24))
378 (setq month (1- month))
382 (setq year (1- year))
384 (setq day (timezone-last-day-of-month month year))
387 (vector year month day hour minute second))
389 (defsubst timezone-fix-time-2 (date local timezone)
390 "Convert DATE (default timezone LOCAL) to YYYY-MM-DD-HH-MM-SS-ZONE vector.
391 If LOCAL is nil, it is assumed to be GMT.
392 If TIMEZONE is nil, use the local time zone."
393 (let* ((date (timezone-parse-date date))
394 (year (string-to-int (aref date 0)))
395 (year (cond ((< year 50)
400 (month (string-to-int (aref date 1)))
401 (day (string-to-int (aref date 2)))
402 (time (timezone-parse-time (aref date 3)))
403 (hour (string-to-int (aref time 0)))
404 (minute (string-to-int (aref time 1)))
405 (second (string-to-int (aref time 2)))
406 (local (or (aref date 4) local)) ;Use original if defined
409 (timezone-time-zone-from-absolute
410 (timezone-absolute-from-gregorian month day year)
411 (+ second (* 60 (+ minute (* 60 hour)))))))
412 (diff (- (timezone-zone-to-minute timezone)
413 (timezone-zone-to-minute local)))
414 (minute (+ minute diff))
415 (hour-fix (timezone-floor minute 60)))
416 (setq hour (+ hour hour-fix))
417 (setq minute (- minute (* 60 hour-fix)))
418 ;; HOUR may be larger than 24 or smaller than 0.
419 (cond ((<= 24 hour) ;24 -> 00
420 (setq hour (- hour 24))
422 (if (< (timezone-last-day-of-month month year) day)
424 (setq month (1+ month))
429 (setq year (1+ year))
433 (setq hour (+ hour 24))
437 (setq month (1- month))
441 (setq year (1- year))
443 (setq day (timezone-last-day-of-month month year))
446 (vector year month day hour minute second timezone)))
448 (defun timezone-fix-time (a1 a2 a3 &optional a4 a5 a6)
450 (Old API: A1=YEAR A2=MONTH A3=DAY A4=HOUR A5=MINUTE A6=SECOND).
451 Convert DATE (default timezone LOCAL) to YYYY-MM-DD-HH-MM-SS-ZONE vector.
452 If LOCAL is nil, it is assumed to be GMT.
453 If TIMEZONE is nil, use the local time zone.
454 (New API: A1=DATE A2=LOCAL A3=TIMEZONE)"
456 (timezone-fix-time-1 a1 a2 a3 a4 a5 a6)
457 (timezone-fix-time-2 a1 a2 a3)))
459 ;; Partly copied from Calendar program by Edward M. Reingold.
462 (defun timezone-last-day-of-month (month year)
463 "The last day in MONTH during YEAR."
464 (if (and (= month 2) (timezone-leap-year-p year))
466 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
468 (defun timezone-leap-year-p (year)
469 "Return t if YEAR is a Gregorian leap year."
470 (or (and (zerop (% year 4))
471 (not (zerop (% year 100))))
472 (zerop (% year 400))))
474 (defun timezone-day-number (month day year)
475 "Return the day number within the year of the date MONTH/DAY/YEAR."
476 (let ((day-of-year (+ day (* 31 (1- month)))))
479 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
480 (if (timezone-leap-year-p year)
481 (setq day-of-year (1+ day-of-year)))))
484 (defun timezone-absolute-from-gregorian (month day year)
485 "The number of days between the Gregorian date 12/31/1 BC and MONTH/DAY/YEAR.
486 The Gregorian date Sunday, December 31, 1 BC is imaginary."
487 (+ (timezone-day-number month day year);; Days this year
488 (* 365 (1- year));; + Days in prior years
489 (/ (1- year) 4);; + Julian leap years
490 (- (/ (1- year) 100));; - century years
491 (/ (1- year) 400)));; + Gregorian leap years
493 (defun timezone-abs (n)
494 "Return the absolute value of N."
497 (if (< n 0) (- n) n)))
499 (defun timezone-floor (n &optional divisor)
500 "Return the largest integer no grater than N.
501 With optional DIVISOR, return the largest integer no greater than ARG/DIVISOR."
507 (- (/ (- divisor 1 n) divisor))
510 (product-provide (provide 'timezone) (require 'apel-ver))
512 ;;; timezone.el ends here