Mercurial > emacs
annotate lisp/gnus/parse-time.el @ 42811:cf0c0ef57504
*** empty log message ***
| author | Jason Rumney <jasonr@gnu.org> |
|---|---|
| date | Thu, 17 Jan 2002 19:29:24 +0000 |
| parents | a26d9b55abb6 |
| children |
| rev | line source |
|---|---|
|
38413
a26d9b55abb6
Some fixes to follow coding conventions in files from Gnus.
Pavel Jan?k <Pavel@Janik.cz>
parents:
31716
diff
changeset
|
1 ;;; parse-time.el --- parsing time strings |
| 17493 | 2 |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
3 ;; Copyright (C) 1996, 2000 by Free Software Foundation, Inc. |
| 17493 | 4 |
|
30316
a0e2fa7d8bb7
Correct author's mail address.
Gerd Moellmann <gerd@gnu.org>
parents:
19489
diff
changeset
|
5 ;; Author: Erik Naggum <erik@naggum.no> |
| 17493 | 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 2, or (at your option) | |
| 13 ;; 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; see the file COPYING. If not, write to | |
| 22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
| 23 ;; Boston, MA 02111-1307, USA. | |
| 24 | |
| 25 ;;; Commentary: | |
| 26 | |
| 27 ;; With the introduction of the `encode-time', `decode-time', and | |
| 28 ;; `format-time-string' functions, dealing with time became simpler in | |
| 29 ;; Emacs. However, parsing time strings is still largely a matter of | |
| 30 ;; heuristics and no common interface has been designed. | |
| 31 | |
| 32 ;; `parse-time-string' parses a time in a string and returns a list of 9 | |
| 33 ;; values, just like `decode-time', where unspecified elements in the | |
| 34 ;; string are returned as nil. `encode-time' may be applied on these | |
| 35 ;; valuse to obtain an internal time value. | |
| 36 | |
| 37 ;;; Code: | |
| 38 | |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
39 (eval-when-compile (require 'cl)) ;and ah ain't kiddin' 'bout it |
| 17493 | 40 |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
41 (defvar parse-time-syntax (make-vector 256 nil)) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
42 (defvar parse-time-digits (make-vector 256 nil)) |
| 17493 | 43 |
| 44 ;; Byte-compiler warnings | |
| 45 (defvar elt) | |
| 46 (defvar val) | |
| 47 | |
| 48 (unless (aref parse-time-digits ?0) | |
| 49 (loop for i from ?0 to ?9 | |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
50 do (aset parse-time-digits i (- i ?0)))) |
| 17493 | 51 |
| 52 (unless (aref parse-time-syntax ?0) | |
| 53 (loop for i from ?0 to ?9 | |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
54 do (aset parse-time-syntax i ?0)) |
| 17493 | 55 (loop for i from ?A to ?Z |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
56 do (aset parse-time-syntax i ?A)) |
| 17493 | 57 (loop for i from ?a to ?z |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
58 do (aset parse-time-syntax i ?a)) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
59 (aset parse-time-syntax ?+ 1) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
60 (aset parse-time-syntax ?- -1) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
61 (aset parse-time-syntax ?: ?d) |
| 17493 | 62 ) |
| 63 | |
| 64 (defsubst digit-char-p (char) | |
| 65 (aref parse-time-digits char)) | |
| 66 | |
| 67 (defsubst parse-time-string-chars (char) | |
| 68 (aref parse-time-syntax char)) | |
| 69 | |
| 70 (put 'parse-error 'error-conditions '(parse-error error)) | |
| 71 (put 'parse-error 'error-message "Parsing error") | |
| 72 | |
| 73 (defsubst parse-integer (string &optional start end) | |
| 74 "[CL] Parse and return the integer in STRING, or nil if none." | |
| 75 (let ((integer 0) | |
| 76 (digit 0) | |
| 77 (index (or start 0)) | |
| 78 (end (or end (length string)))) | |
| 79 (when (< index end) | |
| 80 (let ((sign (aref string index))) | |
| 81 (if (or (eq sign ?+) (eq sign ?-)) | |
| 82 (setq sign (parse-time-string-chars sign) | |
| 83 index (1+ index)) | |
| 84 (setq sign 1)) | |
| 85 (while (and (< index end) | |
| 86 (setq digit (digit-char-p (aref string index)))) | |
| 87 (setq integer (+ (* integer 10) digit) | |
| 88 index (1+ index))) | |
| 89 (if (/= index end) | |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
90 (signal 'parse-error `("not an integer" |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
91 ,(substring string (or start 0) end))) |
| 17493 | 92 (* sign integer)))))) |
| 93 | |
| 94 (defun parse-time-tokenize (string) | |
| 95 "Tokenize STRING into substrings." | |
| 96 (let ((start nil) | |
| 97 (end (length string)) | |
| 98 (all-digits nil) | |
| 99 (list ()) | |
| 100 (index 0) | |
| 101 (c nil)) | |
| 102 (while (< index end) | |
| 103 (while (and (< index end) ;skip invalid characters | |
| 104 (not (setq c (parse-time-string-chars (aref string index))))) | |
| 105 (incf index)) | |
| 106 (setq start index all-digits (eq c ?0)) | |
| 107 (while (and (< (incf index) end) ;scan valid characters | |
| 108 (setq c (parse-time-string-chars (aref string index)))) | |
| 109 (setq all-digits (and all-digits (eq c ?0)))) | |
| 110 (if (<= index end) | |
| 111 (push (if all-digits (parse-integer string start index) | |
| 112 (substring string start index)) | |
| 113 list))) | |
| 114 (nreverse list))) | |
| 115 | |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
116 (defvar parse-time-months '(("jan" . 1) ("feb" . 2) ("mar" . 3) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
117 ("apr" . 4) ("may" . 5) ("jun" . 6) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
118 ("jul" . 7) ("aug" . 8) ("sep" . 9) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
119 ("oct" . 10) ("nov" . 11) ("dec" . 12))) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
120 (defvar parse-time-weekdays '(("sun" . 0) ("mon" . 1) ("tue" . 2) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
121 ("wed" . 3) ("thu" . 4) ("fri" . 5) ("sat" . 6))) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
122 (defvar parse-time-zoneinfo `(("z" 0) ("ut" 0) ("gmt" 0) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
123 ("pst" ,(* -8 3600)) ("pdt" ,(* -7 3600) t) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
124 ("mst" ,(* -7 3600)) ("mdt" ,(* -6 3600) t) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
125 ("cst" ,(* -6 3600)) ("cdt" ,(* -5 3600) t) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
126 ("est" ,(* -5 3600)) ("edt" ,(* -4 3600) t)) |
| 17493 | 127 "(zoneinfo seconds-off daylight-savings-time-p)") |
| 128 | |
| 129 (defvar parse-time-rules | |
| 130 `(((6) parse-time-weekdays) | |
| 131 ((3) (1 31)) | |
| 132 ((4) parse-time-months) | |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
133 ((5) (100 4038)) |
| 17493 | 134 ((2 1 0) |
| 135 ,#'(lambda () (and (stringp elt) | |
| 136 (= (length elt) 8) | |
| 137 (= (aref elt 2) ?:) | |
| 138 (= (aref elt 5) ?:))) | |
| 139 [0 2] [3 5] [6 8]) | |
| 140 ((8 7) parse-time-zoneinfo | |
| 141 ,#'(lambda () (car val)) | |
| 142 ,#'(lambda () (cadr val))) | |
| 143 ((8) | |
| 144 ,#'(lambda () | |
| 145 (and (stringp elt) | |
| 146 (= 5 (length elt)) | |
| 147 (or (= (aref elt 0) ?+) (= (aref elt 0) ?-)))) | |
| 148 ,#'(lambda () (* 60 (+ (parse-integer elt 3 5) | |
| 149 (* 60 (parse-integer elt 1 3))) | |
| 150 (if (= (aref elt 0) ?-) -1 1)))) | |
| 151 ((5 4 3) | |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
152 ,#'(lambda () (and (stringp elt) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
153 (= (length elt) 10) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
154 (= (aref elt 4) ?-) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
155 (= (aref elt 7) ?-))) |
| 17493 | 156 [0 4] [5 7] [8 10]) |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
157 ((2 1 0) |
| 17493 | 158 ,#'(lambda () (and (stringp elt) (= (length elt) 5) (= (aref elt 2) ?:))) |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
159 [0 2] [3 5] ,#'(lambda () 0)) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
160 ((2 1 0) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
161 ,#'(lambda () (and (stringp elt) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
162 (= (length elt) 4) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
163 (= (aref elt 1) ?:))) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
164 [0 1] [2 4] ,#'(lambda () 0)) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
165 ((2 1 0) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
166 ,#'(lambda () (and (stringp elt) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
167 (= (length elt) 7) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
168 (= (aref elt 1) ?:))) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
169 [0 1] [2 4] [5 7]) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
170 ((5) (50 110) ,#'(lambda () (+ 1900 elt))) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
171 ((5) (0 49) ,#'(lambda () (+ 2000 elt)))) |
| 17493 | 172 "(slots predicate extractor...)") |
| 173 | |
| 174 (defun parse-time-string (string) | |
| 175 "Parse the time-string STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ). | |
| 176 The values are identical to those of `decode-time', but any values that are | |
| 177 unknown are returned as nil." | |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
178 (let ((time (list nil nil nil nil nil nil nil nil nil)) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
179 (temp (parse-time-tokenize (downcase string)))) |
| 17493 | 180 (while temp |
| 181 (let ((elt (pop temp)) | |
| 182 (rules parse-time-rules) | |
| 183 (exit nil)) | |
| 184 (while (and (not (null rules)) (not exit)) | |
| 185 (let* ((rule (pop rules)) | |
| 186 (slots (pop rule)) | |
| 187 (predicate (pop rule)) | |
| 188 (val)) | |
|
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
189 (when (and (not (nth (car slots) time)) ;not already set |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
190 (setq val (cond ((and (consp predicate) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
191 (not (eq (car predicate) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
192 'lambda))) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
193 (and (numberp elt) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
194 (<= (car predicate) elt) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
195 (<= elt (cadr predicate)) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
196 elt)) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
197 ((symbolp predicate) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
198 (cdr (assoc elt |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
199 (symbol-value predicate)))) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
200 ((funcall predicate))))) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
201 (setq exit t) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
202 (while slots |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
203 (let ((new-val (and rule |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
204 (let ((this (pop rule))) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
205 (if (vectorp this) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
206 (parse-integer |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
207 elt (aref this 0) (aref this 1)) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
208 (funcall this)))))) |
|
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
30316
diff
changeset
|
209 (rplaca (nthcdr (pop slots) time) (or new-val val))))))))) |
| 17493 | 210 time)) |
| 211 | |
| 212 (provide 'parse-time) | |
| 213 | |
| 214 ;;; parse-time.el ends here |
