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