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