59530dc177b03c1caa7a8be25f75083bc9664cfb
[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  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 (defvar parse-time-syntax (make-vector 256 nil))
41 (defvar parse-time-digits (make-vector 256 nil))
42
43 ;; Byte-compiler warnings
44 (defvar parse-time-elt)
45 (defvar parse-time-val)
46
47 (unless (aref parse-time-digits ?0)
48   (loop for i from ?0 to ?9
49     do (aset parse-time-digits i (- i ?0))))
50
51 (unless (aref parse-time-syntax ?0)
52   (loop for i from ?0 to ?9
53     do (aset parse-time-syntax i ?0))
54   (loop for i from ?A to ?Z
55     do (aset parse-time-syntax i ?A))
56   (loop for i from ?a to ?z
57     do (aset parse-time-syntax i ?a))
58   (aset parse-time-syntax ?+ 1)
59   (aset parse-time-syntax ?- -1)
60   (aset parse-time-syntax ?: ?d)
61   )
62
63 (defsubst digit-char-p (char)
64   (aref parse-time-digits char))
65
66 (defsubst parse-time-string-chars (char)
67   (and (< char (length parse-time-syntax))
68        (aref parse-time-syntax char)))
69
70 (put 'parse-error 'error-conditions '(parse-error error))
71 (put 'parse-error 'error-message "Parsing error")
72
73 (defsubst parse-integer (string &optional start end)
74   "[CL] Parse and return the integer in STRING, or nil if none."
75   (let ((integer 0)
76         (digit 0)
77         (index (or start 0))
78         (end (or end (length string))))
79     (when (< index end)
80       (let ((sign (aref string index)))
81         (if (or (eq sign ?+) (eq sign ?-))
82             (setq sign (parse-time-string-chars sign)
83                   index (1+ index))
84           (setq sign 1))
85         (while (and (< index end)
86                     (setq digit (digit-char-p (aref string index))))
87           (setq integer (+ (* integer 10) digit)
88                 index (1+ index)))
89         (if (/= index end)
90             (signal 'parse-error `("not an integer"
91                                    ,(substring string (or start 0) end)))
92           (* sign integer))))))
93
94 (defun parse-time-tokenize (string)
95   "Tokenize STRING into substrings."
96   (let ((start nil)
97         (end (length string))
98         (all-digits nil)
99         (list ())
100         (index 0)
101         (c nil))
102     (while (< index end)
103       (while (and (< index end)         ;skip invalid characters
104                   (not (setq c (parse-time-string-chars (aref string index)))))
105         (incf index))
106       (setq start index all-digits (eq c ?0))
107       (while (and (< (incf index) end)  ;scan valid characters
108                   (setq c (parse-time-string-chars (aref string index))))
109         (setq all-digits (and all-digits (eq c ?0))))
110       (if (<= index end)
111           (push (if all-digits (parse-integer string start index)
112                   (substring string start index))
113                 list)))
114     (nreverse list)))
115
116 (defvar parse-time-months '(("jan" . 1) ("feb" . 2) ("mar" . 3)
117                             ("apr" . 4) ("may" . 5) ("jun" . 6)
118                             ("jul" . 7) ("aug" . 8) ("sep" . 9)
119                             ("oct" . 10) ("nov" . 11) ("dec" . 12)
120                             ("january" . 1) ("february" . 2)
121                             ("march" . 3) ("april" . 4) ("june" . 6)
122                             ("july" . 7) ("august" . 8)
123                             ("september" . 9) ("october" . 10)
124                             ("november" . 11) ("december" . 12)))
125 (defvar parse-time-weekdays '(("sun" . 0) ("mon" . 1) ("tue" . 2)
126                               ("wed" . 3) ("thu" . 4) ("fri" . 5)
127                               ("sat" . 6) ("sunday" . 0) ("monday" . 1)
128                               ("tuesday" . 2) ("wednesday" . 3)
129                               ("thursday" . 4) ("friday" . 5)
130                               ("saturday" . 6)))
131 (defvar parse-time-zoneinfo `(("z" 0) ("ut" 0) ("gmt" 0)
132                               ("pst" ,(* -8 3600)) ("pdt" ,(* -7 3600) t)
133                               ("mst" ,(* -7 3600)) ("mdt" ,(* -6 3600) t)
134                               ("cst" ,(* -6 3600)) ("cdt" ,(* -5 3600) t)
135                               ("est" ,(* -5 3600)) ("edt" ,(* -4 3600) t))
136   "(zoneinfo seconds-off daylight-savings-time-p)")
137
138 (defvar parse-time-rules
139   `(((6) parse-time-weekdays)
140     ((3) (1 31))
141     ((4) parse-time-months)
142     ((5) (100 4038))
143     ((2 1 0)
144      ,#'(lambda () (and (stringp parse-time-elt)
145                         (= (length parse-time-elt) 8)
146                         (= (aref parse-time-elt 2) ?:)
147                         (= (aref parse-time-elt 5) ?:)))
148      [0 2] [3 5] [6 8])
149     ((8 7) parse-time-zoneinfo
150      ,#'(lambda () (car parse-time-val))
151      ,#'(lambda () (cadr parse-time-val)))
152     ((8)
153      ,#'(lambda ()
154           (and (stringp parse-time-elt)
155                (= 5 (length parse-time-elt))
156                (or (= (aref parse-time-elt 0) ?+)
157                    (= (aref parse-time-elt 0) ?-))))
158      ,#'(lambda () (* 60 (+ (parse-integer parse-time-elt 3 5)
159                             (* 60 (parse-integer parse-time-elt 1 3)))
160                       (if (= (aref parse-time-elt 0) ?-) -1 1))))
161     ((5 4 3)
162      ,#'(lambda () (and (stringp parse-time-elt)
163                         (= (length parse-time-elt) 10)
164                         (= (aref parse-time-elt 4) ?-)
165                         (= (aref parse-time-elt 7) ?-)))
166      [0 4] [5 7] [8 10])
167     ((2 1 0)
168      ,#'(lambda () (and (stringp parse-time-elt)
169                         (= (length parse-time-elt) 5)
170                         (= (aref parse-time-elt 2) ?:)))
171      [0 2] [3 5] ,#'(lambda () 0))
172     ((2 1 0)
173      ,#'(lambda () (and (stringp parse-time-elt)
174                         (= (length parse-time-elt) 4)
175                         (= (aref parse-time-elt 1) ?:)))
176      [0 1] [2 4] ,#'(lambda () 0))
177     ((2 1 0)
178      ,#'(lambda () (and (stringp parse-time-elt)
179                         (= (length parse-time-elt) 7)
180                         (= (aref parse-time-elt 1) ?:)))
181      [0 1] [2 4] [5 7])
182     ((5) (50 110) ,#'(lambda () (+ 1900 parse-time-elt)))
183     ((5) (0 49) ,#'(lambda () (+ 2000 parse-time-elt))))
184   "(slots predicate extractor...)")
185
186 ;;;###autoload
187 (defun parse-time-string (string)
188   "Parse the time-string STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ).
189 The values are identical to those of `decode-time', but any values that are
190 unknown are returned as nil."
191   (let ((time (list nil nil nil nil nil nil nil nil nil))
192         (temp (parse-time-tokenize (downcase string))))
193     (while temp
194       (let ((parse-time-elt (pop temp))
195             (rules parse-time-rules)
196             (exit nil))
197         (while (and rules (not exit))
198           (let* ((rule (pop rules))
199                  (slots (pop rule))
200                  (predicate (pop rule))
201                  (parse-time-val))
202             (when (and (not (nth (car slots) time)) ;not already set
203                        (setq parse-time-val (cond ((and (consp predicate)
204                                              (not (eq (car predicate)
205                                                       'lambda)))
206                                         (and (numberp parse-time-elt)
207                                              (<= (car predicate) parse-time-elt)
208                                              (<= parse-time-elt (cadr predicate))
209                                              parse-time-elt))
210                                        ((symbolp predicate)
211                                         (cdr (assoc parse-time-elt
212                                                     (symbol-value predicate))))
213                                        ((funcall predicate)))))
214               (setq exit t)
215               (while slots
216                 (let ((new-val (and rule
217                                     (let ((this (pop rule)))
218                                       (if (vectorp this)
219                                           (parse-integer
220                                            parse-time-elt
221                                            (aref this 0) (aref this 1))
222                                         (funcall this))))))
223                   (rplaca (nthcdr (pop slots) time)
224                           (or new-val parse-time-val)))))))))
225     time))
226
227 (provide 'parse-time)
228
229 ;; arch-tag: 07066094-45a8-4c68-b307-86195e2c1103
230 ;;; parse-time.el ends here