parse-time.el 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. ;;; parse-time.el --- parsing time strings
  2. ;; Copyright (C) 1996, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Erik Naggum <erik@naggum.no>
  4. ;; Keywords: util
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; With the introduction of the `encode-time', `decode-time', and
  18. ;; `format-time-string' functions, dealing with time became simpler in
  19. ;; Emacs. However, parsing time strings is still largely a matter of
  20. ;; heuristics and no common interface has been designed.
  21. ;; `parse-time-string' parses a time in a string and returns a list of 9
  22. ;; values, just like `decode-time', where unspecified elements in the
  23. ;; string are returned as nil. `encode-time' may be applied on these
  24. ;; values to obtain an internal time value.
  25. ;;; Code:
  26. (eval-when-compile (require 'cl)) ;and ah ain't kiddin' 'bout it
  27. (defvar parse-time-digits (make-vector 256 nil))
  28. ;; Byte-compiler warnings
  29. (defvar parse-time-elt)
  30. (defvar parse-time-val)
  31. (unless (aref parse-time-digits ?0)
  32. (loop for i from ?0 to ?9
  33. do (aset parse-time-digits i (- i ?0))))
  34. (defsubst digit-char-p (char)
  35. (aref parse-time-digits char))
  36. (defsubst parse-time-string-chars (char)
  37. (save-match-data
  38. (let (case-fold-search str)
  39. (cond ((eq char ?+) 1)
  40. ((eq char ?-) -1)
  41. ((eq char ?:) ?d)
  42. ((string-match "[[:upper:]]" (setq str (string char))) ?A)
  43. ((string-match "[[:lower:]]" str) ?a)
  44. ((string-match "[[:digit:]]" str) ?0)))))
  45. (put 'parse-error 'error-conditions '(parse-error error))
  46. (put 'parse-error 'error-message "Parsing error")
  47. (defsubst parse-integer (string &optional start end)
  48. "[CL] Parse and return the integer in STRING, or nil if none."
  49. (let ((integer 0)
  50. (digit 0)
  51. (index (or start 0))
  52. (end (or end (length string))))
  53. (when (< index end)
  54. (let ((sign (aref string index)))
  55. (if (or (eq sign ?+) (eq sign ?-))
  56. (setq sign (parse-time-string-chars sign)
  57. index (1+ index))
  58. (setq sign 1))
  59. (while (and (< index end)
  60. (setq digit (digit-char-p (aref string index))))
  61. (setq integer (+ (* integer 10) digit)
  62. index (1+ index)))
  63. (if (/= index end)
  64. (signal 'parse-error `("not an integer"
  65. ,(substring string (or start 0) end)))
  66. (* sign integer))))))
  67. (defun parse-time-tokenize (string)
  68. "Tokenize STRING into substrings."
  69. (let ((start nil)
  70. (end (length string))
  71. (all-digits nil)
  72. (list ())
  73. (index 0)
  74. (c nil))
  75. (while (< index end)
  76. (while (and (< index end) ;skip invalid characters
  77. (not (setq c (parse-time-string-chars (aref string index)))))
  78. (incf index))
  79. (setq start index all-digits (eq c ?0))
  80. (while (and (< (incf index) end) ;scan valid characters
  81. (setq c (parse-time-string-chars (aref string index))))
  82. (setq all-digits (and all-digits (eq c ?0))))
  83. (if (<= index end)
  84. (push (if all-digits (parse-integer string start index)
  85. (substring string start index))
  86. list)))
  87. (nreverse list)))
  88. (defvar parse-time-months '(("jan" . 1) ("feb" . 2) ("mar" . 3)
  89. ("apr" . 4) ("may" . 5) ("jun" . 6)
  90. ("jul" . 7) ("aug" . 8) ("sep" . 9)
  91. ("oct" . 10) ("nov" . 11) ("dec" . 12)
  92. ("january" . 1) ("february" . 2)
  93. ("march" . 3) ("april" . 4) ("june" . 6)
  94. ("july" . 7) ("august" . 8)
  95. ("september" . 9) ("october" . 10)
  96. ("november" . 11) ("december" . 12)))
  97. (defvar parse-time-weekdays '(("sun" . 0) ("mon" . 1) ("tue" . 2)
  98. ("wed" . 3) ("thu" . 4) ("fri" . 5)
  99. ("sat" . 6) ("sunday" . 0) ("monday" . 1)
  100. ("tuesday" . 2) ("wednesday" . 3)
  101. ("thursday" . 4) ("friday" . 5)
  102. ("saturday" . 6)))
  103. (defvar parse-time-zoneinfo `(("z" 0) ("ut" 0) ("gmt" 0)
  104. ("pst" ,(* -8 3600)) ("pdt" ,(* -7 3600) t)
  105. ("mst" ,(* -7 3600)) ("mdt" ,(* -6 3600) t)
  106. ("cst" ,(* -6 3600)) ("cdt" ,(* -5 3600) t)
  107. ("est" ,(* -5 3600)) ("edt" ,(* -4 3600) t))
  108. "(zoneinfo seconds-off daylight-savings-time-p)")
  109. (defvar parse-time-rules
  110. `(((6) parse-time-weekdays)
  111. ((3) (1 31))
  112. ((4) parse-time-months)
  113. ((5) (100 4038))
  114. ((2 1 0)
  115. ,#'(lambda () (and (stringp parse-time-elt)
  116. (= (length parse-time-elt) 8)
  117. (= (aref parse-time-elt 2) ?:)
  118. (= (aref parse-time-elt 5) ?:)))
  119. [0 2] [3 5] [6 8])
  120. ((8 7) parse-time-zoneinfo
  121. ,#'(lambda () (car parse-time-val))
  122. ,#'(lambda () (cadr parse-time-val)))
  123. ((8)
  124. ,#'(lambda ()
  125. (and (stringp parse-time-elt)
  126. (= 5 (length parse-time-elt))
  127. (or (= (aref parse-time-elt 0) ?+)
  128. (= (aref parse-time-elt 0) ?-))))
  129. ,#'(lambda () (* 60 (+ (parse-integer parse-time-elt 3 5)
  130. (* 60 (parse-integer parse-time-elt 1 3)))
  131. (if (= (aref parse-time-elt 0) ?-) -1 1))))
  132. ((5 4 3)
  133. ,#'(lambda () (and (stringp parse-time-elt)
  134. (= (length parse-time-elt) 10)
  135. (= (aref parse-time-elt 4) ?-)
  136. (= (aref parse-time-elt 7) ?-)))
  137. [0 4] [5 7] [8 10])
  138. ((2 1 0)
  139. ,#'(lambda () (and (stringp parse-time-elt)
  140. (= (length parse-time-elt) 5)
  141. (= (aref parse-time-elt 2) ?:)))
  142. [0 2] [3 5] ,#'(lambda () 0))
  143. ((2 1 0)
  144. ,#'(lambda () (and (stringp parse-time-elt)
  145. (= (length parse-time-elt) 4)
  146. (= (aref parse-time-elt 1) ?:)))
  147. [0 1] [2 4] ,#'(lambda () 0))
  148. ((2 1 0)
  149. ,#'(lambda () (and (stringp parse-time-elt)
  150. (= (length parse-time-elt) 7)
  151. (= (aref parse-time-elt 1) ?:)))
  152. [0 1] [2 4] [5 7])
  153. ((5) (50 110) ,#'(lambda () (+ 1900 parse-time-elt)))
  154. ((5) (0 49) ,#'(lambda () (+ 2000 parse-time-elt))))
  155. "(slots predicate extractor...)")
  156. ;;;###autoload(put 'parse-time-rules 'risky-local-variable t)
  157. ;;;###autoload
  158. (defun parse-time-string (string)
  159. "Parse the time-string STRING into (SEC MIN HOUR DAY MON YEAR DOW DST TZ).
  160. The values are identical to those of `decode-time', but any values that are
  161. unknown are returned as nil."
  162. (let ((time (list nil nil nil nil nil nil nil nil nil))
  163. (temp (parse-time-tokenize (downcase string))))
  164. (while temp
  165. (let ((parse-time-elt (pop temp))
  166. (rules parse-time-rules)
  167. (exit nil))
  168. (while (and rules (not exit))
  169. (let* ((rule (pop rules))
  170. (slots (pop rule))
  171. (predicate (pop rule))
  172. (parse-time-val))
  173. (when (and (not (nth (car slots) time)) ;not already set
  174. (setq parse-time-val
  175. (cond ((and (consp predicate)
  176. (not (eq (car predicate)
  177. 'lambda)))
  178. (and (numberp parse-time-elt)
  179. (<= (car predicate) parse-time-elt)
  180. (<= parse-time-elt (cadr predicate))
  181. parse-time-elt))
  182. ((symbolp predicate)
  183. (cdr (assoc parse-time-elt
  184. (symbol-value predicate))))
  185. ((funcall predicate)))))
  186. (setq exit t)
  187. (while slots
  188. (let ((new-val (if rule
  189. (let ((this (pop rule)))
  190. (if (vectorp this)
  191. (parse-integer
  192. parse-time-elt
  193. (aref this 0) (aref this 1))
  194. (funcall this)))
  195. parse-time-val)))
  196. (rplaca (nthcdr (pop slots) time) new-val))))))))
  197. time))
  198. (provide 'parse-time)
  199. ;;; parse-time.el ends here