Remove compat code in Tramp
[emacs.git] / lisp / calendar / time-date.el
bloba1d946eac740384237b032780193a2d4cb269cf4
1 ;;; time-date.el --- Date and time handling functions
3 ;; Copyright (C) 1998-2016 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/>.
24 ;;; Commentary:
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 ;; it represents a time stamp before the epoch. Typically, there are
34 ;; more time values than the underlying system time type supports,
35 ;; but the reverse can also be true.
37 ;;; Code:
39 (defmacro with-decoded-time-value (varlist &rest body)
40 "Decode a time value and bind it according to VARLIST, then eval BODY.
42 The value of the last form in BODY is returned.
44 Each element of the list VARLIST is a list of the form
45 \(HIGH-SYMBOL LOW-SYMBOL MICRO-SYMBOL [PICO-SYMBOL [TYPE-SYMBOL]] TIME-VALUE).
46 The time value TIME-VALUE is decoded and the result is bound to
47 the symbols HIGH-SYMBOL, LOW-SYMBOL and MICRO-SYMBOL.
48 The optional PICO-SYMBOL is bound to the picoseconds part.
50 The optional TYPE-SYMBOL is bound to the type of the time value.
51 Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH
52 LOW), type 2 is the list (HIGH LOW MICRO), and type 3 is the
53 list (HIGH LOW MICRO PICO)."
54 (declare (indent 1)
55 (debug ((&rest (symbolp symbolp symbolp
56 &or [symbolp symbolp form] [symbolp form] form))
57 body)))
58 (if varlist
59 (let* ((elt (pop varlist))
60 (high (pop elt))
61 (low (pop elt))
62 (micro (pop elt))
63 (pico (unless (<= (length elt) 2)
64 (pop elt)))
65 (type (unless (eq (length elt) 1)
66 (pop elt)))
67 (time-value (car elt))
68 (gensym (make-symbol "time")))
69 `(let* ,(append `((,gensym (or ,time-value (current-time)))
70 (,gensym
71 (cond
72 ((integerp ,gensym)
73 (list (ash ,gensym -16)
74 (logand ,gensym 65535)))
75 ((floatp ,gensym)
76 (let* ((usec (* 1000000 (mod ,gensym 1)))
77 (ps (round (* 1000000 (mod usec 1))))
78 (us (floor usec))
79 (lo (floor (mod ,gensym 65536)))
80 (hi (floor ,gensym 65536)))
81 (if (eq ps 1000000)
82 (progn
83 (setq ps 0)
84 (setq us (1+ us))
85 (if (eq us 1000000)
86 (progn
87 (setq us 0)
88 (setq lo (1+ lo))
89 (if (eq lo 65536)
90 (progn
91 (setq lo 0)
92 (setq hi (1+ hi))))))))
93 (list hi lo us ps)))
94 (t ,gensym)))
95 (,high (pop ,gensym))
96 ,low ,micro)
97 (when pico `(,pico))
98 (when type `(,type)))
99 (if (consp ,gensym)
100 (progn
101 (setq ,low (pop ,gensym))
102 (if ,gensym
103 (progn
104 (setq ,micro (car ,gensym))
105 ,(cond (pico
106 `(if (cdr ,gensym)
107 ,(append `(setq ,pico (cadr ,gensym))
108 (when type `(,type 3)))
109 ,(append `(setq ,pico 0)
110 (when type `(,type 2)))))
111 (type
112 `(setq type 2))))
113 ,(append `(setq ,micro 0)
114 (when pico `(,pico 0))
115 (when type `(,type 1)))))
116 ,(append `(setq ,low ,gensym ,micro 0)
117 (when pico `(,pico 0))
118 (when type `(,type 0))))
119 (with-decoded-time-value ,varlist ,@body)))
120 `(progn ,@body)))
122 (defun encode-time-value (high low micro pico &optional type)
123 "Encode HIGH, LOW, MICRO, and PICO into a time value of type TYPE.
124 Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH LOW),
125 type 2 is (HIGH LOW MICRO), and type 3 is (HIGH LOW MICRO PICO).
127 For backward compatibility, if only four arguments are given,
128 it is assumed that PICO was omitted and should be treated as zero."
129 (when (null type)
130 (setq type pico)
131 (setq pico 0))
132 (cond
133 ((eq type 0) (cons high low))
134 ((eq type 1) (list high low))
135 ((eq type 2) (list high low micro))
136 ((eq type 3) (list high low micro pico))))
138 (make-obsolete 'encode-time-value nil "25.1")
139 (make-obsolete 'with-decoded-time-value nil "25.1")
141 (autoload 'parse-time-string "parse-time")
142 (autoload 'timezone-make-date-arpa-standard "timezone")
144 ;;;###autoload
145 ;; `parse-time-string' isn't sufficiently general or robust. It fails
146 ;; to grok some of the formats that timezone does (e.g. dodgy
147 ;; post-2000 stuff from some Elms) and either fails or returns bogus
148 ;; values. timezone-make-date-arpa-standard should help.
149 (defun date-to-time (date)
150 "Parse a string DATE that represents a date-time and return a time value.
151 If DATE lacks timezone information, GMT is assumed."
152 (condition-case err
153 (apply 'encode-time (parse-time-string date))
154 (error
155 (let ((overflow-error '(error "Specified time is not representable")))
156 (if (equal err overflow-error)
157 (apply 'signal err)
158 (condition-case err
159 (apply 'encode-time
160 (parse-time-string
161 (timezone-make-date-arpa-standard date)))
162 (error
163 (if (equal err overflow-error)
164 (apply 'signal err)
165 (error "Invalid date: %s" date)))))))))
167 ;;;###autoload
168 (defalias 'time-to-seconds 'float-time)
170 ;;;###autoload
171 (defun seconds-to-time (seconds)
172 "Convert SECONDS to a time value."
173 (time-add 0 seconds))
175 ;;;###autoload
176 (defun days-to-time (days)
177 "Convert DAYS into a time value."
178 (let ((time (condition-case nil (seconds-to-time (* 86400.0 days))
179 (range-error (list most-positive-fixnum 65535)))))
180 (if (integerp days)
181 (setcdr (cdr time) nil))
182 time))
184 ;;;###autoload
185 (defun time-since (time)
186 "Return the time elapsed since TIME.
187 TIME should be either a time value or a date-time string."
188 (when (stringp time)
189 ;; Convert date strings to internal time.
190 (setq time (date-to-time time)))
191 (time-subtract nil time))
193 ;;;###autoload
194 (define-obsolete-function-alias 'subtract-time 'time-subtract "26.1")
196 ;;;###autoload
197 (defun date-to-day (date)
198 "Return the number of days between year 1 and DATE.
199 DATE should be a date-time string."
200 (time-to-days (date-to-time date)))
202 ;;;###autoload
203 (defun days-between (date1 date2)
204 "Return the number of days between DATE1 and DATE2.
205 DATE1 and DATE2 should be date-time strings."
206 (- (date-to-day date1) (date-to-day date2)))
208 ;;;###autoload
209 (defun date-leap-year-p (year)
210 "Return t if YEAR is a leap year."
211 (or (and (zerop (% year 4))
212 (not (zerop (% year 100))))
213 (zerop (% year 400))))
215 (defun time-date--day-in-year (tim)
216 "Return the day number within the year corresponding to the decoded time TIM."
217 (let* ((month (nth 4 tim))
218 (day (nth 3 tim))
219 (year (nth 5 tim))
220 (day-of-year (+ day (* 31 (1- month)))))
221 (when (> month 2)
222 (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
223 (when (date-leap-year-p year)
224 (setq day-of-year (1+ day-of-year))))
225 day-of-year))
227 ;;;###autoload
228 (defun time-to-day-in-year (time)
229 "Return the day number within the year corresponding to TIME."
230 (time-date--day-in-year (decode-time time)))
232 ;;;###autoload
233 (defun time-to-days (time)
234 "The number of days between the Gregorian date 0001-12-31bce and TIME.
235 TIME should be a time value.
236 The Gregorian date Sunday, December 31, 1bce is imaginary."
237 (let* ((tim (decode-time time))
238 (year (nth 5 tim)))
239 (+ (time-date--day-in-year tim) ; Days this year
240 (* 365 (1- year)) ; + Days in prior years
241 (/ (1- year) 4) ; + Julian leap years
242 (- (/ (1- year) 100)) ; - century years
243 (/ (1- year) 400)))) ; + Gregorian leap years
245 (defun time-to-number-of-days (time)
246 "Return the number of days represented by TIME.
247 Returns a floating point number."
248 (/ (float-time time) (* 60 60 24)))
250 ;;;###autoload
251 (defun safe-date-to-time (date)
252 "Parse a string DATE that represents a date-time and return a time value.
253 If DATE is malformed, return a time value of zeros."
254 (condition-case ()
255 (date-to-time date)
256 (error '(0 0))))
259 ;;;###autoload
260 (defun format-seconds (string seconds)
261 "Use format control STRING to format the number SECONDS.
262 The valid format specifiers are:
263 %y is the number of (365-day) years.
264 %d is the number of days.
265 %h is the number of hours.
266 %m is the number of minutes.
267 %s is the number of seconds.
268 %z is a non-printing control flag (see below).
269 %% is a literal \"%\".
271 Upper-case specifiers are followed by the unit-name (e.g. \"years\").
272 Lower-case specifiers return only the unit.
274 \"%\" may be followed by a number specifying a width, with an
275 optional leading \".\" for zero-padding. For example, \"%.3Y\" will
276 return something of the form \"001 year\".
278 The \"%z\" specifier does not print anything. When it is used, specifiers
279 must be given in order of decreasing size. To the left of \"%z\", nothing
280 is output until the first non-zero unit is encountered.
282 This function does not work for SECONDS greater than `most-positive-fixnum'."
283 (let ((start 0)
284 (units '(("y" "year" 31536000)
285 ("d" "day" 86400)
286 ("h" "hour" 3600)
287 ("m" "minute" 60)
288 ("s" "second" 1)
289 ("z")))
290 (case-fold-search t)
291 spec match usedunits zeroflag larger prev name unit num zeropos)
292 (while (string-match "%\\.?[0-9]*\\(.\\)" string start)
293 (setq start (match-end 0)
294 spec (match-string 1 string))
295 (unless (string-equal spec "%")
296 (or (setq match (assoc (downcase spec) units))
297 (error "Bad format specifier: `%s'" spec))
298 (if (assoc (downcase spec) usedunits)
299 (error "Multiple instances of specifier: `%s'" spec))
300 (if (string-equal (car match) "z")
301 (setq zeroflag t)
302 (unless larger
303 (setq unit (nth 2 match)
304 larger (and prev (> unit prev))
305 prev unit)))
306 (push match usedunits)))
307 (and zeroflag larger
308 (error "Units are not in decreasing order of size"))
309 (dolist (u units)
310 (setq spec (car u)
311 name (cadr u)
312 unit (nth 2 u))
313 (when (string-match (format "%%\\(\\.?[0-9]+\\)?\\(%s\\)" spec) string)
314 (if (string-equal spec "z") ; must be last in units
315 (setq string
316 (replace-regexp-in-string
317 "%z" ""
318 (substring string (min (or zeropos (match-end 0))
319 (match-beginning 0)))))
320 ;; Cf article-make-date-line in gnus-art.
321 (setq num (floor seconds unit)
322 seconds (- seconds (* num unit)))
323 ;; Start position of the first non-zero unit.
324 (or zeropos
325 (setq zeropos (unless (zerop num) (match-beginning 0))))
326 (setq string
327 (replace-match
328 (format (concat "%" (match-string 1 string) "d%s") num
329 (if (string-equal (match-string 2 string) spec)
330 "" ; lower-case, no unit-name
331 (format " %s%s" name
332 (if (= num 1) "" "s"))))
333 t t string))))))
334 (replace-regexp-in-string "%%" "%" string))
336 (defvar seconds-to-string
337 (list (list 1 "ms" 0.001)
338 (list 100 "s" 1)
339 (list (* 60 100) "m" 60.0)
340 (list (* 3600 30) "h" 3600.0)
341 (list (* 3600 24 400) "d" (* 3600.0 24.0))
342 (list nil "y" (* 365.25 24 3600)))
343 "Formatting used by the function `seconds-to-string'.")
344 ;;;###autoload
345 (defun seconds-to-string (delay)
346 "Convert the time interval in seconds to a short string."
347 (cond ((> 0 delay) (concat "-" (seconds-to-string (- delay))))
348 ((= 0 delay) "0s")
349 (t (let ((sts seconds-to-string) here)
350 (while (and (car (setq here (pop sts)))
351 (<= (car here) delay)))
352 (concat (format "%.2f" (/ delay (car (cddr here)))) (cadr here))))))
354 (provide 'time-date)
356 ;;; time-date.el ends here