* gnus-spec.el (gnus-lrm-string-p): `bidi-string-mark-left-to-right' has been renamed.
[gnus] / lisp / parse-time.el
1 ;;; parse-time.el --- parsing time strings
2
3 ;; Copyright (C) 1996, 2000-2011  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 (eval-when-compile (require 'cl))       ;and ah ain't kiddin' 'bout it
38
39 (eval-and-compile
40   (when (featurep 'xemacs)
41     (defvar parse-time-syntax (make-vector 256 nil))))
42 (defvar parse-time-digits (make-vector 256 nil))
43
44 ;; Byte-compiler warnings
45 (defvar parse-time-elt)
46 (defvar parse-time-val)
47
48 (unless (aref parse-time-digits ?0)
49   (loop for i from ?0 to ?9
50     do (aset parse-time-digits i (- i ?0))))
51
52 (when (featurep 'xemacs)
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 (defsubst digit-char-p (char)
65   (aref parse-time-digits char))
66
67 (eval-and-compile
68   (if (featurep 'xemacs)
69       (defsubst parse-time-string-chars (char)
70         (and (< char (length parse-time-syntax))
71              (aref parse-time-syntax char)))
72     (defsubst parse-time-string-chars (char)
73       (save-match-data
74         (let (case-fold-search str)
75           (cond ((eq char ?+) 1)
76                 ((eq char ?-) -1)
77                 ((eq char ?:) ?d)
78                 ((string-match "[[:upper:]]" (setq str (string char))) ?A)
79                 ((string-match "[[:lower:]]" str) ?a)
80                 ((string-match "[[:digit:]]" str) ?0)))))))
81
82 (put 'parse-error 'error-conditions '(parse-error error))
83 (put 'parse-error 'error-message "Parsing error")
84
85 (defsubst parse-integer (string &optional start end)
86   "[CL] Parse and return the integer in STRING, or nil if none."
87   (let ((integer 0)
88         (digit 0)
89         (index (or start 0))
90         (end (or end (length string))))
91     (when (< index end)
92       (let ((sign (aref string index)))
93         (if (or (eq sign ?+) (eq sign ?-))
94             (setq sign (parse-time-string-chars sign)
95                   index (1+ index))
96           (setq sign 1))
97         (while (and (< index end)
98                     (setq digit (digit-char-p (aref string index))))
99           (setq integer (+ (* integer 10) digit)
100                 index (1+ index)))
101         (if (/= index end)
102             (signal 'parse-error `("not an integer"
103                                    ,(substring string (or start 0) end)))
104           (* sign integer))))))
105
106 (defun parse-time-tokenize (string)
107   "Tokenize STRING into substrings."
108   (let ((start nil)
109         (end (length string))
110         (all-digits nil)
111         (list ())
112         (index 0)
113         (c nil))
114     (while (< index end)
115       (while (and (< index end)         ;skip invalid characters
116                   (not (setq c (parse-time-string-chars (aref string index)))))
117         (incf index))
118       (setq start index all-digits (eq c ?0))
119       (while (and (< (incf index) end)  ;scan valid characters
120                   (setq c (parse-time-string-chars (aref string index))))
121         (setq all-digits (and all-digits (eq c ?0))))
122       (if (<= index end)
123           (push (if all-digits (parse-integer string start index)
124                   (substring string start index))
125                 list)))
126     (nreverse list)))
127
128 (defvar parse-time-months '(("jan" . 1) ("feb" . 2) ("mar" . 3)
129                             ("apr" . 4) ("may" . 5) ("jun" . 6)
130                             ("jul" . 7) ("aug" . 8) ("sep" . 9)
131                             ("oct" . 10) ("nov" . 11) ("dec" . 12)
132                             ("january" . 1) ("february" . 2)
133                             ("march" . 3) ("april" . 4) ("june" . 6)
134                             ("july" . 7) ("august" . 8)
135                             ("september" . 9) ("october" . 10)
136                             ("november" . 11) ("december" . 12)))
137 (defvar parse-time-weekdays '(("sun" . 0) ("mon" . 1) ("tue" . 2)
138                               ("wed" . 3) ("thu" . 4) ("fri" . 5)
139                               ("sat" . 6) ("sunday" . 0) ("monday" . 1)
140                               ("tuesday" . 2) ("wednesday" . 3)
141                               ("thursday" . 4) ("friday" . 5)
142                               ("saturday" . 6)))
143 (defvar parse-time-zoneinfo `(("z" 0) ("ut" 0) ("gmt" 0)
144                               ("pst" ,(* -8 3600)) ("pdt" ,(* -7 3600) t)
145                               ("mst" ,(* -7 3600)) ("mdt" ,(* -6 3600) t)
146                               ("cst" ,(* -6 3600)) ("cdt" ,(* -5 3600) t)
147                               ("est" ,(* -5 3600)) ("edt" ,(* -4 3600) t))
148   "(zoneinfo seconds-off daylight-savings-time-p)")
149
150 (defvar parse-time-rules
151   `(((6) parse-time-weekdays)
152     ((3) (1 31))
153     ((4) parse-time-months)
154     ((5) (100 4038))
155     ((2 1 0)
156      ,#'(lambda () (and (stringp parse-time-elt)
157                         (= (length parse-time-elt) 8)
158                         (= (aref parse-time-elt 2) ?:)
159                         (= (aref parse-time-elt 5) ?:)))
160      [0 2] [3 5] [6 8])
161     ((8 7) parse-time-zoneinfo