1 ;;; parse-time.el --- Parsing time strings
3 ;; Copyright (C) 1996, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 ;; Free Software Foundation, Inc.
6 ;; Author: Erik Naggum <erik@naggum.no>
9 ;; This file is part of GNU Emacs.
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, or (at your option)
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; With the introduction of the `encode-time', `decode-time', and
29 ;; `format-time-string' functions, dealing with time became simpler in
30 ;; Emacs. However, parsing time strings is still largely a matter of
31 ;; heuristics and no common interface has been designed.
33 ;; `parse-time-string' parses a time in a string and returns a list of 9
34 ;; values, just like `decode-time', where unspecified elements in the
35 ;; string are returned as nil. `encode-time' may be applied on these
36 ;; values to obtain an internal time value.
40 (eval-when-compile (require 'cl)) ;and ah ain't kiddin' 'bout it
42 (defvar parse-time-syntax (make-vector 256 nil))
43 (defvar parse-time-digits (make-vector 256 nil))
45 ;; Byte-compiler warnings
49 (unless (aref parse-time-digits ?0)
50 (loop for i from ?0 to ?9
51 do (aset parse-time-digits i (- i ?0))))
53 (unless (aref parse-time-syntax ?0)
54 (loop for i from ?0 to ?9
55 do (aset parse-time-syntax i ?0))
56 (loop for i from ?A to ?Z
57 do (aset parse-time-syntax i ?A))
58 (loop for i from ?a to ?z
59 do (aset parse-time-syntax i ?a))
60 (aset parse-time-syntax ?+ 1)
61 (aset parse-time-syntax ?- -1)
62 (aset parse-time-syntax ?: ?d)
65 (defsubst digit-char-p (char)
66 (aref parse-time-digits char))
68 (defsubst parse-time-string-chars (char)
69 (and (< char (length parse-time-syntax))
70 (aref parse-time-syntax char)))
72 (put 'parse-error 'error-conditions '(parse-error error))
73 (put 'parse-error 'error-message "Parsing error")
75 (defsubst parse-integer (string &optional start end)
76 "[CL] Parse and return the integer in STRING, or nil if none."
80 (end (or end (length string))))
82 (let ((sign (aref string index)))
83 (if (or (eq sign ?+) (eq sign ?-))
84 (setq sign (parse-time-string-chars sign)
87 (while (and (< index end)
88 (setq digit (digit-char-p (aref string index))))
89 (setq integer (+ (* integer 10) digit)
92 (signal 'parse-error `("not an integer"
93 ,(substring string (or start 0) end)))
96 (defun parse-time-tokenize (string)
97 "Tokenize STRING into substrings."
105 (while (and (< index end) ;skip invalid characters
106 (not (setq c (parse-time-string-chars (aref string index)))))
108 (setq start index all-digits (eq c ?0))
109 (while (and (< (incf index) end) ;scan valid characters
110 (setq c (parse-time-string-chars (aref string index))))
111 (setq all-digits (and all-digits (eq c ?0))))
113 (push (if all-digits (parse-integer string start index)
114 (substring string start index))
118 (defvar parse-time-months '(("jan" . 1) ("feb" . 2) ("mar" . 3)
119 ("apr" . 4) ("may" . 5) ("jun" . 6)
120 ("jul" . 7) ("aug" . 8) ("sep" . 9)
121 ("oct" . 10) ("nov" . 11) ("dec" . 12)))
122 (defvar parse-time-weekdays '(("sun" . 0) ("mon" . 1) ("tue" . 2)
123 ("wed" . 3) ("thu" . 4) ("fri" . 5) ("sat" . 6)))
124 (defvar parse-time-zoneinfo `(("z" 0) ("ut" 0) ("gmt" 0)
125 ("pst" ,(* -8 3600)) ("pdt" ,(* -7 3600) t)
126 ("mst" ,(* -7 3600)) ("mdt" ,(* -6 3600) t)
127 ("cst" ,(* -6 3600)) ("cdt" ,(* -5 3600) t)
128 ("est" ,(* -5 3600)) ("edt" ,(* -4 3600) t))
129 "(zoneinfo seconds-off daylight-savings-time-p)")
131 (defvar parse-time-rules
132 `(((6) parse-time-weekdays)
134 ((4) parse-time-months)
137 ,#'(lambda () (and (stringp elt)
140 (= (aref elt 5) ?:)))
142 ((8 7) parse-time-zoneinfo
143 ,#'(lambda () (car val))
144 ,#'(lambda () (cadr val)))
149 (or (= (aref elt 0) ?+) (= (aref elt 0) ?-))))
150 ,#'(lambda () (* 60 (+ (parse-integer elt 3 5)
151 (* 60 (parse-integer elt 1 3)))
152 (if (= (aref elt 0) ?-) -1 1))))
154 ,#'(lambda () (and (stringp elt)
157 (= (aref elt 7) ?-)))
160 ,#'(lambda () (and (stringp elt) (= (length elt) 5) (= (aref elt 2) ?:)))
161 [0 2] [3 5] ,#'(lambda () 0))
163 ,#'(lambda () (and (stringp elt)
165 (= (aref elt 1) ?:)))
166 [0 1] [2 4] ,#'(lambda () 0))
168 ,#'(lambda () (and (stringp elt)
170 (= (aref elt 1) ?:)))
172 ((5) (50 110) ,#'(lambda () (+ 1900 elt)))
173 ((5) (0 49) ,#'(lambda () (+ 2000 elt))))
174 "(slots predicate extractor...)")
176 (defun parse-time-string (string)
177 "Parse the time-string STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ).
178 The values are identical to those of `decode-time', but any values that are
179 unknown are returned as nil."
180 (let ((time (list nil nil nil nil nil nil nil nil nil))
181 (temp (parse-time-tokenize (downcase string))))
183 (let ((elt (pop temp))
184 (rules parse-time-rules)
186 (while (and (not (null rules)) (not exit))
187 (let* ((rule (pop rules))
189 (predicate (pop rule))
191 (when (and (not (nth (car slots) time)) ;not already set
192 (setq val (cond ((and (consp predicate)
193 (not (eq (car predicate)
196 (<= (car predicate) elt)
197 (<= elt (cadr predicate))
201 (symbol-value predicate))))
202 ((funcall predicate)))))
205 (let ((new-val (and rule
206 (let ((this (pop rule)))
209 elt (aref this 0) (aref this 1))
211 (rplaca (nthcdr (pop slots) time) (or new-val val)))))))))
214 (provide 'parse-time)
216 ;;; arch-tag: 07066094-45a8-4c68-b307-86195e2c1103
217 ;;; parse-time.el ends here