]> code.delx.au - gnu-emacs/blob - lisp/calendar/parse-time.el
Use ‘T *restrict’ proto, not ‘T[restrict]’
[gnu-emacs] / lisp / calendar / parse-time.el
1 ;;; parse-time.el --- parsing time strings
2
3 ;; Copyright (C) 1996, 2000-2016 Free Software Foundation, Inc.
4
5 ;; Author: Erik Naggum <erik@naggum.no>
6 ;; Keywords: util
7
8 ;; This file is part of GNU Emacs.
9
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.
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
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
33 ;; values to obtain an internal time value.
34
35 ;;; Code:
36
37 (require 'cl-lib)
38
39 ;; Byte-compiler warnings
40 (defvar parse-time-elt)
41 (defvar parse-time-val)
42
43 (defsubst parse-time-string-chars (char)
44 (cond ((<= ?a char ?z) ?a)
45 ((<= ?0 char ?9) ?0)
46 ((eq char ?+) 1)
47 ((eq char ?-) -1)
48 ((eq char ?:) ?d)))
49
50 (defun parse-time-tokenize (string)
51 "Tokenize STRING into substrings."
52 (let ((start nil)
53 (end (length string))
54 (all-digits nil)
55 (list ())
56 (index 0)
57 (c nil))
58 (while (< index end)
59 (while (and (< index end) ;Skip invalid characters.
60 (not (setq c (parse-time-string-chars (aref string index)))))
61 (cl-incf index))
62 (setq start index all-digits (eq c ?0))
63 (while (and (< (cl-incf index) end) ;Scan valid characters.
64 (setq c (parse-time-string-chars (aref string index))))
65 (setq all-digits (and all-digits (eq c ?0))))
66 (if (<= index end)
67 (push (if all-digits (cl-parse-integer string :start start :end index)
68 (substring string start index))
69 list)))
70 (nreverse list)))
71
72 (defvar parse-time-months '(("jan" . 1) ("feb" . 2) ("mar" . 3)
73 ("apr" . 4) ("may" . 5) ("jun" . 6)
74 ("jul" . 7) ("aug" . 8) ("sep" . 9)
75 ("oct" . 10) ("nov" . 11) ("dec" . 12)
76 ("january" . 1) ("february" . 2)
77 ("march" . 3) ("april" . 4) ("june" . 6)
78 ("july" . 7) ("august" . 8)
79 ("september" . 9) ("october" . 10)
80 ("november" . 11) ("december" . 12)))
81 (defvar parse-time-weekdays '(("sun" . 0) ("mon" . 1) ("tue" . 2)
82 ("wed" . 3) ("thu" . 4) ("fri" . 5)
83 ("sat" . 6) ("sunday" . 0) ("monday" . 1)
84 ("tuesday" . 2) ("wednesday" . 3)
85 ("thursday" . 4) ("friday" . 5)
86 ("saturday" . 6)))
87 (defvar parse-time-zoneinfo `(("z" 0) ("ut" 0) ("gmt" 0)
88 ("pst" ,(* -8 3600)) ("pdt" ,(* -7 3600) t)
89 ("mst" ,(* -7 3600)) ("mdt" ,(* -6 3600) t)
90 ("cst" ,(* -6 3600)) ("cdt" ,(* -5 3600) t)
91 ("est" ,(* -5 3600)) ("edt" ,(* -4 3600) t))
92 "(zoneinfo seconds-off daylight-savings-time-p)")
93
94 (defvar parse-time-rules
95 `(((6) parse-time-weekdays)
96 ((3) (1 31))
97 ((4) parse-time-months)
98 ((5) (100 ,most-positive-fixnum))
99 ((2 1 0)
100 ,#'(lambda () (and (stringp parse-time-elt)
101 (= (length parse-time-elt) 8)
102 (= (aref parse-time-elt 2) ?:)
103 (= (aref parse-time-elt 5) ?:)))
104 [0 2] [3 5] [6 8])
105 ((8 7) parse-time-zoneinfo
106 ,#'(lambda () (car parse-time-val))
107 ,#'(lambda () (cadr parse-time-val)))
108 ((8)
109 ,#'(lambda ()
110 (and (stringp parse-time-elt)
111 (= 5 (length parse-time-elt))
112 (or (= (aref parse-time-elt 0) ?+)
113 (= (aref parse-time-elt 0) ?-))))
114 ,#'(lambda () (* 60 (+ (cl-parse-integer parse-time-elt :start 3 :end 5)
115 (* 60 (cl-parse-integer parse-time-elt :start 1 :end 3)))
116 (if (= (aref parse-time-elt 0) ?-) -1 1))))
117 ((5 4 3)
118 ,#'(lambda () (and (stringp parse-time-elt)
119 (= (length parse-time-elt) 10)
120 (= (aref parse-time-elt 4) ?-)
121 (= (aref parse-time-elt 7) ?-)))
122 [0 4] [5 7] [8 10])
123 ((2 1 0)
124 ,#'(lambda () (and (stringp parse-time-elt)
125 (= (length parse-time-elt) 5)
126 (= (aref parse-time-elt 2) ?:)))
127 [0 2] [3 5] ,#'(lambda () 0))
128 ((2 1 0)
129 ,#'(lambda () (and (stringp parse-time-elt)
130 (= (length parse-time-elt) 4)
131 (= (aref parse-time-elt 1) ?:)))
132 [0 1] [2 4] ,#'(lambda () 0))
133 ((2 1 0)
134 ,#'(lambda () (and (stringp parse-time-elt)
135 (= (length parse-time-elt) 7)
136 (= (aref parse-time-elt 1) ?:)))
137 [0 1] [2 4] [5 7])
138 ((5) (50 110) ,#'(lambda () (+ 1900 parse-time-elt)))
139 ((5) (0 49) ,#'(lambda () (+ 2000 parse-time-elt))))
140 "(slots predicate extractor...)")
141 ;;;###autoload(put 'parse-time-rules 'risky-local-variable t)
142
143 ;;;###autoload
144 (defun parse-time-string (string)
145 "Parse the time-string STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ).
146 The values are identical to those of `decode-time', but any values that are
147 unknown are returned as nil."
148 (let ((time (list nil nil nil nil nil nil nil nil nil))
149 (temp (parse-time-tokenize (downcase string))))
150 (while temp
151 (let ((parse-time-elt (pop temp))
152 (rules parse-time-rules)
153 (exit nil))
154 (while (and rules (not exit))
155 (let* ((rule (pop rules))
156 (slots (pop rule))
157 (predicate (pop rule))
158 (parse-time-val))
159 (when (and (not (nth (car slots) time)) ;not already set
160 (setq parse-time-val
161 (cond ((and (consp predicate)
162 (not (eq (car predicate)
163 'lambda)))
164 (and (numberp parse-time-elt)
165 (<= (car predicate) parse-time-elt)
166 (<= parse-time-elt (cadr predicate))
167 parse-time-elt))
168 ((symbolp predicate)
169 (cdr (assoc parse-time-elt
170 (symbol-value predicate))))
171 ((funcall predicate)))))
172 (setq exit t)
173 (while slots
174 (let ((new-val (if rule
175 (let ((this (pop rule)))
176 (if (vectorp this)
177 (cl-parse-integer
178 parse-time-elt
179 :start (aref this 0)
180 :end (aref this 1))
181 (funcall this)))
182 parse-time-val)))
183 (rplaca (nthcdr (pop slots) time) new-val))))))))
184 time))
185
186 (defconst parse-time-iso8601-regexp
187 (let* ((dash "-?")
188 (colon ":?")
189 (4digit "\\([0-9][0-9][0-9][0-9]\\)")
190 (2digit "\\([0-9][0-9]\\)")
191 (date-fullyear 4digit)
192 (date-month 2digit)
193 (date-mday 2digit)
194 (time-hour 2digit)
195 (time-minute 2digit)
196 (time-second 2digit)
197 (time-secfrac "\\(\\.[0-9]+\\)?")
198 (time-numoffset (concat "[-+]\\(" time-hour "\\):" time-minute))
199 (time-offset (concat "Z" time-numoffset))
200 (partial-time (concat time-hour colon time-minute colon time-second
201 time-secfrac))
202 (full-date (concat date-fullyear dash date-month dash date-mday))
203 (full-time (concat partial-time time-offset))
204 (date-time (concat full-date "T" full-time)))
205 (list (concat "^" full-date)
206 (concat "T" partial-time)
207 (concat "Z" time-numoffset)))
208 "List of regular expressions matching ISO 8601 dates.
209 1st regular expression matches the date.
210 2nd regular expression matches the time.
211 3rd regular expression matches the (optional) timezone specification.")
212
213 (defun parse-iso8601-time-string (date-string)
214 (let* ((date-re (nth 0 parse-time-iso8601-regexp))
215 (time-re (nth 1 parse-time-iso8601-regexp))
216 (tz-re (nth 2 parse-time-iso8601-regexp))
217 re-start
218 time seconds minute hour fractional-seconds
219 day month year day-of-week dst tz)
220 ;; We need to populate 'time' with
221 ;; (SEC MIN HOUR DAY MON YEAR DOW DST TZ)
222
223 ;; Nobody else handles iso8601 correctly, let's do it ourselves.
224 (when (string-match date-re date-string re-start)
225 (setq year (string-to-number (match-string 1 date-string))
226 month (string-to-number (match-string 2 date-string))
227 day (string-to-number (match-string 3 date-string))
228 re-start (match-end 0))
229 (when (string-match time-re date-string re-start)
230 (setq hour (string-to-number (match-string 1 date-string))
231 minute (string-to-number (match-string 2 date-string))
232 seconds (string-to-number (match-string 3 date-string))
233 fractional-seconds (string-to-number (or
234 (match-string 4 date-string)
235 "0"))
236 re-start (match-end 0))
237 (when (string-match tz-re date-string re-start)
238 (setq tz (match-string 1 date-string)))
239 (setq time (list seconds minute hour day month year day-of-week dst tz))))
240
241 ;; Fall back to having Gnus do fancy things for us.
242 (when (not time)
243 (setq time (parse-time-string date-string)))
244
245 (and time
246 (apply 'encode-time time))))
247
248 (provide 'parse-time)
249
250 ;;; parse-time.el ends here