1 ;;; parse-time.el --- parsing time strings
3 ;; Copyright (C) 1996, 2000-2015 Free Software Foundation, Inc.
5 ;; Author: Erik Naggum <erik@naggum.no>
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/>.
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.
39 ;; Byte-compiler warnings
40 (defvar parse-time-elt
)
41 (defvar parse-time-val
)
43 (defsubst parse-time-string-chars
(char)
45 (let (case-fold-search str
)
46 (cond ((eq char ?
+) 1)
49 ((string-match "[[:upper:]]" (setq str
(string char
))) ?A
)
50 ((string-match "[[:lower:]]" str
) ?a
)
51 ((string-match "[[:digit:]]" str
) ?
0)))))
53 (defun parse-time-tokenize (string)
54 "Tokenize STRING into substrings."
62 (while (and (< index end
) ;Skip invalid characters.
63 (not (setq c
(parse-time-string-chars (aref string index
)))))
65 (setq start index all-digits
(eq c ?
0))
66 (while (and (< (cl-incf index
) end
) ;Scan valid characters.
67 (setq c
(parse-time-string-chars (aref string index
))))
68 (setq all-digits
(and all-digits
(eq c ?
0))))
70 (push (if all-digits
(cl-parse-integer string
:start start
:end index
)
71 (substring string start index
))
75 (defvar parse-time-months
'(("jan" .
1) ("feb" .
2) ("mar" .
3)
76 ("apr" .
4) ("may" .
5) ("jun" .
6)
77 ("jul" .
7) ("aug" .
8) ("sep" .
9)
78 ("oct" .
10) ("nov" .
11) ("dec" .
12)
79 ("january" .
1) ("february" .
2)
80 ("march" .
3) ("april" .
4) ("june" .
6)
81 ("july" .
7) ("august" .
8)
82 ("september" .
9) ("october" .
10)
83 ("november" .
11) ("december" .
12)))
84 (defvar parse-time-weekdays
'(("sun" .
0) ("mon" .
1) ("tue" .
2)
85 ("wed" .
3) ("thu" .
4) ("fri" .
5)
86 ("sat" .
6) ("sunday" .
0) ("monday" .
1)
87 ("tuesday" .
2) ("wednesday" .
3)
88 ("thursday" .
4) ("friday" .
5)
90 (defvar parse-time-zoneinfo
`(("z" 0) ("ut" 0) ("gmt" 0)
91 ("pst" ,(* -
8 3600)) ("pdt" ,(* -
7 3600) t
)
92 ("mst" ,(* -
7 3600)) ("mdt" ,(* -
6 3600) t
)
93 ("cst" ,(* -
6 3600)) ("cdt" ,(* -
5 3600) t
)
94 ("est" ,(* -
5 3600)) ("edt" ,(* -
4 3600) t
))
95 "(zoneinfo seconds-off daylight-savings-time-p)")
97 (defvar parse-time-rules
98 `(((6) parse-time-weekdays
)
100 ((4) parse-time-months
)
101 ((5) (100 ,most-positive-fixnum
))
103 ,#'(lambda () (and (stringp parse-time-elt
)
104 (= (length parse-time-elt
) 8)
105 (= (aref parse-time-elt
2) ?
:)
106 (= (aref parse-time-elt
5) ?
:)))
108 ((8 7) parse-time-zoneinfo
109 ,#'(lambda () (car parse-time-val
))
110 ,#'(lambda () (cadr parse-time-val
)))
113 (and (stringp parse-time-elt
)
114 (= 5 (length parse-time-elt
))
115 (or (= (aref parse-time-elt
0) ?
+)
116 (= (aref parse-time-elt
0) ?-
))))
117 ,#'(lambda () (* 60 (+ (cl-parse-integer parse-time-elt
:start
3 :end
5)
118 (* 60 (cl-parse-integer parse-time-elt
:start
1 :end
3)))
119 (if (= (aref parse-time-elt
0) ?-
) -
1 1))))
121 ,#'(lambda () (and (stringp parse-time-elt
)
122 (= (length parse-time-elt
) 10)
123 (= (aref parse-time-elt
4) ?-
)
124 (= (aref parse-time-elt
7) ?-
)))
127 ,#'(lambda () (and (stringp parse-time-elt
)
128 (= (length parse-time-elt
) 5)
129 (= (aref parse-time-elt
2) ?
:)))
130 [0 2] [3 5] ,#'(lambda () 0))
132 ,#'(lambda () (and (stringp parse-time-elt
)
133 (= (length parse-time-elt
) 4)
134 (= (aref parse-time-elt
1) ?
:)))
135 [0 1] [2 4] ,#'(lambda () 0))
137 ,#'(lambda () (and (stringp parse-time-elt
)
138 (= (length parse-time-elt
) 7)
139 (= (aref parse-time-elt
1) ?
:)))
141 ((5) (50 110) ,#'(lambda () (+ 1900 parse-time-elt
)))
142 ((5) (0 49) ,#'(lambda () (+ 2000 parse-time-elt
))))
143 "(slots predicate extractor...)")
144 ;;;###autoload(put 'parse-time-rules 'risky-local-variable t)
147 (defun parse-time-string (string)
148 "Parse the time-string STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ).
149 The values are identical to those of `decode-time', but any values that are
150 unknown are returned as nil."
151 (let ((time (list nil nil nil nil nil nil nil nil nil
))
152 (temp (parse-time-tokenize (downcase string
))))
154 (let ((parse-time-elt (pop temp
))
155 (rules parse-time-rules
)
157 (while (and rules
(not exit
))
158 (let* ((rule (pop rules
))
160 (predicate (pop rule
))
162 (when (and (not (nth (car slots
) time
)) ;not already set
164 (cond ((and (consp predicate
)
165 (not (eq (car predicate
)
167 (and (numberp parse-time-elt
)
168 (<= (car predicate
) parse-time-elt
)
169 (<= parse-time-elt
(cadr predicate
))
172 (cdr (assoc parse-time-elt
173 (symbol-value predicate
))))
174 ((funcall predicate
)))))
177 (let ((new-val (if rule
178 (let ((this (pop rule
)))
186 (rplaca (nthcdr (pop slots
) time
) new-val
))))))))
189 (defconst parse-time-iso8601-regexp
192 (4digit "\\([0-9][0-9][0-9][0-9]\\)")
193 (2digit "\\([0-9][0-9]\\)")
194 (date-fullyear 4digit
)
200 (time-secfrac "\\(\\.[0-9]+\\)?")
201 (time-numoffset (concat "[-+]\\(" time-hour
"\\):" time-minute
))
202 (time-offset (concat "Z" time-numoffset
))
203 (partial-time (concat time-hour colon time-minute colon time-second
205 (full-date (concat date-fullyear dash date-month dash date-mday
))
206 (full-time (concat partial-time time-offset
))
207 (date-time (concat full-date
"T" full-time
)))
208 (list (concat "^" full-date
)
209 (concat "T" partial-time
)
210 (concat "Z" time-numoffset
)))
211 "List of regular expressions matching ISO 8601 dates.
212 1st regular expression matches the date.
213 2nd regular expression matches the time.
214 3rd regular expression matches the (optional) timezone specification.")
216 (defun parse-iso8601-time-string (date-string)
217 (let* ((date-re (nth 0 parse-time-iso8601-regexp
))
218 (time-re (nth 1 parse-time-iso8601-regexp
))
219 (tz-re (nth 2 parse-time-iso8601-regexp
))
221 time seconds minute hour fractional-seconds
222 day month year day-of-week dst tz
)
223 ;; We need to populate 'time' with
224 ;; (SEC MIN HOUR DAY MON YEAR DOW DST TZ)
226 ;; Nobody else handles iso8601 correctly, let's do it ourselves.
227 (when (string-match date-re date-string re-start
)
228 (setq year
(string-to-number (match-string 1 date-string
))
229 month
(string-to-number (match-string 2 date-string
))
230 day
(string-to-number (match-string 3 date-string
))
231 re-start
(match-end 0))
232 (when (string-match time-re date-string re-start
)
233 (setq hour
(string-to-number (match-string 1 date-string
))
234 minute
(string-to-number (match-string 2 date-string
))
235 seconds
(string-to-number (match-string 3 date-string
))
236 fractional-seconds
(string-to-number (or
237 (match-string 4 date-string
)
239 re-start
(match-end 0))
240 (when (string-match tz-re date-string re-start
)
241 (setq tz
(match-string 1 date-string
)))
242 (setq time
(list seconds minute hour day month year day-of-week dst tz
))))
244 ;; Fall back to having Gnus do fancy things for us.
246 (setq time
(parse-time-string date-string
)))
249 (apply 'encode-time time
))))
251 (provide 'parse-time
)
253 ;;; parse-time.el ends here