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