(mml2015-epg-sign): Save the signing keys in
[gnus] / lisp / parse-time.el
1 ;;; parse-time.el --- Parsing time strings
2
3 ;; Copyright (C) 1996, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 ;;   Free Software Foundation, Inc.
5
6 ;; Author: Erik Naggum <erik@naggum.no>
7 ;; Keywords: util
8
9 ;; This file is part of GNU Emacs.
10
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 2, or (at your option)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
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.
32
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.
37
38 ;;; Code:
39
40 (eval-when-compile (require 'cl))       ;and ah ain't kiddin' 'bout it
41
42 (defvar parse-time-syntax (make-vector 256 nil))
43 (defvar parse-time-digits (make-vector 256 nil))
44
45 ;; Byte-compiler warnings
46 (defvar elt)
47 (defvar val)
48
49 (unless (aref parse-time-digits ?0)
50   (loop for i from ?0 to ?9
51     do (aset parse-time-digits i (- i ?0))))
52
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)
63   )
64
65 (defsubst digit-char-p (char)
66   (aref parse-time-digits char))
67
68 (defsubst parse-time-string-chars (char)
69   (aref parse-time-syntax char))
70
71 (put 'parse-error 'error-conditions '(parse-error error))
72 (put 'parse-error 'error-message "Parsing error")
73
74 (defsubst parse-integer (string &optional start end)
75   "[CL] Parse and return the integer in STRING, or nil if none."
76   (let ((integer 0)
77         (digit 0)
78         (index (or start 0))
79         (end (or end (length string))))
80     (when (< index end)
81       (let ((sign (aref string index)))
82         (if (or (eq sign ?+) (eq sign ?-))
83             (setq sign (parse-time-string-chars sign)
84                   index (1+ index))
85           (setq sign 1))
86         (while (and (< index end)
87                     (setq digit (digit-char-p (aref string index))))
88           (setq integer (+ (* integer 10) digit)
89                 index (1+ index)))
90         (if (/= index end)
91             (signal 'parse-error `("not an integer"
92                                    ,(substring string (or start 0) end)))
93           (* sign integer))))))
94
95 (defun parse-time-tokenize (string)
96   "Tokenize STRING into substrings."
97   (let ((start nil)
98         (end (length string))
99         (all-digits nil)
100         (list ())
101         (index 0)
102         (c nil))
103     (while (< index end)
104       (while (and (< index end)         ;skip invalid characters
105                   (not (setq c (parse-time-string-chars (aref string index)))))
106         (incf index))
107       (setq start index all-digits (eq c ?0))
108       (while (and (< (incf index) end)  ;scan valid characters
109                   (setq c (parse-time-string-chars (aref string index))))
110         (setq all-digits (and all-digits (eq c ?0))))
111       (if (<= index end)
112           (push (if all-digits (parse-integer string start index)
113                   (substring string start index))
114                 list)))
115     (nreverse list)))
116
117 (defvar parse-time-months '(("jan" . 1) ("feb" . 2) ("mar" . 3)
118                             ("apr" . 4) ("may" . 5) ("jun" . 6)
119                             ("jul" . 7) ("aug" . 8) ("sep" . 9)
120                             ("oct" . 10) ("nov" . 11) ("dec" . 12)))
121 (defvar parse-time-weekdays '(("sun" . 0) ("mon" . 1) ("tue" . 2)
122                               ("wed" . 3) ("thu" . 4) ("fri" . 5) ("sat" . 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)")
129
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 elt)
137                         (= (length elt) 8)
138                         (= (aref elt 2) ?:)
139                         (= (aref elt 5) ?:)))
140      [0 2] [3 5] [6 8])
141     ((8 7) parse-time-zoneinfo
142      ,#'(lambda () (car val))
143      ,#'(lambda () (cadr val)))
144     ((8)
145      ,#'(lambda ()
146           (and (stringp elt)
147                (= 5 (length elt))
148                (or (= (aref elt 0) ?+) (= (aref elt 0) ?-))))
149      ,#'(lambda () (* 60 (+ (parse-integer elt 3 5)
150                             (* 60 (parse-integer elt 1 3)))
151                       (if (= (aref elt 0) ?-) -1 1))))
152     ((5 4 3)
153      ,#'(lambda () (and (stringp elt)
154                         (= (length elt) 10)
155                         (= (aref elt 4) ?-)
156                         (= (aref elt 7) ?-)))
157      [0 4] [5 7] [8 10])
158     ((2 1 0)
159      ,#'(lambda () (and (stringp elt) (= (length elt) 5) (= (aref elt 2) ?:)))
160      [0 2] [3 5] ,#'(lambda () 0))
161     ((2 1 0)
162      ,#'(lambda () (and (stringp elt)
163                         (= (length elt) 4)
164                         (= (aref elt 1) ?:)))
165      [0 1] [2 4] ,#'(lambda () 0))
166     ((2 1 0)
167      ,#'(lambda () (and (stringp elt)
168                         (= (length elt) 7)
169                         (= (aref elt 1) ?:)))
170      [0 1] [2 4] [5 7])
171     ((5) (50 110) ,#'(lambda () (+ 1900 elt)))
172     ((5) (0 49) ,#'(lambda () (+ 2000 elt))))
173   "(slots predicate extractor...)")
174
175 (defun parse-time-string (string)
176   "Parse the time-string STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ).
177 The values are identical to those of `decode-time', but any values that are
178 unknown are returned as nil."
179   (let ((time (list nil nil nil nil nil nil nil nil nil))
180         (temp (parse-time-tokenize (downcase string))))
181     (while temp
182       (let ((elt (pop temp))
183             (rules parse-time-rules)
184             (exit nil))
185         (while (and (not (null rules)) (not exit))
186           (let* ((rule (pop rules))
187                  (slots (pop rule))
188                  (predicate (pop rule))
189                  (val))
190             (when (and (not (nth (car slots) time)) ;not already set
191                        (setq val (cond ((and (consp predicate)
192                                              (not (eq (car predicate)
193                                                       'lambda)))
194                                         (and (numberp elt)
195                                              (<= (car predicate) elt)
196                                              (<= elt (cadr predicate))
197                                              elt))
198                                        ((symbolp predicate)
199                                         (cdr (assoc elt
200                                                     (symbol-value predicate))))
201                                        ((funcall predicate)))))
202               (setq exit t)
203               (while slots
204                 (let ((new-val (and rule
205                                     (let ((this (pop rule)))
206                                       (if (vectorp this)
207                                           (parse-integer
208                                            elt (aref this 0) (aref this 1))
209                                         (funcall this))))))
210                   (rplaca (nthcdr (pop slots) time) (or new-val val)))))))))
211     time))
212
213 (provide 'parse-time)
214
215 ;;; arch-tag: 07066094-45a8-4c68-b307-86195e2c1103
216 ;;; parse-time.el ends here