Mercurial > emacs
comparison lisp/calendar/time-date.el @ 91831:41b16ef40f59
Re-fill copyright years.
(format-seconds): New function.
(emacs-uptime): Use format-seconds.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Thu, 14 Feb 2008 08:58:42 +0000 |
parents | f9b4ba32c775 |
children | f47b22aa9aac |
comparison
equal
deleted
inserted
replaced
91830:98b6c9d8faca | 91831:41b16ef40f59 |
---|---|
1 ;;; time-date.el --- Date and time handling functions | 1 ;;; time-date.el --- Date and time handling functions |
2 | 2 |
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 | 3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, |
4 ;; Free Software Foundation, Inc. | 4 ;; 2007, 2008 Free Software Foundation, Inc. |
5 | 5 |
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | 6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> |
7 ;; Masanobu Umeda <umerin@mse.kyutech.ac.jp> | 7 ;; Masanobu Umeda <umerin@mse.kyutech.ac.jp> |
8 ;; Keywords: mail news util | 8 ;; Keywords: mail news util |
9 | 9 |
254 (date-to-time date) | 254 (date-to-time date) |
255 (error '(0 0)))) | 255 (error '(0 0)))) |
256 | 256 |
257 | 257 |
258 ;;;###autoload | 258 ;;;###autoload |
259 (defun format-seconds (string seconds &optional nonzero) | |
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. | |
267 %% is a literal \"%\". | |
268 | |
269 Upper-case specifiers are followed by the unit-name (e.g. \"years\"). | |
270 Lower-case specifiers return only the unit. | |
271 | |
272 \"%\" may be followed by a number specifying a width, with an | |
273 optional leading \".\" for zero-padding. For example, \"%.3Y\" will | |
274 return something of the form \"001 year\". | |
275 | |
276 If the optional argument NONZERO is non-nil, then nothing is output until | |
277 the first non-zero unit (or the last unit) is encountered. In this case, | |
278 specifiers must be used in order of decreasing size. | |
279 | |
280 This does not work for input SECONDS greater than `most-positive-fixnum'." | |
281 (let ((start 0) | |
282 (units '(("y" "year" 31536000) | |
283 ("d" "day" 86400) | |
284 ("h" "hour" 3600) | |
285 ("m" "minute" 60) | |
286 ("s" "second" 1))) | |
287 (case-fold-search t) | |
288 spec match outunits unit prev name next) | |
289 (setq nonzero (not nonzero)) | |
290 (while (string-match "%\\.?[0-9]*\\(.\\)" string start) | |
291 (setq start (match-end 0) | |
292 spec (match-string 1 string)) | |
293 (unless (string-equal spec "%") | |
294 (or (setq match (assoc-string spec units t)) | |
295 (error "Bad format specifier: `%s'" spec)) | |
296 (if (assoc-string spec outunits t) | |
297 (error "Multiple instances of specifier: `%s'" spec)) | |
298 (unless nonzero | |
299 (setq unit (nth 2 match)) | |
300 (and prev (> unit prev) | |
301 (error "Units are not in decreasing order of size")) | |
302 (setq prev unit)) | |
303 (push match outunits))) | |
304 ;; Cf article-make-date-line in gnus-art. | |
305 (dolist (ulist units) | |
306 (setq spec (car ulist) | |
307 name (cadr ulist) | |
308 unit (nth 2 ulist)) | |
309 (when (string-match (format "%%\\(\\.?[0-9]+\\)?\\(%s\\)" spec) string) | |
310 (setq num (floor seconds unit) | |
311 seconds (- seconds (* num unit))) | |
312 (or nonzero | |
313 (setq nonzero (not (zerop num))) | |
314 ;; Start of the next unit specifier, if there is one. | |
315 (setq next (save-match-data | |
316 (string-match "%\\.?[0-9]*[a-z]" | |
317 string (match-end 0))))) | |
318 ;; If there are no more specifiers, we have to print this one, | |
319 ;; even if it is zero. | |
320 (or nonzero (setq nonzero (not next))) | |
321 (setq string | |
322 (if nonzero | |
323 (replace-match | |
324 (format (concat "%" (match-string 1 string) "d%s") num | |
325 (if (string-equal (match-string 2 string) spec) | |
326 "" ; lower-case, no unit-name | |
327 (format " %s%s" name | |
328 (if (= num 1) "" "s")))) | |
329 t t string) | |
330 ;; If we haven't found a non-zero unit yet, delete | |
331 ;; everything up to the next format specifier. | |
332 (substring string next)))))) | |
333 (replace-regexp-in-string "%%" "%" string)) | |
334 | |
335 | |
336 ;; This doesn't really belong here - perhaps in time.el? | |
337 ;;;###autoload | |
259 (defun emacs-uptime () | 338 (defun emacs-uptime () |
260 "Return a string giving the uptime of this instance of Emacs." | 339 "Return a string giving the uptime of this instance of Emacs." |
261 (interactive) | 340 (interactive) |
262 (let* ((sec (time-to-seconds | 341 (let ((str |
263 (time-subtract (current-time) emacs-startup-time))) | 342 (format-seconds "%Y, %D, %H, %M, %S" |
264 (prev) | 343 (time-to-seconds |
265 (num) | 344 (time-subtract (current-time) emacs-startup-time)) |
266 (str | 345 t))) |
267 ;; cf article-make-date-line in gnus-art. | |
268 ;; Worth having a general time-date `format-seconds' | |
269 ;; function that converts a number of seconds into so many | |
270 ;; years, hours, etc? | |
271 (mapconcat | |
272 (lambda (unit) | |
273 (if (zerop (setq num (floor sec (cdr unit)))) | |
274 "" | |
275 (setq sec (- sec (* num (cdr unit)))) | |
276 (prog1 | |
277 (format "%s%d %s%s" (if prev ", " "") num | |
278 (symbol-name (car unit)) | |
279 (if (= num 1) "" "s")) | |
280 (setq prev t)))) | |
281 '((year . 31536000) ; 365-day year | |
282 (day . 86400) | |
283 (hour . 3600) | |
284 (minute . 60) | |
285 (second . 1)) | |
286 ""))) | |
287 (if (interactive-p) | 346 (if (interactive-p) |
288 (message "%s" str) | 347 (message "%s" str) |
289 str))) | 348 str))) |
290 | 349 |
291 (provide 'time-date) | 350 (provide 'time-date) |