1 ;;; htz.el --- Timezone-based time and date support for Hyperbole.
3 ;; Copyright (C) 1991-1995, 2008 Free Software Foundation, Inc.
4 ;; Developed with support from Motorola Inc.
6 ;; Adapted from Timezone package for GNU Emacs
7 ;; Copyright(C) 1990 Masanobu UMEDA (umerin@mse.kyutech.ac.jp)
9 ;; Author: Bob Weiner, Brown U.
10 ;; Maintainer: Mats Lidell <matsl@contactor.se>
11 ;; Keywords: calendar, hypermedia
13 ;; This file is part of GNU Hyperbole.
15 ;; GNU Hyperbole is free software; you can redistribute it and/or
16 ;; modify it under the terms of the GNU General Public License as
17 ;; published by the Free Software Foundation; either version 3, or (at
18 ;; your option) any later version.
20 ;; GNU Hyperbole is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ;; General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
32 ;; All date parsing functions accept the output of any other parsing
33 ;; function as input, so one can convert to a sortable date format, do a
34 ;; compare and the display the result in a user selected format.
35 ;; All date formats use a 4-digit year, so there are no problems around the
36 ;; turn of the century.
38 ;; Hyperbole uses this package to normalize all worldwide date times to
39 ;; Greenwich Mean Time, so that Hyperbole buttons created by users in
40 ;; different timezones will sort by time properly.
46 ;;; Other required Elisp libraries
55 (defun htz:date-arpa (&optional date local timezone)
56 "Convert optional DATE or current date to an arpanet standard date.
57 Optional 1st argument LOCAL specifies the default local timezone of the DATE.
58 Optional 2nd argument TIMEZONE specifies a timezone to be represented in."
60 (setq date (htz:date-parse (or date (current-time-string)))))
61 (let* ((year (string-to-number (aref date 0)))
62 (month (string-to-number (aref date 1)))
63 (day (string-to-number (aref date 2)))
64 (time (htz:time-parse (aref date 3)))
65 (hour (string-to-number (aref time 0)))
66 (minute (string-to-number (aref time 1)))
67 (second (string-to-number (aref time 2)))
68 (local (or (aref date 4) local htz:local)) ;Use original if defined
69 (timezone (or timezone local))
70 (diff (- (htz:zone-to-hour timezone)
71 (htz:zone-to-hour local)))
72 (new (htz:time-fix year month day
73 (+ hour diff) minute second)))
74 (htz:date-make-arpa (aref new 0) (aref new 1) (aref new 2)
76 (aref new 3) (aref new 4) (aref new 5))
79 (defun htz:date-parse (date &optional parsed-current-date)
80 "Parse DATE string and return a vector [year month day time timezone].
81 19 is prepended to year if necessary. Timezone in DATE is optional, it
82 defaults to the value of htz:local.
84 Recognizes the following styles:
85 (1) 14 Apr 89 03:20[:12] [GMT]
86 (2) Fri, 17 Mar [19]89 4:01[:33] [GMT]
87 (3) Mon Jan 16 16:12[:37] [GMT] 1989
88 (4) 19911014:07:51:08 or 1991101407:51:08 'sortable date'
89 (5) Mar 29 14:00 'ls -l date' requires 'parsed-current-date' arg
90 (6) Mar 7 1994 'ls -l date' requires 'parsed-current-date' arg"
91 (let ((date (or date ""))
96 (zone nil)) ; This may be nil.
98 "\\([0-9][0-9][0-9][0-9]\\)\\([0-1][0-9]\\)\\([0-3][0-9]\\):?\\([0-2][0-9]:[0-5][0-9:]+\\)[ ]*\\'" date)
100 (setq year 1 month 2 day 3 time 4 zone nil))
102 "\\([0-9]+\\) \\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+:[0-9:]+\\)[ ]*\\'" date)
103 ;; Styles: (1) and (2) without timezone
104 (setq year 3 month 2 day 1 time 4 zone nil))
106 "\\([0-9]+\\) \\([^ ,]+\\) \\([0-9]+\\) \\([0-9]+:[0-9:]+\\)[ ]*\\([-+a-zA-Z0-9]+\\)" date)
107 ;; Styles: (1) and (2) with timezone and buggy timezone
108 (setq year 3 month 2 day 1 time 4 zone 5))
110 "\\([^ ,]+\\) +\\([0-9]+\\) \\([0-9]+:[0-9:]+\\) \\([0-9]+\\)" date)
111 ;; Styles: (3) without timezone
112 (setq year 4 month 1 day 2 time 3 zone nil))
114 "\\([^ ,]+\\) +\\([0-9]+\\) \\([0-9]+:[0-9:]+\\) \\([-+a-zA-Z0-9]+\\) \\([0-9]+\\)" date)
115 ;; Styles: (3) with timezone
116 (setq year 5 month 1 day 2 time 3 zone 4))
117 ((string-match "^\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9]+:[0-9:]+\\)$" date)
119 (setq year nil month 1 day 2 time 3 zone nil))
121 "^\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9][0-9][0-9][0-9]\\)$" date)
123 (setq year 3 month 1 day 2 time nil zone nil))
124 (t (error "(htz:date-parse): Invalid date format: '%s'" date)))
127 (substring date (match-beginning year) (match-end year))
128 year (if (/= (length year) 2) year
129 (let* ((yr (substring (current-time-string) -4))
130 (curr-yr (substring yr 2))
131 (century (substring yr 0 2)))
132 (concat (if (string< curr-yr yr)
134 (1- (string-to-number century)))
137 (setq year (if (vectorp parsed-current-date)
138 (aref parsed-current-date 0)
141 (setq month (substring date
142 (match-beginning month) (match-end month))
143 month (if (/= (string-to-number month) 0) month
145 (cdr (assoc (upcase month) htz:months-assoc)))))
146 (setq month (if (vectorp parsed-current-date)
147 (aref parsed-current-date 1)
150 (setq day (substring date (match-beginning day) (match-end day)))
151 (setq day (if (vectorp parsed-current-date)
152 (aref parsed-current-date 2)
155 (setq time (substring date (match-beginning time) (match-end time)))
156 (setq time (if (vectorp parsed-current-date)
157 (aref parsed-current-date 3))))
159 (setq zone (substring date (match-beginning zone) (match-end zone)))
160 (setq zone (if (vectorp parsed-current-date)
161 (aref parsed-current-date 4)
164 (vector year month day time zone)))
166 (defun htz:date-sortable (&optional date local timezone)
167 "Convert optional DATE or current date to a sortable date string.
168 Optional 1st argument LOCAL specifies the local timezone of the DATE.
169 Optional 2nd argument TIMEZONE specifies an output timezone to use."
171 (setq date (htz:date-parse (or date (current-time-string)))))
172 (let* ((year (string-to-number (aref date 0)))
173 (month (string-to-number (aref date 1)))
174 (day (string-to-number (aref date 2)))
175 (time (htz:time-parse (aref date 3)))
176 (hour (string-to-number (aref time 0)))
177 (minute (string-to-number (aref time 1)))
178 (second (string-to-number (aref time 2)))
179 (local (or (aref date 4) local htz:local)) ;Use original if defined
180 (timezone (or timezone local))
181 (diff (- (htz:zone-to-hour timezone)
182 (htz:zone-to-hour local)))
183 (new (htz:time-fix year month day
184 (+ hour diff) minute second)))
185 (htz:date-make-sortable (aref new 0) (aref new 1) (aref new 2)
186 (htz:time-make-string
187 (aref new 3) (aref new 4) (aref new 5)))))
190 ;;; Parsers and Constructors of Date and Time
193 (defun htz:date-sortable-gmt (&optional date local)
194 "Convert optional DATE or current date to a sortable date string in Greenwich Mean Time.
195 Optional argument LOCAL specifies the local timezone of the DATE."
196 (htz:date-sortable date local "GMT"))
198 (defun htz:date-unix (&optional date local timezone)
199 "Convert DATE or current date to a unix standard date.
200 Optional 1st argument LOCAL specifies the local timezone of the DATE (default
201 is the timezone embedded in the date or if there is none, then the value of
202 the variable, htz:local). Optional 2nd argument TIMEZONE specifies the
203 timezone in which the date is returned; it defaults to the value of
206 (setq date (htz:date-parse (or date (current-time-string)))))
207 (or local (setq local (or (aref date 4) htz:local)))
208 (let* ((year (string-to-number (aref date 0)))
209 (month (string-to-number (aref date 1)))
210 (day (string-to-number (aref date 2)))
211 (time (htz:time-parse (aref date 3)))
212 (hour (string-to-number (aref time 0)))
213 (minute (string-to-number (aref time 1)))
214 (second (string-to-number (aref time 2)))
215 (timezone (or timezone local))
216 (diff (- (htz:zone-to-hour timezone)
217 (htz:zone-to-hour local)))
218 (fixed (htz:time-fix year month day
219 (+ hour diff) minute second)))
221 (aref fixed 0) (aref fixed 1) (aref fixed 2)
222 (htz:time-make-string (aref fixed 3) (aref fixed 4) (aref fixed 5))
225 (defun htz:span-in-days (start-date end-date)
226 "Return span in days between START-DATE and END-DATE strings.
227 See 'htz:date-parse' for a list of acceptable date formats."
229 (let* ((parsed-current-date (htz:date-parse (current-time-string)))
230 (htz-start-date (htz:date-parse start-date parsed-current-date))
231 (htz-end-date (htz:date-parse end-date parsed-current-date))
233 (list (string-to-number (aref htz-start-date 1)) ;; month
234 (string-to-number (aref htz-start-date 2)) ;; day
235 (string-to-number (aref htz-start-date 0)))) ;; year
237 (list (string-to-number (aref htz-end-date 1)) ;; month
238 (string-to-number (aref htz-end-date 2)) ;; day
239 (string-to-number (aref htz-end-date 0)))) ;; year
241 (- (calendar-absolute-from-julian cal-end-date)
242 (calendar-absolute-from-julian cal-start-date))))
244 (defun htz:time-parse (time)
245 "Parse TIME (HH:MM:SS) and return a vector [hour minute second]."
246 (let ((time (or time ""))
250 (cond ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)\\'" time)
252 (setq hour 1 minute 2 second 3))
253 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\'" time)
255 (setq hour 1 minute 2 second nil)))
256 ;; Return [hour minute second]
259 (substring time (match-beginning hour) (match-end hour)) "0")
261 (substring time (match-beginning minute) (match-end minute)) "0")
263 (substring time (match-beginning second) (match-end second)) "0"))))
266 ;;; Private functions
269 (defun htz:date-make-arpa (year month day time &optional timezone)
270 "Make arpanet standard date string from YEAR, MONTH, DAY, and TIME.
271 Optional argument TIMEZONE specifies a time zone."
272 (format "%02d %s %02d %s%s"
274 (capitalize (car (rassq month htz:months-assoc)))
275 (- year (* (/ year 100) 100)) ;1990 -> 90
277 (if timezone (concat " " timezone) "")
280 (defun htz:date-make-unix (year month day time &optional timezone)
281 "Approximate Unix date format from YEAR, MONTH, DAY, and TIME.
282 Optional argument TIMEZONE specifies a time zone."
283 (format "%s %02d %s%s %04d"
284 (capitalize (car (rassq month htz:months-assoc)))
285 day time (if timezone (concat " " timezone) "") year))
287 (defun htz:date-make-sortable (year month day time)
288 "Make sortable date string from YEAR, MONTH, DAY, and TIME."
289 (format "%04d%02d%02d:%s" year month day time))
291 (defun htz:last-day-of-month (month year)
292 "The last day in MONTH during YEAR."
293 (if (and (= month 2) (htz:leap-year-p year))
295 (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
297 (defun htz:leap-year-p (year)
298 "Returns t if YEAR is a Gregorian leap year."
299 (or (and (zerop (% year 4))
300 (not (zerop (% year 100))))
301 (zerop (% year 400))))
303 (defun htz:time-fix (year month day hour minute second)
305 (cond ((<= 24 hour) ; 24 -> 00
306 (setq hour (- hour 24))
308 (if (< (htz:last-day-of-month month year) day)
310 (setq month (1+ month))
315 (setq year (1+ year)))))))
317 (setq hour (+ hour 24))
321 (setq month (1- month))
325 (setq year (1- year))))
326 (setq day (htz:last-day-of-month month year))))))
327 (vector year month day hour minute second))
329 ;; Partly copied from Calendar program by Edward M. Reingold.
330 (defun htz:time-make-string (hour minute second)
331 "Make time string from HOUR, MINUTE, and SECOND."
332 (format "%02d:%02d:%02d" hour minute second))
334 (defun htz:zone-to-hour (timezone)
335 "Translate TIMEZONE (in zone name or integer) to integer hour."
339 (or (cdr (assoc (upcase timezone)
340 htz:world-timezones))
341 (and (fboundp 'current-time-zone)
342 (if (listp (current-time-zone))
343 (car (cdr (current-time-zone)))
344 (current-time-zone)))
346 (if (stringp timezone)
347 (setq timezone (string-to-number timezone)))
349 (error "(htz:zone-to-hour): Nil timezone sent as argument")))
353 ;;; Private variables
357 (or (getenv "TZ") (getenv "TIMEZONE")
358 (cadr (current-time-zone))
360 "date" nil '(if (re-search-backward
361 " \\([-+a-zA-Z0-9]+\\) [0-9]+$" nil t)
362 (buffer-substring (match-beginning 1) (match-end 1)))))
363 "Holds string giving the timezone for the local machine.")
365 (defvar htz:world-timezones
374 ("AST" . -400) ;by <clamen@CS.CMU.EDU>
375 ("NST" . -330) ;by <clamen@CS.CMU.EDU>
381 ("GMT+1" . +100) ("GMT+2" . +200) ("GMT+3" . +300)
382 ("GMT+4" . +400) ("GMT+5" . +500) ("GMT+6" . +600)
383 ("GMT+7" . +700) ("GMT+8" . +800) ("GMT+9" . +900)
384 ("GMT+10" . +1000) ("GMT+11" . +1100) ("GMT+12" . +1200) ("GMT+13" . +1300)
385 ("GMT-1" . -100) ("GMT-2" . -200) ("GMT-3" . -300)
386 ("GMT-4" . -400) ("GMT-5" . -500) ("GMT-6" . -600)
387 ("GMT-7" . -700) ("GMT-8" . -800) ("GMT-9" . -900)
388 ("GMT-10" . -1000) ("GMT-11" . -1100) ("GMT-12" . -1200))
389 "*Time differentials of timezone from GMT in +-HHMM form.
390 This list is obsolescent, and is present only for backwards compatibility,
391 because time zone names are ambiguous in practice.
392 Use `current-time-zone' instead.")
394 (defvar htz:months-assoc
395 '(("JAN" . 1)("FEB" . 2)("MAR" . 3)
396 ("APR" . 4)("MAY" . 5)("JUN" . 6)
397 ("JUL" . 7)("AUG" . 8)("SEP" . 9)
398 ("OCT" . 10)("NOV" . 11)("DEC" . 12))
399 "Alist of first three letters of a month and its numerical representation.")