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