Fix comment typo.
[gnus] / lisp / parse-time.el
1 ;;; parse-time.el --- parsing time strings
2
3 ;; Copyright (C) 1996, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;;   2008, 2009, 2010  Free Software Foundation, Inc.
5
6 ;; Author: Erik Naggum <erik@naggum.no>
7 ;; Keywords: util
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; With the introduction of the `encode-time', `decode-time', and
27 ;; `format-time-string' functions, dealing with time became simpler in
28 ;; Emacs.  However, parsing time strings is still largely a matter of
29 ;; heuristics and no common interface has been designed.
30
31 ;; `parse-time-string' parses a time in a string and returns a list of 9
32 ;; values, just like `decode-time', where unspecified elements in the
33 ;; string are returned as nil.  `encode-time' may be applied on these
34 ;; values to obtain an internal time value.
35
36 ;;; Code:
37
38 (eval-when-compile (require 'cl))       ;and ah ain't kiddin' 'bout it
39
40 (eval-and-compile
41   (when (featurep 'xemacs)
42     (defvar parse-time-syntax (make-vector 256 nil))))
43 (defvar parse-time-digits (make-vector 256 nil))
44
45 ;; Byte-compiler warnings
46 (defvar parse-time-elt)
47 (defvar parse-time-val)
48
49 (unless (aref parse-time-digits ?0)
50   (loop for i from ?0 to ?9
51     do (aset parse-time-digits i (- i ?0))))
52
53 (when (featurep 'xemacs)
54   (unless (aref parse-time-syntax ?0)
55     (loop for i from ?0 to ?9
56           do (aset parse-time-syntax i ?0))
57     (loop for i from ?A to ?Z
58           do (aset parse-time-syntax i ?A))
59     (loop for i from ?a to ?z
60           do (aset parse-time-syntax i ?a))
61     (aset parse-time-syntax ?+ 1)
62     (aset parse-time-syntax ?- -1)
63     (aset parse-time-syntax ?: ?d)))
64
65 (defsubst digit-char-p (char)
66   (aref parse-time-digits char))
67
68 (eval-and-compile
69   (if (featurep 'xemacs)
70       (defsubst parse-time-string-chars (char)
71         (and (< char (length parse-time-syntax))
72              (aref parse-time-syntax char)))
73     (defsubst parse-time-string-chars (char)
74       (save-match-data
75         (let (case-fold-search str)
76           (cond ((eq char ?+) 1)
77                 ((eq char ?-) -1)
78                 ((eq char ?:) ?d)
79                 ((string-match "[[:upper:]]" (setq str (string char))) ?A)
80                 ((string-match "[[:lower:]]" str) ?a)
81                 ((string-match "[[:digit:]]" str) ?0)))))))
82
83 (put 'parse-error 'error-conditions '(parse-error error))
84 (put 'parse-error 'error-message "Parsing error")
85
86 (defsubst parse-integer (string &optional start end)
87   "[CL] Parse and return the integer in STRING, or nil if none."
88   (let ((integer 0)
89         (digit 0)
90         (index (or start 0))
91         (end (or end (length string))))
92     (when (< index end)
93       (let ((sign (aref string index)))
94         (if (or (eq sign ?+) (eq sign ?-))
95             (setq sign (parse-time-string-chars sign)
96                   index (1+ index))
97           (setq sign 1))
98         (while (and (< index end)
99                     (setq digit (digit-char-p (aref string index))))
100           (setq integer (+ (* integer 10) digit)
101                 index (1+ index)))
102         (if (/= index end)
103             (signal 'parse-error `("not an integer"
104                                    ,(substring string (or start 0) end)))
105           (* sign integer))))))
106
107 (defun parse-time-tokenize (string)
108   "Tokenize STRING into substrings."
109   (let ((start nil)
110         (end (length string))
111         (all-digits nil)
112         (list ())
113         (index 0)
114         (c nil))
115     (while (< index end)
116       (while (and (< index end)         ;skip invalid characters
117                   (not (setq c (parse-time-string-chars (aref string index)))))
118         (incf index))
119       (setq start index all-digits (eq c ?0))
120       (while (and (< (incf index) end)  ;scan valid characters
121                   (setq c (parse-time-string-chars (aref string index))))
122         (setq all-digits (and all-digits (eq c ?0))))
123       (if (<= index end)
124           (push (if all-digits (parse-integer string start index)
125                   (substring string start index))
126                 list)))
127     (nreverse list)))
128
129 (defvar parse-time-months '(("jan" . 1) ("feb" . 2) ("mar" . 3)
130                             ("apr" . 4) ("may" . 5) ("jun" . 6)
131                             ("jul" . 7) ("aug" . 8) ("sep" . 9)
132                             ("oct" . 10) ("nov" . 11) ("dec" . 12)
133                             ("january" . 1) ("february" . 2)
134                             ("march" . 3) ("april" . 4) ("june" . 6)
135                             ("july" . 7) ("august" . 8)
136                             ("september" . 9) ("october" . 10)
137                             ("november" . 11) ("december" . 12)))
138 (defvar parse-time-weekdays '(("sun" . 0) ("mon" . 1) ("tue" . 2)
139                               ("wed" . 3) ("thu" . 4) ("fri" . 5)
140                               ("sat" . 6) ("sunday" . 0) ("monday" . 1)
141                               ("tuesday" . 2) ("wednesday" . 3)
142                               ("thursday" . 4) ("friday" . 5)
143                               ("saturday" . 6)))
144 (defvar parse-time-zoneinfo `(("z" 0) ("ut" 0) ("gmt" 0)
145                               ("pst" ,(* -8 3600)) ("pdt" ,(* -7 3600) t)
146                               ("mst" ,(* -7 3600)) ("mdt" ,(* -6 3600) t)
147                               ("cst" ,(* -6 3600)) ("cdt" ,(* -5 3600) t)
148                               ("est" ,(* -5 3600)) ("edt" ,(* -4 3600) t))
149   "(zoneinfo seconds-off daylight-savings-time-p)")
150
151 (defvar parse-time-rules
152   `(((6) parse-time-weekdays)
153     ((3) (1 31))
154     ((4) parse-time-months)
155     ((5) (100 4038))
156     ((2 1 0)
157      ,#'(lambda () (and (stringp parse-time-elt)
158                         (= (length parse-time-elt) 8)
159                         (= (aref parse-time-elt 2) ?:)
160                         (= (aref parse-time-elt 5) ?:)))
161      [0 2] [3 5] [6 8])
162     ((8 7) parse-time-zoneinfo
163      ,#'(lambda () (car parse-time-val))
164      ,#'(lambda () (cadr parse-time-val)))
165     ((8)
166      ,#'(lambda ()
167           (and (stringp parse-time-elt)
168                (= 5 (length parse-time-elt))
169                (or (= (aref parse-time-elt 0) ?+)
170                    (= (aref parse-time-elt 0) ?-))))
171      ,#'(lambda () (* 60 (+ (parse-integer parse-time-elt 3 5)
172                             (* 60 (parse-integer parse-time-elt 1 3)))
173                       (if (= (aref parse-time-elt 0) ?-) -1 1))))
174     ((5 4 3)
175      ,#'(lambda () (and (stringp parse-time-elt)
176                         (= (length parse-time-elt) 10)
177                         (= (aref parse-time-elt 4) ?-)
178                         (= (aref parse-time-elt 7) ?-)))
179      [0 4] [5 7] [8 10])
180     ((2 1 0)
181      ,#'(lambda () (and (stringp parse-time-elt)
182                         (= (length parse-time-elt) 5)
183                         (= (aref parse-time-elt 2) ?:)))
184      [0 2] [3 5] ,#'(lambda () 0))
185     ((2 1 0)
186      ,#'(lambda () (and (stringp parse-time-elt)
187                         (= (length parse-time-elt) 4)
188                         (= (aref parse-time-elt 1) ?:)))
189      [0 1] [2 4] ,#'(lambda () 0))
190     ((2 1 0)
191      ,#'(lambda () (and (stringp parse-time-elt)
192                         (= (length parse-time-elt) 7)
193                         (= (aref parse-time-elt 1) ?:)))
194      [0 1] [2 4] [5 7])
195     ((5) (50 110) ,#'(lambda () (+ 1900 parse-time-elt)))
196     ((5) (0 49) ,#'(lambda () (+ 2000 parse-time-elt))))
197   "(slots predicate extractor...)")
198 ;;;###autoload(put 'parse-time-rules 'risky-local-variable t)
199
200 ;;;###autoload
201 (defun parse-time-string (string)
202   "Parse the time-string STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ).
203 The values are identical to those of `decode-time', but any values that are
204 unknown are returned as nil."
205   (let ((time (list nil nil nil nil nil nil nil nil nil))
206         (temp (parse-time-tokenize (downcase string))))
207     (while temp
208       (let ((parse-time-elt (pop temp))
209             (rules parse-time-rules)
210             (exit nil))
211         (while (and rules (not exit))
212           (let* ((rule (pop rules))
213                  (slots (pop rule))
214                  (predicate (pop rule))
215                  (parse-time-val))
216             (when (and (not (nth (car slots) time)) ;not already set
217                        (setq parse-time-val (cond ((and (consp predicate)
218                                              (not (eq (car predicate)
219                                                       'lambda)))
220                                         (and (numberp parse-time-elt)
221                                              (<= (car predicate) parse-time-elt)
222                                              (<= parse-time-elt (cadr predicate))
223                                              parse-time-elt))
224                                        ((symbolp predicate)
225                                         (cdr (assoc parse-time-elt
226                                                     (symbol-value predicate))))
227                                        ((funcall predicate)))))
228               (setq exit t)
229               (while slots
230                 (let ((new-val (and rule
231                                     (let ((this (pop rule)))
232                                       (if (vectorp this)
233                                           (parse-integer
234                                            parse-time-elt
235                                            (aref this 0) (aref this 1))
236                                         (funcall this))))))
237                   (rplaca (nthcdr (pop slots) time)
238                           (or new-val parse-time-val)))))))))
239     time))
240
241 (provide 'parse-time)
242
243 ;;; parse-time.el ends here