Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / calendar / timeclock.el
blob016fa2f8738a1913ea6a7dca5474e2f8a97279e1
1 ;;; timeclock.el --- mode for keeping track of how much you work
3 ;; Copyright (C) 1999-2014 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 :type 'file
87 :group 'timeclock)
89 (defcustom timeclock-workday (* 8 60 60)
90 "The length of a work period in seconds."
91 :type 'integer
92 :group 'timeclock)
94 (defcustom timeclock-relative t
95 "Whether to make reported time relative to `timeclock-workday'.
96 For example, if the length of a normal workday is eight hours, and you
97 work four hours on Monday, then the amount of time \"remaining\" on
98 Tuesday is twelve hours -- relative to an averaged work period of
99 eight hours -- or eight hours, non-relative. So relative time takes
100 into account any discrepancy of time under-worked or over-worked on
101 previous days. This only affects the timeclock mode line display."
102 :type 'boolean
103 :group 'timeclock)
105 (defcustom timeclock-get-project-function 'timeclock-ask-for-project
106 "The function used to determine the name of the current project.
107 When clocking in, and no project is specified, this function will be
108 called to determine what is the current project to be worked on.
109 If this variable is nil, no questions will be asked."
110 :type 'function
111 :group 'timeclock)
113 (defcustom timeclock-get-reason-function 'timeclock-ask-for-reason
114 "A function used to determine the reason for clocking out.
115 When clocking out, and no reason is specified, this function will be
116 called to determine what is the reason.
117 If this variable is nil, no questions will be asked."
118 :type 'function
119 :group 'timeclock)
121 (defcustom timeclock-get-workday-function nil
122 "A function used to determine the length of today's workday.
123 The first time that a user clocks in each day, this function will be
124 called to determine what is the length of the current workday. If
125 the return value is nil, or equal to `timeclock-workday', nothing special
126 will be done. If it is a quantity different from `timeclock-workday',
127 however, a record will be output to the timelog file to note the fact that
128 that day has a length that is different from the norm."
129 :type '(choice (const nil) function)
130 :group 'timeclock)
132 (defcustom timeclock-ask-before-exiting t
133 "If non-nil, ask if the user wants to clock out before exiting Emacs.
134 This variable only has effect if set with \\[customize]."
135 :set (lambda (symbol value)
136 (if value
137 (add-hook 'kill-emacs-query-functions 'timeclock-query-out)
138 (remove-hook 'kill-emacs-query-functions 'timeclock-query-out))
139 (set symbol value))
140 :type 'boolean
141 :group 'timeclock)
143 (defvar timeclock-update-timer nil
144 "The timer used to update `timeclock-mode-string'.")
146 ;; For byte-compiler.
147 (defvar display-time-hook)
148 (defvar timeclock-mode-line-display)
150 (defcustom timeclock-use-display-time t
151 "If non-nil, use `display-time-hook' for doing mode line updates.
152 The advantage of this is that one less timer has to be set running
153 amok in Emacs's process space. The disadvantage is that it requires
154 you to have `display-time' running. If you don't want to use
155 `display-time', but still want the mode line to show how much time is
156 left, set this variable to nil. Changing the value of this variable
157 while timeclock information is being displayed in the mode line has no
158 effect. You should call the function `timeclock-mode-line-display' with
159 a positive argument to force an update."
160 :set (lambda (symbol value)
161 (let ((currently-displaying
162 (and (boundp 'timeclock-mode-line-display)
163 timeclock-mode-line-display)))
164 ;; if we're changing to the state that
165 ;; `timeclock-mode-line-display' is already using, don't
166 ;; bother toggling it. This happens on the initial loading
167 ;; of timeclock.el.
168 (if (and currently-displaying
169 (or (and value
170 (boundp 'display-time-hook)
171 (memq 'timeclock-update-mode-line
172 display-time-hook))
173 (and (not value)
174 timeclock-update-timer)))
175 (setq currently-displaying nil))
176 (and currently-displaying
177 (setq timeclock-mode-line-display nil))
178 (set symbol value)
179 (and currently-displaying
180 (setq timeclock-mode-line-display t))
181 ;; FIXME: The return value isn't used, AFAIK!
182 value))
183 :type 'boolean
184 :group 'timeclock
185 :require 'time)
187 (defcustom timeclock-first-in-hook nil
188 "A hook run for the first \"in\" event each day.
189 Note that this hook is run before recording any events. Thus the
190 value of `timeclock-hours-today', `timeclock-last-event' and the
191 return value of function `timeclock-last-period' are relative previous
192 to today."
193 :type 'hook
194 :group 'timeclock)
196 (defcustom timeclock-load-hook nil
197 "Hook that gets run after timeclock has been loaded."
198 :type 'hook
199 :group 'timeclock)
201 (defcustom timeclock-in-hook nil
202 "A hook run every time an \"in\" event is recorded."
203 :type 'hook
204 :group 'timeclock)
206 (defcustom timeclock-day-over-hook nil
207 "A hook that is run when the workday has been completed.
208 This hook is only run if the current time remaining is being displayed
209 in the mode line. See the variable `timeclock-mode-line-display'."
210 :type 'hook
211 :group 'timeclock)
213 (defcustom timeclock-out-hook nil
214 "A hook run every time an \"out\" event is recorded."
215 :type 'hook
216 :group 'timeclock)
218 (defcustom timeclock-done-hook nil
219 "A hook run every time a project is marked as completed."
220 :type 'hook
221 :group 'timeclock)
223 (defcustom timeclock-event-hook nil
224 "A hook run every time any event is recorded."
225 :type 'hook
226 :group 'timeclock)
228 (defvar timeclock-last-event nil
229 "A list containing the last event that was recorded.
230 The format of this list is (CODE TIME PROJECT).")
232 (defvar timeclock-last-event-workday nil
233 "The number of seconds in the workday of `timeclock-last-event'.")
235 ;;; Internal Variables:
237 (defvar timeclock-discrepancy nil
238 "A variable containing the time discrepancy before the last event.
239 Normally, timeclock assumes that you intend to work for
240 `timeclock-workday' seconds every day. Any days in which you work
241 more or less than this amount is considered either a positive or
242 a negative discrepancy. If you work in such a manner that the
243 discrepancy is always brought back to zero, then you will by
244 definition have worked an average amount equal to `timeclock-workday'
245 each day.")
247 (defvar timeclock-elapsed nil
248 "A variable containing the time elapsed for complete periods today.
249 This value is not accurate enough to be useful by itself. Rather,
250 call `timeclock-workday-elapsed', to determine how much time has been
251 worked so far today. Also, if `timeclock-relative' is nil, this value
252 will be the same as `timeclock-discrepancy'.")
254 (defvar timeclock-use-elapsed nil
255 "Non-nil if the mode line should display time elapsed, not remaining.")
257 (defvar timeclock-last-period nil
258 "Integer representing the number of seconds in the last period.
259 Note that you shouldn't access this value, but instead should use the
260 function `timeclock-last-period'.")
262 (defvar timeclock-mode-string nil
263 "The timeclock string (optionally) displayed in the mode line.
264 The time is bracketed by <> if you are clocked in, otherwise by [].")
266 (defvar timeclock-day-over nil
267 "The date of the last day when notified \"day over\" for.")
269 ;;; User Functions:
271 (define-obsolete-function-alias 'timeclock-modeline-display
272 'timeclock-mode-line-display "24.3")
273 (define-obsolete-variable-alias 'timeclock-modeline-display
274 'timeclock-mode-line-display "24.3")
276 ;;;###autoload
277 (define-minor-mode timeclock-mode-line-display
278 "Toggle display of the amount of time left today in the mode line.
279 If `timeclock-use-display-time' is non-nil (the default), then
280 the function `display-time-mode' must be active, and the mode line
281 will be updated whenever the time display is updated. Otherwise,
282 the timeclock will use its own sixty second timer to do its
283 updating. With prefix ARG, turn mode line display on if and only
284 if ARG is positive. Returns the new status of timeclock mode line
285 display (non-nil means on)."
286 :global t
287 ;; cf display-time-mode.
288 (setq timeclock-mode-string "")
289 (or global-mode-string (setq global-mode-string '("")))
290 (if timeclock-mode-line-display
291 (progn
292 (or (memq 'timeclock-mode-string global-mode-string)
293 (setq global-mode-string
294 (append global-mode-string '(timeclock-mode-string))))
295 (add-hook 'timeclock-event-hook 'timeclock-update-mode-line)
296 (when timeclock-update-timer
297 (cancel-timer timeclock-update-timer)
298 (setq timeclock-update-timer nil))
299 (if (boundp 'display-time-hook)
300 (remove-hook 'display-time-hook 'timeclock-update-mode-line))
301 (if timeclock-use-display-time
302 (progn
303 ;; Update immediately so there is a visible change
304 ;; on calling this function.
305 (if display-time-mode
306 (timeclock-update-mode-line)
307 (message "Activate `display-time-mode' or turn off \
308 `timeclock-use-display-time' to see timeclock information"))
309 (add-hook 'display-time-hook 'timeclock-update-mode-line))
310 (setq timeclock-update-timer
311 (run-at-time nil 60 'timeclock-update-mode-line))))
312 (setq global-mode-string
313 (delq 'timeclock-mode-string global-mode-string))
314 (remove-hook 'timeclock-event-hook 'timeclock-update-mode-line)
315 (if (boundp 'display-time-hook)
316 (remove-hook 'display-time-hook
317 'timeclock-update-mode-line))
318 (when timeclock-update-timer
319 (cancel-timer timeclock-update-timer)
320 (setq timeclock-update-timer nil))))
322 (defsubst timeclock-time-to-date (time)
323 "Convert the TIME value to a textual date string."
324 (format-time-string "%Y/%m/%d" time))
326 ;;;###autoload
327 (defun timeclock-in (&optional arg project find-project)
328 "Clock in, recording the current time moment in the timelog.
329 With a numeric prefix ARG, record the fact that today has only that
330 many hours in it to be worked. If ARG is a non-numeric prefix argument
331 \(non-nil, but not a number), 0 is assumed (working on a holiday or
332 weekend). *If not called interactively, ARG should be the number of
333 _seconds_ worked today*. This feature only has effect the first time
334 this function is called within a day.
336 PROJECT is the project being clocked into. If PROJECT is nil, and
337 FIND-PROJECT is non-nil -- or the user calls `timeclock-in'
338 interactively -- call the function `timeclock-get-project-function' to
339 discover the name of the project."
340 (interactive
341 (list (and current-prefix-arg
342 (if (numberp current-prefix-arg)
343 (* current-prefix-arg 60 60)
344 0))))
345 (if (equal (car timeclock-last-event) "i")
346 (error "You've already clocked in!")
347 (unless timeclock-last-event
348 (timeclock-reread-log))
349 ;; Either no log file, or day has rolled over.
350 (unless (and timeclock-last-event
351 (equal (timeclock-time-to-date
352 (cadr timeclock-last-event))
353 (timeclock-time-to-date (current-time))))
354 (let ((workday (or (and (numberp arg) arg)
355 (and arg 0)
356 (and timeclock-get-workday-function
357 (funcall timeclock-get-workday-function))
358 timeclock-workday)))
359 (run-hooks 'timeclock-first-in-hook)
360 ;; settle the discrepancy for the new day
361 (setq timeclock-discrepancy
362 (- (or timeclock-discrepancy 0) workday))
363 (if (not (= workday timeclock-workday))
364 (timeclock-log "h" (number-to-string
365 (/ workday (if (zerop (% workday (* 60 60)))
366 60 60.0) 60))))))
367 (timeclock-log "i" (or project
368 (and timeclock-get-project-function
369 (or find-project
370 (called-interactively-p 'interactive))
371 (funcall timeclock-get-project-function))))
372 (run-hooks 'timeclock-in-hook)))
374 ;;;###autoload
375 (defun timeclock-out (&optional arg reason find-reason)
376 "Clock out, recording the current time moment in the timelog.
377 If a prefix ARG is given, the user has completed the project that was
378 begun during the last time segment.
380 REASON is the user's reason for clocking out. If REASON is nil, and
381 FIND-REASON is non-nil -- or the user calls `timeclock-out'
382 interactively -- call the function `timeclock-get-reason-function' to
383 discover the reason."
384 (interactive "P")
385 (or timeclock-last-event
386 (error "You haven't clocked in!"))
387 (if (equal (downcase (car timeclock-last-event)) "o")
388 (error "You've already clocked out!")
389 (timeclock-log
390 (if arg "O" "o")
391 (or reason
392 (and timeclock-get-reason-function
393 (or find-reason (called-interactively-p 'interactive))
394 (funcall timeclock-get-reason-function))))
395 (run-hooks 'timeclock-out-hook)
396 (if arg
397 (run-hooks 'timeclock-done-hook))))
399 ;; Should today-only be removed in favor of timeclock-relative? - gm
400 (defsubst timeclock-workday-remaining (&optional today-only)
401 "Return the number of seconds until the workday is complete.
402 The amount returned is relative to the value of `timeclock-workday'.
403 If TODAY-ONLY is non-nil, the value returned will be relative only to
404 the time worked today, and not to past time."
405 (let ((discrep (timeclock-find-discrep)))
406 (if discrep
407 (- (if today-only (cadr discrep)
408 (car discrep)))
409 0.0)))
411 ;;;###autoload
412 (defun timeclock-status-string (&optional show-seconds today-only)
413 "Report the overall timeclock status at the present moment.
414 If SHOW-SECONDS is non-nil, display second resolution.
415 If TODAY-ONLY is non-nil, the display will be relative only to time
416 worked today, ignoring the time worked on previous days."
417 (interactive "P")
418 (let ((remainder (timeclock-workday-remaining
419 (or today-only
420 (not timeclock-relative))))
421 (last-in (equal (car timeclock-last-event) "i"))
422 status)
423 (setq status
424 (format "Currently %s since %s (%s), %s %s, leave at %s"
425 (if last-in "IN" "OUT")
426 (if show-seconds
427 (format-time-string "%-I:%M:%S %p"
428 (nth 1 timeclock-last-event))
429 (format-time-string "%-I:%M %p"
430 (nth 1 timeclock-last-event)))
431 (or (nth 2 timeclock-last-event)
432 (if last-in "**UNKNOWN**" "workday over"))
433 (timeclock-seconds-to-string remainder show-seconds t)
434 (if (> remainder 0)
435 "remaining" "over")
436 (timeclock-when-to-leave-string show-seconds today-only)))
437 (if (called-interactively-p 'interactive)
438 (message "%s" status)
439 status)))
441 ;;;###autoload
442 (defun timeclock-change (&optional arg project)
443 "Change to working on a different project.
444 This clocks out of the current project, then clocks in on a new one.
445 With a prefix ARG, consider the previous project as finished at the
446 time of changeover. PROJECT is the name of the last project you were
447 working on."
448 (interactive "P")
449 (timeclock-out arg)
450 (timeclock-in nil project (called-interactively-p 'interactive)))
452 ;;;###autoload
453 (defun timeclock-query-out ()
454 "Ask the user whether to clock out.
455 This is a useful function for adding to `kill-emacs-query-functions'."
456 (and (equal (car timeclock-last-event) "i")
457 (y-or-n-p "You're currently clocking time, clock out? ")
458 (timeclock-out))
459 ;; Unconditionally return t for `kill-emacs-query-functions'.
462 ;;;###autoload
463 (defun timeclock-reread-log ()
464 "Re-read the timeclock, to account for external changes.
465 Returns the new value of `timeclock-discrepancy'."
466 (interactive)
467 (setq timeclock-discrepancy nil)
468 (timeclock-find-discrep)
469 (if (and timeclock-discrepancy timeclock-mode-line-display)
470 (timeclock-update-mode-line))
471 timeclock-discrepancy)
473 (defun timeclock-seconds-to-string (seconds &optional show-seconds
474 reverse-leader)
475 "Convert SECONDS into a compact time string.
476 If SHOW-SECONDS is non-nil, make the resolution of the return string
477 include the second count. If REVERSE-LEADER is non-nil, it means to
478 output a \"+\" if the time value is negative, rather than a \"-\".
479 This is used when negative time values have an inverted meaning (such
480 as with time remaining, where negative time really means overtime)."
481 (if show-seconds
482 (format "%s%d:%02d:%02d"
483 (if (< seconds 0) (if reverse-leader "+" "-") "")
484 (truncate (/ (abs seconds) 60 60))
485 (% (truncate (/ (abs seconds) 60)) 60)
486 (% (truncate (abs seconds)) 60))
487 (format "%s%d:%02d"
488 (if (< seconds 0) (if reverse-leader "+" "-") "")
489 (truncate (/ (abs seconds) 60 60))
490 (% (truncate (/ (abs seconds) 60)) 60))))
492 (defsubst timeclock-currently-in-p ()
493 "Return non-nil if the user is currently clocked in."
494 (equal (car timeclock-last-event) "i"))
496 ;;;###autoload
497 (defun timeclock-workday-remaining-string (&optional show-seconds
498 today-only)
499 "Return a string representing the amount of time left today.
500 Display second resolution if SHOW-SECONDS is non-nil. If TODAY-ONLY
501 is non-nil, the display will be relative only to time worked today.
502 See `timeclock-relative' for more information about the meaning of
503 \"relative to today\"."
504 (interactive)
505 (let ((string (timeclock-seconds-to-string
506 (timeclock-workday-remaining today-only)
507 show-seconds t)))
508 (if (called-interactively-p 'interactive)
509 (message "%s" string)
510 string)))
512 (defsubst timeclock-workday-elapsed ()
513 "Return the number of seconds worked so far today.
514 If RELATIVE is non-nil, the amount returned will be relative to past
515 time worked. The default is to return only the time that has elapsed
516 so far today."
517 (let ((discrep (timeclock-find-discrep)))
518 (if discrep
519 (nth 2 discrep)
520 0.0)))
522 ;;;###autoload
523 (defun timeclock-workday-elapsed-string (&optional show-seconds)
524 "Return a string representing the amount of time worked today.
525 Display seconds resolution if SHOW-SECONDS is non-nil. If RELATIVE is
526 non-nil, the amount returned will be relative to past time worked."
527 (interactive)
528 (let ((string (timeclock-seconds-to-string (timeclock-workday-elapsed)
529 show-seconds)))
530 (if (called-interactively-p 'interactive)
531 (message "%s" string)
532 string)))
534 (defalias 'timeclock-time-to-seconds (if (fboundp 'float-time) 'float-time
535 'time-to-seconds))
537 (defalias 'timeclock-seconds-to-time 'seconds-to-time)
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 (timeclock-seconds-to-time
545 (- (timeclock-time-to-seconds (current-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 (when (and (< remainder 0)
651 (not (and timeclock-day-over
652 (equal timeclock-day-over
653 (timeclock-time-to-date
654 (current-time))))))
655 (setq timeclock-day-over
656 (timeclock-time-to-date (current-time)))
657 (run-hooks 'timeclock-day-over-hook))
658 (setq timeclock-mode-string
659 (propertize
660 (format " %c%s%c "
661 (if last-in ?< ?[)
662 (timeclock-seconds-to-string remainder nil t)
663 (if last-in ?> ?]))
664 'help-echo "timeclock: time remaining"))))
666 (put 'timeclock-mode-string 'risky-local-variable t)
668 (defun timeclock-log (code &optional project)
669 "Log the event CODE to the timeclock log, at the time of call.
670 If PROJECT is a string, it represents the project which the event is
671 being logged for. Normally only \"in\" events specify a project."
672 (let ((extant-timelog (find-buffer-visiting timeclock-file)))
673 (with-current-buffer (find-file-noselect timeclock-file t)
674 (save-excursion
675 (save-restriction
676 (widen)
677 (goto-char (point-max))
678 (if (not (bolp))
679 (insert "\n"))
680 (let ((now (current-time)))
681 (insert code " "
682 (format-time-string "%Y/%m/%d %H:%M:%S" now)
683 (or (and (stringp project)
684 (> (length project) 0)
685 (concat " " project))
687 "\n")
688 (if (equal (downcase code) "o")
689 (setq timeclock-last-period
690 (- (timeclock-time-to-seconds now)
691 (timeclock-time-to-seconds
692 (cadr timeclock-last-event)))
693 timeclock-discrepancy
694 (+ timeclock-discrepancy
695 timeclock-last-period)))
696 (setq timeclock-last-event (list code now project)))))
697 (save-buffer)
698 (unless extant-timelog (kill-buffer (current-buffer)))))
699 (run-hooks 'timeclock-event-hook))
701 (defvar timeclock-moment-regexp
702 (concat "\\([bhioO]\\)\\s-+"
703 "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)\\s-+"
704 "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)[ \t]*" "\\([^\n]*\\)"))
706 (defsubst timeclock-read-moment ()
707 "Read the moment under point from the timelog."
708 (if (looking-at timeclock-moment-regexp)
709 (let ((code (match-string 1))
710 (year (string-to-number (match-string 2)))
711 (mon (string-to-number (match-string 3)))
712 (mday (string-to-number (match-string 4)))
713 (hour (string-to-number (match-string 5)))
714 (min (string-to-number (match-string 6)))
715 (sec (string-to-number (match-string 7)))
716 (project (match-string 8)))
717 (list code (encode-time sec min hour mday mon year) project))))
719 (defun timeclock-last-period (&optional moment)
720 "Return the value of the last event period.
721 If the last event was a clock-in, the period will be open ended, and
722 growing every second. Otherwise, it is a fixed amount which has been
723 recorded to disk. If MOMENT is non-nil, use that as the current time.
724 This is only provided for coherency when used by
725 `timeclock-discrepancy'."
726 (if (equal (car timeclock-last-event) "i")
727 (- (timeclock-time-to-seconds (or moment (current-time)))
728 (timeclock-time-to-seconds
729 (cadr timeclock-last-event)))
730 timeclock-last-period))
732 (defsubst timeclock-entry-length (entry)
733 "Return the length of ENTRY in seconds."
734 (- (timeclock-time-to-seconds (cadr entry))
735 (timeclock-time-to-seconds (car entry))))
737 (defsubst timeclock-entry-begin (entry)
738 "Return the start time of ENTRY."
739 (car entry))
741 (defsubst timeclock-entry-end (entry)
742 "Return the end time of ENTRY."
743 (cadr entry))
745 (defsubst timeclock-entry-project (entry)
746 "Return the project of ENTRY."
747 (nth 2 entry))
749 (defsubst timeclock-entry-comment (entry)
750 "Return the comment of ENTRY."
751 (nth 3 entry))
753 (defsubst timeclock-entry-list-length (entry-list)
754 "Return the total length of ENTRY-LIST in seconds."
755 (let ((length 0))
756 (dolist (entry entry-list)
757 (setq length (+ length (timeclock-entry-length entry))))
758 length))
760 (defsubst timeclock-entry-list-begin (entry-list)
761 "Return the start time of the first element of ENTRY-LIST."
762 (timeclock-entry-begin (car entry-list)))
764 (defsubst timeclock-entry-list-end (entry-list)
765 "Return the end time of the last element of ENTRY-LIST."
766 (timeclock-entry-end (car (last entry-list))))
768 (defsubst timeclock-entry-list-span (entry-list)
769 "Return the total time in seconds spanned by ENTRY-LIST."
770 (- (timeclock-time-to-seconds (timeclock-entry-list-end entry-list))
771 (timeclock-time-to-seconds (timeclock-entry-list-begin entry-list))))
773 (defsubst timeclock-entry-list-break (entry-list)
774 "Return the total break time (span - length) in ENTRY-LIST."
775 (- (timeclock-entry-list-span entry-list)
776 (timeclock-entry-list-length entry-list)))
778 (defsubst timeclock-entry-list-projects (entry-list)
779 "Return a list of all the projects in ENTRY-LIST."
780 (let (projects proj)
781 (dolist (entry entry-list)
782 (setq proj (timeclock-entry-project entry))
783 (if projects
784 (add-to-list 'projects proj)
785 (setq projects (list proj))))
786 projects))
788 (defsubst timeclock-day-required (day)
789 "Return the required length of DAY in seconds, default `timeclock-workday'."
790 (or (car day) timeclock-workday))
792 (defsubst timeclock-day-length (day)
793 "Return the actual length of DAY in seconds."
794 (timeclock-entry-list-length (cdr day)))
796 (defsubst timeclock-day-debt (day)
797 "Return the debt (required - actual) associated with DAY, in seconds."
798 (- (timeclock-day-required day)
799 (timeclock-day-length day)))
801 (defsubst timeclock-day-begin (day)
802 "Return the start time of DAY."
803 (timeclock-entry-list-begin (cdr day)))
805 (defsubst timeclock-day-end (day)
806 "Return the end time of DAY."
807 (timeclock-entry-list-end (cdr day)))
809 (defsubst timeclock-day-span (day)
810 "Return the span of DAY."
811 (timeclock-entry-list-span (cdr day)))
813 (defsubst timeclock-day-break (day)
814 "Return the total break time of DAY."
815 (timeclock-entry-list-break (cdr day)))
817 (defsubst timeclock-day-projects (day)
818 "Return a list of all the projects in DAY."
819 (timeclock-entry-list-projects (cddr day)))
821 (defun timeclock-day-list-template (func day-list)
822 "Template for summing the result of FUNC on each element of DAY-LIST."
823 (let ((length 0))
824 (dolist (day day-list)
825 (setq length (+ length (funcall func day))))
826 length))
828 (defun timeclock-day-list-required (day-list)
829 "Return total required length of DAY-LIST, in seconds."
830 (timeclock-day-list-template #'timeclock-day-required day-list))
832 (defun timeclock-day-list-length (day-list)
833 "Return actual length of DAY-LIST, in seconds."
834 (timeclock-day-list-template #'timeclock-day-length day-list))
836 (defun timeclock-day-list-debt (day-list)
837 "Return total debt (required - actual) of DAY-LIST."
838 (timeclock-day-list-template #'timeclock-day-debt day-list))
840 (defsubst timeclock-day-list-begin (day-list)
841 "Return the start time of DAY-LIST."
842 (timeclock-day-begin (car day-list)))
844 (defsubst timeclock-day-list-end (day-list)
845 "Return the end time of DAY-LIST."
846 (timeclock-day-end (car (last day-list))))
848 (defun timeclock-day-list-span (day-list)
849 "Return the span of DAY-LIST."
850 (timeclock-day-list-template #'timeclock-day-span day-list))
852 (defun timeclock-day-list-break (day-list)
853 "Return the total break of DAY-LIST."
854 (timeclock-day-list-template #'timeclock-day-break day-list))
856 (defun timeclock-day-list-projects (day-list)
857 "Return a list of all the projects in DAY-LIST."
858 (let (projects)
859 (dolist (day day-list)
860 (dolist (proj (timeclock-day-projects day))
861 (if projects
862 (add-to-list 'projects proj)
863 (setq projects (list proj)))))
864 projects))
866 (defsubst timeclock-current-debt (&optional log-data)
867 "Return the seconds debt from LOG-DATA, default `timeclock-log-data'."
868 (nth 0 (or log-data (timeclock-log-data))))
870 (defsubst timeclock-day-alist (&optional log-data)
871 "Return the date alist from LOG-DATA, default `timeclock-log-data'."
872 (nth 1 (or log-data (timeclock-log-data))))
874 (defun timeclock-day-list (&optional log-data)
875 "Return a list of the cdrs of the date alist from LOG-DATA."
876 (let (day-list)
877 (dolist (date-list (timeclock-day-alist log-data))
878 (setq day-list (cons (cdr date-list) day-list)))
879 day-list))
881 (defsubst timeclock-project-alist (&optional log-data)
882 "Return the project alist from LOG-DATA, default `timeclock-log-data'."
883 (nth 2 (or log-data (timeclock-log-data))))
885 (defun timeclock-log-data (&optional recent-only filename)
886 "Return the contents of the timelog file, in a useful format.
887 If the optional argument RECENT-ONLY is non-nil, only show the contents
888 from the last point where the time debt (see below) was set.
889 If the optional argument FILENAME is non-nil, it is used instead of
890 the file specified by `timeclock-file.'
892 A timelog contains data in the form of a single entry per line.
893 Each entry has the form:
895 CODE YYYY/MM/DD HH:MM:SS [COMMENT]
897 CODE is one of: b, h, i, o or O. COMMENT is optional when the code is
898 i, o or O. The meanings of the codes are:
900 b Set the current time balance, or \"time debt\". Useful when
901 archiving old log data, when a debt must be carried forward.
902 The COMMENT here is the number of seconds of debt.
904 h Set the required working time for the given day. This must
905 be the first entry for that day. The COMMENT in this case is
906 the number of hours in this workday. Floating point amounts
907 are allowed.
909 i Clock in. The COMMENT in this case should be the name of the
910 project worked on.
912 o Clock out. COMMENT is unnecessary, but can be used to provide
913 a description of how the period went, for example.
915 O Final clock out. Whatever project was being worked on, it is
916 now finished. Useful for creating summary reports.
918 When this function is called, it will return a data structure with the
919 following format:
921 (DEBT ENTRIES-BY-DAY ENTRIES-BY-PROJECT)
923 DEBT is a floating point number representing the number of seconds
924 \"owed\" before any work was done. For a new file (one without a 'b'
925 entry), this is always zero.
927 The two entries lists have similar formats. They are both alists,
928 where the CAR is the index, and the CDR is a list of time entries.
929 For ENTRIES-BY-DAY, the CAR is a textual date string, of the form
930 YYYY/MM/DD. For ENTRIES-BY-PROJECT, it is the name of the project
931 worked on, or t for the default project.
933 The CDR for ENTRIES-BY-DAY is slightly different than for
934 ENTRIES-BY-PROJECT. It has the following form:
936 (DAY-LENGTH TIME-ENTRIES...)
938 For ENTRIES-BY-PROJECT, there is no DAY-LENGTH member. It is simply a
939 list of TIME-ENTRIES. Note that if DAY-LENGTH is nil, it means
940 whatever is the default should be used.
942 A TIME-ENTRY is a recorded time interval. It has the following format
943 \(although generally one does not have to manipulate these entries
944 directly; see below):
946 (BEGIN-TIME END-TIME PROJECT [COMMENT] [FINAL-P])
948 Anyway, suffice it to say there are a lot of structures. Typically
949 the user is expected to manipulate to the day(s) or project(s) that he
950 or she wants, at which point the following helper functions may be
951 used:
953 timeclock-day-required
954 timeclock-day-length
955 timeclock-day-debt
956 timeclock-day-begin
957 timeclock-day-end
958 timeclock-day-span
959 timeclock-day-break
960 timeclock-day-projects
962 timeclock-day-list-required
963 timeclock-day-list-length
964 timeclock-day-list-debt
965 timeclock-day-list-begin
966 timeclock-day-list-end
967 timeclock-day-list-span
968 timeclock-day-list-break
969 timeclock-day-list-projects
971 timeclock-entry-length
972 timeclock-entry-begin
973 timeclock-entry-end
974 timeclock-entry-project
975 timeclock-entry-comment
977 timeclock-entry-list-length
978 timeclock-entry-list-begin
979 timeclock-entry-list-end
980 timeclock-entry-list-span
981 timeclock-entry-list-break
982 timeclock-entry-list-projects
984 A few comments should make the use of the above functions obvious:
986 `required' is the amount of time that must be spent during a day, or
987 sequence of days, in order to have no debt.
989 `length' is the actual amount of time that was spent.
991 `debt' is the difference between required time and length. A
992 negative debt signifies overtime.
994 `begin' is the earliest moment at which work began.
996 `end' is the final moment work was done.
998 `span' is the difference between begin and end.
1000 `break' is the difference between span and length.
1002 `project' is the project that was worked on, and `projects' is a
1003 list of all the projects that were worked on during a given period.
1005 `comment', where it applies, could mean anything.
1007 There are a few more functions available, for locating day and entry
1008 lists:
1010 timeclock-day-alist LOG-DATA
1011 timeclock-project-alist LOG-DATA
1012 timeclock-current-debt LOG-DATA
1014 See the documentation for the given function if more info is needed."
1015 (let ((log-data (list 0.0 nil nil))
1016 (now (current-time))
1017 last-date-limited last-date-seconds last-date
1018 (line 0) last beg day entry event)
1019 (with-temp-buffer
1020 (insert-file-contents (or filename timeclock-file))
1021 (when recent-only
1022 (goto-char (point-max))
1023 (unless (re-search-backward "^b\\s-+" nil t)
1024 (goto-char (point-min))))
1025 (while (or (setq event (timeclock-read-moment))
1026 (and beg (not last)
1027 (setq last t event (list "o" now))))
1028 (setq line (1+ line))
1029 (cond ((equal (car event) "b")
1030 (setcar log-data (string-to-number (nth 2 event))))
1031 ((equal (car event) "h")
1032 (setq last-date-limited (timeclock-time-to-date (cadr event))
1033 last-date-seconds (* (string-to-number (nth 2 event))
1034 3600.0)))
1035 ((equal (car event) "i")
1036 (if beg
1037 (error "Error in format of timelog file, line %d" line)
1038 (setq beg t))
1039 (setq entry (list (cadr event) nil
1040 (and (> (length (nth 2 event)) 0)
1041 (nth 2 event))))
1042 (let ((date (timeclock-time-to-date (cadr event))))
1043 (if (and last-date
1044 (not (equal date last-date)))
1045 (progn
1046 (setcar (cdr log-data)
1047 (cons (cons last-date day)
1048 (cadr log-data)))
1049 (setq day (list (and last-date-limited
1050 last-date-seconds))))
1051 (unless day
1052 (setq day (list (and last-date-limited
1053 last-date-seconds)))))
1054 (setq last-date date
1055 last-date-limited nil)))
1056 ((equal (downcase (car event)) "o")
1057 (if (not beg)
1058 (error "Error in format of timelog file, line %d" line)
1059 (setq beg nil))
1060 (setcar (cdr entry) (cadr event))
1061 (let ((desc (and (> (length (nth 2 event)) 0)
1062 (nth 2 event))))
1063 (if desc
1064 (nconc entry (list (nth 2 event))))
1065 (if (equal (car event) "O")
1066 (nconc entry (if desc
1067 (list t)
1068 (list nil t))))
1069 (nconc day (list entry))
1070 (setq desc (nth 2 entry))
1071 (let ((proj (assoc desc (nth 2 log-data))))
1072 (if (null proj)
1073 (setcar (cddr log-data)
1074 (cons (cons desc (list entry))
1075 (nth 2 log-data)))
1076 (nconc (cdr proj) (list entry)))))))
1077 (forward-line))
1078 (if day
1079 (setcar (cdr log-data)
1080 (cons (cons last-date day)
1081 (cadr log-data))))
1082 log-data)))
1084 (defun timeclock-find-discrep ()
1085 "Calculate time discrepancies, in seconds.
1086 The result is a three element list, containing the total time
1087 discrepancy, today's discrepancy, and the time worked today."
1088 ;; This is not implemented in terms of the functions above, because
1089 ;; it's a bit wasteful to read all of that data in, just to throw
1090 ;; away more than 90% of the information afterwards.
1092 ;; If it were implemented using those functions, it would look
1093 ;; something like this:
1094 ;; (let ((days (timeclock-day-alist (timeclock-log-data)))
1095 ;; (total 0.0))
1096 ;; (while days
1097 ;; (setq total (+ total (- (timeclock-day-length (cdar days))
1098 ;; (timeclock-day-required (cdar days))))
1099 ;; days (cdr days)))
1100 ;; total)
1101 (let* ((now (current-time))
1102 (todays-date (timeclock-time-to-date now))
1103 (first t) (accum 0) (elapsed 0)
1104 event beg last-date
1105 last-date-limited last-date-seconds)
1106 (unless timeclock-discrepancy
1107 (when (file-readable-p timeclock-file)
1108 (setq timeclock-project-list nil
1109 timeclock-last-project nil
1110 timeclock-reason-list nil
1111 timeclock-elapsed 0)
1112 (with-temp-buffer
1113 (insert-file-contents timeclock-file)
1114 (goto-char (point-max))
1115 (unless (re-search-backward "^b\\s-+" nil t)
1116 (goto-char (point-min)))
1117 (while (setq event (timeclock-read-moment))
1118 (cond ((equal (car event) "b")
1119 (setq accum (string-to-number (nth 2 event))))
1120 ((equal (car event) "h")
1121 (setq last-date-limited
1122 (timeclock-time-to-date (cadr event))
1123 last-date-seconds
1124 (* (string-to-number (nth 2 event)) 3600.0)))
1125 ((equal (car event) "i")
1126 (when (and (nth 2 event)
1127 (> (length (nth 2 event)) 0))
1128 (add-to-list 'timeclock-project-list (nth 2 event))
1129 (setq timeclock-last-project (nth 2 event)))
1130 (let ((date (timeclock-time-to-date (cadr event))))
1131 (if (if last-date
1132 (not (equal date last-date))
1133 first)
1134 (setq first nil
1135 accum (- accum (if last-date-limited
1136 last-date-seconds
1137 timeclock-workday))))
1138 (setq last-date date
1139 last-date-limited nil)
1140 (if beg
1141 (error "Error in format of timelog file!")
1142 (setq beg (timeclock-time-to-seconds (cadr event))))))
1143 ((equal (downcase (car event)) "o")
1144 (if (and (nth 2 event)
1145 (> (length (nth 2 event)) 0))
1146 (add-to-list 'timeclock-reason-list (nth 2 event)))
1147 (if (not beg)
1148 (error "Error in format of timelog file!")
1149 (setq timeclock-last-period
1150 (- (timeclock-time-to-seconds (cadr event)) beg)
1151 accum (+ timeclock-last-period accum)
1152 beg nil))
1153 (if (equal last-date todays-date)
1154 (setq timeclock-elapsed
1155 (+ timeclock-last-period timeclock-elapsed)))))
1156 (setq timeclock-last-event event
1157 timeclock-last-event-workday
1158 (if (equal (timeclock-time-to-date now) last-date-limited)
1159 last-date-seconds
1160 timeclock-workday))
1161 (forward-line))
1162 (setq timeclock-discrepancy accum))))
1163 (unless timeclock-last-event-workday
1164 (setq timeclock-last-event-workday timeclock-workday))
1165 (setq accum (or timeclock-discrepancy 0)
1166 elapsed (or timeclock-elapsed elapsed))
1167 (if timeclock-last-event
1168 (if (equal (car timeclock-last-event) "i")
1169 (let ((last-period (timeclock-last-period now)))
1170 (setq accum (+ accum last-period)
1171 elapsed (+ elapsed last-period)))
1172 (if (not (equal (timeclock-time-to-date
1173 (cadr timeclock-last-event))
1174 (timeclock-time-to-date now)))
1175 (setq accum (- accum timeclock-last-event-workday)))))
1176 (list accum (- elapsed timeclock-last-event-workday)
1177 elapsed)))
1179 ;;; A reporting function that uses timeclock-log-data
1181 (defun timeclock-day-base (&optional time)
1182 "Given a time within a day, return 0:0:0 within that day.
1183 If optional argument TIME is non-nil, use that instead of the current time."
1184 (let ((decoded (decode-time (or time (current-time)))))
1185 (setcar (nthcdr 0 decoded) 0)
1186 (setcar (nthcdr 1 decoded) 0)
1187 (setcar (nthcdr 2 decoded) 0)
1188 (apply 'encode-time decoded)))
1190 (defun timeclock-mean (l)
1191 "Compute the arithmetic mean of the values in the list L."
1192 (let ((total 0)
1193 (count 0))
1194 (dolist (thisl l)
1195 (setq total (+ total thisl)
1196 count (1+ count)))
1197 (if (zerop count)
1199 (/ total count))))
1201 (defun timeclock-generate-report (&optional html-p)
1202 "Generate a summary report based on the current timelog file.
1203 By default, the report is in plain text, but if the optional argument
1204 HTML-P is non-nil, HTML markup is added."
1205 (interactive "P")
1206 (let ((log (timeclock-log-data))
1207 (today (timeclock-day-base)))
1208 (if html-p (insert "<p>"))
1209 (insert "Currently ")
1210 (let ((project (nth 2 timeclock-last-event))
1211 (begin (nth 1 timeclock-last-event))
1212 done)
1213 (if (timeclock-currently-in-p)
1214 (insert "IN")
1215 (if (zerop (length project))
1216 (progn (insert "Done Working Today")
1217 (setq done t))
1218 (insert "OUT")))
1219 (unless done
1220 (insert " since " (format-time-string "%Y/%m/%d %-I:%M %p" begin))
1221 (if html-p
1222 (insert "<br>\n<b>")
1223 (insert "\n*"))
1224 (if (timeclock-currently-in-p)
1225 (insert "Working on "))
1226 (if html-p
1227 (insert project "</b><br>\n")
1228 (insert project "*\n"))
1229 (let ((proj-data (cdr (assoc project (timeclock-project-alist log))))
1230 (two-weeks-ago (timeclock-seconds-to-time
1231 (- (timeclock-time-to-seconds today)
1232 (* 2 7 24 60 60))))
1233 two-week-len today-len)
1234 (while proj-data
1235 (if (not (time-less-p
1236 (timeclock-entry-begin (car proj-data)) today))
1237 (setq today-len (timeclock-entry-list-length proj-data)
1238 proj-data nil)
1239 (if (and (null two-week-len)
1240 (not (time-less-p
1241 (timeclock-entry-begin (car proj-data))
1242 two-weeks-ago)))
1243 (setq two-week-len (timeclock-entry-list-length proj-data)))
1244 (setq proj-data (cdr proj-data))))
1245 (if (null two-week-len)
1246 (setq two-week-len today-len))
1247 (if html-p (insert "<p>"))
1248 (if today-len
1249 (insert "\nTime spent on this task today: "
1250 (timeclock-seconds-to-string today-len)
1251 ". In the last two weeks: "
1252 (timeclock-seconds-to-string two-week-len))
1253 (if two-week-len
1254 (insert "\nTime spent on this task in the last two weeks: "
1255 (timeclock-seconds-to-string two-week-len))))
1256 (if html-p (insert "<br>"))
1257 (insert "\n"
1258 (timeclock-seconds-to-string (timeclock-workday-elapsed))
1259 " worked today, "
1260 (timeclock-seconds-to-string (timeclock-workday-remaining))
1261 " remaining, done at "
1262 (timeclock-when-to-leave-string) "\n")))
1263 (if html-p (insert "<p>"))
1264 (insert "\nThere have been "
1265 (number-to-string
1266 (length (timeclock-day-alist log)))
1267 " days of activity, starting "
1268 (caar (last (timeclock-day-alist log))))
1269 (if html-p (insert "</p>"))
1270 (when html-p
1271 (insert "<p>
1272 <table>
1273 <td width=\"25\"><br></td><td>
1274 <table border=1 cellpadding=3>
1275 <tr><th><i>Statistics</i></th>
1276 <th>Entire</th>
1277 <th>-30 days</th>
1278 <th>-3 mons</th>
1279 <th>-6 mons</th>
1280 <th>-1 year</th>
1281 </tr>")
1282 (let* ((day-list (timeclock-day-list))
1283 (thirty-days-ago (timeclock-seconds-to-time
1284 (- (timeclock-time-to-seconds today)
1285 (* 30 24 60 60))))
1286 (three-months-ago (timeclock-seconds-to-time
1287 (- (timeclock-time-to-seconds today)
1288 (* 90 24 60 60))))
1289 (six-months-ago (timeclock-seconds-to-time
1290 (- (timeclock-time-to-seconds today)
1291 (* 180 24 60 60))))
1292 (one-year-ago (timeclock-seconds-to-time
1293 (- (timeclock-time-to-seconds today)
1294 (* 365 24 60 60))))
1295 (time-in (vector (list t) (list t) (list t) (list t) (list t)))
1296 (time-out (vector (list t) (list t) (list t) (list t) (list t)))
1297 (breaks (vector (list t) (list t) (list t) (list t) (list t)))
1298 (workday (vector (list t) (list t) (list t) (list t) (list t)))
1299 (lengths (vector '(0 0) thirty-days-ago three-months-ago
1300 six-months-ago one-year-ago)))
1301 ;; collect statistics from complete timelog
1302 (dolist (day day-list)
1303 (let ((i 0) (l 5))
1304 (while (< i l)
1305 (unless (time-less-p
1306 (timeclock-day-begin day)
1307 (aref lengths i))
1308 (let ((base (timeclock-time-to-seconds
1309 (timeclock-day-base
1310 (timeclock-day-begin day)))))
1311 (nconc (aref time-in i)
1312 (list (- (timeclock-time-to-seconds
1313 (timeclock-day-begin day))
1314 base)))
1315 (let ((span (timeclock-day-span day))
1316 (len (timeclock-day-length day))
1317 (req (timeclock-day-required day)))
1318 ;; If the day's actual work length is less than
1319 ;; 70% of its span, then likely the exit time
1320 ;; and break amount are not worthwhile adding to
1321 ;; the statistic
1322 (when (and (> span 0)
1323 (> (/ (float len) (float span)) 0.70))
1324 (nconc (aref time-out i)
1325 (list (- (timeclock-time-to-seconds
1326 (timeclock-day-end day))
1327 base)))
1328 (nconc (aref breaks i) (list (- span len))))
1329 (if req
1330 (setq len (+ len (- timeclock-workday req))))
1331 (nconc (aref workday i) (list len)))))
1332 (setq i (1+ i)))))
1333 ;; average statistics
1334 (let ((i 0) (l 5))
1335 (while (< i l)
1336 (aset time-in i (timeclock-mean (cdr (aref time-in i))))
1337 (aset time-out i (timeclock-mean (cdr (aref time-out i))))
1338 (aset breaks i (timeclock-mean (cdr (aref breaks i))))
1339 (aset workday i (timeclock-mean (cdr (aref workday i))))
1340 (setq i (1+ i))))
1341 ;; Output the HTML table
1342 (insert "<tr>\n")
1343 (insert "<td align=\"center\">Time in</td>\n")
1344 (let ((i 0) (l 5))
1345 (while (< i l)
1346 (insert "<td align=\"right\">"
1347 (timeclock-seconds-to-string (aref time-in i))
1348 "</td>\n")
1349 (setq i (1+ i))))
1350 (insert "</tr>\n")
1352 (insert "<tr>\n")
1353 (insert "<td align=\"center\">Time out</td>\n")
1354 (let ((i 0) (l 5))
1355 (while (< i l)
1356 (insert "<td align=\"right\">"
1357 (timeclock-seconds-to-string (aref time-out i))
1358 "</td>\n")
1359 (setq i (1+ i))))
1360 (insert "</tr>\n")
1362 (insert "<tr>\n")
1363 (insert "<td align=\"center\">Break</td>\n")
1364 (let ((i 0) (l 5))
1365 (while (< i l)
1366 (insert "<td align=\"right\">"
1367 (timeclock-seconds-to-string (aref breaks i))
1368 "</td>\n")
1369 (setq i (1+ i))))
1370 (insert "</tr>\n")
1372 (insert "<tr>\n")
1373 (insert "<td align=\"center\">Workday</td>\n")
1374 (let ((i 0) (l 5))
1375 (while (< i l)
1376 (insert "<td align=\"right\">"
1377 (timeclock-seconds-to-string (aref workday i))
1378 "</td>\n")
1379 (setq i (1+ i))))
1380 (insert "</tr>\n"))
1381 (insert "<tfoot>
1382 <td colspan=\"6\" align=\"center\">
1383 <i>These are approximate figures</i></td>
1384 </tfoot>
1385 </table>
1386 </td></table>")))))
1388 ;;; A helpful little function
1390 (defun timeclock-visit-timelog ()
1391 "Open the file named by `timeclock-file' in another window."
1392 (interactive)
1393 (find-file-other-window timeclock-file))
1395 (provide 'timeclock)
1397 (run-hooks 'timeclock-load-hook)
1399 ;; make sure we know the list of reasons, projects, and have computed
1400 ;; the last event and current discrepancy.
1401 (if (file-readable-p timeclock-file)
1402 (timeclock-reread-log))
1404 ;;; timeclock.el ends here