time-date.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. ;;; time-date.el --- Date and time handling functions
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Masanobu Umeda <umerin@mse.kyutech.ac.jp>
  5. ;; Keywords: mail news util
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Time values come in three formats. The oldest format is a cons
  19. ;; cell of the form (HIGH . LOW). This format is obsolete, but still
  20. ;; supported. The two other formats are the lists (HIGH LOW) and
  21. ;; (HIGH LOW MICRO). The first two formats specify HIGH * 2^16 + LOW
  22. ;; seconds; the third format specifies HIGH * 2^16 + LOW + MICRO /
  23. ;; 1000000 seconds. We should have 0 <= MICRO < 1000000 and 0 <= LOW
  24. ;; < 2^16. If the time value represents a point in time, then HIGH is
  25. ;; nonnegative. If the time value is a time difference, then HIGH can
  26. ;; be negative as well. The macro `with-decoded-time-value' and the
  27. ;; function `encode-time-value' make it easier to deal with these
  28. ;; three formats. See `time-subtract' for an example of how to use
  29. ;; them.
  30. ;;; Code:
  31. (defmacro with-decoded-time-value (varlist &rest body)
  32. "Decode a time value and bind it according to VARLIST, then eval BODY.
  33. The value of the last form in BODY is returned.
  34. Each element of the list VARLIST is a list of the form
  35. \(HIGH-SYMBOL LOW-SYMBOL MICRO-SYMBOL [TYPE-SYMBOL] TIME-VALUE).
  36. The time value TIME-VALUE is decoded and the result it bound to
  37. the symbols HIGH-SYMBOL, LOW-SYMBOL and MICRO-SYMBOL.
  38. The optional TYPE-SYMBOL is bound to the type of the time value.
  39. Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH
  40. LOW), and type 2 is the list (HIGH LOW MICRO)."
  41. (declare (indent 1)
  42. (debug ((&rest (symbolp symbolp symbolp &or [symbolp form] form))
  43. body)))
  44. (if varlist
  45. (let* ((elt (pop varlist))
  46. (high (pop elt))
  47. (low (pop elt))
  48. (micro (pop elt))
  49. (type (unless (eq (length elt) 1)
  50. (pop elt)))
  51. (time-value (car elt))
  52. (gensym (make-symbol "time")))
  53. `(let* ,(append `((,gensym ,time-value)
  54. (,high (pop ,gensym))
  55. ,low ,micro)
  56. (when type `(,type)))
  57. (if (consp ,gensym)
  58. (progn
  59. (setq ,low (pop ,gensym))
  60. (if ,gensym
  61. ,(append `(setq ,micro (car ,gensym))
  62. (when type `(,type 2)))
  63. ,(append `(setq ,micro 0)
  64. (when type `(,type 1)))))
  65. ,(append `(setq ,low ,gensym ,micro 0)
  66. (when type `(,type 0))))
  67. (with-decoded-time-value ,varlist ,@body)))
  68. `(progn ,@body)))
  69. (defun encode-time-value (high low micro type)
  70. "Encode HIGH, LOW, and MICRO into a time value of type TYPE.
  71. Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH LOW),
  72. and type 2 is the list (HIGH LOW MICRO)."
  73. (cond
  74. ((eq type 0) (cons high low))
  75. ((eq type 1) (list high low))
  76. ((eq type 2) (list high low micro))))
  77. (autoload 'parse-time-string "parse-time")
  78. (autoload 'timezone-make-date-arpa-standard "timezone")
  79. ;;;###autoload
  80. ;; `parse-time-string' isn't sufficiently general or robust. It fails
  81. ;; to grok some of the formats that timezone does (e.g. dodgy
  82. ;; post-2000 stuff from some Elms) and either fails or returns bogus
  83. ;; values. timezone-make-date-arpa-standard should help.
  84. (defun date-to-time (date)
  85. "Parse a string DATE that represents a date-time and return a time value.
  86. If DATE lacks timezone information, GMT is assumed."
  87. (condition-case ()
  88. (apply 'encode-time (parse-time-string date))
  89. (error (condition-case ()
  90. (apply 'encode-time
  91. (parse-time-string
  92. (timezone-make-date-arpa-standard date)))
  93. (error (error "Invalid date: %s" date))))))
  94. ;; Bit of a mess. Emacs has float-time since at least 21.1.
  95. ;; This file is synced to Gnus, and XEmacs packages may have been written
  96. ;; using time-to-seconds from the Gnus library.
  97. ;;;###autoload(if (or (featurep 'emacs)
  98. ;;;###autoload (and (fboundp 'float-time)
  99. ;;;###autoload (subrp (symbol-function 'float-time))))
  100. ;;;###autoload (progn
  101. ;;;###autoload (defalias 'time-to-seconds 'float-time)
  102. ;;;###autoload (make-obsolete 'time-to-seconds 'float-time "21.1"))
  103. ;;;###autoload (autoload 'time-to-seconds "time-date"))
  104. (eval-when-compile
  105. (or (featurep 'emacs)
  106. (and (fboundp 'float-time)
  107. (subrp (symbol-function 'float-time)))
  108. (defun time-to-seconds (time)
  109. "Convert time value TIME to a floating point number."
  110. (with-decoded-time-value ((high low micro time))
  111. (+ (* 1.0 high 65536)
  112. low
  113. (/ micro 1000000.0))))))
  114. ;;;###autoload
  115. (defun seconds-to-time (seconds)
  116. "Convert SECONDS (a floating point number) to a time value."
  117. (list (floor seconds 65536)
  118. (floor (mod seconds 65536))
  119. (floor (* (- seconds (ffloor seconds)) 1000000))))
  120. ;;;###autoload
  121. (defun time-less-p (t1 t2)
  122. "Return non-nil if time value T1 is earlier than time value T2."
  123. (with-decoded-time-value ((high1 low1 micro1 t1)
  124. (high2 low2 micro2 t2))
  125. (or (< high1 high2)
  126. (and (= high1 high2)
  127. (or (< low1 low2)
  128. (and (= low1 low2)
  129. (< micro1 micro2)))))))
  130. ;;;###autoload
  131. (defun days-to-time (days)
  132. "Convert DAYS into a time value."
  133. (let* ((seconds (* 1.0 days 60 60 24))
  134. (high (condition-case nil (floor (/ seconds 65536))
  135. (range-error most-positive-fixnum))))
  136. (list high (condition-case nil (floor (- seconds (* 1.0 high 65536)))
  137. (range-error 65535)))))
  138. ;;;###autoload
  139. (defun time-since (time)
  140. "Return the time elapsed since TIME.
  141. TIME should be either a time value or a date-time string."
  142. (when (stringp time)
  143. ;; Convert date strings to internal time.
  144. (setq time (date-to-time time)))
  145. (time-subtract (current-time) time))
  146. ;;;###autoload
  147. (defalias 'subtract-time 'time-subtract)
  148. ;;;###autoload
  149. (defun time-subtract (t1 t2)
  150. "Subtract two time values, T1 minus T2.
  151. Return the difference in the format of a time value."
  152. (with-decoded-time-value ((high low micro type t1)
  153. (high2 low2 micro2 type2 t2))
  154. (setq high (- high high2)
  155. low (- low low2)
  156. micro (- micro micro2)
  157. type (max type type2))
  158. (when (< micro 0)
  159. (setq low (1- low)
  160. micro (+ micro 1000000)))
  161. (when (< low 0)
  162. (setq high (1- high)
  163. low (+ low 65536)))
  164. (encode-time-value high low micro type)))
  165. ;;;###autoload
  166. (defun time-add (t1 t2)
  167. "Add two time values T1 and T2. One should represent a time difference."
  168. (with-decoded-time-value ((high low micro type t1)
  169. (high2 low2 micro2 type2 t2))
  170. (setq high (+ high high2)
  171. low (+ low low2)
  172. micro (+ micro micro2)
  173. type (max type type2))
  174. (when (>= micro 1000000)
  175. (setq low (1+ low)
  176. micro (- micro 1000000)))
  177. (when (>= low 65536)
  178. (setq high (1+ high)
  179. low (- low 65536)))
  180. (encode-time-value high low micro type)))
  181. ;;;###autoload
  182. (defun date-to-day (date)
  183. "Return the number of days between year 1 and DATE.
  184. DATE should be a date-time string."
  185. (time-to-days (date-to-time date)))
  186. ;;;###autoload
  187. (defun days-between (date1 date2)
  188. "Return the number of days between DATE1 and DATE2.
  189. DATE1 and DATE2 should be date-time strings."
  190. (- (date-to-day date1) (date-to-day date2)))
  191. ;;;###autoload
  192. (defun date-leap-year-p (year)
  193. "Return t if YEAR is a leap year."
  194. (or (and (zerop (% year 4))
  195. (not (zerop (% year 100))))
  196. (zerop (% year 400))))
  197. ;;;###autoload
  198. (defun time-to-day-in-year (time)
  199. "Return the day number within the year corresponding to TIME."
  200. (let* ((tim (decode-time time))
  201. (month (nth 4 tim))
  202. (day (nth 3 tim))
  203. (year (nth 5 tim))
  204. (day-of-year (+ day (* 31 (1- month)))))
  205. (when (> month 2)
  206. (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
  207. (when (date-leap-year-p year)
  208. (setq day-of-year (1+ day-of-year))))
  209. day-of-year))
  210. ;;;###autoload
  211. (defun time-to-days (time)
  212. "The number of days between the Gregorian date 0001-12-31bce and TIME.
  213. TIME should be a time value.
  214. The Gregorian date Sunday, December 31, 1bce is imaginary."
  215. (let* ((tim (decode-time time))
  216. (year (nth 5 tim)))
  217. (+ (time-to-day-in-year time) ; Days this year
  218. (* 365 (1- year)) ; + Days in prior years
  219. (/ (1- year) 4) ; + Julian leap years
  220. (- (/ (1- year) 100)) ; - century years
  221. (/ (1- year) 400)))) ; + Gregorian leap years
  222. (defun time-to-number-of-days (time)
  223. "Return the number of days represented by TIME.
  224. Returns a floating point number."
  225. (/ (funcall (eval-when-compile
  226. (if (or (featurep 'emacs)
  227. (and (fboundp 'float-time)
  228. (subrp (symbol-function 'float-time))))
  229. 'float-time
  230. 'time-to-seconds)) time) (* 60 60 24)))
  231. ;;;###autoload
  232. (defun safe-date-to-time (date)
  233. "Parse a string DATE that represents a date-time and return a time value.
  234. If DATE is malformed, return a time value of zeros."
  235. (condition-case ()
  236. (date-to-time date)
  237. (error '(0 0))))
  238. ;;;###autoload
  239. (defun format-seconds (string seconds)
  240. "Use format control STRING to format the number SECONDS.
  241. The valid format specifiers are:
  242. %y is the number of (365-day) years.
  243. %d is the number of days.
  244. %h is the number of hours.
  245. %m is the number of minutes.
  246. %s is the number of seconds.
  247. %z is a non-printing control flag (see below).
  248. %% is a literal \"%\".
  249. Upper-case specifiers are followed by the unit-name (e.g. \"years\").
  250. Lower-case specifiers return only the unit.
  251. \"%\" may be followed by a number specifying a width, with an
  252. optional leading \".\" for zero-padding. For example, \"%.3Y\" will
  253. return something of the form \"001 year\".
  254. The \"%z\" specifier does not print anything. When it is used, specifiers
  255. must be given in order of decreasing size. To the left of \"%z\", nothing
  256. is output until the first non-zero unit is encountered.
  257. This function does not work for SECONDS greater than `most-positive-fixnum'."
  258. (let ((start 0)
  259. (units '(("y" "year" 31536000)
  260. ("d" "day" 86400)
  261. ("h" "hour" 3600)
  262. ("m" "minute" 60)
  263. ("s" "second" 1)
  264. ("z")))
  265. (case-fold-search t)
  266. spec match usedunits zeroflag larger prev name unit num zeropos)
  267. (while (string-match "%\\.?[0-9]*\\(.\\)" string start)
  268. (setq start (match-end 0)
  269. spec (match-string 1 string))
  270. (unless (string-equal spec "%")
  271. (or (setq match (assoc (downcase spec) units))
  272. (error "Bad format specifier: `%s'" spec))
  273. (if (assoc (downcase spec) usedunits)
  274. (error "Multiple instances of specifier: `%s'" spec))
  275. (if (string-equal (car match) "z")
  276. (setq zeroflag t)
  277. (unless larger
  278. (setq unit (nth 2 match)
  279. larger (and prev (> unit prev))
  280. prev unit)))
  281. (push match usedunits)))
  282. (and zeroflag larger
  283. (error "Units are not in decreasing order of size"))
  284. (dolist (u units)
  285. (setq spec (car u)
  286. name (cadr u)
  287. unit (nth 2 u))
  288. (when (string-match (format "%%\\(\\.?[0-9]+\\)?\\(%s\\)" spec) string)
  289. (if (string-equal spec "z") ; must be last in units
  290. (setq string
  291. (replace-regexp-in-string
  292. "%z" ""
  293. (substring string (min (or zeropos (match-end 0))
  294. (match-beginning 0)))))
  295. ;; Cf article-make-date-line in gnus-art.
  296. (setq num (floor seconds unit)
  297. seconds (- seconds (* num unit)))
  298. ;; Start position of the first non-zero unit.
  299. (or zeropos
  300. (setq zeropos (unless (zerop num) (match-beginning 0))))
  301. (setq string
  302. (replace-match
  303. (format (concat "%" (match-string 1 string) "d%s") num
  304. (if (string-equal (match-string 2 string) spec)
  305. "" ; lower-case, no unit-name
  306. (format " %s%s" name
  307. (if (= num 1) "" "s"))))
  308. t t string))))))
  309. (replace-regexp-in-string "%%" "%" string))
  310. (provide 'time-date)
  311. ;;; time-date.el ends here