Commit | Line | Data |
---|---|---|
707f2b38 | 1 | ;;; time-date.el --- Date and time handling functions |
dbfca9c4 | 2 | |
6afa3d67 GM |
3 | ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, |
4 | ;; 2007, 2008 Free Software Foundation, Inc. | |
c113de23 GM |
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 | |
075969b4 | 14 | ;; the Free Software Foundation; either version 3, or (at your option) |
c113de23 GM |
15 | ;; 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; see the file COPYING. If not, write to the | |
3a35cf56 LK |
24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 | ;; Boston, MA 02110-1301, USA. | |
c113de23 GM |
26 | |
27 | ;;; Commentary: | |
28 | ||
ca2d9ad8 LK |
29 | ;; Time values come in three formats. The oldest format is a cons |
30 | ;; cell of the form (HIGH . LOW). This format is obsolete, but still | |
31 | ;; supported. The two other formats are the lists (HIGH LOW) and | |
32 | ;; (HIGH LOW MICRO). The first two formats specify HIGH * 2^16 + LOW | |
33 | ;; seconds; the third format specifies HIGH * 2^16 + LOW + MICRO / | |
34 | ;; 1000000 seconds. We should have 0 <= MICRO < 1000000 and 0 <= LOW | |
35 | ;; < 2^16. If the time value represents a point in time, then HIGH is | |
36 | ;; nonnegative. If the time value is a time difference, then HIGH can | |
37 | ;; be negative as well. The macro `with-decoded-time-value' and the | |
38 | ;; function `encode-time-value' make it easier to deal with these | |
39 | ;; three formats. See `time-subtract' for an example of how to use | |
40 | ;; them. | |
41 | ||
c113de23 GM |
42 | ;;; Code: |
43 | ||
ca2d9ad8 LK |
44 | (defmacro with-decoded-time-value (varlist &rest body) |
45 | "Decode a time value and bind it according to VARLIST, then eval BODY. | |
46 | ||
47 | The value of the last form in BODY is returned. | |
48 | ||
49 | Each element of the list VARLIST is a list of the form | |
50 | \(HIGH-SYMBOL LOW-SYMBOL MICRO-SYMBOL [TYPE-SYMBOL] TIME-VALUE). | |
51 | The time value TIME-VALUE is decoded and the result it bound to | |
52 | the symbols HIGH-SYMBOL, LOW-SYMBOL and MICRO-SYMBOL. | |
53 | ||
54 | The optional TYPE-SYMBOL is bound to the type of the time value. | |
55 | Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH | |
e07ea833 | 56 | LOW), and type 2 is the list (HIGH LOW MICRO)." |
ca2d9ad8 LK |
57 | (declare (indent 1) |
58 | (debug ((&rest (symbolp symbolp symbolp &or [symbolp form] form)) | |
59 | body))) | |
60 | (if varlist | |
61 | (let* ((elt (pop varlist)) | |
62 | (high (pop elt)) | |
63 | (low (pop elt)) | |
64 | (micro (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 ,time-value) | |
70 | (,high (pop ,gensym)) | |
71 | ,low ,micro) | |
72 | (when type `(,type))) | |
73 | (if (consp ,gensym) | |
74 | (progn | |
75 | (setq ,low (pop ,gensym)) | |
76 | (if ,gensym | |
77 | ,(append `(setq ,micro (car ,gensym)) | |
78 | (when type `(,type 2))) | |
79 | ,(append `(setq ,micro 0) | |
80 | (when type `(,type 1))))) | |
81 | ,(append `(setq ,low ,gensym ,micro 0) | |
82 | (when type `(,type 0)))) | |
83 | (with-decoded-time-value ,varlist ,@body))) | |
84 | `(progn ,@body))) | |
85 | ||
86 | (defun encode-time-value (high low micro type) | |
87 | "Encode HIGH, LOW, and MICRO into a time value of type TYPE. | |
88 | Type 0 is the cons cell (HIGH . LOW), type 1 is the list (HIGH LOW), | |
e4b07c8b | 89 | and type 2 is the list (HIGH LOW MICRO)." |
ca2d9ad8 LK |
90 | (cond |
91 | ((eq type 0) (cons high low)) | |
92 | ((eq type 1) (list high low)) | |
93 | ((eq type 2) (list high low micro)))) | |
c113de23 | 94 | |
bbcb5072 | 95 | (autoload 'parse-time-string "parse-time") |
ce2a8a6a DL |
96 | (autoload 'timezone-make-date-arpa-standard "timezone") |
97 | ||
c113de23 GM |
98 | ;;;###autoload |
99 | (defun date-to-time (date) | |
7d4005cc | 100 | "Parse a string DATE that represents a date-time and return a time value." |
c113de23 | 101 | (condition-case () |
ce2a8a6a DL |
102 | (apply 'encode-time |
103 | (parse-time-string | |
104 | ;; `parse-time-string' isn't sufficiently general or | |
105 | ;; robust. It fails to grok some of the formats that | |
23f87bed | 106 | ;; timezone does (e.g. dodgy post-2000 stuff from some |
ce2a8a6a DL |
107 | ;; Elms) and either fails or returns bogus values. Lars |
108 | ;; reverted this change, but that loses non-trivially | |
109 | ;; often for me. -- fx | |
110 | (timezone-make-date-arpa-standard date))) | |
c113de23 GM |
111 | (error (error "Invalid date: %s" date)))) |
112 | ||
0e48150a | 113 | ;;;###autoload |
c113de23 | 114 | (defun time-to-seconds (time) |
74fcda73 RS |
115 | "Convert time value TIME to a floating point number. |
116 | You can use `float-time' instead." | |
ca2d9ad8 | 117 | (with-decoded-time-value ((high low micro time)) |
91472578 | 118 | (+ (* 1.0 high 65536) |
ca2d9ad8 LK |
119 | low |
120 | (/ micro 1000000.0)))) | |
c113de23 | 121 | |
74fcda73 | 122 | ;;;###autoload |
c113de23 | 123 | (defun seconds-to-time (seconds) |
74fcda73 | 124 | "Convert SECONDS (a floating point number) to a time value." |
91472578 MB |
125 | (list (floor seconds 65536) |
126 | (floor (mod seconds 65536)) | |
c113de23 GM |
127 | (floor (* (- seconds (ffloor seconds)) 1000000)))) |
128 | ||
74fcda73 | 129 | ;;;###autoload |
c113de23 | 130 | (defun time-less-p (t1 t2) |
74fcda73 | 131 | "Say whether time value T1 is less than time value T2." |
ca2d9ad8 LK |
132 | (with-decoded-time-value ((high1 low1 micro1 t1) |
133 | (high2 low2 micro2 t2)) | |
134 | (or (< high1 high2) | |
135 | (and (= high1 high2) | |
136 | (or (< low1 low2) | |
137 | (and (= low1 low2) | |
138 | (< micro1 micro2))))))) | |
c113de23 | 139 | |
74fcda73 | 140 | ;;;###autoload |
c113de23 | 141 | (defun days-to-time (days) |
74fcda73 | 142 | "Convert DAYS into a time value." |
c113de23 | 143 | (let* ((seconds (* 1.0 days 60 60 24)) |
91472578 | 144 | (high (condition-case nil (floor (/ seconds 65536)) |
ca2d9ad8 | 145 | (range-error most-positive-fixnum)))) |
91472578 MB |
146 | (list high (condition-case nil (floor (- seconds (* 1.0 high 65536))) |
147 | (range-error 65535))))) | |
c113de23 | 148 | |
74fcda73 | 149 | ;;;###autoload |
c113de23 | 150 | (defun time-since (time) |
74fcda73 RS |
151 | "Return the time elapsed since TIME. |
152 | TIME should be either a time value or a date-time string." | |
c113de23 GM |
153 | (when (stringp time) |
154 | ;; Convert date strings to internal time. | |
155 | (setq time (date-to-time time))) | |
ca2d9ad8 | 156 | (time-subtract (current-time) time)) |
c113de23 | 157 | |
74fcda73 RS |
158 | ;;;###autoload |
159 | (defalias 'subtract-time 'time-subtract) | |
160 | ||
161 | ;;;###autoload | |
162 | (defun time-subtract (t1 t2) | |
7d4005cc | 163 | "Subtract two time values, T1 minus T2. |
74fcda73 | 164 | Return the difference in the format of a time value." |
ca2d9ad8 LK |
165 | (with-decoded-time-value ((high low micro type t1) |
166 | (high2 low2 micro2 type2 t2)) | |
167 | (setq high (- high high2) | |
168 | low (- low low2) | |
169 | micro (- micro micro2) | |
170 | type (max type type2)) | |
171 | (when (< micro 0) | |
172 | (setq low (1- low) | |
173 | micro (+ micro 1000000))) | |
174 | (when (< low 0) | |
175 | (setq high (1- high) | |
91472578 | 176 | low (+ low 65536))) |
ca2d9ad8 | 177 | (encode-time-value high low micro type))) |
c113de23 | 178 | |
74fcda73 RS |
179 | ;;;###autoload |
180 | (defun time-add (t1 t2) | |
7d4005cc | 181 | "Add two time values T1 and T2. One should represent a time difference." |
ca2d9ad8 LK |
182 | (with-decoded-time-value ((high low micro type t1) |
183 | (high2 low2 micro2 type2 t2)) | |
184 | (setq high (+ high high2) | |
185 | low (+ low low2) | |
186 | micro (+ micro micro2) | |
187 | type (max type type2)) | |
188 | (when (>= micro 1000000) | |
189 | (setq low (1+ low) | |
190 | micro (- micro 1000000))) | |
91472578 | 191 | (when (>= low 65536) |
ca2d9ad8 | 192 | (setq high (1+ high) |
91472578 | 193 | low (- low 65536))) |
ca2d9ad8 | 194 | (encode-time-value high low micro type))) |
74fcda73 RS |
195 | |
196 | ;;;###autoload | |
c113de23 | 197 | (defun date-to-day (date) |
74fcda73 RS |
198 | "Return the number of days between year 1 and DATE. |
199 | DATE should be a date-time string." | |
c113de23 GM |
200 | (time-to-days (date-to-time date))) |
201 | ||
74fcda73 | 202 | ;;;###autoload |
c113de23 | 203 | (defun days-between (date1 date2) |
74fcda73 RS |
204 | "Return the number of days between DATE1 and DATE2. |
205 | DATE1 and DATE2 should be date-time strings." | |
c113de23 GM |
206 | (- (date-to-day date1) (date-to-day date2))) |
207 | ||
74fcda73 | 208 | ;;;###autoload |
c113de23 GM |
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)))) | |
214 | ||
74fcda73 | 215 | ;;;###autoload |
c113de23 | 216 | (defun time-to-day-in-year (time) |
1525ea1e | 217 | "Return the day number within the year corresponding to TIME." |
c113de23 GM |
218 | (let* ((tim (decode-time time)) |
219 | (month (nth 4 tim)) | |
220 | (day (nth 3 tim)) | |
221 | (year (nth 5 tim)) | |
222 | (day-of-year (+ day (* 31 (1- month))))) | |
223 | (when (> month 2) | |
224 | (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10))) | |
225 | (when (date-leap-year-p year) | |
226 | (setq day-of-year (1+ day-of-year)))) | |
227 | day-of-year)) | |
228 | ||
74fcda73 | 229 | ;;;###autoload |
c113de23 GM |
230 | (defun time-to-days (time) |
231 | "The number of days between the Gregorian date 0001-12-31bce and TIME. | |
74fcda73 | 232 | TIME should be a time value. |
c113de23 GM |
233 | The Gregorian date Sunday, December 31, 1bce is imaginary." |
234 | (let* ((tim (decode-time time)) | |
235 | (month (nth 4 tim)) | |
236 | (day (nth 3 tim)) | |
237 | (year (nth 5 tim))) | |
238 | (+ (time-to-day-in-year time) ; Days this year | |
239 | (* 365 (1- year)) ; + Days in prior years | |
240 | (/ (1- year) 4) ; + Julian leap years | |
241 | (- (/ (1- year) 100)) ; - century years | |
242 | (/ (1- year) 400)))) ; + Gregorian leap years | |
243 | ||
23f87bed MB |
244 | (defun time-to-number-of-days (time) |
245 | "Return the number of days represented by TIME. | |
246 | The number of days will be returned as a floating point number." | |
ca2d9ad8 | 247 | (/ (time-to-seconds time) (* 60 60 24))) |
23f87bed | 248 | |
c113de23 GM |
249 | ;;;###autoload |
250 | (defun safe-date-to-time (date) | |
7d4005cc | 251 | "Parse a string DATE that represents a date-time and return a time value. |
74fcda73 | 252 | If DATE is malformed, return a time value of zeros." |
c113de23 GM |
253 | (condition-case () |
254 | (date-to-time date) | |
255 | (error '(0 0)))) | |
256 | ||
9ce1b62f | 257 | \f |
6afa3d67 | 258 | ;;;###autoload |
99d8d540 | 259 | (defun format-seconds (string seconds) |
6afa3d67 GM |
260 | "Use format control STRING to format the number SECONDS. |
261 | The valid format specifiers are: | |
262 | %y is the number of (365-day) years. | |
263 | %d is the number of days. | |
264 | %h is the number of hours. | |
265 | %m is the number of minutes. | |
266 | %s is the number of seconds. | |
99d8d540 | 267 | %z is a non-printing control flag (see below). |
6afa3d67 GM |
268 | %% is a literal \"%\". |
269 | ||
270 | Upper-case specifiers are followed by the unit-name (e.g. \"years\"). | |
271 | Lower-case specifiers return only the unit. | |
272 | ||
273 | \"%\" may be followed by a number specifying a width, with an | |
274 | optional leading \".\" for zero-padding. For example, \"%.3Y\" will | |
275 | return something of the form \"001 year\". | |
276 | ||
99d8d540 GM |
277 | The \"%z\" specifier does not print anything. When it is used, specifiers |
278 | must be given in order of decreasing size. To the left of \"%z\", nothing | |
279 | is output until the first non-zero unit is encountered. | |
6afa3d67 | 280 | |
99d8d540 | 281 | This function does not work for SECONDS greater than `most-positive-fixnum'." |
6afa3d67 GM |
282 | (let ((start 0) |
283 | (units '(("y" "year" 31536000) | |
284 | ("d" "day" 86400) | |
285 | ("h" "hour" 3600) | |
286 | ("m" "minute" 60) | |
99d8d540 GM |
287 | ("s" "second" 1) |
288 | ("z"))) | |
6afa3d67 | 289 | (case-fold-search t) |
99d8d540 | 290 | spec match usedunits zeroflag larger prev name unit num zeropos) |
6afa3d67 GM |
291 | (while (string-match "%\\.?[0-9]*\\(.\\)" string start) |
292 | (setq start (match-end 0) | |
293 | spec (match-string 1 string)) | |
294 | (unless (string-equal spec "%") | |
295 | (or (setq match (assoc-string spec units t)) | |
296 | (error "Bad format specifier: `%s'" spec)) | |
99d8d540 | 297 | (if (assoc-string spec usedunits t) |
6afa3d67 | 298 | (error "Multiple instances of specifier: `%s'" spec)) |
99d8d540 GM |
299 | (if (string-equal (car match) "z") |
300 | (setq zeroflag t) | |
301 | (unless larger | |
302 | (setq unit (nth 2 match) | |
303 | larger (and prev (> unit prev)) | |
304 | prev unit))) | |
305 | (push match usedunits))) | |
306 | (and zeroflag larger | |
307 | (error "Units are not in decreasing order of size")) | |
308 | (dolist (u units) | |
309 | (setq spec (car u) | |
310 | name (cadr u) | |
311 | unit (nth 2 u)) | |
6afa3d67 | 312 | (when (string-match (format "%%\\(\\.?[0-9]+\\)?\\(%s\\)" spec) string) |
99d8d540 GM |
313 | (if (string-equal spec "z") ; must be last in units |
314 | (setq string | |
315 | (replace-regexp-in-string | |
316 | "%z" "" | |
317 | (substring string (min (or zeropos (match-end 0)) | |
318 | (match-beginning 0))))) | |
319 | ;; Cf article-make-date-line in gnus-art. | |
320 | (setq num (floor seconds unit) | |
321 | seconds (- seconds (* num unit))) | |
322 | ;; Start position of the first non-zero unit. | |
323 | (or zeropos | |
324 | (setq zeropos (unless (zerop num) (match-beginning 0)))) | |
325 | (setq string | |
326 | (replace-match | |
327 | (format (concat "%" (match-string 1 string) "d%s") num | |
328 | (if (string-equal (match-string 2 string) spec) | |
329 | "" ; lower-case, no unit-name | |
330 | (format " %s%s" name | |
331 | (if (= num 1) "" "s")))) | |
332 | t t string)))))) | |
6afa3d67 GM |
333 | (replace-regexp-in-string "%%" "%" string)) |
334 | ||
9ce1b62f | 335 | |
c113de23 GM |
336 | (provide 'time-date) |
337 | ||
cbee283d | 338 | ;; arch-tag: addcf07b-b20a-465b-af72-550b8ac5190f |
c113de23 | 339 | ;;; time-date.el ends here |