Initial Commit
[packages] / xemacs-packages / hyperbole / htz.el
1 ;;; htz.el --- Timezone-based time and date support for Hyperbole.
2
3 ;; Copyright (C) 1991-1995, 2008 Free Software Foundation, Inc.
4 ;; Developed with support from Motorola Inc.
5 ;;
6 ;; Adapted from Timezone package for GNU Emacs
7 ;; Copyright(C) 1990 Masanobu UMEDA (umerin@mse.kyutech.ac.jp)
8
9 ;; Author: Bob Weiner, Brown U.
10 ;; Maintainer: Mats Lidell <matsl@contactor.se>
11 ;; Keywords: calendar, hypermedia
12
13 ;; This file is part of GNU Hyperbole.
14
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.
19
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.
24
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.
29
30 ;;; Commentary:
31 ;;
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.
37 ;;
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.
41 ;;
42
43 ;;; Code:
44
45 ;;;
46 ;;; Other required Elisp libraries
47 ;;;
48
49 (require 'hypb)
50
51 ;;;
52 ;;; Public functions
53 ;;;
54
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."
59   (or (vectorp date)
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)
75                              (htz:time-make-string
76                               (aref new 3) (aref new 4) (aref new 5))
77                              timezone)))
78
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.
83
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 ""))
92         (year nil)
93         (month nil)
94         (day nil)
95         (time nil)
96         (zone nil)) ; This may be nil.
97     (cond ((string-match
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)
99            ;; Style (4)
100            (setq year 1 month 2 day 3 time 4 zone nil))
101           ((string-match
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))
105           ((string-match
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))
109           ((string-match
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))
113           ((string-match
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)
118            ;; Style: (5)
119            (setq year nil month 1 day 2 time 3 zone nil))
120           ((string-match
121             "^\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9][0-9][0-9][0-9]\\)$" date)
122            ;; Style: (6)
123            (setq year 3 month 1 day 2 time nil zone nil))
124           (t (error "(htz:date-parse): Invalid date format: '%s'" date)))
125     (if year
126           (setq year
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)
133                                      (format "%02d"
134                                              (1- (string-to-number century)))
135                                    century)
136                                  year))))
137       (setq year (if (vectorp parsed-current-date)
138                      (aref parsed-current-date 0)
139                    "0")))
140     (if month
141         (setq month (substring date
142                                (match-beginning month) (match-end month))
143               month (if (/= (string-to-number month) 0) month
144                       (int-to-string
145                        (cdr (assoc (upcase month) htz:months-assoc)))))
146       (setq month (if (vectorp parsed-current-date)
147                       (aref parsed-current-date 1)
148                     "0")))
149     (if day
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)
153                   "0")))
154     (if time
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))))
158     (if zone
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)
162                    htz:local)))
163     ;; Return a vector.
164     (vector year month day time zone)))
165
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."
170   (or (vectorp date)
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)))))
188
189 ;;;
190 ;;; Parsers and Constructors of Date and Time
191 ;;;
192
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"))
197
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
204 htz:local."
205   (or (vectorp date)
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)))
220     (htz:date-make-unix
221      (aref fixed 0) (aref fixed 1) (aref fixed 2)
222      (htz:time-make-string (aref fixed 3) (aref fixed 4) (aref fixed 5))
223      timezone)))
224
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."
228   (require 'calendar)
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))
232          (cal-start-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
236          (cal-end-date
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
240          )
241     (- (calendar-absolute-from-julian cal-end-date)
242        (calendar-absolute-from-julian cal-start-date))))
243
244 (defun htz:time-parse (time)
245   "Parse TIME (HH:MM:SS) and return a vector [hour minute second]."
246   (let ((time (or time ""))
247         (hour nil)
248         (minute nil)
249         (second nil))
250     (cond ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)\\'" time)
251            ;; HH:MM:SS
252            (setq hour 1 minute 2 second 3))
253           ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\'" time)
254            ;; HH:MM
255            (setq hour 1 minute 2 second nil)))
256     ;; Return [hour minute second]
257     (vector
258      (if hour
259          (substring time (match-beginning hour) (match-end hour)) "0")
260      (if minute
261          (substring time (match-beginning minute) (match-end minute)) "0")
262      (if second
263          (substring time (match-beginning second) (match-end second)) "0"))))
264
265 ;;;
266 ;;; Private functions
267 ;;;
268
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"
273           day
274           (capitalize (car (rassq month htz:months-assoc)))
275           (- year (* (/ year 100) 100)) ;1990 -> 90
276           time
277           (if timezone (concat " " timezone) "")
278           ))
279
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))
286
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))
290
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))
294       29
295     (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month))))
296
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))))
302
303 (defun htz:time-fix (year month day hour minute second)
304   "Fix date and time."
305   (cond ((<= 24 hour)                   ; 24 -> 00
306          (setq hour (- hour 24))
307          (setq day  (1+ day))
308          (if (< (htz:last-day-of-month month year) day)
309              (progn
310                (setq month (1+ month))
311                (setq day 1)
312                (if (< 12 month)
313                    (progn
314                      (setq month 1)
315                      (setq year (1+ year)))))))
316         ((> 0 hour)
317          (setq hour (+ hour 24))
318          (setq day  (1- day))
319          (if (> 1 day)
320              (progn
321                (setq month (1- month))
322                (if (> 1 month)
323                    (progn
324                      (setq month 12)
325                      (setq year (1- year))))
326                (setq day (htz:last-day-of-month month year))))))
327   (vector year month day hour minute second))
328
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))
333
334 (defun htz:zone-to-hour (timezone)
335   "Translate TIMEZONE (in zone name or integer) to integer hour."
336   (if timezone
337       (progn
338         (setq timezone
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)))
345                   timezone))
346         (if (stringp timezone)
347             (setq timezone (string-to-number timezone)))
348         (/ timezone 100))
349     (error "(htz:zone-to-hour): Nil timezone sent as argument")))
350
351
352 ;;;
353 ;;; Private variables
354 ;;;
355
356 (defvar htz:local
357   (or (getenv "TZ") (getenv "TIMEZONE")
358       (cadr (current-time-zone))
359       (hypb:call-process-p
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.")
364
365 (defvar htz:world-timezones
366   '(("PST" .  -800)
367     ("PDT" .  -700)
368     ("MST" .  -700)
369     ("MDT" .  -600)
370     ("CST" .  -600)
371     ("CDT" .  -500)
372     ("EST" .  -500)
373     ("EDT" .  -400)
374     ("AST" .  -400)                     ;by <clamen@CS.CMU.EDU>
375     ("NST" .  -330)                     ;by <clamen@CS.CMU.EDU>
376     ("GMT" .  +000)
377     ("BST" .  +100)
378     ("MET" .  +100)
379     ("EET" .  +200)
380     ("JST" .  +900)
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.")
393
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.")
400
401
402 (provide 'htz)
403
404 ;;; htz.el ends here