Stop worrying about Alliant in bytecode.c
[emacs.git] / lisp / calendar / timeclock.el
blobf1f48e9995886e67093080658ad4467864ea125f
1 ;;; timeclock.el --- mode for keeping track of how much you work
3 ;; Copyright (C) 1999-2016 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Created: 25 Mar 1999
7 ;; Version: 2.6.1
8 ;; Keywords: calendar data
10 ;; This file is part of GNU Emacs.
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
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This mode is for keeping track of time intervals. You can use it
28 ;; for whatever purpose you like, but the typical scenario is to keep
29 ;; track of how much time you spend working on certain projects.
31 ;; Use `timeclock-in' when you start on a project, and `timeclock-out'
32 ;; when you're done. Once you've collected some data, you can use
33 ;; `timeclock-workday-remaining' to see how much time is left to be
34 ;; worked today (where `timeclock-workday' specifies the length of the
35 ;; working day), and `timeclock-when-to-leave' to calculate when you're free.
37 ;; You'll probably want to bind the timeclock commands to some handy
38 ;; keystrokes. At the moment, C-x t is unused:
40 ;; (require 'timeclock)
42 ;; (define-key ctl-x-map "ti" 'timeclock-in)
43 ;; (define-key ctl-x-map "to" 'timeclock-out)
44 ;; (define-key ctl-x-map "tc" 'timeclock-change)
45 ;; (define-key ctl-x-map "tr" 'timeclock-reread-log)
46 ;; (define-key ctl-x-map "tu" 'timeclock-update-mode-line)
47 ;; (define-key ctl-x-map "tw" 'timeclock-when-to-leave-string)
49 ;; If you want Emacs to display the amount of time "left" to your
50 ;; workday in the mode-line, you can either set the value of
51 ;; `timeclock-mode-line-display' to t using M-x customize, or you can
52 ;; add this code to your init file:
54 ;; (require 'timeclock)
55 ;; (timeclock-mode-line-display)
57 ;; To cancel this mode line display at any time, just call
58 ;; `timeclock-mode-line-display' again.
60 ;; You may also want Emacs to ask you before exiting, if you are
61 ;; currently working on a project. This can be done either by setting
62 ;; `timeclock-ask-before-exiting' to t using M-x customize (this is
63 ;; the default), or by adding the following to your init file:
65 ;; (add-hook 'kill-emacs-query-functions 'timeclock-query-out)
67 ;; NOTE: If you change your timelog file without using timeclock's
68 ;; functions, or if you change the value of any of timeclock's
69 ;; customizable variables, you should run the command
70 ;; `timeclock-reread-log'. This will recompute any discrepancies in
71 ;; your average working time, and will make sure that the various
72 ;; display functions return the correct value.
74 ;;; History:
76 ;;; Code:
78 (defgroup timeclock nil
79 "Keeping track of the time that gets spent."
80 :group 'data)
82 ;;; User Variables:
84 (defcustom timeclock-file (locate-user-emacs-file "timelog" ".timelog")
85 "The file used to store timeclock data in."
86 :version "24.4" ; added locate-user-emacs-file
87 :type 'file
88 :group 'timeclock)
90 (defcustom timeclock-workday (* 8 60 60)
91 "The length of a work period in seconds."
92 :type 'integer
93 :group 'timeclock)
95 (defcustom timeclock-relative t
96 "Whether to make reported time relative to `timeclock-workday'.
97 For example, if the length of a normal workday is eight hours, and you
98 work four hours on Monday, then the amount of time \"remaining\" on
99 Tuesday is twelve hours -- relative to an averaged work period of
100 eight hours -- or eight hours, non-relative. So relative time takes
101 into account any discrepancy of time under-worked or over-worked on
102 previous days. This only affects the timeclock mode line display."
103 :type 'boolean
104 :group 'timeclock)
106 (defcustom timeclock-get-project-function 'timeclock-ask-for-project
107 "The function used to determine the name of the current project.
108 When clocking in, and no project is specified, this function will be
109 called to determine what is the current project to be worked on.
110 If this variable is nil, no questions will be asked."
111 :type 'function
112 :group 'timeclock)
114 (defcustom timeclock-get-reason-function 'timeclock-ask-for-reason
115 "A function used to determine the reason for clocking out.
116 When clocking out, and no reason is specified, this function will be
117 called to determine what is the reason.
118 If this variable is nil, no questions will be asked."
119 :type 'function
120 :group 'timeclock)
122 (defcustom timeclock-get-workday-function nil
123 "A function used to determine the length of today's workday.
124 The first time that a user clocks in each day, this function will be
125 called to determine what is the length of the current workday. If
126 the return value is nil, or equal to `timeclock-workday', nothing special
127 will be done. If it is a quantity different from `timeclock-workday',
128 however, a record will be output to the timelog file to note the fact that
129 that day has a length that is different from the norm."
130 :type '(choice (const nil) function)
131 :group 'timeclock)
133 (defcustom timeclock-ask-before-exiting t
134 "If non-nil, ask if the user wants to clock out before exiting Emacs.
135 This variable only has effect if set with \\[customize]."
136 :set (lambda (symbol value)
137 (if value
138 (add-hook 'kill-emacs-query-functions 'timeclock-query-out)
139 (remove-hook 'kill-emacs-query-functions 'timeclock-query-out))
140 (set symbol value))
141 :type 'boolean
142 :group 'timeclock)
144 (defvar timeclock-update-timer nil
145 "The timer used to update `timeclock-mode-string'.")
147 ;; For byte-compiler.
148 (defvar display-time-hook)
149 (defvar timeclock-mode-line-display)
151 (defcustom timeclock-use-display-time t
152 "If non-nil, use `display-time-hook' for doing mode line updates.
153 The advantage of this is that one less timer has to be set running
154 amok in Emacs's process space. The disadvantage is that it requires
155 you to have `display-time' running. If you don't want to use
156 `display-time', but still want the mode line to show how much time is
157 left, set this variable to nil. Changing the value of this variable
158 while timeclock information is being displayed in the mode line has no
159 effect. You should call the function `timeclock-mode-line-display' with
160 a positive argument to force an update."
161 :set (lambda (symbol value)
162 (let ((currently-displaying
163 (and (boundp 'timeclock-mode-line-display)
164 timeclock-mode-line-display)))
165 ;; if we're changing to the state that
166 ;; `timeclock-mode-line-display' is already using, don't
167 ;; bother toggling it. This happens on the initial loading
168 ;; of timeclock.el.
169 (if (and currently-displaying
170 (or (and value
171 (boundp 'display-time-hook)
172 (memq 'timeclock-update-mode-line
173 display-time-hook))
174 (and (not value)
175 timeclock-update-timer)))
176 (setq currently-displaying nil))
177 (and currently-displaying
178 (setq timeclock-mode-line-display nil))
179 (set symbol value)
180 (and currently-displaying
181 (setq timeclock-mode-line-display t))
182 ;; FIXME: The return value isn't used, AFAIK!
183 value))
184 :type 'boolean
185 :group 'timeclock
186 :require 'time)
188 (defcustom timeclock-first-in-hook nil
189 "A hook run for the first \"in\" event each day.
190 Note that this hook is run before recording any events. Thus the
191 value of `timeclock-hours-today', `timeclock-last-event' and the
192 return value of function `timeclock-last-period' are relative previous
193 to today."
194 :type 'hook
195 :group 'timeclock)
197 (defcustom timeclock-load-hook nil
198 "Hook that gets run after timeclock has been loaded."
199 :type 'hook
200 :group 'timeclock)
202 (defcustom timeclock-in-hook nil
203 "A hook run every time an \"in\" event is recorded."
204 :type 'hook
205 :group 'timeclock)
207 (defcustom timeclock-day-over-hook nil
208 "A hook that is run when the workday has been completed.
209 This hook is only run if the current time remaining is being displayed
210 in the mode line. See the variable `timeclock-mode-line-display'."
211 :type 'hook
212 :group 'timeclock)
214 (defcustom timeclock-out-hook nil
215 "A hook run every time an \"out\" event is recorded."
216 :type 'hook
217 :group 'timeclock)
219 (defcustom timeclock-done-hook nil
220 "A hook run every time a project is marked as completed."
221 :type 'hook
222 :group 'timeclock)
224 (defcustom timeclock-event-hook nil
225 "A hook run every time any event is recorded."
226 :type 'hook
227 :group 'timeclock)
229 (defvar timeclock-last-event nil
230 "A list containing the last event that was recorded.
231 The format of this list is (CODE TIME PROJECT).")
233 (defvar timeclock-last-event-workday nil
234 "The number of seconds in the workday of `timeclock-last-event'.")
236 ;;; Internal Variables:
238 (defvar timeclock-discrepancy nil
239 "A variable containing the time discrepancy before the last event.
240 Normally, timeclock assumes that you intend to work for
241 `timeclock-workday' seconds every day. Any days in which you work
242 more or less than this amount is considered either a positive or
243 a negative discrepancy. If you work in such a manner that the
244 discrepancy is always brought back to zero, then you will by
245 definition have worked an average amount equal to `timeclock-workday'
246 each day.")
248 (defvar timeclock-elapsed nil
249 "A variable containing the time elapsed for complete periods today.
250 This value is not accurate enough to be useful by itself. Rather,
251 call `timeclock-workday-elapsed', to determine how much time has been
252 worked so far today. Also, if `timeclock-relative' is nil, this value
253 will be the same as `timeclock-discrepancy'.")
255 (defvar timeclock-use-elapsed nil
256 "Non-nil if the mode line should display time elapsed, not remaining.")
258 (defvar timeclock-last-period nil
259 "Integer representing the number of seconds in the last period.
260 Note that you shouldn't access this value, but instead should use the
261 function `timeclock-last-period'.")
263 (defvar timeclock-mode-string nil
264 "The timeclock string (optionally) displayed in the mode line.
265 The time is bracketed by <> if you are clocked in, otherwise by [].")
267 (defvar timeclock-day-over nil
268 "The date of the last day when notified \"day over\" for.")
270 ;;; User Functions:
272 (define-obsolete-function-alias 'timeclock-modeline-display
273 'timeclock-mode-line-display "24.3")
274 (define-obsolete-variable-alias 'timeclock-modeline-display
275 'timeclock-mode-line-display "24.3")
277 ;;;###autoload
278 (define-minor-mode timeclock-mode-line-display
279 "Toggle display of the amount of time left today in the mode line.
280 If `timeclock-use-display-time' is non-nil (the default), then
281 the function `display-time-mode' must be active, and the mode line
282 will be updated whenever the time display is updated. Otherwise,
283 the timeclock will use its own sixty second timer to do its
284 updating. With prefix ARG, turn mode line display on if and only
285 if ARG is positive. Returns the new status of timeclock mode line
286 display (non-nil means on)."
287 :global t
288 ;; cf display-time-mode.
289 (setq timeclock-mode-string "")
290 (or global-mode-string (setq global-mode-string '("")))
291 (if timeclock-mode-line-display
292 (progn
293 (or (memq 'timeclock-mode-string global-mode-string)
294 (setq global-mode-string
295 (append global-mode-string '(timeclock-mode-string))))
296 (add-hook 'timeclock-event-hook 'timeclock-update-mode-line)
297 (when timeclock-update-timer
298 (cancel-timer timeclock-update-timer)
299 (setq timeclock-update-timer nil))
300 (if (boundp 'display-time-hook)
301 (remove-hook 'display-time-hook 'timeclock-update-mode-line))
302 (if timeclock-use-display-time
303 (progn
304 ;; Update immediately so there is a visible change
305 ;; on calling this function.
306 (if display-time-mode
307 (timeclock-update-mode-line)
308 (message "Activate `display-time-mode' or turn off \
309 `timeclock-use-display-time' to see timeclock information"))
310 (add-hook 'display-time-hook 'timeclock-update-mode-line))
311 (setq timeclock-update-timer
312 (run-at-time nil 60 'timeclock-update-mode-line))))
313 (setq global-mode-string
314 (delq 'timeclock-mode-string global-mode-string))
315 (remove-hook 'timeclock-event-hook 'timeclock-update-mode-line)
316 (if (boundp 'display-time-hook)
317 (remove-hook 'display-time-hook
318 'timeclock-update-mode-line))
319 (when timeclock-update-timer
320 (cancel-timer timeclock-update-timer)
321 (setq timeclock-update-timer nil))))
323 (defsubst timeclock-time-to-date (&optional time)
324 "Convert the TIME value to a textual date string."
325 (format-time-string "%Y/%m/%d" time))
327 ;;;###autoload
328 (defun timeclock-in (&optional arg project find-project)
329 "Clock in, recording the current time moment in the timelog.
330 With a numeric prefix ARG, record the fact that today has only that
331 many hours in it to be worked. If ARG is a non-numeric prefix argument
332 \(non-nil, but not a number), 0 is assumed (working on a holiday or
333 weekend). *If not called interactively, ARG should be the number of
334 _seconds_ worked today*. This feature only has effect the first time
335 this function is called within a day.
337 PROJECT is the project being clocked into. If PROJECT is nil, and
338 FIND-PROJECT is non-nil -- or the user calls `timeclock-in'
339 interactively -- call the function `timeclock-get-project-function' to
340 discover the name of the project."
341 (interactive
342 (list (and current-prefix-arg
343 (if (numberp current-prefix-arg)
344 (* current-prefix-arg 60 60)
345 0))))
346 (if (equal (car timeclock-last-event) "i")
347 (error "You've already clocked in!")
348 (unless timeclock-last-event
349 (timeclock-reread-log))
350 ;; Either no log file, or day has rolled over.
351 (unless (and timeclock-last-event
352 (equal (timeclock-time-to-date
353 (cadr timeclock-last-event))
354 (timeclock-time-to-date)))
355 (let ((workday (or (and (numberp arg) arg)
356 (and arg 0)
357 (and timeclock-get-workday-function
358 (funcall timeclock-get-workday-function))
359 timeclock-workday)))
360 (run-hooks 'timeclock-first-in-hook)
361 ;; settle the discrepancy for the new day
362 (setq timeclock-discrepancy
363 (- (or timeclock-discrepancy 0) workday))
364 (if (not (= workday timeclock-workday))
365 (timeclock-log "h" (number-to-string
366 (/ workday (if (zerop (% workday (* 60 60)))
367 60 60.0) 60))))))
368 (timeclock-log "i" (or project
369 (and timeclock-get-project-function
370 (or find-project
371 (called-interactively-p 'interactive))
372 (funcall timeclock-get-project-function))))
373 (run-hooks 'timeclock-in-hook)))
375 ;;;###autoload
376 (defun timeclock-out (&optional arg reason find-reason)
377 "Clock out, recording the current time moment in the timelog.
378 If a prefix ARG is given, the user has completed the project that was
379 begun during the last time segment.
381 REASON is the user's reason for clocking out. If REASON is nil, and
382 FIND-REASON is non-nil -- or the user calls `timeclock-out'
383 interactively -- call the function `timeclock-get-reason-function' to
384 discover the reason."
385 (interactive "P")
386 (or timeclock-last-event
387 (error "You haven't clocked in!"))
388 (if (equal (downcase (car timeclock-last-event)) "o")
389 (error "You've already clocked out!")
390 (timeclock-log
391 (if arg "O" "o")
392 (or reason
393 (and timeclock-get-reason-function
394 (or find-reason (called-interactively-p 'interactive))
395 (funcall timeclock-get-reason-function))))
396 (run-hooks 'timeclock-out-hook)
397 (if arg
398 (run-hooks 'timeclock-done-hook))))
400 ;; Should today-only be removed in favor of timeclock-relative? - gm
401 (defsubst timeclock-workday-remaining (&optional today-only)
402 "Return the number of seconds until the workday is complete.
403 The amount returned is relative to the value of `timeclock-workday'.
404 If TODAY-ONLY is non-nil, the value returned will be relative only to
405 the time worked today, and not to past time."
406 (let ((discrep (timeclock-find-discrep)))
407 (if discrep
408 (- (if today-only (cadr discrep)
409 (car discrep)))
410 0.0)))
412 ;;;###autoload
413 (defun timeclock-status-string (&optional show-seconds today-only)
414 "Report the overall timeclock status at the present moment.
415 If SHOW-SECONDS is non-nil, display second resolution.
416 If TODAY-ONLY is non-nil, the display will be relative only to time
417 worked today, ignoring the time worked on previous days."
418 (interactive "P")
419 (let ((remainder (timeclock-workday-remaining
420 (or today-only
421 (not timeclock-relative))))
422 (last-in (equal (car timeclock-last-event) "i"))
423 status)
424 (setq status
425 (format "Currently %s since %s (%s), %s %s, leave at %s"
426 (if last-in "IN" "OUT")
427 (if show-seconds
428 (format-time-string "%-I:%M:%S %p"
429 (nth 1 timeclock-last-event))
430 (format-time-string "%-I:%M %p"
431 (nth 1 timeclock-last-event)))
432 (or (nth 2 timeclock-last-event)
433 (if last-in "**UNKNOWN**" "workday over"))
434 (timeclock-seconds-to-string remainder show-seconds t)
435 (if (> remainder 0)
436 "remaining" "over")
437 (timeclock-when-to-leave-string show-seconds today-only)))
438 (if (called-interactively-p 'interactive)
439 (message "%s" status)
440 status)))
442 ;;;###autoload
443 (defun timeclock-change (&optional arg project)
444 "Change to working on a different project.
445 This clocks out of the current project, then clocks in on a new one.
446 With a prefix ARG, consider the previous project as finished at the
447 time of changeover. PROJECT is the name of the last project you were
448 working on."
449 (interactive "P")
450 (timeclock-out arg)
451 (timeclock-in nil project (called-interactively-p 'interactive)))
453 ;;;###autoload
454 (defun timeclock-query-out ()
455 "Ask the user whether to clock out.
456 This is a useful function for adding to `kill-emacs-query-functions'."
457 (and (equal (car timeclock-last-event) "i")
458 (y-or-n-p "You're currently clocking time, clock out? ")
459 (timeclock-out))
460 ;; Unconditionally return t for `kill-emacs-query-functions'.
463 ;;;###autoload
464 (defun timeclock-reread-log ()
465 "Re-read the timeclock, to account for external changes.
466 Returns the new value of `timeclock-discrepancy'."
467 (interactive)
468 (setq timeclock-discrepancy nil)
469 (timeclock-find-discrep)
470 (if (and timeclock-discrepancy timeclock-mode-line-display)
471 (timeclock-update-mode-line))
472 timeclock-discrepancy)
474 (defun timeclock-seconds-to-string (seconds &optional show-seconds
475 reverse-leader)
476 "Convert SECONDS into a compact time string.
477 If SHOW-SECONDS is non-nil, make the resolution of the return string
478 include the second count. If REVERSE-LEADER is non-nil, it means to
479 output a \"+\" if the time value is negative, rather than a \"-\".
480 This is used when negative time values have an inverted meaning (such
481 as with time remaining, where negative time really means overtime)."
482 (if show-seconds
483 (format "%s%d:%02d:%02d"
484 (if (< seconds 0) (if reverse-leader "+" "-") "")
485 (truncate (/ (abs seconds) 60 60))
486 (% (truncate (/ (abs seconds) 60)) 60)
487 (% (truncate (abs seconds)) 60))
488 (format "%s%d:%02d"
489 (if (< seconds 0) (if reverse-leader "+" "-") "")
490 (truncate (/ (abs seconds) 60 60))
491 (% (truncate (/ (abs seconds) 60)) 60))))
493 (defsubst timeclock-currently-in-p ()
494 "Return non-nil if the user is currently clocked in."
495 (equal (car timeclock-last-event) "i"))
497 ;;;###autoload
498 (defun timeclock-workday-remaining-string (&optional show-seconds
499 today-only)
500 "Return a string representing the amount of time left today.
501 Display second resolution if SHOW-SECONDS is non-nil. If TODAY-ONLY
502 is non-nil, the display will be relative only to time worked today.
503 See `timeclock-relative' for more information about the meaning of
504 \"relative to today\"."
505 (interactive)
506 (let ((string (timeclock-seconds-to-string
507 (timeclock-workday-remaining today-only)
508 show-seconds t)))
509 (if (called-interactively-p 'interactive)
510 (message "%s" string)
511 string)))
513 (defsubst timeclock-workday-elapsed ()
514 "Return the number of seconds worked so far today.
515 If RELATIVE is non-nil, the amount returned will be relative to past
516 time worked. The default is to return only the time that has elapsed
517 so far today."
518 (let ((discrep (timeclock-find-discrep)))
519 (if discrep
520 (nth 2 discrep)
521 0.0)))
523 ;;;###autoload
524 (defun timeclock-workday-elapsed-string (&optional show-seconds)
525 "Return a string representing the amount of time worked today.
526 Display seconds resolution if SHOW-SECONDS is non-nil. If RELATIVE is
527 non-nil, the amount returned will be relative to past time worked."
528 (interactive)
529 (let ((string (timeclock-seconds-to-string (timeclock-workday-elapsed)
530 show-seconds)))
531 (if (called-interactively-p 'interactive)
532 (message "%s" string)
533 string)))
535 (define-obsolete-function-alias 'timeclock-time-to-seconds 'float-time "25.2")
536 (define-obsolete-function-alias 'timeclock-seconds-to-time 'seconds-to-time
537 "25.2")
539 ;; Should today-only be removed in favor of timeclock-relative? - gm
540 (defsubst timeclock-when-to-leave (&optional today-only)
541 "Return a time value representing the end of today's workday.
542 If TODAY-ONLY is non-nil, the value returned will be relative only to
543 the time worked today, and not to past time."
544 (seconds-to-time
545 (- (float-time)
546 (let ((discrep (timeclock-find-discrep)))
547 (if discrep
548 (if today-only
549 (cadr discrep)
550 (car discrep))
551 0.0)))))
553 ;;;###autoload
554 (defun timeclock-when-to-leave-string (&optional show-seconds
555 today-only)
556 "Return a string representing the end of today's workday.
557 This string is relative to the value of `timeclock-workday'. If
558 SHOW-SECONDS is non-nil, the value printed/returned will include
559 seconds. If TODAY-ONLY is non-nil, the value returned will be
560 relative only to the time worked today, and not to past time."
561 ;; Should today-only be removed in favor of timeclock-relative? - gm
562 (interactive)
563 (let* ((then (timeclock-when-to-leave today-only))
564 (string
565 (if show-seconds
566 (format-time-string "%-I:%M:%S %p" then)
567 (format-time-string "%-I:%M %p" then))))
568 (if (called-interactively-p 'interactive)
569 (message "%s" string)
570 string)))
572 (defun timeclock-make-hours-explicit (old-default)
573 "Specify all workday lengths in `timeclock-file'.
574 OLD-DEFAULT hours are set for every day that has no number indicated."
575 (interactive "P")
576 (if old-default (setq old-default (prefix-numeric-value old-default))
577 (error "`timelog-make-hours-explicit' requires an explicit argument"))
578 (let ((extant-timelog (find-buffer-visiting timeclock-file))
579 current-date)
580 (with-current-buffer (find-file-noselect timeclock-file t)
581 (unwind-protect
582 (save-excursion
583 (save-restriction
584 (widen)
585 (goto-char (point-min))
586 (while (progn (skip-chars-forward "\n") (not (eobp)))
587 ;; This is just a variant of `timeclock-moment-regexp'.
588 (unless (looking-at
589 (concat "^\\([bhioO]\\) \\([0-9]+/[0-9]+/[0-9]+\\) "
590 "\\([0-9]+:[0-9]+:[0-9]+\\)"))
591 (error "Can't parse `%s'" timeclock-file))
592 (let ((this-date (match-string 2)))
593 (unless (or (and current-date
594 (string= this-date current-date))
595 (string= (match-string 1) "h"))
596 (insert (format "h %s %s %s\n" (match-string 2)
597 (match-string 3) old-default)))
598 (if (string-match "^[ih]" (match-string 1)) ; ignore logouts
599 (setq current-date this-date)))
600 (forward-line))
601 (save-buffer)))
602 (unless extant-timelog (kill-buffer (current-buffer)))))))
604 ;;; Internal Functions:
606 (defvar timeclock-project-list nil)
607 (defvar timeclock-last-project nil)
609 (defun timeclock-completing-read (prompt alist &optional default)
610 "A version of `completing-read' that works on both Emacs and XEmacs.
611 PROMPT, ALIST and DEFAULT are used for the PROMPT, COLLECTION and DEF
612 arguments of `completing-read'."
613 (if (featurep 'xemacs)
614 (let ((str (completing-read prompt alist)))
615 (if (or (null str) (zerop (length str)))
616 default
617 str))
618 (completing-read prompt alist nil nil nil nil default)))
620 (defun timeclock-ask-for-project ()
621 "Ask the user for the project they are clocking into."
622 (timeclock-completing-read
623 (format "Clock into which project (default %s): "
624 (or timeclock-last-project
625 (car timeclock-project-list)))
626 (mapcar 'list timeclock-project-list)
627 (or timeclock-last-project
628 (car timeclock-project-list))))
630 (defvar timeclock-reason-list nil)
632 (defun timeclock-ask-for-reason ()
633 "Ask the user for the reason they are clocking out."
634 (timeclock-completing-read "Reason for clocking out: "
635 (mapcar 'list timeclock-reason-list)))
637 (define-obsolete-function-alias 'timeclock-update-modeline
638 'timeclock-update-mode-line "24.3")
640 (defun timeclock-update-mode-line ()
641 "Update the `timeclock-mode-string' displayed in the mode line.
642 The value of `timeclock-relative' affects the display as described in
643 that variable's documentation."
644 (interactive)
645 (let ((remainder
646 (if timeclock-use-elapsed
647 (timeclock-workday-elapsed)
648 (timeclock-workday-remaining (not timeclock-relative))))
649 (last-in (equal (car timeclock-last-event) "i"))
650 (todays-date (timeclock-time-to-date)))
651 (when (and (< remainder 0)
652 (not (and timeclock-day-over
653 (equal timeclock-day-over todays-date))))
654 (setq timeclock-day-over todays-date)
655 (run-hooks 'timeclock-day-over-hook))
656 (setq timeclock-mode-string
657 (propertize
658 (format " %c%s%c "
659 (if last-in ?< ?[)
660 (timeclock-seconds-to-string remainder nil t)
661 (if last-in ?> ?]))
662 'help-echo "timeclock: time remaining"))))
664 (put 'timeclock-mode-string 'risky-local-variable t)
666 (defun timeclock-log (code &optional project)
667 "Log the event CODE to the timeclock log, at the time of call.
668 If PROJECT is a string, it represents the project which the event is
669 being logged for. Normally only \"in\" events specify a project."
670 (let ((extant-timelog (find-buffer-visiting timeclock-file)))
671 (with-current-buffer (find-file-noselect timeclock-file t)
672 (save-excursion
673 (save-restriction
674 (widen)
675 (goto-char (point-max))
676 (if (not (bolp))
677 (insert "\n"))
678 (let ((now (current-time)))
679 (insert code " "
680 (format-time-string "%Y/%m/%d %H:%M:%S" now)
681 (or (and (stringp project)
682 (> (length project) 0)
683 (concat " " project))
685 "\n")
686 (if (equal (downcase code) "o")
687 (setq timeclock-last-period
688 (- (float-time now)
689 (float-time (cadr timeclock-last-event)))
690 timeclock-discrepancy
691 (+ timeclock-discrepancy
692 timeclock-last-period)))
693 (setq timeclock-last-event (list code now project)))))
694 (save-buffer)
695 (unless extant-timelog (kill-buffer (current-buffer)))))
696 (run-hooks 'timeclock-event-hook))
698 (defvar timeclock-moment-regexp
699 (concat "\\([bhioO]\\)\\s-+"
700 "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)\\s-+"
701 "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)[ \t]*" "\\([^\n]*\\)"))
703 (defsubst timeclock-read-moment ()
704 "Read the moment under point from the timelog."
705 (if (looking-at timeclock-moment-regexp)
706 (let ((code (match-string 1))
707 (year (string-to-number (match-string 2)))
708 (mon (string-to-number (match-string 3)))
709 (mday (string-to-number (match-string 4)))
710 (hour (string-to-number (match-string 5)))
711 (min (string-to-number (match-string 6)))
712 (sec (string-to-number (match-string 7)))
713 (project (match-string 8)))
714 (list code (encode-time sec min hour mday mon year) project))))
716 (defun timeclock-last-period (&optional moment)
717 "Return the value of the last event period.
718 If the last event was a clock-in, the period will be open ended, and
719 growing every second. Otherwise, it is a fixed amount which has been
720 recorded to disk. If MOMENT is non-nil, use that as the current time.
721 This is only provided for coherency when used by
722 `timeclock-discrepancy'."
723 (if (equal (car timeclock-last-event) "i")
724 (- (float-time moment)
725 (float-time (cadr timeclock-last-event)))
726 timeclock-last-period))
728 (defsubst timeclock-entry-length (entry)
729 "Return the length of ENTRY in seconds."
730 (- (float-time (cadr entry))
731 (float-time (car entry))))
733 (defsubst timeclock-entry-begin (entry)
734 "Return the start time of ENTRY."
735 (car entry))
737 (defsubst timeclock-entry-end (entry)
738 "Return the end time of ENTRY."
739 (cadr entry))
741 (defsubst timeclock-entry-project (entry)
742 "Return the project of ENTRY."
743 (nth 2 entry))
745 (defsubst timeclock-entry-comment (entry)
746 "Return the comment of ENTRY."
747 (nth 3 entry))
749 (defsubst timeclock-entry-list-length (entry-list)
750 "Return the total length of ENTRY-LIST in seconds."
751 (let ((length 0))
752 (dolist (entry entry-list)
753 (setq length (+ length (timeclock-entry-length entry))))
754 length))
756 (defsubst timeclock-entry-list-begin (entry-list)
757 "Return the start time of the first element of ENTRY-LIST."
758 (timeclock-entry-begin (car entry-list)))
760 (defsubst timeclock-entry-list-end (entry-list)
761 "Return the end time of the last element of ENTRY-LIST."
762 (timeclock-entry-end (car (last entry-list))))
764 (defsubst timeclock-entry-list-span (entry-list)
765 "Return the total time in seconds spanned by ENTRY-LIST."
766 (- (float-time (timeclock-entry-list-end entry-list))
767 (float-time (timeclock-entry-list-begin entry-list))))
769 (defsubst timeclock-entry-list-break (entry-list)
770 "Return the total break time (span - length) in ENTRY-LIST."
771 (- (timeclock-entry-list-span entry-list)
772 (timeclock-entry-list-length entry-list)))
774 (defsubst timeclock-entry-list-projects (entry-list)
775 "Return a list of all the projects in ENTRY-LIST."
776 (let (projects proj)
777 (dolist (entry entry-list)
778 (setq proj (timeclock-entry-project entry))
779 (if projects
780 (add-to-list 'projects proj)
781 (setq projects (list proj))))
782 projects))
784 (defsubst timeclock-day-required (day)
785 "Return the required length of DAY in seconds, default `timeclock-workday'."
786 (or (car day) timeclock-workday))
788 (defsubst timeclock-day-length (day)
789 "Return the actual length of DAY in seconds."
790 (timeclock-entry-list-length (cdr day)))
792 (defsubst timeclock-day-debt (day)
793 "Return the debt (required - actual) associated with DAY, in seconds."
794 (- (timeclock-day-required day)
795 (timeclock-day-length day)))
797 (defsubst timeclock-day-begin (day)
798 "Return the start time of DAY."
799 (timeclock-entry-list-begin (cdr day)))
801 (defsubst timeclock-day-end (day)
802 "Return the end time of DAY."
803 (timeclock-entry-list-end (cdr day)))
805 (defsubst timeclock-day-span (day)
806 "Return the span of DAY."
807 (timeclock-entry-list-span (cdr day)))
809 (defsubst timeclock-day-break (day)
810 "Return the total break time of DAY."
811 (timeclock-entry-list-break (cdr day)))
813 (defsubst timeclock-day-projects (day)
814 "Return a list of all the projects in DAY."
815 (timeclock-entry-list-projects (cddr day)))
817 (defun timeclock-day-list-template (func day-list)
818 "Template for summing the result of FUNC on each element of DAY-LIST."
819 (let ((length 0))
820 (dolist (day day-list)
821 (setq length (+ length (funcall func day))))
822 length))
824 (defun timeclock-day-list-required (day-list)
825 "Return total required length of DAY-LIST, in seconds."
826 (timeclock-day-list-template #'timeclock-day-required day-list))
828 (defun timeclock-day-list-length (day-list)
829 "Return actual length of DAY-LIST, in seconds."
830 (timeclock-day-list-template #'timeclock-day-length day-list))
832 (defun timeclock-day-list-debt (day-list)
833 "Return total debt (required - actual) of DAY-LIST."
834 (timeclock-day-list-template #'timeclock-day-debt day-list))
836 (defsubst timeclock-day-list-begin (day-list)
837 "Return the start time of DAY-LIST."
838 (timeclock-day-begin (car day-list)))
840 (defsubst timeclock-day-list-end (day-list)
841 "Return the end time of DAY-LIST."
842 (timeclock-day-end (car (last day-list))))
844 (defun timeclock-day-list-span (day-list)
845 "Return the span of DAY-LIST."
846 (timeclock-day-list-template #'timeclock-day-span day-list))
848 (defun timeclock-day-list-break (day-list)
849 "Return the total break of DAY-LIST."
850 (timeclock-day-list-template #'timeclock-day-break day-list))
852 (defun timeclock-day-list-projects (day-list)
853 "Return a list of all the projects in DAY-LIST."
854 (let (projects)
855 (dolist (day day-list)
856 (dolist (proj (timeclock-day-projects day))
857 (if projects
858 (add-to-list 'projects proj)
859 (setq projects (list proj)))))
860 projects))
862 (defsubst timeclock-current-debt (&optional log-data)
863 "Return the seconds debt from LOG-DATA, default `timeclock-log-data'."
864 (nth 0 (or log-data (timeclock-log-data))))
866 (defsubst timeclock-day-alist (&optional log-data)
867 "Return the date alist from LOG-DATA, default `timeclock-log-data'."
868 (nth 1 (or log-data (timeclock-log-data))))
870 (defun timeclock-day-list (&optional log-data)
871 "Return a list of the cdrs of the date alist from LOG-DATA."
872 (let (day-list)
873 (dolist (date-list (timeclock-day-alist log-data))
874 (setq day-list (cons (cdr date-list) day-list)))
875 day-list))
877 (defsubst timeclock-project-alist (&optional log-data)
878 "Return the project alist from LOG-DATA, default `timeclock-log-data'."
879 (nth 2 (or log-data (timeclock-log-data))))
881 (defun timeclock-log-data (&optional recent-only filename)
882 "Return the contents of the timelog file, in a useful format.
883 If the optional argument RECENT-ONLY is non-nil, only show the contents
884 from the last point where the time debt (see below) was set.
885 If the optional argument FILENAME is non-nil, it is used instead of
886 the file specified by `timeclock-file.'
888 A timelog contains data in the form of a single entry per line.
889 Each entry has the form:
891 CODE YYYY/MM/DD HH:MM:SS [COMMENT]
893 CODE is one of: b, h, i, o or O. COMMENT is optional when the code is
894 i, o or O. The meanings of the codes are:
896 b Set the current time balance, or \"time debt\". Useful when
897 archiving old log data, when a debt must be carried forward.
898 The COMMENT here is the number of seconds of debt.
900 h Set the required working time for the given day. This must
901 be the first entry for that day. The COMMENT in this case is
902 the number of hours in this workday. Floating point amounts
903 are allowed.
905 i Clock in. The COMMENT in this case should be the name of the
906 project worked on.
908 o Clock out. COMMENT is unnecessary, but can be used to provide
909 a description of how the period went, for example.
911 O Final clock out. Whatever project was being worked on, it is
912 now finished. Useful for creating summary reports.
914 When this function is called, it will return a data structure with the
915 following format:
917 (DEBT ENTRIES-BY-DAY ENTRIES-BY-PROJECT)
919 DEBT is a floating point number representing the number of seconds
920 “owed” before any work was done. For a new file (one without a `b'
921 entry), this is always zero.
923 The two entries lists have similar formats. They are both alists,
924 where the CAR is the index, and the CDR is a list of time entries.
925 For ENTRIES-BY-DAY, the CAR is a textual date string, of the form
926 YYYY/MM/DD. For ENTRIES-BY-PROJECT, it is the name of the project
927 worked on, or t for the default project.
929 The CDR for ENTRIES-BY-DAY is slightly different than for
930 ENTRIES-BY-PROJECT. It has the following form:
932 (DAY-LENGTH TIME-ENTRIES...)
934 For ENTRIES-BY-PROJECT, there is no DAY-LENGTH member. It is simply a
935 list of TIME-ENTRIES. Note that if DAY-LENGTH is nil, it means
936 whatever is the default should be used.
938 A TIME-ENTRY is a recorded time interval. It has the following format
939 \(although generally one does not have to manipulate these entries
940 directly; see below):
942 (BEGIN-TIME END-TIME PROJECT [COMMENT] [FINAL-P])
944 Anyway, suffice it to say there are a lot of structures. Typically
945 the user is expected to manipulate to the day(s) or project(s) that he
946 or she wants, at which point the following helper functions may be
947 used:
949 timeclock-day-required
950 timeclock-day-length
951 timeclock-day-debt
952 timeclock-day-begin
953 timeclock-day-end
954 timeclock-day-span
955 timeclock-day-break
956 timeclock-day-projects
958 timeclock-day-list-required
959 timeclock-day-list-length
960 timeclock-day-list-debt
961 timeclock-day-list-begin
962 timeclock-day-list-end
963 timeclock-day-list-span
964 timeclock-day-list-break
965 timeclock-day-list-projects
967 timeclock-entry-length
968 timeclock-entry-begin
969 timeclock-entry-end
970 timeclock-entry-project
971 timeclock-entry-comment
973 timeclock-entry-list-length
974 timeclock-entry-list-begin
975 timeclock-entry-list-end
976 timeclock-entry-list-span
977 timeclock-entry-list-break
978 timeclock-entry-list-projects
980 A few comments should make the use of the above functions obvious:
982 `required' is the amount of time that must be spent during a day, or
983 sequence of days, in order to have no debt.
985 `length' is the actual amount of time that was spent.
987 `debt' is the difference between required time and length. A
988 negative debt signifies overtime.
990 `begin' is the earliest moment at which work began.
992 `end' is the final moment work was done.
994 `span' is the difference between begin and end.
996 `break' is the difference between span and length.
998 `project' is the project that was worked on, and `projects' is a
999 list of all the projects that were worked on during a given period.
1001 `comment', where it applies, could mean anything.
1003 There are a few more functions available, for locating day and entry
1004 lists:
1006 timeclock-day-alist LOG-DATA
1007 timeclock-project-alist LOG-DATA
1008 timeclock-current-debt LOG-DATA
1010 See the documentation for the given function if more info is needed."
1011 (let ((log-data (list 0.0 nil nil))
1012 (now (current-time))
1013 last-date-limited last-date-seconds last-date
1014 (line 0) last beg day entry event)
1015 (with-temp-buffer
1016 (insert-file-contents (or filename timeclock-file))
1017 (when recent-only
1018 (goto-char (point-max))
1019 (unless (re-search-backward "^b\\s-+" nil t)
1020 (goto-char (point-min))))
1021 (while (or (setq event (timeclock-read-moment))
1022 (and beg (not last)
1023 (setq last t event (list "o" now))))
1024 (setq line (1+ line))
1025 (cond ((equal (car event) "b")
1026 (setcar log-data (string-to-number (nth 2 event))))
1027 ((equal (car event) "h")
1028 (setq last-date-limited (timeclock-time-to-date (cadr event))
1029 last-date-seconds (* (string-to-number (nth 2 event))
1030 3600.0)))
1031 ((equal (car event) "i")
1032 (if beg
1033 (error "Error in format of timelog file, line %d" line)
1034 (setq beg t))
1035 (setq entry (list (cadr event) nil
1036 (and (> (length (nth 2 event)) 0)
1037 (nth 2 event))))
1038 (let ((date (timeclock-time-to-date (cadr event))))
1039 (if (and last-date
1040 (not (equal date last-date)))
1041 (progn
1042 (setcar (cdr log-data)
1043 (cons (cons last-date day)
1044 (cadr log-data)))
1045 (setq day (list (and last-date-limited
1046 last-date-seconds))))
1047 (unless day
1048 (setq day (list (and last-date-limited
1049 last-date-seconds)))))
1050 (setq last-date date
1051 last-date-limited nil)))
1052 ((equal (downcase (car event)) "o")
1053 (if (not beg)
1054 (error "Error in format of timelog file, line %d" line)
1055 (setq beg nil))
1056 (setcar (cdr entry) (cadr event))
1057 (let ((desc (and (> (length (nth 2 event)) 0)
1058 (nth 2 event))))
1059 (if desc
1060 (nconc entry (list (nth 2 event))))
1061 (if (equal (car event) "O")
1062 (nconc entry (if desc
1063 (list t)
1064 (list nil t))))
1065 (nconc day (list entry))
1066 (setq desc (nth 2 entry))
1067 (let ((proj (assoc desc (nth 2 log-data))))
1068 (if (null proj)
1069 (setcar (cddr log-data)
1070 (cons (cons desc (list entry))
1071 (nth 2 log-data)))
1072 (nconc (cdr proj) (list entry)))))))
1073 (forward-line))
1074 (if day
1075 (setcar (cdr log-data)
1076 (cons (cons last-date day)
1077 (cadr log-data))))
1078 log-data)))
1080 (defun timeclock-find-discrep ()
1081 "Calculate time discrepancies, in seconds.
1082 The result is a three element list, containing the total time
1083 discrepancy, today's discrepancy, and the time worked today."
1084 ;; This is not implemented in terms of the functions above, because
1085 ;; it's a bit wasteful to read all of that data in, just to throw
1086 ;; away more than 90% of the information afterwards.
1088 ;; If it were implemented using those functions, it would look
1089 ;; something like this:
1090 ;; (let ((days (timeclock-day-alist (timeclock-log-data)))
1091 ;; (total 0.0))
1092 ;; (while days
1093 ;; (setq total (+ total (- (timeclock-day-length (cdar days))
1094 ;; (timeclock-day-required (cdar days))))
1095 ;; days (cdr days)))
1096 ;; total)
1097 (let* ((now (current-time))
1098 (todays-date (timeclock-time-to-date now))
1099 (first t) (accum 0) (elapsed 0)
1100 event beg last-date
1101 last-date-limited last-date-seconds)
1102 (unless timeclock-discrepancy
1103 (when (file-readable-p timeclock-file)
1104 (setq timeclock-project-list nil
1105 timeclock-last-project nil
1106 timeclock-reason-list nil
1107 timeclock-elapsed 0)
1108 (with-temp-buffer
1109 (insert-file-contents timeclock-file)
1110 (goto-char (point-max))
1111 (unless (re-search-backward "^b\\s-+" nil t)
1112 (goto-char (point-min)))
1113 (while (setq event (timeclock-read-moment))
1114 (cond ((equal (car event) "b")
1115 (setq accum (string-to-number (nth 2 event))))
1116 ((equal (car event) "h")
1117 (setq last-date-limited
1118 (timeclock-time-to-date (cadr event))
1119 last-date-seconds
1120 (* (string-to-number (nth 2 event)) 3600.0)))
1121 ((equal (car event) "i")
1122 (when (and (nth 2 event)
1123 (> (length (nth 2 event)) 0))
1124 (add-to-list 'timeclock-project-list (nth 2 event))
1125 (setq timeclock-last-project (nth 2 event)))
1126 (let ((date (timeclock-time-to-date (cadr event))))
1127 (if (if last-date
1128 (not (equal date last-date))
1129 first)
1130 (setq first nil
1131 accum (- accum (if last-date-limited
1132 last-date-seconds
1133 timeclock-workday))))
1134 (setq last-date date
1135 last-date-limited nil)
1136 (if beg
1137 (error "Error in format of timelog file!")
1138 (setq beg (float-time (cadr event))))))
1139 ((equal (downcase (car event)) "o")
1140 (if (and (nth 2 event)
1141 (> (length (nth 2 event)) 0))
1142 (add-to-list 'timeclock-reason-list (nth 2 event)))
1143 (if (not beg)
1144 (error "Error in format of timelog file!")
1145 (setq timeclock-last-period
1146 (- (float-time (cadr event)) beg)
1147 accum (+ timeclock-last-period accum)
1148 beg nil))
1149 (if (equal last-date todays-date)
1150 (setq timeclock-elapsed
1151 (+ timeclock-last-period timeclock-elapsed)))))
1152 (setq timeclock-last-event event
1153 timeclock-last-event-workday
1154 (if (equal todays-date last-date-limited)
1155 last-date-seconds
1156 timeclock-workday))
1157 (forward-line))
1158 (setq timeclock-discrepancy accum))))
1159 (unless timeclock-last-event-workday
1160 (setq timeclock-last-event-workday timeclock-workday))
1161 (setq accum (or timeclock-discrepancy 0)
1162 elapsed (or timeclock-elapsed elapsed))
1163 (if timeclock-last-event
1164 (if (equal (car timeclock-last-event) "i")
1165 (let ((last-period (timeclock-last-period now)))
1166 (setq accum (+ accum last-period)
1167 elapsed (+ elapsed last-period)))
1168 (if (not (equal (timeclock-time-to-date
1169 (cadr timeclock-last-event))
1170 (timeclock-time-to-date now)))
1171 (setq accum (- accum timeclock-last-event-workday)))))
1172 (list accum (- elapsed timeclock-last-event-workday)
1173 elapsed)))
1175 ;;; A reporting function that uses timeclock-log-data
1177 (defun timeclock-day-base (&optional time)
1178 "Given a time within a day, return 0:0:0 within that day.
1179 If optional argument TIME is non-nil, use that instead of the current time."
1180 (let ((decoded (decode-time time)))
1181 (setcar (nthcdr 0 decoded) 0)
1182 (setcar (nthcdr 1 decoded) 0)
1183 (setcar (nthcdr 2 decoded) 0)
1184 (apply 'encode-time decoded)))
1186 (defun timeclock-mean (l)
1187 "Compute the arithmetic mean of the values in the list L."
1188 (let ((total 0)
1189 (count 0))
1190 (dolist (thisl l)
1191 (setq total (+ total thisl)
1192 count (1+ count)))
1193 (if (zerop count)
1195 (/ total count))))
1197 (defun timeclock-generate-report (&optional html-p)
1198 "Generate a summary report based on the current timelog file.
1199 By default, the report is in plain text, but if the optional argument
1200 HTML-P is non-nil, HTML markup is added."
1201 (interactive "P")
1202 (let ((log (timeclock-log-data))
1203 (today (timeclock-day-base)))
1204 (if html-p (insert "<p>"))
1205 (insert "Currently ")
1206 (let ((project (nth 2 timeclock-last-event))
1207 (begin (nth 1 timeclock-last-event))
1208 done)
1209 (if (timeclock-currently-in-p)
1210 (insert "IN")
1211 (if (zerop (length project))
1212 (progn (insert "Done Working Today")
1213 (setq done t))
1214 (insert "OUT")))
1215 (unless done
1216 (insert " since " (format-time-string "%Y/%m/%d %-I:%M %p" begin))
1217 (if html-p
1218 (insert "<br>\n<b>")
1219 (insert "\n*"))
1220 (if (timeclock-currently-in-p)
1221 (insert "Working on "))
1222 (if html-p
1223 (insert project "</b><br>\n")
1224 (insert project "*\n"))
1225 (let ((proj-data (cdr (assoc project (timeclock-project-alist log))))
1226 (two-weeks-ago (seconds-to-time
1227 (- (float-time today)
1228 (* 2 7 24 60 60))))
1229 two-week-len today-len)
1230 (while proj-data
1231 (if (not (time-less-p
1232 (timeclock-entry-begin (car proj-data)) today))
1233 (setq today-len (timeclock-entry-list-length proj-data)
1234 proj-data nil)
1235 (if (and (null two-week-len)
1236 (not (time-less-p
1237 (timeclock-entry-begin (car proj-data))
1238 two-weeks-ago)))
1239 (setq two-week-len (timeclock-entry-list-length proj-data)))
1240 (setq proj-data (cdr proj-data))))
1241 (if (null two-week-len)
1242 (setq two-week-len today-len))
1243 (if html-p (insert "<p>"))
1244 (if today-len
1245 (insert "\nTime spent on this task today: "
1246 (timeclock-seconds-to-string today-len)
1247 ". In the last two weeks: "
1248 (timeclock-seconds-to-string two-week-len))
1249 (if two-week-len
1250 (insert "\nTime spent on this task in the last two weeks: "
1251 (timeclock-seconds-to-string two-week-len))))
1252 (if html-p (insert "<br>"))
1253 (insert "\n"
1254 (timeclock-seconds-to-string (timeclock-workday-elapsed))
1255 " worked today, "
1256 (timeclock-seconds-to-string (timeclock-workday-remaining))
1257 " remaining, done at "
1258 (timeclock-when-to-leave-string) "\n")))
1259 (if html-p (insert "<p>"))
1260 (insert "\nThere have been "
1261 (number-to-string
1262 (length (timeclock-day-alist log)))
1263 " days of activity, starting "
1264 (caar (last (timeclock-day-alist log))))
1265 (if html-p (insert "</p>"))
1266 (when html-p
1267 (insert "<p>
1268 <table>
1269 <td width=\"25\"><br></td><td>
1270 <table border=1 cellpadding=3>
1271 <tr><th><i>Statistics</i></th>
1272 <th>Entire</th>
1273 <th>-30 days</th>
1274 <th>-3 mons</th>
1275 <th>-6 mons</th>
1276 <th>-1 year</th>
1277 </tr>")
1278 (let* ((day-list (timeclock-day-list))
1279 (thirty-days-ago (seconds-to-time
1280 (- (float-time today)
1281 (* 30 24 60 60))))
1282 (three-months-ago (seconds-to-time
1283 (- (float-time today)
1284 (* 90 24 60 60))))
1285 (six-months-ago (seconds-to-time
1286 (- (float-time today)
1287 (* 180 24 60 60))))
1288 (one-year-ago (seconds-to-time
1289 (- (float-time today)
1290 (* 365 24 60 60))))
1291 (time-in (vector (list t) (list t) (list t) (list t) (list t)))
1292 (time-out (vector (list t) (list t) (list t) (list t) (list t)))
1293 (breaks (vector (list t) (list t) (list t) (list t) (list t)))
1294 (workday (vector (list t) (list t) (list t) (list t) (list t)))
1295 (lengths (vector '(0 0) thirty-days-ago three-months-ago
1296 six-months-ago one-year-ago)))
1297 ;; collect statistics from complete timelog
1298 (dolist (day day-list)
1299 (let ((i 0) (l 5))
1300 (while (< i l)
1301 (unless (time-less-p
1302 (timeclock-day-begin day)
1303 (aref lengths i))
1304 (let ((base (float-time
1305 (timeclock-day-base
1306 (timeclock-day-begin day)))))
1307 (nconc (aref time-in i)
1308 (list (- (float-time (timeclock-day-begin day))
1309 base)))
1310 (let ((span (timeclock-day-span day))
1311 (len (timeclock-day-length day))
1312 (req (timeclock-day-required day)))
1313 ;; If the day's actual work length is less than
1314 ;; 70% of its span, then likely the exit time
1315 ;; and break amount are not worthwhile adding to
1316 ;; the statistic
1317 (when (and (> span 0)
1318 (> (/ (float len) (float span)) 0.70))
1319 (nconc (aref time-out i)
1320 (list (- (float-time (timeclock-day-end day))
1321 base)))
1322 (nconc (aref breaks i) (list (- span len))))
1323 (if req
1324 (setq len (+ len (- timeclock-workday req))))
1325 (nconc (aref workday i) (list len)))))
1326 (setq i (1+ i)))))
1327 ;; average statistics
1328 (let ((i 0) (l 5))
1329 (while (< i l)
1330 (aset time-in i (timeclock-mean (cdr (aref time-in i))))
1331 (aset time-out i (timeclock-mean (cdr (aref time-out i))))
1332 (aset breaks i (timeclock-mean (cdr (aref breaks i))))
1333 (aset workday i (timeclock-mean (cdr (aref workday i))))
1334 (setq i (1+ i))))
1335 ;; Output the HTML table
1336 (insert "<tr>\n")
1337 (insert "<td align=\"center\">Time in</td>\n")
1338 (let ((i 0) (l 5))
1339 (while (< i l)
1340 (insert "<td align=\"right\">"
1341 (timeclock-seconds-to-string (aref time-in i))
1342 "</td>\n")
1343 (setq i (1+ i))))
1344 (insert "</tr>\n")
1346 (insert "<tr>\n")
1347 (insert "<td align=\"center\">Time out</td>\n")
1348 (let ((i 0) (l 5))
1349 (while (< i l)
1350 (insert "<td align=\"right\">"
1351 (timeclock-seconds-to-string (aref time-out i))
1352 "</td>\n")
1353 (setq i (1+ i))))
1354 (insert "</tr>\n")
1356 (insert "<tr>\n")
1357 (insert "<td align=\"center\">Break</td>\n")
1358 (let ((i 0) (l 5))
1359 (while (< i l)
1360 (insert "<td align=\"right\">"
1361 (timeclock-seconds-to-string (aref breaks i))
1362 "</td>\n")
1363 (setq i (1+ i))))
1364 (insert "</tr>\n")
1366 (insert "<tr>\n")
1367 (insert "<td align=\"center\">Workday</td>\n")
1368 (let ((i 0) (l 5))
1369 (while (< i l)
1370 (insert "<td align=\"right\">"
1371 (timeclock-seconds-to-string (aref workday i))
1372 "</td>\n")
1373 (setq i (1+ i))))
1374 (insert "</tr>\n"))
1375 (insert "<tfoot>
1376 <td colspan=\"6\" align=\"center\">
1377 <i>These are approximate figures</i></td>
1378 </tfoot>
1379 </table>
1380 </td></table>")))))
1382 ;;; A helpful little function
1384 (defun timeclock-visit-timelog ()
1385 "Open the file named by `timeclock-file' in another window."
1386 (interactive)
1387 (find-file-other-window timeclock-file))
1389 (provide 'timeclock)
1391 (run-hooks 'timeclock-load-hook)
1393 ;; make sure we know the list of reasons, projects, and have computed
1394 ;; the last event and current discrepancy.
1395 (if (file-readable-p timeclock-file)
1396 (timeclock-reread-log))
1398 ;;; timeclock.el ends here