Fix.
[elisp/apel.git] / timezone.el
1 ;;; timezone.el --- time zone package for GNU Emacs
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
4
5 ;; Author: Masanobu Umeda
6 ;; Maintainer: umerin@mse.kyutech.ac.jp
7 ;; Keywords: news
8
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)
12 ;; any later version.
13
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.
18
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.
22
23 ;;; Commentary:
24
25 ;; Modified 1 February 1994 by Kyle Jones to fix broken
26 ;; timezone-floor function.
27
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.
31
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.
34
35 ;; Modified 5 October 1999 by Yuuichi Teranishi to fix y2k problem
36 ;; of timezone-parse-date function.
37
38 ;;; Code:
39
40 (defvar timezone-world-timezones
41   '(("PST" .  -800)
42     ("PDT" .  -700)
43     ("MST" .  -700)
44     ("MDT" .  -600)
45     ("CST" .  -600)
46     ("CDT" .  -500)
47     ("EST" .  -500)
48     ("EDT" .  -400)
49     ("AST" .  -400)                     ;by <clamen@CS.CMU.EDU>
50     ("NST" .  -330)                     ;by <clamen@CS.CMU.EDU>
51     ("UT"  .  +000)
52     ("GMT" .  +000)
53     ("BST" .  +100)
54     ("MET" .  +100)
55     ("EET" .  +200)
56     ("JST" .  +900)
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.")
69
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.")
76
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))
87                              (aref new 6))
88     ))
89
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)))
100     ))
101
102 \f
103 ;;
104 ;; Parsers and Constructors of Date and Time
105 ;;
106
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."
110   (let ((zone
111          (if (listp timezone)
112              (let* ((m (timezone-zone-to-minute timezone))
113                     (absm (if (< m 0) (- m) m)))
114                (format "%c%02d%02d"
115                        (if (< m 0) ?- ?+) (/ absm 60) (% absm 60)))
116            timezone)))
117     (format "%02d %s %04d %s %s"
118             day
119             (capitalize (car (rassq month timezone-months-assoc)))
120             year
121             time
122             zone)))
123
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))
128
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))
132
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]"
144   (condition-case nil
145       (progn
146   ;; Get rid of any text properties.
147   (and (stringp date)
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 ""))
153         (year nil)
154         (month nil)
155         (day nil)
156         (time nil)
157         (zone nil))                     ;This may be nil.
158     (cond ((string-match
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))
162           ((string-match
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))
166           ((string-match
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))
170           ((string-match
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))
174           ((string-match
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))
178           ((string-match
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))
182           ((string-match
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))
186           ((string-match
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))
190           )
191     (if year
192         (progn
193           (setq year
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)))
199                 (when (>= yr 100)
200                   ;; What does a three digit year mean?
201                   (setq yr (- yr 100)))
202                 (setq year (format "%d%02d"
203                                    (if (< yr 70)
204                                        20
205                                      19)
206                                    yr))))
207           (let ((string (substring date
208                                    (match-beginning month)
209                                    (+ (match-beginning month) 3))))
210             (setq month
211                   (int-to-string
212                    (cdr (assoc (upcase string) timezone-months-assoc)))))
213
214           (setq day
215                 (substring date (match-beginning day) (match-end day)))
216           (setq time
217                 (substring date (match-beginning time) (match-end time)))))
218     (if zone
219         (setq zone
220               (substring date (match-beginning zone) (match-end zone))))
221     ;; Return a vector.
222     (if year
223         (vector year month day time zone)
224       (vector "0" "0" "0" "0" nil))
225     )
226   )
227     (t (signal 'error (list "Invalid date string" date)))))
228
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 ""))
233         (hour nil)
234         (minute nil)
235         (second nil))
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))
248           )
249     ;; Return [hour minute second]
250     (vector
251      (if hour
252          (substring time (match-beginning hour) (match-end hour)) "0")
253      (if minute
254          (substring time (match-beginning minute) (match-end minute)) "0")
255      (if second
256          (substring time (match-beginning second) (match-end second)) "0"))
257     ))
258
259 \f
260 ;; Miscellaneous
261
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."
266   (cond
267      ((consp timezone)
268       (/ (car timezone) 60))
269      (timezone
270       (progn
271         (setq timezone
272               (or (and (stringp timezone) (cdr (assoc (upcase timezone) timezone-world-timezones)))
273                   ;; +900
274                   timezone))
275         (if (stringp timezone)
276             (setq timezone (string-to-int timezone)))
277         ;; Taking account of minute in timezone.
278         ;; HHMM -> MM
279         (let* ((abszone (timezone-abs timezone))
280                (minutes (+ (* 60 (/ abszone 100)) (% abszone 100))))
281           (if (< timezone 0) (- minutes) minutes))))
282      (t 0)))
283
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))
308          hi lo
309          r
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))
315     (setq r (+ (/ 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.
330     ;;      (cons hi lo))
331     (cons hi lo)
332     ))
333
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)))
342           (and utc-time
343                (let ((zone (current-time-zone utc-time)))
344                  (and (car zone) zone))))))
345
346 (defsubst timezone-fix-time-1 (year month day hour minute second)
347   "Fix date and time.
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.
351   (let ((hour-fix
352          (if (< minute 0)
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))
362          (setq day  (1+ day))
363          (if (< (timezone-last-day-of-month month year) day)
364              (progn
365                (setq month (1+ month))
366                (setq day 1)
367                (if (< 12 month)
368                    (progn
369                      (setq month 1)
370                      (setq year (1+ year))
371                      ))
372                )))
373         ((> 0 hour)
374          (setq hour (+ hour 24))
375          (setq day  (1- day))
376          (if (> 1 day)
377              (progn
378                (setq month (1- month))
379                (if (> 1 month)
380                    (progn
381                      (setq month 12)
382                      (setq year (1- year))
383                      ))
384                (setq day (timezone-last-day-of-month month year))
385                )))
386         )
387   (vector year month day hour minute second))
388
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)
396                         (+ year 2000))
397                        ((< year 100)
398                         (+ year 1900))
399                        (t year)))
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
407          (timezone
408           (or timezone
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))
421            (setq day  (1+ day))
422            (if (< (timezone-last-day-of-month month year) day)
423                (progn
424                  (setq month (1+ month))
425                  (setq day 1)
426                  (if (< 12 month)
427                      (progn
428                        (setq month 1)
429                        (setq year (1+ year))
430                        ))
431                  )))
432           ((> 0 hour)
433            (setq hour (+ hour 24))
434            (setq day  (1- day))
435            (if (> 1 day)
436                (progn
437                  (setq month (1- month))
438                  (if (> 1 month)
439                      (progn
440                        (setq month 12)
441                        (setq year (1- year))
442                        ))
443                  (setq day (timezone-last-day-of-month month year))
444                  )))
445           )
446     (vector year month day hour minute second timezone)))
447
448 (defun timezone-fix-time (a1 a2 a3 &optional a4 a5 a6)
449   "Fix date and time.
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)"
455   (if a4
456       (timezone-fix-time-1 a1 a2 a3 a4 a5 a6)
457     (timezone-fix-time-2 a1 a2 a3)))
458
459 ;; Partly copied from Calendar program by Edward M. Reingold.
460 ;; Thanks a lot.
461
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))
465       29
466     (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
467
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))))
473
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)))))
477     (if (> month 2)
478         (progn
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)))))
482     day-of-year))
483
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
492
493 (defun timezone-abs (n)
494   "Return the absolute value of N."
495   (if (fboundp 'abs)
496       (abs n)
497     (if (< n 0) (- n) n)))
498
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."
502   (if (fboundp 'floor)
503       (floor n divisor)
504     (if (null divisor)
505         (setq divisor 1))
506     (if (< n 0)
507         (- (/ (- divisor 1 n) divisor))
508       (/ n divisor))))
509
510 (product-provide (provide 'timezone) (require 'apel-ver))
511
512 ;;; timezone.el ends here