]> code.delx.au - gnu-emacs/blob - lisp/calendar/time-date.el
(time-to-seconds): In Emacs, make it an obsolete alias for float-time.
[gnu-emacs] / lisp / calendar / time-date.el
1 ;;; time-date.el --- Date and time handling functions
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu Umeda <umerin@mse.kyutech.ac.jp>
8 ;; Keywords: mail news util
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
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
38 ;; them.
39
40 ;;; Code:
41
42 ;; Only necessary for `declare' when compiling Gnus with Emacs 21.
43 (eval-when-compile (require 'cl))
44
45 (defmacro with-decoded-time-value (varlist &rest body)
46 "Decode a time value and bind it according to VARLIST, then eval BODY.
47
48 The value of the last form in BODY is returned.
49
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.
54
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)."
58 (declare (indent 1)
59 (debug ((&rest (symbolp symbolp symbolp &or [symbolp form] form))
60 body)))
61 (if varlist
62 (let* ((elt (pop varlist))
63 (high (pop elt))
64 (low (pop elt))
65 (micro (pop elt))
66 (type (unless (eq (length elt) 1)
67 (pop elt)))
68 (time-value (car elt))
69 (gensym (make-symbol "time")))
70 `(let* ,(append `((,gensym ,time-value)
71 (,high (pop ,gensym))
72 ,low ,micro)
73 (when type `(,type)))
74 (if (consp ,gensym)
75 (progn
76 (setq ,low (pop ,gensym))
77 (if ,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)))
85 `(progn ,@body)))
86
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)."
91 (cond
92 ((eq type 0) (cons high low))
93 ((eq type 1) (list high low))
94 ((eq type 2) (list high low micro))))
95
96 (autoload 'parse-time-string "parse-time")
97 (autoload 'timezone-make-date-arpa-standard "timezone")
98
99 ;;;###autoload
100 (defun date-to-time (date)
101 "Parse a string DATE that represents a date-time and return a time value."
102 (condition-case ()
103 (apply 'encode-time
104 (parse-time-string
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))))
113
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 (featurep 'xemacs)
118 ;;;###autoload (autoload 'time-to-seconds "time-date")
119 ;;;###autoload (define-obsolete-function-alias 'time-to-seconds 'float-time "21.1"))
120
121 (if (featurep 'xemacs)
122 (defun time-to-seconds (time)
123 "Convert time value TIME to a floating point number."
124 (with-decoded-time-value ((high low micro time))
125 (+ (* 1.0 high 65536)
126 low
127 (/ micro 1000000.0)))))
128
129 ;;;###autoload
130 (defun seconds-to-time (seconds)
131 "Convert SECONDS (a floating point number) to a time value."
132 (list (floor seconds 65536)
133 (floor (mod seconds 65536))
134 (floor (* (- seconds (ffloor seconds)) 1000000))))
135
136 ;;;###autoload
137 (defun time-less-p (t1 t2)
138 "Say whether time value T1 is less than time value T2."
139 (with-decoded-time-value ((high1 low1 micro1 t1)
140 (high2 low2 micro2 t2))
141 (or (< high1 high2)
142 (and (= high1 high2)
143 (or (< low1 low2)
144 (and (= low1 low2)
145 (< micro1 micro2)))))))
146
147 ;;;###autoload
148 (defun days-to-time (days)
149 "Convert DAYS into a time value."
150 (let* ((seconds (* 1.0 days 60 60 24))
151 (high (condition-case nil (floor (/ seconds 65536))
152 (range-error most-positive-fixnum))))
153 (list high (condition-case nil (floor (- seconds (* 1.0 high 65536)))
154 (range-error 65535)))))
155
156 ;;;###autoload
157 (defun time-since (time)
158 "Return the time elapsed since TIME.
159 TIME should be either a time value or a date-time string."
160 (when (stringp time)
161 ;; Convert date strings to internal time.
162 (setq time (date-to-time time)))
163 (time-subtract (current-time) time))
164
165 ;;;###autoload
166 (defalias 'subtract-time 'time-subtract)
167
168 ;;;###autoload
169 (defun time-subtract (t1 t2)
170 "Subtract two time values, T1 minus T2.
171 Return the difference in the format of a time value."
172 (with-decoded-time-value ((high low micro type t1)
173 (high2 low2 micro2 type2 t2))
174 (setq high (- high high2)
175 low (- low low2)
176 micro (- micro micro2)
177 type (max type type2))
178 (when (< micro 0)
179 (setq low (1- low)
180 micro (+ micro 1000000)))
181 (when (< low 0)
182 (setq high (1- high)
183 low (+ low 65536)))
184 (encode-time-value high low micro type)))
185
186 ;;;###autoload
187 (defun time-add (t1 t2)
188 "Add two time values T1 and T2. One should represent a time difference."
189 (with-decoded-time-value ((high low micro type t1)
190 (high2 low2 micro2 type2 t2))
191 (setq high (+ high high2)
192 low (+ low low2)
193 micro (+ micro micro2)
194 type (max type type2))
195 (when (>= micro 1000000)
196 (setq low (1+ low)
197 micro (- micro 1000000)))
198 (when (>= low 65536)
199 (setq high (1+ high)
200 low (- low 65536)))
201 (encode-time-value high low micro type)))
202
203 ;;;###autoload
204 (defun date-to-day (date)
205 "Return the number of days between year 1 and DATE.
206 DATE should be a date-time string."
207 (time-to-days (date-to-time date)))
208
209 ;;;###autoload
210 (defun days-between (date1 date2)
211 "Return the number of days between DATE1 and DATE2.
212 DATE1 and DATE2 should be date-time strings."
213 (- (date-to-day date1) (date-to-day date2)))
214
215 ;;;###autoload
216 (defun date-leap-year-p (year)
217 "Return t if YEAR is a leap year."
218 (or (and (zerop (% year 4))
219 (not (zerop (% year 100))))
220 (zerop (% year 400))))
221
222 ;;;###autoload
223 (defun time-to-day-in-year (time)
224 "Return the day number within the year corresponding to TIME."
225 (let* ((tim (decode-time time))
226 (month (nth 4 tim))
227 (day (nth 3 tim))
228 (year (nth 5 tim))
229 (day-of-year (+ day (* 31 (1- month)))))
230 (when (> month 2)
231 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
232 (when (date-leap-year-p year)
233 (setq day-of-year (1+ day-of-year))))
234 day-of-year))
235
236 ;;;###autoload
237 (defun time-to-days (time)
238 "The number of days between the Gregorian date 0001-12-31bce and TIME.
239 TIME should be a time value.
240 The Gregorian date Sunday, December 31, 1bce is imaginary."
241 (let* ((tim (decode-time time))
242 (month (nth 4 tim))
243 (day (nth 3 tim))
244 (year (nth 5 tim)))
245 (+ (time-to-day-in-year time) ; Days this year
246 (* 365 (1- year)) ; + Days in prior years
247 (/ (1- year) 4) ; + Julian leap years
248 (- (/ (1- year) 100)) ; - century years
249 (/ (1- year) 400)))) ; + Gregorian leap years
250
251 (defun time-to-number-of-days (time)
252 "Return the number of days represented by TIME.
253 The number of days will be returned as a floating point number."
254 (/ (if (featurep 'xemacs)
255 (time-to-seconds time)
256 (float-time time)) (* 60 60 24)))
257
258 ;;;###autoload
259 (defun safe-date-to-time (date)
260 "Parse a string DATE that represents a date-time and return a time value.
261 If DATE is malformed, return a time value of zeros."
262 (condition-case ()
263 (date-to-time date)
264 (error '(0 0))))
265
266 \f
267 ;;;###autoload
268 (defun format-seconds (string seconds)
269 "Use format control STRING to format the number SECONDS.
270 The valid format specifiers are:
271 %y is the number of (365-day) years.
272 %d is the number of days.
273 %h is the number of hours.
274 %m is the number of minutes.
275 %s is the number of seconds.
276 %z is a non-printing control flag (see below).
277 %% is a literal \"%\".
278
279 Upper-case specifiers are followed by the unit-name (e.g. \"years\").
280 Lower-case specifiers return only the unit.
281
282 \"%\" may be followed by a number specifying a width, with an
283 optional leading \".\" for zero-padding. For example, \"%.3Y\" will
284 return something of the form \"001 year\".
285
286 The \"%z\" specifier does not print anything. When it is used, specifiers
287 must be given in order of decreasing size. To the left of \"%z\", nothing
288 is output until the first non-zero unit is encountered.
289
290 This function does not work for SECONDS greater than `most-positive-fixnum'."
291 (let ((start 0)
292 (units '(("y" "year" 31536000)
293 ("d" "day" 86400)
294 ("h" "hour" 3600)
295 ("m" "minute" 60)
296 ("s" "second" 1)
297 ("z")))
298 (case-fold-search t)
299 spec match usedunits zeroflag larger prev name unit num zeropos)
300 (while (string-match "%\\.?[0-9]*\\(.\\)" string start)
301 (setq start (match-end 0)
302 spec (match-string 1 string))
303 (unless (string-equal spec "%")
304 ;; `assoc-string' is not available in Emacs 21. So when compiling
305 ;; Gnus (`time-date.el' is part of Gnus) with Emacs 21, we get a
306 ;; warning here. But `format-seconds' is not used anywhere in Gnus so
307 ;; it's not a real problem. --rsteib
308 (or (setq match (assoc-string spec units t))
309 (error "Bad format specifier: `%s'" spec))
310 (if (assoc-string spec usedunits t)
311 (error "Multiple instances of specifier: `%s'" spec))
312 (if (string-equal (car match) "z")
313 (setq zeroflag t)
314 (unless larger
315 (setq unit (nth 2 match)
316 larger (and prev (> unit prev))
317 prev unit)))
318 (push match usedunits)))
319 (and zeroflag larger
320 (error "Units are not in decreasing order of size"))
321 (dolist (u units)
322 (setq spec (car u)
323 name (cadr u)
324 unit (nth 2 u))
325 (when (string-match (format "%%\\(\\.?[0-9]+\\)?\\(%s\\)" spec) string)
326 (if (string-equal spec "z") ; must be last in units
327 (setq string
328 (replace-regexp-in-string
329 "%z" ""
330 (substring string (min (or zeropos (match-end 0))
331 (match-beginning 0)))))
332 ;; Cf article-make-date-line in gnus-art.
333 (setq num (floor seconds unit)
334 seconds (- seconds (* num unit)))
335 ;; Start position of the first non-zero unit.
336 (or zeropos
337 (setq zeropos (unless (zerop num) (match-beginning 0))))
338 (setq string
339 (replace-match
340 (format (concat "%" (match-string 1 string) "d%s") num
341 (if (string-equal (match-string 2 string) spec)
342 "" ; lower-case, no unit-name
343 (format " %s%s" name
344 (if (= num 1) "" "s"))))
345 t t string))))))
346 (replace-regexp-in-string "%%" "%" string))
347
348
349 (provide 'time-date)
350
351 ;; arch-tag: addcf07b-b20a-465b-af72-550b8ac5190f
352 ;;; time-date.el ends here