]> code.delx.au - gnu-emacs/blob - lisp/calendar/parse-time.el
Update copyright year to 2016
[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 (save-match-data
45 (let (case-fold-search str)
46 (cond ((eq char ?+) 1)
47 ((eq char ?-) -1)
48 ((eq char ?:) ?d)
49 ((string-match "[[:upper:]]" (setq str (string char))) ?A)
50 ((string-match "[[:lower:]]" str) ?a)
51 ((string-match "[[:digit:]]" str) ?0)))))
52
53 (defun parse-time-tokenize (string)
54 "Tokenize STRING into substrings."
55 (let ((start nil)
56 (end (length string))
57 (all-digits nil)
58 (list ())
59 (index 0)
60 (c nil))
61 (while (< index end)
62 (while (and (< index end) ;Skip invalid characters.
63 (not (setq c (parse-time-string-chars (aref string index)))))
64 (cl-incf 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))))
69 (if (<= index end)
70 (push (if all-digits (cl-parse-integer string :start start :end index)
71 (substring string start index))
72 list)))
73 (nreverse list)))
74
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)
89 ("saturday" . 6)))
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)")
96
97 (defvar parse-time-rules
98 `(((6) parse-time-weekdays)
99 ((3) (1 31))
100 ((4) parse-time-months)
101 ((5) (100 ,most-positive-fixnum))
102 ((2 1 0)
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) ?:)))
107 [0 2] [3 5] [6 8])
108 ((8 7) parse-time-zoneinfo
109 ,#'(lambda () (car parse-time-val))
110 ,#'(lambda () (cadr parse-time-val)))
111 ((8)
112 ,#'(lambda ()
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))))
120 ((5 4 3)
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) ?-)))
125 [0 4] [5 7] [8 10])
126 ((2 1 0)
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))
131 ((2 1 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))
136 ((2 1 0)
137 ,#'(lambda () (and (stringp parse-time-elt)
138 (= (length parse-time-elt) 7)
139 (= (aref parse-time-elt 1) ?:)))
140 [0 1] [2 4] [5 7])
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)
145
146 ;;;###autoload
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))))
153 (while temp
154 (let ((parse-time-elt (pop temp))
155 (rules parse-time-rules)
156 (exit nil))
157 (while (and rules (not exit))
158 (let* ((rule (pop rules))
159 (slots (pop rule))
160 (predicate (pop rule))
161 (parse-time-val))
162 (when (and (not (nth (car slots) time)) ;not already set
163 (setq parse-time-val
164 (cond ((and (consp predicate)
165 (not (eq (car predicate)
166 'lambda)))
167 (and (numberp parse-time-elt)
168 (<= (car predicate) parse-time-elt)
169 (<= parse-time-elt (cadr predicate))
170 parse-time-elt))
171 ((symbolp predicate)
172 (cdr (assoc parse-time-elt
173 (symbol-value predicate))))
174 ((funcall predicate)))))
175 (setq exit t)
176 (while slots
177 (let ((new-val (if rule
178 (let ((this (pop rule)))
179 (if (vectorp this)
180 (cl-parse-integer
181 parse-time-elt
182 :start (aref this 0)
183 :end (aref this 1))
184 (funcall this)))
185 parse-time-val)))
186 (rplaca (nthcdr (pop slots) time) new-val))))))))
187 time))
188
189 (defconst parse-time-iso8601-regexp
190 (let* ((dash "-?")
191 (colon ":?")
192 (4digit "\\([0-9][0-9][0-9][0-9]\\)")
193 (2digit "\\([0-9][0-9]\\)")
194 (date-fullyear 4digit)
195 (date-month 2digit)
196 (date-mday 2digit)
197 (time-hour 2digit)
198 (time-minute 2digit)
199 (time-second 2digit)
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
204 time-secfrac))
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.")
215
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))
220 re-start
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)
225
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)
238 "0"))
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))))
243
244 ;; Fall back to having Gnus do fancy things for us.
245 (when (not time)
246 (setq time (parse-time-string date-string)))
247
248 (and time
249 (apply 'encode-time time))))
250
251 (provide 'parse-time)
252
253 ;;; parse-time.el ends here