1 ;;; time-date.el --- Date and time handling functions
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu Umeda <umerin@mse.kyutech.ac.jp>
8 ;; Keywords: mail news util
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Time values come in three formats. The oldest format is a cons
28 ;; cell of the form (HIGH . LOW). This format is obsolete, but still
29 ;; supported. The two other formats are the lists (HIGH LOW) and
30 ;; (HIGH LOW MICRO). The first two formats specify HIGH * 2^16 + LOW
31 ;; seconds; the third format specifies HIGH * 2^16 + LOW + MICRO /
32 ;; 1000000 seconds. We should have 0 <= MICRO < 1000000 and 0 <= LOW
33 ;; < 2^16. If the time value represents a point in time, then HIGH is
34 ;; nonnegative. If the time value is a time difference, then HIGH can
35 ;; be negative as well. The macro `with-decoded-time-value' and the
36 ;; function `encode-time-value' make it easier to deal with these
37 ;; three formats. See `time-subtract' for an example of how to use
42 ;; Only necessary for `declare' when compiling Gnus with Emacs 21.
43 (eval-when-compile (require 'cl))
45 (defmacro with-decoded-time-value (varlist &rest body)
46 "Decode a time value and bind it according to VARLIST, then eval BODY.
48 The value of the last form in BODY is returned.
50 Each element of the list VARLIST is a list of the form
51 \(HIGH-SYMBOL LOW-SYMBOL MICRO-SYMBOL [TYPE-SYMBOL] TIME-VALUE).
52 The time value TIME-VALUE is decoded and the result it bound to
53 the symbols HIGH-SYMBOL, LOW-SYMBOL and MICRO-SYMBOL.
55 The optional TYPE-SYMBOL is bound to the type of the time value.
56 Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH
57 LOW), and type 2 is the list (HIGH LOW MICRO)."
59 (debug ((&rest (symbolp symbolp symbolp &or [symbolp form] form))
62 (let* ((elt (pop varlist))
66 (type (unless (eq (length elt) 1)
68 (time-value (car elt))
69 (gensym (make-symbol "time")))
70 `(let* ,(append `((,gensym ,time-value)
76 (setq ,low (pop ,gensym))
78 ,(append `(setq ,micro (car ,gensym))
79 (when type `(,type 2)))
80 ,(append `(setq ,micro 0)
81 (when type `(,type 1)))))
82 ,(append `(setq ,low ,gensym ,micro 0)
83 (when type `(,type 0))))
84 (with-decoded-time-value ,varlist ,@body)))
87 (defun encode-time-value (high low micro type)
88 "Encode HIGH, LOW, and MICRO into a time value of type TYPE.
89 Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH LOW),
90 and type 2 is the list (HIGH LOW MICRO)."
92 ((eq type 0) (cons high low))
93 ((eq type 1) (list high low))
94 ((eq type 2) (list high low micro))))
96 (autoload 'parse-time-string "parse-time")
97 (autoload 'timezone-make-date-arpa-standard "timezone")
100 (defun date-to-time (date)
101 "Parse a string DATE that represents a date-time and return a time value."
105 ;; `parse-time-string' isn't sufficiently general or
106 ;; robust. It fails to grok some of the formats that
107 ;; timezone does (e.g. dodgy post-2000 stuff from some
108 ;; Elms) and either fails or returns bogus values. Lars
109 ;; reverted this change, but that loses non-trivially
110 ;; often for me. -- fx
111 (timezone-make-date-arpa-standard date)))
112 (error (error "Invalid date: %s" date))))
114 ;; Bit of a mess. Emacs has float-time since at least 21.1.
115 ;; This file is synced to Gnus, and XEmacs packages may have been written
116 ;; using time-to-seconds from the Gnus library.
117 ;;;###autoload(if (and (fboundp 'float-time)
118 ;;;###autoload (subrp (symbol-function 'float-time)))
119 ;;;###autoload (progn
120 ;;;###autoload (defalias 'time-to-seconds 'float-time)
121 ;;;###autoload (make-obsolete 'time-to-seconds 'float-time "21.1"))
122 ;;;###autoload (autoload 'time-to-seconds "time-date"))
125 (unless (and (fboundp 'float-time)
126 (subrp (symbol-function 'float-time)))
127 (defun time-to-seconds (time)
128 "Convert time value TIME to a floating point number."
129 (with-decoded-time-value ((high low micro time))
130 (+ (* 1.0 high 65536)
132 (/ micro 1000000.0))))))
135 (unless (fboundp 'with-no-warnings)
136 (defmacro with-no-warnings (&rest body)
140 (defun seconds-to-time (seconds)
141 "Convert SECONDS (a floating point number) to a time value."
142 (list (floor seconds 65536)
143 (floor (mod seconds 65536))
144 (floor (* (- seconds (ffloor seconds)) 1000000))))
147 (defun time-less-p (t1 t2)
148 "Say whether time value T1 is less than time value T2."
149 (with-decoded-time-value ((high1 low1 micro1 t1)
150 (high2 low2 micro2 t2))
155 (< micro1 micro2)))))))
158 (defun days-to-time (days)
159 "Convert DAYS into a time value."
160 (let* ((seconds (* 1.0 days 60 60 24))
161 (high (condition-case nil (floor (/ seconds 65536))
162 (range-error most-positive-fixnum))))
163 (list high (condition-case nil (floor (- seconds (* 1.0 high 65536)))
164 (range-error 65535)))))
167 (defun time-since (time)
168 "Return the time elapsed since TIME.
169 TIME should be either a time value or a date-time string."
171 ;; Convert date strings to internal time.
172 (setq time (date-to-time time)))
173 (time-subtract (current-time) time))
176 (defalias 'subtract-time 'time-subtract)
179 (defun time-subtract (t1 t2)
180 "Subtract two time values, T1 minus T2.
181 Return the difference in the format of a time value."
182 (with-decoded-time-value ((high low micro type t1)
183 (high2 low2 micro2 type2 t2))
184 (setq high (- high high2)
186 micro (- micro micro2)
187 type (max type type2))
190 micro (+ micro 1000000)))
194 (encode-time-value high low micro type)))
197 (defun time-add (t1 t2)
198 "Add two time values T1 and T2. One should represent a time difference."
199 (with-decoded-time-value ((high low micro type t1)
200 (high2 low2 micro2 type2 t2))
201 (setq high (+ high high2)
203 micro (+ micro micro2)
204 type (max type type2))
205 (when (>= micro 1000000)
207 micro (- micro 1000000)))
211 (encode-time-value high low micro type)))
214 (defun date-to-day (date)
215 "Return the number of days between year 1 and DATE.
216 DATE should be a date-time string."
217 (time-to-days (date-to-time date)))
220 (defun days-between (date1 date2)
221 "Return the number of days between DATE1 and DATE2.
222 DATE1 and DATE2 should be date-time strings."
223 (- (date-to-day date1) (date-to-day date2)))
226 (defun date-leap-year-p (year)
227 "Return t if YEAR is a leap year."
228 (or (and (zerop (% year 4))
229 (not (zerop (% year 100))))
230 (zerop (% year 400))))
233 (defun time-to-day-in-year (time)
234 "Return the day number within the year corresponding to TIME."
235 (let* ((tim (decode-time time))
239 (day-of-year (+ day (* 31 (1- month)))))
241 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
242 (when (date-leap-year-p year)
243 (setq day-of-year (1+ day-of-year))))
247 (defun time-to-days (time)
248 "The number of days between the Gregorian date 0001-12-31bce and TIME.
249 TIME should be a time value.
250 The Gregorian date Sunday, December 31, 1bce is imaginary."
251 (let* ((tim (decode-time time))
255 (+ (time-to-day-in-year time) ; Days this year
256 (* 365 (1- year)) ; + Days in prior years
257 (/ (1- year) 4) ; + Julian leap years
258 (- (/ (1- year) 100)) ; - century years
259 (/ (1- year) 400)))) ; + Gregorian leap years
262 (if (and (fboundp 'float-time)
263 (subrp (symbol-function 'float-time)))
264 (defun time-to-number-of-days (time)
265 "Return the number of days represented by TIME.
266 The number of days will be returned as a floating point number."
267 (/ (float-time time) (* 60 60 24)))
268 (defun time-to-number-of-days (time)
269 "Return the number of days represented by TIME.
270 The number of days will be returned as a floating point number."
271 (/ (with-no-warnings (time-to-seconds time)) (* 60 60 24)))))
274 (defun safe-date-to-time (date)
275 "Parse a string DATE that represents a date-time and return a time value.
276 If DATE is malformed, return a time value of zeros."
283 (defun format-seconds (string seconds)
284 "Use format control STRING to format the number SECONDS.
285 The valid format specifiers are:
286 %y is the number of (365-day) years.
287 %d is the number of days.
288 %h is the number of hours.
289 %m is the number of minutes.
290 %s is the number of seconds.
291 %z is a non-printing control flag (see below).
292 %% is a literal \"%\".
294 Upper-case specifiers are followed by the unit-name (e.g. \"years\").
295 Lower-case specifiers return only the unit.
297 \"%\" may be followed by a number specifying a width, with an
298 optional leading \".\" for zero-padding. For example, \"%.3Y\" will
299 return something of the form \"001 year\".
301 The \"%z\" specifier does not print anything. When it is used, specifiers
302 must be given in order of decreasing size. To the left of \"%z\", nothing
303 is output until the first non-zero unit is encountered.
305 This function does not work for SECONDS greater than `most-positive-fixnum'."
307 (units '(("y" "year" 31536000)
314 spec match usedunits zeroflag larger prev name unit num zeropos)
315 (while (string-match "%\\.?[0-9]*\\(.\\)" string start)
316 (setq start (match-end 0)
317 spec (match-string 1 string))
318 (unless (string-equal spec "%")
319 ;; `assoc-string' is not available in Emacs 21. So when compiling
320 ;; Gnus (`time-date.el' is part of Gnus) with Emacs 21, we get a
321 ;; warning here. But `format-seconds' is not used anywhere in Gnus so
322 ;; it's not a real problem. --rsteib
323 (or (setq match (assoc-string spec units t))
324 (error "Bad format specifier: `%s'" spec))
325 (if (assoc-string spec usedunits t)
326 (error "Multiple instances of specifier: `%s'" spec))
327 (if (string-equal (car match) "z")
330 (setq unit (nth 2 match)
331 larger (and prev (> unit prev))
333 (push match usedunits)))
335 (error "Units are not in decreasing order of size"))
340 (when (string-match (format "%%\\(\\.?[0-9]+\\)?\\(%s\\)" spec) string)
341 (if (string-equal spec "z") ; must be last in units
343 (replace-regexp-in-string
345 (substring string (min (or zeropos (match-end 0))
346 (match-beginning 0)))))
347 ;; Cf article-make-date-line in gnus-art.
348 (setq num (floor seconds unit)
349 seconds (- seconds (* num unit)))
350 ;; Start position of the first non-zero unit.
352 (setq zeropos (unless (zerop num) (match-beginning 0))))
355 (format (concat "%" (match-string 1 string) "d%s") num
356 (if (string-equal (match-string 2 string) spec)
357 "" ; lower-case, no unit-name
359 (if (= num 1) "" "s"))))
361 (replace-regexp-in-string "%%" "%" string))
366 ;; arch-tag: addcf07b-b20a-465b-af72-550b8ac5190f
367 ;;; time-date.el ends here