(format-seconds): Remove `nonzero' argument in favor of `%z' specifier.
[bpt/emacs.git] / lisp / time.el
CommitLineData
b7eb6e8a 1;;; time.el --- display time, load and mail indicator in mode line of Emacs -*-coding: utf-8 -*-
c2acf685 2
0d30b337 3;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1996, 2000, 2001, 2002,
409cc4a3 4;; 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
c2acf685
SM
5
6;; Maintainer: FSF
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
b4aa6026 12;; the Free Software Foundation; either version 3, or (at your option)
c2acf685
SM
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
c2acf685
SM
24
25;;; Commentary:
26
27;; Facilities to display current time/date and a new-mail indicator
aff2ba04
RS
28;; in the Emacs mode line. The entry point is `display-time'.
29
30;; Display time world in a buffer, the entry point is
31;; `display-time-world'.
c2acf685
SM
32
33;;; Code:
34
35(defgroup display-time nil
36 "Display time and load in mode line of Emacs."
97904e51 37 :group 'mode-line
c2acf685
SM
38 :group 'mail)
39
40
c2acf685
SM
41(defcustom display-time-mail-file nil
42 "*File name of mail inbox file, for indicating existence of new mail.
cbb7527f 43Non-nil and not a string means don't check for mail; nil means use
c2acf685 44default, which is system-dependent, and is the same as used by Rmail."
fe6456af 45 :type '(choice (const :tag "None" none)
c2acf685
SM
46 (const :tag "Default" nil)
47 (file :format "%v"))
48 :group 'display-time)
49
cc351ed4
PJ
50(defcustom display-time-mail-directory nil
51 "*Name of mail inbox directory, for indicating existence of new mail.
ad68bb68
RS
52Any nonempty regular file in the directory is regarded as newly arrived mail.
53If nil, do not check a directory for arriving mail."
cc351ed4
PJ
54 :type '(choice (const :tag "None" nil)
55 (directory :format "%v"))
56 :group 'display-time)
57
c2acf685
SM
58(defcustom display-time-mail-function nil
59 "*Function to call, for indicating existence of new mail.
ad68bb68
RS
60If nil, that means use the default method: check that the file
61specified by `display-time-mail-file' is nonempty or that the
62directory `display-time-mail-directory' contains nonempty files."
c2acf685
SM
63 :type '(choice (const :tag "Default" nil)
64 (function))
65 :group 'display-time)
66
4b05e68d 67(defcustom display-time-default-load-average 0
b7eb6e8a 68 "*Which load average value will be shown in the mode line.
4b05e68d
PJ
69Almost every system can provide values of load for past 1 minute, past 5 or
70past 15 minutes. The default is to display 1 minute load average."
71 :type '(choice (const :tag "1 minute load" 0)
72 (const :tag "5 minutes load" 1)
fe6456af
RS
73 (const :tag "15 minutes load" 2)
74 (const :tag "None" nil))
4b05e68d
PJ
75 :group 'display-time)
76
fe6456af 77(defvar display-time-load-average nil
b7eb6e8a 78 "Load average currently being shown in mode line.")
4b05e68d
PJ
79
80(defcustom display-time-load-average-threshold 0.1
b7b4e4d4
PJ
81 "*Load-average values below this value won't be shown in the mode line."
82 :type 'number
83 :group 'display-time)
4b05e68d 84
c2acf685
SM
85;;;###autoload
86(defcustom display-time-day-and-date nil "\
87*Non-nil means \\[display-time] should display day and date as well as time."
88 :type 'boolean
89 :group 'display-time)
90
91(defvar display-time-timer nil)
92
93(defcustom display-time-interval 60
94 "*Seconds between updates of time in the mode line."
95 :type 'integer
96 :group 'display-time)
97
98(defcustom display-time-24hr-format nil
99 "*Non-nil indicates time should be displayed as hh:mm, 0 <= hh <= 23.
7d326782 100A value of nil means 1 <= hh <= 12, and an AM/PM suffix is used."
c2acf685
SM
101 :type 'boolean
102 :group 'display-time)
103
104(defvar display-time-string nil)
105
106(defcustom display-time-hook nil
107 "*List of functions to be called when the time is updated on the mode line."
108 :type 'hook
109 :group 'display-time)
110
111(defvar display-time-server-down-time nil
112 "Time when mail file's file system was recorded to be down.
113If that file system seems to be up, the value is nil.")
114
dca2168c 115(defcustom zoneinfo-style-world-list
aff2ba04
RS
116 '(("America/Los_Angeles" "Seattle")
117 ("America/New_York" "New York")
118 ("Europe/London" "London")
119 ("Europe/Paris" "Paris")
120 ("Asia/Calcutta" "Bangalore")
121 ("Asia/Tokyo" "Tokyo"))
dca2168c
EZ
122 "Alist of zoneinfo-style time zones and places for `display-time-world'.
123Each element has the form (TIMEZONE LABEL).
124TIMEZONE should be a string of the form AREA/LOCATION, where AREA is
125the name of a region -- a continent or ocean, and LOCATION is the name
126of a specific location, e.g., a city, within that region.
127LABEL is a string to display as the label of that TIMEZONE's time."
128 :group 'display-time
129 :type '(repeat (list string string))
130 :version "23.1")
131
132(defcustom legacy-style-world-list
133 '(("PST8PDT" "Seattle")
134 ("EST5EDT" "New York")
9db0c29b 135 ("GMT0BST" "London")
dca2168c 136 ("CET-1CDT" "Paris")
9db0c29b
EZ
137 ("IST-5:30" "Bangalore")
138 ("JST-9" "Tokyo"))
dca2168c
EZ
139 "Alist of traditional-style time zones and places for `display-time-world'.
140Each element has the form (TIMEZONE LABEL).
141TIMEZONE should be a string of the form:
142
143 std[+|-]offset[dst[offset][,date[/time],date[/time]]]
144
145See the documentation of the TZ environment variable on your system,
146for more details about the format of TIMEZONE.
147LABEL is a string to display as the label of that TIMEZONE's time."
148 :group 'display-time
149 :type '(repeat (list string string))
150 :version "23.1")
151
152(defcustom display-time-world-list
36d4b145
JR
153 ;; Determine if zoneinfo style timezones are supported by testing that
154 ;; America/New York and Europe/London return different timezones.
155 (let (gmt nyt)
156 (set-time-zone-rule "America/New York")
157 (setq nyt (format-time-string "%z"))
158 (set-time-zone-rule "Europe/London")
159 (setq gmt (format-time-string "%z"))
160 (set-time-zone-rule nil)
161 (if (string-equal nyt gmt)
162 legacy-style-world-list
163 zoneinfo-style-world-list))
dca2168c 164 "Alist of time zones and places for `display-time-world' to display.
aff2ba04 165Each element has the form (TIMEZONE LABEL).
dca2168c
EZ
166TIMEZONE should be in the format supported by `set-time-zone-rule' on
167your system. See the documentation of `zoneinfo-style-world-list' and
168\`legacy-style-world-list' for two widely used formats.
169LABEL is a string to display as the label of that TIMEZONE's time."
aff2ba04
RS
170 :group 'display-time
171 :type '(repeat (list string string))
172 :version "23.1")
173
658dcd47 174(defcustom display-time-world-time-format "%A %d %B %R %Z"
aff2ba04
RS
175 "Format of the time displayed, see `format-time-string'."
176 :group 'display-time
177 :type 'string
178 :version "23.1")
179
180(defcustom display-time-world-buffer-name "*wclock*"
181 "Name of the wclock buffer."
182 :group 'display-time
183 :type 'string
184 :version "23.1")
185
186(defcustom display-time-world-timer-enable t
187 "If non-nil, a timer will update the world clock."
188 :group 'display-time
189 :type 'boolean
190 :version "23.1")
191
192(defcustom display-time-world-timer-second 60
193 "Interval in seconds for updating the world clock."
194 :group 'display-time
195 :type 'integer
196 :version "23.1")
197
198(defvar display-time-world-mode-map
199 (let ((map (make-sparse-keymap)))
200 (define-key map "q" 'kill-this-buffer)
201 map)
202 "Keymap of Display Time World mode")
203
c2acf685
SM
204;;;###autoload
205(defun display-time ()
206 "Enable display of time, load level, and mail flag in mode lines.
207This display updates automatically every minute.
208If `display-time-day-and-date' is non-nil, the current day and date
209are displayed as well.
210This runs the normal hook `display-time-hook' after each update."
211 (interactive)
212 (display-time-mode 1))
213
b7eb6e8a
DL
214;; This business used to be simpler when all mode lines had the same
215;; face and the image could just be pbm. Now we try to rely on an xpm
216;; image with a transparent background. Otherwise, set the background
217;; for pbm.
218
219(defcustom display-time-mail-face nil
c2acf685 220 "Face to use for `display-time-mail-string'.
b7eb6e8a 221If `display-time-use-mail-icon' is non-nil, the image's
6bd47b39
JB
222background color is the background of this face. Set this to
223make the mail indicator stand out on a color display."
83033a3a 224 :group 'mode-line-faces
c2acf685 225 :group 'display-time
bf247b6e 226 :version "22.1"
b7eb6e8a 227 :type '(choice (const :tag "None" nil) face))
c2acf685
SM
228
229(defvar display-time-mail-icon
cc351ed4 230 (find-image '((:type xpm :file "letter.xpm" :ascent center)
b7eb6e8a
DL
231 (:type pbm :file "letter.pbm" :ascent center)))
232 "Image specification to offer as the mail indicator on a graphic display.
233See `display-time-use-mail-icon' and `display-time-mail-face'.")
c2acf685 234
b7eb6e8a 235;; Fixme: Default to icon on graphical display?
c2acf685 236(defcustom display-time-use-mail-icon nil
d7baab9c 237 "Non-nil means use an icon as mail indicator on a graphic display.
b7eb6e8a
DL
238Otherwise use `display-time-mail-string'. The icon may consume less
239of the mode line. It is specified by `display-time-mail-icon'."
c2acf685
SM
240 :group 'display-time
241 :type 'boolean)
242
b7eb6e8a
DL
243;; Fixme: maybe default to the character if we can display Unicode.
244(defcustom display-time-mail-string "Mail"
245 "String to use as the mail indicator in `display-time-string-forms'.
246This can use the Unicode letter character if you can display it."
247 :group 'display-time
bf247b6e 248 :version "22.1"
b7eb6e8a
DL
249 :type '(choice (const "Mail")
250 ;; Use :tag here because the Lucid menu won't display
251 ;; multibyte text.
252 (const :tag "Unicode letter character" "✉")
253 string))
254
c2acf685 255(defcustom display-time-format nil
d7baab9c 256 "*String specifying format for displaying the time in the mode line.
c2acf685
SM
257See the function `format-time-string' for an explanation of
258how to write this string. If this is nil, the defaults
259depend on `display-time-day-and-date' and `display-time-24hr-format'."
260 :type '(choice (const :tag "Default" nil)
261 string)
262 :group 'display-time)
263
264(defcustom display-time-string-forms
265 '((if (and (not display-time-format) display-time-day-and-date)
266 (format-time-string "%a %b %e " now)
267 "")
e93b2f39
SM
268 (propertize
269 (format-time-string (or display-time-format
270 (if display-time-24hr-format "%H:%M" "%-I:%M%p"))
271 now)
ef3e9175 272 'help-echo (format-time-string "%a %b %e, %Y" now))
c2acf685
SM
273 load
274 (if mail
275 ;; Build the string every time to act on customization.
b7eb6e8a
DL
276 ;; :set-after doesn't help for `customize-option'. I think it
277 ;; should.
278 (concat
279 " "
280 (propertize
281 display-time-mail-string
282 'display `(when (and display-time-use-mail-icon
283 (display-graphic-p))
284 ,@display-time-mail-icon
285 ,@(if (and display-time-mail-face
286 (memq (plist-get (cdr display-time-mail-icon)
287 :type)
288 '(pbm xbm)))
289 (let ((bg (face-attribute display-time-mail-face
290 :background)))
291 (if (stringp bg)
292 (list :background bg)))))
293 'face display-time-mail-face
294 'help-echo "You have new mail; mouse-2: Read mail"
3a02b63e 295 'mouse-face 'mode-line-highlight
b7eb6e8a
DL
296 'local-map (make-mode-line-mouse-map 'mouse-2
297 read-mail-command)))
c2acf685 298 ""))
d7baab9c 299 "*List of expressions governing display of the time in the mode line.
c2acf685
SM
300For most purposes, you can control the time format using `display-time-format'
301which is a more standard interface.
302
303This expression is a list of expressions that can involve the keywords
304`load', `day', `month', and `year', `12-hours', `24-hours', `minutes',
305`seconds', all numbers in string form, and `monthname', `dayname', `am-pm',
306and `time-zone' all alphabetic strings, and `mail' a true/nil value.
307
308For example, the form
309
310 '((substring year -2) \"/\" month \"/\" day
311 \" \" 24-hours \":\" minutes \":\" seconds
312 (if time-zone \" (\") time-zone (if time-zone \")\")
313 (if mail \" Mail\" \"\"))
314
315would give mode line times like `94/12/30 21:07:48 (UTC)'."
316 :type 'sexp
317 :group 'display-time)
318
319(defun display-time-event-handler ()
320 (display-time-update)
321 ;; Do redisplay right now, if no input pending.
322 (sit-for 0)
323 (let* ((current (current-time))
324 (timer display-time-timer)
325 ;; Compute the time when this timer will run again, next.
326 (next-time (timer-relative-time
327 (list (aref timer 1) (aref timer 2) (aref timer 3))
328 (* 5 (aref timer 4)) 0)))
329 ;; If the activation time is far in the past,
330 ;; skip executions until we reach a time in the future.
331 ;; This avoids a long pause if Emacs has been suspended for hours.
332 (or (> (nth 0 next-time) (nth 0 current))
333 (and (= (nth 0 next-time) (nth 0 current))
334 (> (nth 1 next-time) (nth 1 current)))
335 (and (= (nth 0 next-time) (nth 0 current))
336 (= (nth 1 next-time) (nth 1 current))
337 (> (nth 2 next-time) (nth 2 current)))
338 (progn
339 (timer-set-time timer (timer-next-integral-multiple-of-time
340 current display-time-interval)
341 display-time-interval)
342 (timer-activate timer)))))
343
4b05e68d
PJ
344(defun display-time-next-load-average ()
345 (interactive)
346 (if (= 3 (setq display-time-load-average (1+ display-time-load-average)))
347 (setq display-time-load-average 0))
348 (display-time-update)
349 (sit-for 0))
350
cc351ed4
PJ
351(defun display-time-mail-check-directory ()
352 (let ((mail-files (directory-files display-time-mail-directory t))
353 (size 0))
354 (while (and mail-files (= size 0))
355 ;; Count size of regular files only.
356 (setq size (+ size (or (and (file-regular-p (car mail-files))
357 (nth 7 (file-attributes (car mail-files))))
358 0)))
359 (setq mail-files (cdr mail-files)))
360 (if (> size 0)
361 size
362 nil)))
363
c2acf685 364(defun display-time-update ()
b7eb6e8a
DL
365 "Update the display-time info for the mode line.
366However, don't redisplay right now.
367
368This is used for things like Rmail `g' that want to force an
369update which can wait for the next redisplay."
c2acf685
SM
370 (let* ((now (current-time))
371 (time (current-time-string now))
fe6456af
RS
372 (load (if (null display-time-load-average)
373 ""
374 (condition-case ()
375 ;; Do not show values less than
376 ;; `display-time-load-average-threshold'.
377 (if (> (* display-time-load-average-threshold 100)
378 (nth display-time-load-average (load-average)))
379 ""
380 ;; The load average number is mysterious, so
381 ;; provide some help.
b7eb6e8a
DL
382 (let ((str (format " %03d"
383 (nth display-time-load-average
384 (load-average)))))
fe6456af
RS
385 (propertize
386 (concat (substring str 0 -2) "." (substring str -2))
b7eb6e8a
DL
387 'local-map (make-mode-line-mouse-map
388 'mouse-2 'display-time-next-load-average)
3a02b63e 389 'mouse-face 'mode-line-highlight
b7eb6e8a
DL
390 'help-echo (concat
391 "System load average for past "
392 (if (= 0 display-time-load-average)
393 "1 minute"
394 (if (= 1 display-time-load-average)
395 "5 minutes"
396 "15 minutes"))
397 "; mouse-2: next"))))
fe6456af 398 (error ""))))
c2acf685
SM
399 (mail-spool-file (or display-time-mail-file
400 (getenv "MAIL")
401 (concat rmail-spool-directory
402 (user-login-name))))
403 (mail (or (and display-time-mail-function
404 (funcall display-time-mail-function))
cc351ed4
PJ
405 (and display-time-mail-directory
406 (display-time-mail-check-directory))
c2acf685
SM
407 (and (stringp mail-spool-file)
408 (or (null display-time-server-down-time)
409 ;; If have been down for 20 min, try again.
410 (> (- (nth 1 now) display-time-server-down-time)
411 1200)
412 (and (< (nth 1 now) display-time-server-down-time)
b7eb6e8a
DL
413 (> (- (nth 1 now)
414 display-time-server-down-time)
c2acf685
SM
415 -64336)))
416 (let ((start-time (current-time)))
417 (prog1
418 (display-time-file-nonempty-p mail-spool-file)
b7eb6e8a
DL
419 (if (> (- (nth 1 (current-time))
420 (nth 1 start-time))
c2acf685
SM
421 20)
422 ;; Record that mail file is not accessible.
423 (setq display-time-server-down-time
424 (nth 1 (current-time)))
425 ;; Record that mail file is accessible.
426 (setq display-time-server-down-time nil)))))))
427 (24-hours (substring time 11 13))
027a4b6b 428 (hour (string-to-number 24-hours))
c2acf685
SM
429 (12-hours (int-to-string (1+ (% (+ hour 11) 12))))
430 (am-pm (if (>= hour 12) "pm" "am"))
431 (minutes (substring time 14 16))
432 (seconds (substring time 17 19))
433 (time-zone (car (cdr (current-time-zone now))))
434 (day (substring time 8 10))
435 (year (substring time 20 24))
436 (monthname (substring time 4 7))
437 (month
438 (cdr
439 (assoc
440 monthname
441 '(("Jan" . "1") ("Feb" . "2") ("Mar" . "3") ("Apr" . "4")
442 ("May" . "5") ("Jun" . "6") ("Jul" . "7") ("Aug" . "8")
443 ("Sep" . "9") ("Oct" . "10") ("Nov" . "11") ("Dec" . "12")))))
444 (dayname (substring time 0 3)))
445 (setq display-time-string
446 (mapconcat 'eval display-time-string-forms ""))
447 ;; This is inside the let binding, but we are not going to document
448 ;; what variables are available.
449 (run-hooks 'display-time-hook))
450 (force-mode-line-update))
451
452(defun display-time-file-nonempty-p (file)
453 (and (file-exists-p file)
454 (< 0 (nth 7 (file-attributes (file-chase-links file))))))
455
57ce63c4
SM
456;;;###autoload
457(define-minor-mode display-time-mode
458 "Toggle display of time, load level, and mail flag in mode lines.
459With a numeric arg, enable this display if arg is positive.
460
461When this display is enabled, it updates automatically every minute.
462If `display-time-day-and-date' is non-nil, the current day and date
463are displayed as well.
464This runs the normal hook `display-time-hook' after each update."
bb7a71c5 465 :global t :group 'display-time
fe6456af
RS
466 (and display-time-timer (cancel-timer display-time-timer))
467 (setq display-time-timer nil)
468 (setq display-time-string "")
469 (or global-mode-string (setq global-mode-string '("")))
470 (setq display-time-load-average display-time-default-load-average)
471 (if display-time-mode
472 (progn
473 (or (memq 'display-time-string global-mode-string)
474 (setq global-mode-string
475 (append global-mode-string '(display-time-string))))
476 ;; Set up the time timer.
477 (setq display-time-timer
478 (run-at-time t display-time-interval
479 'display-time-event-handler))
480 ;; Make the time appear right away.
481 (display-time-update)
482 ;; When you get new mail, clear "Mail" from the mode line.
483 (add-hook 'rmail-after-get-new-mail-hook
484 'display-time-event-handler))
485 (remove-hook 'rmail-after-get-new-mail-hook
486 'display-time-event-handler)))
c2acf685 487
aff2ba04
RS
488
489(defun display-time-world-mode ()
490 "Major mode for buffer that displays times in various time zones.
491See `display-time-world'."
492 (interactive)
493 (kill-all-local-variables)
494 (setq
495 major-mode 'display-time-world-mode
496 mode-name "World clock")
497 (use-local-map display-time-world-mode-map))
498
499(defun display-time-world-display (alist)
500 "Replace current buffer text with times in various zones, based on ALIST."
501 (let ((inhibit-read-only t)
502 (buffer-undo-list t))
503 (erase-buffer)
504 (let ((max-width 0)
505 (result ()))
506 (unwind-protect
507 (dolist (zone alist)
508 (let* ((label (cadr zone))
509 (width (string-width label)))
510 (set-time-zone-rule (car zone))
511 (setq result
512 (append result
513 (list
514 label width
515 (format-time-string display-time-world-time-format))))
516 (when (> width max-width)
517 (setq max-width width))))
518 (set-time-zone-rule nil))
519 (while result
520 (insert (pop result)
521 (make-string (1+ (- max-width (pop result))) ?\s)
522 (pop result) "\n")))
523 (delete-backward-char 1)))
524
525;;;###autoload
526(defun display-time-world ()
527 "Enable updating display of times in various time zones.
528`display-time-world-list' specifies the zones.
529To turn off the world time display, go to that window and type `q'."
530 (interactive)
531 (when (and display-time-world-timer-enable
532 (not (get-buffer display-time-world-buffer-name)))
533 (run-at-time t display-time-world-timer-second 'display-time-world-timer))
534 (with-current-buffer (get-buffer-create display-time-world-buffer-name)
535 (display-time-world-display display-time-world-list))
536 (pop-to-buffer display-time-world-buffer-name)
537 (fit-window-to-buffer)
538 (display-time-world-mode))
539
540(defun display-time-world-timer ()
541 (if (get-buffer display-time-world-buffer-name)
542 (with-current-buffer (get-buffer display-time-world-buffer-name)
543 (display-time-world-display display-time-world-list))
544 ;; cancel timer
545 (let ((list timer-list))
546 (while list
547 (let ((elt (pop list)))
548 (when (equal (symbol-name (aref elt 5)) "display-time-world-timer")
549 (cancel-timer elt)))))))
550
2736785a
GM
551\f
552;;;###autoload
553(defun emacs-uptime ()
554 "Return a string giving the uptime of this instance of Emacs."
555 (interactive)
556 (let ((str
557 (format-seconds "%Y, %D, %H, %M, %S"
558 (time-to-seconds
559 (time-subtract (current-time) emacs-startup-time))
560 t)))
561 (if (interactive-p)
562 (message "%s" str)
563 str)))
564
c2acf685
SM
565(provide 'time)
566
ab5796a9 567;;; arch-tag: b9c1623f-b5cb-48e4-b650-482a4d23c5a6
c2acf685 568;;; time.el ends here