1 ;;; time-date.el --- Date and time handling functions
3 ;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Masanobu Umeda <umerin@mse.kyutech.ac.jp>
7 ;; Keywords: mail news util
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Time values come in several formats. The oldest format is a cons
27 ;; cell of the form (HIGH . LOW). This format is obsolete, but still
28 ;; supported. The other formats are the lists (HIGH LOW), (HIGH LOW
29 ;; USEC), and (HIGH LOW USEC PSEC). These formats specify the time
30 ;; value equal to HIGH * 2^16 + LOW + USEC * 10^-6 + PSEC * 10^-12
31 ;; seconds, where missing components are treated as zero. HIGH can be
32 ;; negative, either because the value is a time difference, or because
33 ;; the machine supports negative time stamps that fall before the epoch.
34 ;; The macro `with-decoded-time-value' and the function
35 ;; `encode-time-value' make it easier to deal with these formats.
36 ;; See `time-subtract' for an example of how to use them.
40 (defmacro with-decoded-time-value
(varlist &rest body
)
41 "Decode a time value and bind it according to VARLIST, then eval BODY.
43 The value of the last form in BODY is returned.
45 Each element of the list VARLIST is a list of the form
46 \(HIGH-SYMBOL LOW-SYMBOL MICRO-SYMBOL [PICO-SYMBOL [TYPE-SYMBOL]] TIME-VALUE).
47 The time value TIME-VALUE is decoded and the result it bound to
48 the symbols HIGH-SYMBOL, LOW-SYMBOL and MICRO-SYMBOL.
49 The optional PICO-SYMBOL is bound to the picoseconds part.
51 The optional TYPE-SYMBOL is bound to the type of the time value.
52 Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH
53 LOW), type 2 is the list (HIGH LOW MICRO), and type 3 is the
54 list (HIGH LOW MICRO PICO)."
56 (debug ((&rest
(symbolp symbolp symbolp
&or
[symbolp form
] form
))
59 (let* ((elt (pop varlist
))
63 (pico (unless (<= (length elt
) 2)
65 (type (unless (eq (length elt
) 1)
67 (time-value (car elt
))
68 (gensym (make-symbol "time")))
69 `(let* ,(append `((,gensym
,time-value
)
76 (setq ,low
(pop ,gensym
))
79 (setq ,micro
(car ,gensym
))
82 ,(append `(setq ,pico
(cadr ,gensym
))
83 (when type
`(,type
3)))
84 ,(append `(setq ,pico
0)
85 (when type
`(,type
2)))))
88 ,(append `(setq ,micro
0)
89 (when pico
`(,pico
0))
90 (when type
`(,type
1)))))
91 ,(append `(setq ,low
,gensym
,micro
0)
92 (when pico
`(,pico
0))
93 (when type
`(,type
0))))
94 (with-decoded-time-value ,varlist
,@body
)))
97 (defun encode-time-value (high low micro pico
&optional type
)
98 "Encode HIGH, LOW, MICRO, and PICO into a time value of type TYPE.
99 Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH LOW),
100 type 2 is (HIGH LOW MICRO), and type 3 is (HIGH LOW MICRO PICO).
102 For backward compatibility, if only four arguments are given,
103 it is assumed that PICO was omitted and should be treated as zero."
105 ((eq type
0) (cons high low
))
106 ((eq type
1) (list high low
))
107 ((eq type
2) (list high low micro
))
108 ((eq type
3) (list high low micro pico
))
109 ((null type
) (encode-time-value high low micro
0 pico
))))
111 (autoload 'parse-time-string
"parse-time")
112 (autoload 'timezone-make-date-arpa-standard
"timezone")
115 ;; `parse-time-string' isn't sufficiently general or robust. It fails
116 ;; to grok some of the formats that timezone does (e.g. dodgy
117 ;; post-2000 stuff from some Elms) and either fails or returns bogus
118 ;; values. timezone-make-date-arpa-standard should help.
119 (defun date-to-time (date)
120 "Parse a string DATE that represents a date-time and return a time value.
121 If DATE lacks timezone information, GMT is assumed."
123 (apply 'encode-time
(parse-time-string date
))
124 (error (condition-case ()
127 (timezone-make-date-arpa-standard date
)))
128 (error (error "Invalid date: %s" date
))))))
130 ;; Bit of a mess. Emacs has float-time since at least 21.1.
131 ;; This file is synced to Gnus, and XEmacs packages may have been written
132 ;; using time-to-seconds from the Gnus library.
133 ;;;###autoload(if (or (featurep 'emacs)
134 ;;;###autoload (and (fboundp 'float-time)
135 ;;;###autoload (subrp (symbol-function 'float-time))))
136 ;;;###autoload (progn
137 ;;;###autoload (defalias 'time-to-seconds 'float-time)
138 ;;;###autoload (make-obsolete 'time-to-seconds 'float-time "21.1"))
139 ;;;###autoload (autoload 'time-to-seconds "time-date"))
142 (or (featurep 'emacs
)
143 (and (fboundp 'float-time
)
144 (subrp (symbol-function 'float-time
)))
145 (defun time-to-seconds (time)
146 "Convert time value TIME to a floating point number."
147 (with-decoded-time-value ((high low micro pico type time
))
148 (+ (* 1.0 high
65536)
150 (/ (+ (* micro
1e6
) pico
) 1e12
))))))
153 (defun seconds-to-time (seconds)
154 "Convert SECONDS (a floating point number) to a time value."
155 (let* ((usec (* 1000000 (mod seconds
1)))
156 (ps (round (* 1000000 (mod usec
1))))
158 (lo (floor (mod seconds
65536)))
159 (hi (floor seconds
65536)))
171 (setq hi
(1+ hi
))))))))
175 (defun time-less-p (t1 t2
)
176 "Return non-nil if time value T1 is earlier than time value T2."
177 (with-decoded-time-value ((high1 low1 micro1 pico1 type1 t1
)
178 (high2 low2 micro2 pico2 type2 t2
))
183 (or (< micro1 micro2
)
184 (and (= micro1 micro2
)
185 (< pico1 pico2
)))))))))
188 (defun days-to-time (days)
189 "Convert DAYS into a time value."
190 (let* ((seconds (* 1.0 days
60 60 24))
191 (high (condition-case nil
(floor (/ seconds
65536))
192 (range-error most-positive-fixnum
))))
193 (list high
(condition-case nil
(floor (- seconds
(* 1.0 high
65536)))
194 (range-error 65535)))))
197 (defun time-since (time)
198 "Return the time elapsed since TIME.
199 TIME should be either a time value or a date-time string."
201 ;; Convert date strings to internal time.
202 (setq time
(date-to-time time
)))
203 (time-subtract (current-time) time
))
206 (defalias 'subtract-time
'time-subtract
)
209 (defun time-subtract (t1 t2
)
210 "Subtract two time values, T1 minus T2.
211 Return the difference in the format of a time value."
212 (with-decoded-time-value ((high low micro pico type t1
)
213 (high2 low2 micro2 pico2 type2 t2
))
214 (setq high
(- high high2
)
216 micro
(- micro micro2
)
218 type
(max type type2
))
220 (setq micro
(1- micro
)
221 pico
(+ pico
1000000)))
224 micro
(+ micro
1000000)))
228 (encode-time-value high low micro pico type
)))
231 (defun time-add (t1 t2
)
232 "Add two time values T1 and T2. One should represent a time difference."
233 (with-decoded-time-value ((high low micro pico type t1
)
234 (high2 low2 micro2 pico2 type2 t2
))
235 (setq high
(+ high high2
)
237 micro
(+ micro micro2
)
239 type
(max type type2
))
240 (when (>= pico
1000000)
241 (setq micro
(1+ micro
)
242 pico
(- pico
1000000)))
243 (when (>= micro
1000000)
245 micro
(- micro
1000000)))
249 (encode-time-value high low micro pico type
)))
252 (defun date-to-day (date)
253 "Return the number of days between year 1 and DATE.
254 DATE should be a date-time string."
255 (time-to-days (date-to-time date
)))
258 (defun days-between (date1 date2
)
259 "Return the number of days between DATE1 and DATE2.
260 DATE1 and DATE2 should be date-time strings."
261 (- (date-to-day date1
) (date-to-day date2
)))
264 (defun date-leap-year-p (year)
265 "Return t if YEAR is a leap year."
266 (or (and (zerop (% year
4))
267 (not (zerop (% year
100))))
268 (zerop (% year
400))))
271 (defun time-to-day-in-year (time)
272 "Return the day number within the year corresponding to TIME."
273 (let* ((tim (decode-time time
))
277 (day-of-year (+ day
(* 31 (1- month
)))))
279 (setq day-of-year
(- day-of-year
(/ (+ 23 (* 4 month
)) 10)))
280 (when (date-leap-year-p year
)
281 (setq day-of-year
(1+ day-of-year
))))
285 (defun time-to-days (time)
286 "The number of days between the Gregorian date 0001-12-31bce and TIME.
287 TIME should be a time value.
288 The Gregorian date Sunday, December 31, 1bce is imaginary."
289 (let* ((tim (decode-time time
))
291 (+ (time-to-day-in-year time
) ; Days this year
292 (* 365 (1- year
)) ; + Days in prior years
293 (/ (1- year
) 4) ; + Julian leap years
294 (- (/ (1- year
) 100)) ; - century years
295 (/ (1- year
) 400)))) ; + Gregorian leap years
297 (defun time-to-number-of-days (time)
298 "Return the number of days represented by TIME.
299 Returns a floating point number."
300 (/ (funcall (eval-when-compile
301 (if (or (featurep 'emacs
)
302 (and (fboundp 'float-time
)
303 (subrp (symbol-function 'float-time
))))
305 'time-to-seconds
)) time
) (* 60 60 24)))
308 (defun safe-date-to-time (date)
309 "Parse a string DATE that represents a date-time and return a time value.
310 If DATE is malformed, return a time value of zeros."
317 (defun format-seconds (string seconds
)
318 "Use format control STRING to format the number SECONDS.
319 The valid format specifiers are:
320 %y is the number of (365-day) years.
321 %d is the number of days.
322 %h is the number of hours.
323 %m is the number of minutes.
324 %s is the number of seconds.
325 %z is a non-printing control flag (see below).
326 %% is a literal \"%\".
328 Upper-case specifiers are followed by the unit-name (e.g. \"years\").
329 Lower-case specifiers return only the unit.
331 \"%\" may be followed by a number specifying a width, with an
332 optional leading \".\" for zero-padding. For example, \"%.3Y\" will
333 return something of the form \"001 year\".
335 The \"%z\" specifier does not print anything. When it is used, specifiers
336 must be given in order of decreasing size. To the left of \"%z\", nothing
337 is output until the first non-zero unit is encountered.
339 This function does not work for SECONDS greater than `most-positive-fixnum'."
341 (units '(("y" "year" 31536000)
348 spec match usedunits zeroflag larger prev name unit num zeropos
)
349 (while (string-match "%\\.?[0-9]*\\(.\\)" string start
)
350 (setq start
(match-end 0)
351 spec
(match-string 1 string
))
352 (unless (string-equal spec
"%")
353 (or (setq match
(assoc (downcase spec
) units
))
354 (error "Bad format specifier: `%s'" spec
))
355 (if (assoc (downcase spec
) usedunits
)
356 (error "Multiple instances of specifier: `%s'" spec
))
357 (if (string-equal (car match
) "z")
360 (setq unit
(nth 2 match
)
361 larger
(and prev
(> unit prev
))
363 (push match usedunits
)))
365 (error "Units are not in decreasing order of size"))
370 (when (string-match (format "%%\\(\\.?[0-9]+\\)?\\(%s\\)" spec
) string
)
371 (if (string-equal spec
"z") ; must be last in units
373 (replace-regexp-in-string
375 (substring string
(min (or zeropos
(match-end 0))
376 (match-beginning 0)))))
377 ;; Cf article-make-date-line in gnus-art.
378 (setq num
(floor seconds unit
)
379 seconds
(- seconds
(* num unit
)))
380 ;; Start position of the first non-zero unit.
382 (setq zeropos
(unless (zerop num
) (match-beginning 0))))
385 (format (concat "%" (match-string 1 string
) "d%s") num
386 (if (string-equal (match-string 2 string
) spec
)
387 "" ; lower-case, no unit-name
389 (if (= num
1) "" "s"))))
391 (replace-regexp-in-string "%%" "%" string
))
396 ;;; time-date.el ends here