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