Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / calendar / parse-time.el
blob79569f2142c7193ce5a79505bf1ec41251bfb36e
1 ;;; parse-time.el --- parsing time strings
3 ;; Copyright (C) 1996, 2000-2014 Free Software Foundation, Inc.
5 ;; Author: Erik Naggum <erik@naggum.no>
6 ;; Keywords: util
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
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.
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
33 ;; values to obtain an internal time value.
35 ;;; Code:
37 (eval-when-compile (require 'cl-lib))
39 (defvar parse-time-digits (make-vector 256 nil))
41 ;; Byte-compiler warnings
42 (defvar parse-time-elt)
43 (defvar parse-time-val)
45 (unless (aref parse-time-digits ?0)
46 (cl-loop for i from ?0 to ?9
47 do (aset parse-time-digits i (- i ?0))))
49 (defsubst digit-char-p (char)
50 (aref parse-time-digits char))
52 (defsubst parse-time-string-chars (char)
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)))))
62 (put 'parse-error 'error-conditions '(parse-error error))
63 (put 'parse-error 'error-message "Parsing error")
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)
82 (signal 'parse-error `("not an integer"
83 ,(substring string (or start 0) end)))
84 (* sign integer))))))
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 (cl-incf index))
98 (setq start index all-digits (eq c ?0))
99 (while (and (< (cl-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)))
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)
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)))
117 (defvar parse-time-weekdays '(("sun" . 0) ("mon" . 1) ("tue" . 2)
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)))
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))
128 "(zoneinfo seconds-off daylight-savings-time-p)")
130 (defvar parse-time-rules
131 `(((6) parse-time-weekdays)
132 ((3) (1 31))
133 ((4) parse-time-months)
134 ((5) (100 4038))
135 ((2 1 0)
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) ?:)))
140 [0 2] [3 5] [6 8])
141 ((8 7) parse-time-zoneinfo
142 ,#'(lambda () (car parse-time-val))
143 ,#'(lambda () (cadr parse-time-val)))
144 ((8)
145 ,#'(lambda ()
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))))
153 ((5 4 3)
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) ?-)))
158 [0 4] [5 7] [8 10])
159 ((2 1 0)
160 ,#'(lambda () (and (stringp parse-time-elt)
161 (= (length parse-time-elt) 5)
162 (= (aref parse-time-elt 2) ?:)))
163 [0 2] [3 5] ,#'(lambda () 0))
164 ((2 1 0)
165 ,#'(lambda () (and (stringp parse-time-elt)
166 (= (length parse-time-elt) 4)
167 (= (aref parse-time-elt 1) ?:)))
168 [0 1] [2 4] ,#'(lambda () 0))
169 ((2 1 0)
170 ,#'(lambda () (and (stringp parse-time-elt)
171 (= (length parse-time-elt) 7)
172 (= (aref parse-time-elt 1) ?:)))
173 [0 1] [2 4] [5 7])
174 ((5) (50 110) ,#'(lambda () (+ 1900 parse-time-elt)))
175 ((5) (0 49) ,#'(lambda () (+ 2000 parse-time-elt))))
176 "(slots predicate extractor...)")
177 ;;;###autoload(put 'parse-time-rules 'risky-local-variable t)
179 ;;;###autoload
180 (defun parse-time-string (string)
181 "Parse the time-string STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ).
182 The values are identical to those of `decode-time', but any values that are
183 unknown are returned as nil."
184 (let ((time (list nil nil nil nil nil nil nil nil nil))
185 (temp (parse-time-tokenize (downcase string))))
186 (while temp
187 (let ((parse-time-elt (pop temp))
188 (rules parse-time-rules)
189 (exit nil))
190 (while (and rules (not exit))
191 (let* ((rule (pop rules))
192 (slots (pop rule))
193 (predicate (pop rule))
194 (parse-time-val))
195 (when (and (not (nth (car slots) time)) ;not already set
196 (setq parse-time-val
197 (cond ((and (consp predicate)
198 (not (eq (car predicate)
199 'lambda)))
200 (and (numberp parse-time-elt)
201 (<= (car predicate) parse-time-elt)
202 (<= parse-time-elt (cadr predicate))
203 parse-time-elt))
204 ((symbolp predicate)
205 (cdr (assoc parse-time-elt
206 (symbol-value predicate))))
207 ((funcall predicate)))))
208 (setq exit t)
209 (while slots
210 (let ((new-val (if rule
211 (let ((this (pop rule)))
212 (if (vectorp this)
213 (parse-integer
214 parse-time-elt
215 (aref this 0) (aref this 1))
216 (funcall this)))
217 parse-time-val)))
218 (rplaca (nthcdr (pop slots) time) new-val))))))))
219 time))
221 (provide 'parse-time)
223 ;;; parse-time.el ends here