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