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