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