1 ;;; Timezone 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
8 ;; This file is part of GNU Emacs.
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)
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.
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.
28 (defvar timezone-world-timezones
37 ("AST" . -
400) ;by <clamen@CS.CMU.EDU>
38 ("NST" . -
330) ;by <clamen@CS.CMU.EDU>
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.")
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.")
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))
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)))
110 ;; Parsers and Constructors of Date and Time
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"
118 (capitalize (car (rassq month timezone-months-assoc
)))
119 ;;(- year (* (/ year 100) 100)) ;1990 -> 90
120 (if (< year
100) (+ year
1900) year
) ;90->1990
122 (if timezone
(concat " " timezone
) "")
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
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
))
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
""))
149 (zone nil
)) ;This may be nil.
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
))
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))
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
))
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))
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))
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
))))
183 ;; Don't use `match-end' in order to take 3
184 ;; letters from the beginning.
186 (match-beginning month
)
187 (+ (match-beginning month
) 3)))
188 timezone-months-assoc
))))
190 (substring date
(match-beginning day
) (match-end day
)))
192 (substring date
(match-beginning time
) (match-end time
)))))
195 (substring date
(match-beginning zone
) (match-end zone
))))
198 (vector year month day time zone
)
199 (vector "0" "0" "0" "0" nil
))
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
""))
209 (cond ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)\\'" time
)
211 (setq hour
1 minute
2 second
3))
212 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\'" time
)
214 (setq hour
1 minute
2 second nil
))
215 ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time
)
217 (setq hour
1 minute
2 second
3))
218 ((string-match "\\`\\([0-9][0-9]\\)\\([0-9][0-9]\\)\\'" time
)
220 (setq hour
1 minute
2 second nil
))
222 ;; Return [hour minute second]
225 (substring time
(match-beginning hour
) (match-end hour
)) "0")
227 (substring time
(match-beginning minute
) (match-end minute
)) "0")
229 (substring time
(match-beginning second
) (match-end second
)) "0"))
235 (defun timezone-zone-to-minute (timezone)
236 "Translate TIMEZONE (in zone name or integer) to integer minute."
240 (or (cdr (assoc (upcase timezone
) timezone-world-timezones
))
243 (if (stringp timezone
)
244 (setq timezone
(string-to-int timezone
)))
245 ;; Taking account of minute in timezone.
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
)))
255 (defun timezone-fix-time (year month day hour minute second
)
257 ;; MINUTE may be larger than 60 or smaller than -60.
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))
270 (if (< (timezone-last-day-of-month month year
) day
)
272 (setq month
(1+ month
))
277 (setq year
(1+ year
))
281 (setq hour
(+ hour
24))
285 (setq month
(1- month
))
289 (setq year
(1- year
))
291 (setq day
(timezone-last-day-of-month month year
))
294 (vector year month day hour minute second
))
296 ;; Partly copied from Calendar program by Edward M. Reingold.
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
))
303 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month
))))
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))))
311 ;;; timezone.el ends here