1 ;;; timeclock.el --- mode for keeping track of how much you work
3 ;; Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Created: 25 Mar 1999
8 ;; Keywords: calendar data
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; This mode is for keeping track of time intervals. You can use it
30 ;; for whatever purpose you like, but the typical scenario is to keep
31 ;; track of how much time you spend working on certain projects.
33 ;; Use `timeclock-in' when you start on a project, and `timeclock-out'
34 ;; when you're done. Once you've collected some data, you can use
35 ;; `timeclock-workday-remaining' to see how much time is left to be
36 ;; worked today (assuming a typical average of 8 hours a day), and
37 ;; `timeclock-when-to-leave' which will calculate when you're free.
39 ;; You'll probably want to bind the timeclock commands to some handy
40 ;; keystrokes. At the moment, C-x t is unused in Emacs 20:
42 ;; (require 'timeclock)
44 ;; (define-key ctl-x-map "ti" 'timeclock-in)
45 ;; (define-key ctl-x-map "to" 'timeclock-out)
46 ;; (define-key ctl-x-map "tc" 'timeclock-change)
47 ;; (define-key ctl-x-map "tr" 'timeclock-reread-log)
48 ;; (define-key ctl-x-map "tu" 'timeclock-update-modeline)
49 ;; (define-key ctl-x-map "tw" 'timeclock-when-to-leave-string)
51 ;; If you want Emacs to display the amount of time "left" to your
52 ;; workday in the modeline, you can either set the value of
53 ;; `timeclock-modeline-display' to t using M-x customize, or you
54 ;; can add this code to your .emacs file:
56 ;; (require 'timeclock)
57 ;; (timeclock-modeline-display)
59 ;; To cancel this modeline display at any time, just call
60 ;; `timeclock-modeline-display' again.
62 ;; You may also want Emacs to ask you before exiting, if you are
63 ;; current working on a project. This can be done either by setting
64 ;; `timeclock-ask-before-exiting' to t using M-x customize (this is
65 ;; the default), or by adding the following to your .emacs file:
67 ;; (add-hook 'kill-emacs-query-functions 'timeclock-query-out)
69 ;; NOTE: If you change your .timelog file without using timeclock's
70 ;; functions, or if you change the value of any of timeclock's
71 ;; customizable variables, you should run the command
72 ;; `timeclock-reread-log'. This will recompute any discrepancies in
73 ;; your average working time, and will make sure that the various
74 ;; display functions return the correct value.
80 (defgroup timeclock nil
81 "Keeping track time of the time that gets spent."
86 (defcustom timeclock-file
(convert-standard-filename "~/.timelog")
87 "*The file used to store timeclock data in."
91 (defcustom timeclock-workday
(* 8 60 60)
92 "*The length of a work period."
96 (defcustom timeclock-relative t
97 "*When reporting time, make it relative to `timeclock-workday'?
98 For example, if the length of a normal workday is eight hours, and you
99 work four hours on Monday, then the amount of time \"remaining\" on
100 Tuesday is twelve hours -- relative to an averaged work period of
101 eight hours -- or eight hours, non-relative. So relative time takes
102 into account any discrepancy of time under-worked or overworked on
107 (defcustom timeclock-get-project-function
'timeclock-ask-for-project
108 "*The function used to determine the name of the current project.
109 When clocking in, and no project is specified, this function will be
110 called to determine what the current project to be worked on is.
111 If this variable is nil, no questions will be asked."
115 (defcustom timeclock-get-reason-function
'timeclock-ask-for-reason
116 "*A function used to determine the reason for clocking out.
117 When clocking out, and no reason is specified, this function will be
118 called to determine what the reason is.
119 If this variable is nil, no questions will be asked."
123 (defcustom timeclock-get-workday-function nil
124 "*A function used to determine the length of today's workday.
125 The first time that a user clocks in each day, this function will be
126 called to determine what the length of the current workday is. If
127 the return value is nil, or equal to `timeclock-workday', nothing special
128 will be done. If it is a quantity different from `timeclock-workday',
129 however, a record will be output to the timelog file to note the fact that
130 that day has a different length from the norm."
131 :type
'(choice (const nil
) function
)
134 (defcustom timeclock-ask-before-exiting t
135 "*If non-nil, ask if the user wants to clock out before exiting Emacs."
136 :set
(lambda (symbol value
)
138 (add-hook 'kill-emacs-query-functions
'timeclock-query-out
)
139 (remove-hook 'kill-emacs-query-functions
'timeclock-query-out
))
140 (setq timeclock-ask-before-exiting value
))
144 (defvar timeclock-update-timer nil
145 "The timer used to update `timeclock-mode-string'.")
147 ;; For byte-compiler.
148 (defvar display-time-hook
)
149 (defvar timeclock-modeline-display
)
151 (defcustom timeclock-use-display-time t
152 "*If non-nil, use `display-time-hook' for doing modeline updates.
153 The advantage to this is that it means one less timer has to be set
154 running amok in Emacs' process space. The disadvantage is that it
155 requires you to have `display-time' running. If you don't want to use
156 `display-time', but still want the modeline to show how much time is
157 left, set this variable to nil. You will need to restart Emacs (or
158 toggle the function `timeclock-modeline-display') for the change to
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
168 (if (and currently-displaying
170 (boundp 'display-time-hook
)
171 (memq 'timeclock-update-modeline
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
))
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
195 (defcustom timeclock-load-hook nil
196 "*Hook that gets run after timeclock has been loaded."
200 (defcustom timeclock-in-hook nil
201 "*A hook run every time an \"in\" event is recorded."
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'."
212 (defcustom timeclock-out-hook nil
213 "*A hook run every time an \"out\" event is recorded."
217 (defcustom timeclock-done-hook nil
218 "*A hook run every time a project is marked as completed."
222 (defcustom timeclock-event-hook nil
223 "*A hook run every time any event is recorded."
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 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'
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-last-period nil
254 "Integer representing the number of seconds in the last period.
255 Note that you shouldn't access this value, but should use the function
256 `timeclock-last-period' instead.")
258 (defvar timeclock-mode-string nil
259 "The timeclock string (optionally) displayed in the modeline.
260 The time is bracketed by <> if you are clocked in, otherwise by [].")
262 (defvar timeclock-day-over nil
263 "The date of the last day when notified \"day over\" for.")
268 (defun timeclock-modeline-display (&optional arg
)
269 "Toggle display of the amount of time left today in the modeline.
270 If `timeclock-use-display-time' is non-nil (the default), then
271 the function `display-time-mode' must be active, and the modeline
272 will be updated whenever the time display is updated. Otherwise,
273 the timeclock will use its own sixty second timer to do its
274 updating. With prefix ARG, turn modeline display on if and only
275 if ARG is positive. Returns the new status of timeclock modeline
276 display (non-nil means on)."
278 ;; cf display-time-mode.
279 (setq timeclock-mode-string
"")
280 (or global-mode-string
(setq global-mode-string
'("")))
282 (> (prefix-numeric-value arg
) 0)
283 (not timeclock-modeline-display
))))
286 (or (memq 'timeclock-mode-string global-mode-string
)
287 (setq global-mode-string
288 (append global-mode-string
'(timeclock-mode-string))))
289 (unless (memq 'timeclock-update-modeline timeclock-event-hook
)
290 (add-hook 'timeclock-event-hook
'timeclock-update-modeline
))
291 (when timeclock-update-timer
292 (cancel-timer timeclock-update-timer
)
293 (setq timeclock-update-timer nil
))
294 (if (boundp 'display-time-hook
)
295 (remove-hook 'display-time-hook
'timeclock-update-modeline
))
296 (if timeclock-use-display-time
298 ;; Update immediately so there is a visible change
299 ;; on calling this function.
300 (if display-time-mode
(timeclock-update-modeline))
301 (add-hook 'display-time-hook
'timeclock-update-modeline
))
302 (setq timeclock-update-timer
303 (run-at-time nil
60 'timeclock-update-modeline
))))
304 (setq global-mode-string
305 (delq 'timeclock-mode-string global-mode-string
))
306 (remove-hook 'timeclock-event-hook
'timeclock-update-modeline
)
307 (if (boundp 'display-time-hook
)
308 (remove-hook 'display-time-hook
309 'timeclock-update-modeline
))
310 (when timeclock-update-timer
311 (cancel-timer timeclock-update-timer
)
312 (setq timeclock-update-timer nil
)))
313 (force-mode-line-update)
314 (setq timeclock-modeline-display on-p
)))
316 ;; This has to be here so that the function definition of
317 ;; `timeclock-modeline-display' is known to the "set" function.
318 (defcustom timeclock-modeline-display nil
319 "Toggle modeline display of time remaining.
320 You must modify via \\[customize] for this variable to have an effect."
321 :set
(lambda (symbol value
)
322 (setq timeclock-modeline-display
323 (timeclock-modeline-display (or value
0))))
328 (defsubst timeclock-time-to-date
(time)
329 "Convert the TIME value to a textual date string."
330 (format-time-string "%Y/%m/%d" time
))
333 (defun timeclock-in (&optional arg project find-project
)
334 "Clock in, recording the current time moment in the timelog.
335 With a numeric prefix ARG, record the fact that today has only that
336 many hours in it to be worked. If arg is a non-numeric prefix arg
337 \(non-nil, but not a number), 0 is assumed (working on a holiday or
338 weekend). *If not called interactively, ARG should be the number of
339 _seconds_ worked today*. This feature only has effect the first time
340 this function is called within a day.
342 PROJECT as the project being clocked into. If PROJECT is nil, and
343 FIND-PROJECT is non-nil -- or the user calls `timeclock-in'
344 interactively -- call the function `timeclock-get-project-function' to
345 discover the name of the project."
347 (list (and current-prefix-arg
348 (if (numberp current-prefix-arg
)
349 (* current-prefix-arg
60 60)
351 (if (equal (car timeclock-last-event
) "i")
352 (error "You've already clocked in!")
353 (unless timeclock-last-event
354 (timeclock-reread-log))
355 ;; Either no log file, or day has rolled over.
356 (unless (and timeclock-last-event
357 (equal (timeclock-time-to-date
358 (cadr timeclock-last-event
))
359 (timeclock-time-to-date (current-time))))
360 (let ((workday (or (and (numberp arg
) arg
)
362 (and timeclock-get-workday-function
363 (funcall timeclock-get-workday-function
))
365 (run-hooks 'timeclock-first-in-hook
)
366 ;; settle the discrepancy for the new day
367 (setq timeclock-discrepancy
368 (- (or timeclock-discrepancy
0) workday
))
369 (if (not (= workday timeclock-workday
))
370 (timeclock-log "h" (and (numberp arg
)
371 (number-to-string arg
))))))
372 (timeclock-log "i" (or project
373 (and timeclock-get-project-function
374 (or find-project
(interactive-p))
375 (funcall timeclock-get-project-function
))))
376 (run-hooks 'timeclock-in-hook
)))
379 (defun timeclock-out (&optional arg reason find-reason
)
380 "Clock out, recording the current time moment in the timelog.
381 If a prefix ARG is given, the user has completed the project that was
382 begun during the last time segment.
384 REASON is the user's reason for clocking out. If REASON is nil, and
385 FIND-REASON is non-nil -- or the user calls `timeclock-out'
386 interactively -- call the function `timeclock-get-reason-function' to
387 discover the reason."
389 (or timeclock-last-event
390 (error "You haven't clocked in!"))
391 (if (equal (downcase (car timeclock-last-event
)) "o")
392 (error "You've already clocked out!")
396 (and timeclock-get-reason-function
397 (or find-reason
(interactive-p))
398 (funcall timeclock-get-reason-function
))))
399 (run-hooks 'timeclock-out-hook
)
401 (run-hooks 'timeclock-done-hook
))))
403 ;; Should today-only be removed in favour of timeclock-relative? - gm
404 (defsubst timeclock-workday-remaining
(&optional today-only
)
405 "Return the number of seconds until the workday is complete.
406 The amount returned is relative to the value of `timeclock-workday'.
407 If TODAY-ONLY is non-nil, the value returned will be relative only to
408 the time worked today, and not to past time."
409 (let ((discrep (timeclock-find-discrep)))
411 (- (if today-only
(cadr discrep
)
416 (defun timeclock-status-string (&optional show-seconds today-only
)
417 "Report the overall timeclock status at the present moment.
418 If SHOW-SECONDS is non-nil, display second resolution.
419 If TODAY-ONLY is non-nil, the display will be relative only to time
420 worked today, ignoring the time worked on previous days."
422 (let ((remainder (timeclock-workday-remaining)) ; today-only?
423 (last-in (equal (car timeclock-last-event
) "i"))
426 (format "Currently %s since %s (%s), %s %s, leave at %s"
427 (if last-in
"IN" "OUT")
429 (format-time-string "%-I:%M:%S %p"
430 (nth 1 timeclock-last-event
))
431 (format-time-string "%-I:%M %p"
432 (nth 1 timeclock-last-event
)))
433 (or (nth 2 timeclock-last-event
)
434 (if last-in
"**UNKNOWN**" "workday over"))
435 (timeclock-seconds-to-string remainder show-seconds t
)
438 (timeclock-when-to-leave-string show-seconds today-only
)))
444 (defun timeclock-change (&optional arg project
)
445 "Change to working on a different project, by clocking in then out.
446 With a prefix ARG, consider the previous project as having been
447 finished at the time of changeover. PROJECT is the name of the last
448 project you were working on."
451 (timeclock-in nil project
(interactive-p)))
454 (defun timeclock-query-out ()
455 "Ask the user before clocking out.
456 This is a useful function for adding to `kill-emacs-query-functions'."
457 (and (equal (car timeclock-last-event
) "i")
458 (y-or-n-p "You're currently clocking time, clock out? ")
460 ;; Unconditionally return t for `kill-emacs-query-functions'.
464 (defun timeclock-reread-log ()
465 "Re-read the timeclock, to account for external changes.
466 Returns the new value of `timeclock-discrepancy'."
468 (setq timeclock-discrepancy nil
)
469 (timeclock-find-discrep)
470 (if (and timeclock-discrepancy timeclock-modeline-display
)
471 (timeclock-update-modeline))
472 timeclock-discrepancy
)
474 (defun timeclock-seconds-to-string (seconds &optional show-seconds
476 "Convert SECONDS into a compact time string.
477 If SHOW-SECONDS is non-nil, make the resolution of the return string
478 include the second count. If REVERSE-LEADER is non-nil, it means to
479 output a \"+\" if the time value is negative, rather than a \"-\".
480 This is used when negative time values have an inverted meaning (such
481 as with time remaining, where negative time really means overtime)."
483 (format "%s%d:%02d:%02d"
484 (if (< seconds
0) (if reverse-leader
"+" "-") "")
485 (truncate (/ (abs seconds
) 60 60))
486 (%
(truncate (/ (abs seconds
) 60)) 60)
487 (%
(truncate (abs seconds
)) 60))
489 (if (< seconds
0) (if reverse-leader
"+" "-") "")
490 (truncate (/ (abs seconds
) 60 60))
491 (%
(truncate (/ (abs seconds
) 60)) 60))))
493 (defsubst timeclock-currently-in-p
()
494 "Return non-nil if the user is currently clocked in."
495 (equal (car timeclock-last-event
) "i"))
498 (defun timeclock-workday-remaining-string (&optional show-seconds
500 "Return a string representing the amount of time left today.
501 Display second resolution if SHOW-SECONDS is non-nil. If TODAY-ONLY
502 is non-nil, the display will be relative only to time worked today.
503 See `timeclock-relative' for more information about the meaning of
504 \"relative to today\"."
506 (let ((string (timeclock-seconds-to-string
507 (timeclock-workday-remaining today-only
)
513 (defsubst timeclock-workday-elapsed
()
514 "Return the number of seconds worked so far today.
515 If RELATIVE is non-nil, the amount returned will be relative to past
516 time worked. The default is to return only the time that has elapsed
518 (let ((discrep (timeclock-find-discrep)))
524 (defun timeclock-workday-elapsed-string (&optional show-seconds
)
525 "Return a string representing the amount of time worked today.
526 Display seconds resolution if SHOW-SECONDS is non-nil. If RELATIVE is
527 non-nil, the amount returned will be relative to past time worked."
529 (let ((string (timeclock-seconds-to-string (timeclock-workday-elapsed)
535 (defsubst timeclock-time-to-seconds
(time)
536 "Convert TIME to a floating point number."
537 (+ (* (car time
) 65536.0)
539 (/ (or (car (cdr (cdr time
))) 0) 1000000.0)))
541 (defsubst timeclock-seconds-to-time
(seconds)
542 "Convert SECONDS (a floating point number) to an Emacs time structure."
543 (list (floor seconds
65536)
544 (floor (mod seconds
65536))
545 (floor (* (- seconds
(ffloor seconds
)) 1000000))))
547 ;; Should today-only be removed in favour of timeclock-relative? - gm
548 (defsubst timeclock-when-to-leave
(&optional today-only
)
549 "Return a time value representing at when the workday ends today.
550 If TODAY-ONLY is non-nil, the value returned will be relative only to
551 the time worked today, and not to past time."
552 (timeclock-seconds-to-time
553 (- (timeclock-time-to-seconds (current-time))
554 (let ((discrep (timeclock-find-discrep)))
562 (defun timeclock-when-to-leave-string (&optional show-seconds
564 "Return a string representing at what time the workday ends today.
565 This string is relative to the value of `timeclock-workday'. If
566 SHOW-SECONDS is non-nil, the value printed/returned will include
567 seconds. If TODAY-ONLY is non-nil, the value returned will be
568 relative only to the time worked today, and not to past time."
569 ;; Should today-only be removed in favour of timeclock-relative? - gm
571 (let* ((then (timeclock-when-to-leave today-only
))
574 (format-time-string "%-I:%M:%S %p" then
)
575 (format-time-string "%-I:%M %p" then
))))
580 ;;; Internal Functions:
582 (defvar timeclock-project-list nil
)
583 (defvar timeclock-last-project nil
)
585 (defun timeclock-completing-read (prompt alist
&optional default
)
586 "A version of `completing-read' that works on both Emacs and XEmacs."
587 (if (featurep 'xemacs
)
588 (let ((str (completing-read prompt alist
)))
589 (if (or (null str
) (= (length str
) 0))
592 (completing-read prompt alist nil nil nil nil default
)))
594 (defun timeclock-ask-for-project ()
595 "Ask the user for the project they are clocking into."
596 (timeclock-completing-read
597 (format "Clock into which project (default \"%s\"): "
598 (or timeclock-last-project
599 (car timeclock-project-list
)))
600 (mapcar 'list timeclock-project-list
)
601 (or timeclock-last-project
602 (car timeclock-project-list
))))
604 (defvar timeclock-reason-list nil
)
606 (defun timeclock-ask-for-reason ()
607 "Ask the user for the reason they are clocking out."
608 (timeclock-completing-read "Reason for clocking out: "
609 (mapcar 'list timeclock-reason-list
)))
611 (defun timeclock-update-modeline ()
612 "Update the `timeclock-mode-string' displayed in the modeline.
613 The value of `timeclock-relative' affects the display as described in
614 that variable's documentation."
616 (let ((remainder (timeclock-workday-remaining (not timeclock-relative
)))
617 (last-in (equal (car timeclock-last-event
) "i")))
618 (when (and (< remainder
0)
619 (not (and timeclock-day-over
620 (equal timeclock-day-over
621 (timeclock-time-to-date
623 (setq timeclock-day-over
624 (timeclock-time-to-date (current-time)))
625 (run-hooks 'timeclock-day-over-hook
))
626 (setq timeclock-mode-string
630 (timeclock-seconds-to-string remainder nil t
)
632 'help-echo
"timeclock: time remaining"))))
634 (put 'timeclock-mode-string
'risky-local-variable t
)
636 (defun timeclock-log (code &optional project
)
637 "Log the event CODE to the timeclock log, at the time of call.
638 If PROJECT is a string, it represents the project which the event is
639 being logged for. Normally only \"in\" events specify a project."
640 (with-current-buffer (find-file-noselect timeclock-file
)
641 (goto-char (point-max))
644 (let ((now (current-time)))
646 (format-time-string "%Y/%m/%d %H:%M:%S" now
)
649 (> (length project
) 0)
650 (concat " " project
))
653 (if (equal (downcase code
) "o")
654 (setq timeclock-last-period
655 (- (timeclock-time-to-seconds now
)
656 (timeclock-time-to-seconds
657 (cadr timeclock-last-event
)))
658 timeclock-discrepancy
659 (+ timeclock-discrepancy
660 timeclock-last-period
)))
661 (setq timeclock-last-event
(list code now project
)))
663 (run-hooks 'timeclock-event-hook
)
664 (kill-buffer (current-buffer))))
666 (defvar timeclock-moment-regexp
667 (concat "\\([bhioO]\\)\\s-+"
668 "\\([0-9]+\\)/\\([0-9]+\\)/\\([0-9]+\\)\\s-+"
669 "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)[ \t]*" "\\([^\n]*\\)"))
671 (defsubst timeclock-read-moment
()
672 "Read the moment under point from the timelog."
673 (if (looking-at timeclock-moment-regexp
)
674 (let ((code (match-string 1))
675 (year (string-to-number (match-string 2)))
676 (mon (string-to-number (match-string 3)))
677 (mday (string-to-number (match-string 4)))
678 (hour (string-to-number (match-string 5)))
679 (min (string-to-number (match-string 6)))
680 (sec (string-to-number (match-string 7)))
681 (project (match-string 8)))
682 (list code
(encode-time sec min hour mday mon year
) project
))))
684 (defun timeclock-last-period (&optional moment
)
685 "Return the value of the last event period.
686 If the last event was a clock-in, the period will be open ended, and
687 growing every second. Otherwise, it is a fixed amount which has been
688 recorded to disk. If MOMENT is non-nil, use that as the current time.
689 This is only provided for coherency when used by
690 `timeclock-discrepancy'."
691 (if (equal (car timeclock-last-event
) "i")
692 (- (timeclock-time-to-seconds (or moment
(current-time)))
693 (timeclock-time-to-seconds
694 (cadr timeclock-last-event
)))
695 timeclock-last-period
))
697 (defsubst timeclock-entry-length
(entry)
698 (- (timeclock-time-to-seconds (cadr entry
))
699 (timeclock-time-to-seconds (car entry
))))
701 (defsubst timeclock-entry-begin
(entry)
704 (defsubst timeclock-entry-end
(entry)
707 (defsubst timeclock-entry-project
(entry)
710 (defsubst timeclock-entry-comment
(entry)
714 (defsubst timeclock-entry-list-length
(entry-list)
717 (setq length
(+ length
(timeclock-entry-length (car entry-list
))))
718 (setq entry-list
(cdr entry-list
)))
721 (defsubst timeclock-entry-list-begin
(entry-list)
722 (timeclock-entry-begin (car entry-list
)))
724 (defsubst timeclock-entry-list-end
(entry-list)
725 (timeclock-entry-end (car (last entry-list
))))
727 (defsubst timeclock-entry-list-span
(entry-list)
728 (- (timeclock-time-to-seconds (timeclock-entry-list-end entry-list
))
729 (timeclock-time-to-seconds (timeclock-entry-list-begin entry-list
))))
731 (defsubst timeclock-entry-list-break
(entry-list)
732 (- (timeclock-entry-list-span entry-list
)
733 (timeclock-entry-list-length entry-list
)))
735 (defsubst timeclock-entry-list-projects
(entry-list)
738 (let ((project (timeclock-entry-project (car entry-list
))))
740 (add-to-list 'projects project
)
741 (setq projects
(list project
))))
742 (setq entry-list
(cdr entry-list
)))
746 (defsubst timeclock-day-required
(day)
747 (or (car day
) timeclock-workday
))
749 (defsubst timeclock-day-length
(day)
750 (timeclock-entry-list-length (cdr day
)))
752 (defsubst timeclock-day-debt
(day)
753 (- (timeclock-day-required day
)
754 (timeclock-day-length day
)))
756 (defsubst timeclock-day-begin
(day)
757 (timeclock-entry-list-begin (cdr day
)))
759 (defsubst timeclock-day-end
(day)
760 (timeclock-entry-list-end (cdr day
)))
762 (defsubst timeclock-day-span
(day)
763 (timeclock-entry-list-span (cdr day
)))
765 (defsubst timeclock-day-break
(day)
766 (timeclock-entry-list-break (cdr day
)))
768 (defsubst timeclock-day-projects
(day)
769 (timeclock-entry-list-projects (cdr day
)))
771 (defmacro timeclock-day-list-template
(func)
774 (setq length
(+ length
(,(eval func
) (car day-list
))))
775 (setq day-list
(cdr day-list
)))
778 (defun timeclock-day-list-required (day-list)
779 (timeclock-day-list-template 'timeclock-day-required
))
781 (defun timeclock-day-list-length (day-list)
782 (timeclock-day-list-template 'timeclock-day-length
))
784 (defun timeclock-day-list-debt (day-list)
785 (timeclock-day-list-template 'timeclock-day-debt
))
787 (defsubst timeclock-day-list-begin
(day-list)
788 (timeclock-day-begin (car day-list
)))
790 (defsubst timeclock-day-list-end
(day-list)
791 (timeclock-day-end (car (last day-list
))))
793 (defun timeclock-day-list-span (day-list)
794 (timeclock-day-list-template 'timeclock-day-span
))
796 (defun timeclock-day-list-break (day-list)
797 (timeclock-day-list-template 'timeclock-day-break
))
799 (defun timeclock-day-list-projects (day-list)
802 (let ((projs (timeclock-day-projects (car day-list
))))
805 (add-to-list 'projects
(car projs
))
806 (setq projects
(list (car projs
))))
807 (setq projs
(cdr projs
))))
808 (setq day-list
(cdr day-list
)))
812 (defsubst timeclock-current-debt
(&optional log-data
)
813 (nth 0 (or log-data
(timeclock-log-data))))
815 (defsubst timeclock-day-alist
(&optional log-data
)
816 (nth 1 (or log-data
(timeclock-log-data))))
818 (defun timeclock-day-list (&optional log-data
)
819 (let ((alist (timeclock-day-alist log-data
))
822 (setq day-list
(cons (cdar alist
) day-list
)
826 (defsubst timeclock-project-alist
(&optional log-data
)
827 (nth 2 (or log-data
(timeclock-log-data))))
830 (defun timeclock-log-data (&optional recent-only filename
)
831 "Return the contents of the timelog file, in a useful format.
832 If the optional argument RECENT-ONLY is non-nil, only show the contents
833 from the last point where the time debt (see below) was set.
834 If the optional argument FILENAME is non-nil, it is used instead of
835 the file specified by `timeclock-file.'
837 A timelog contains data in the form of a single entry per line.
838 Each entry has the form:
840 CODE YYYY/MM/DD HH:MM:SS [COMMENT]
842 CODE is one of: b, h, i, o or O. COMMENT is optional when the code is
843 i, o or O. The meanings of the codes are:
845 b Set the current time balance, or \"time debt\". Useful when
846 archiving old log data, when a debt must be carried forward.
847 The COMMENT here is the number of seconds of debt.
849 h Set the required working time for the given day. This must
850 be the first entry for that day. The COMMENT in this case is
851 the number of hours that must be worked. Floating point
854 i Clock in. The COMMENT in this case should be the name of the
857 o Clock out. COMMENT is unnecessary, but can be used to provide
858 a description of how the period went, for example.
860 O Final clock out. Whatever project was being worked on, it is
861 now finished. Useful for creating summary reports.
863 When this function is called, it will return a data structure with the
866 (DEBT ENTRIES-BY-DAY ENTRIES-BY-PROJECT)
868 DEBT is a floating point number representing the number of seconds
869 \"owed\" before any work was done. For a new file (one without a 'b'
870 entry), this is always zero.
872 The two entries lists have similar formats. They are both alists,
873 where the CAR is the index, and the CDR is a list of time entries.
874 For ENTRIES-BY-DAY, the CAR is a textual date string, of the form
875 YYYY/MM/DD. For ENTRIES-BY-PROJECT, it is the name of the project
876 worked on, or t for the default project.
878 The CDR for ENTRIES-BY-DAY is slightly different than for
879 ENTRIES-BY-PROJECT. It has the following form:
881 (DAY-LENGTH TIME-ENTRIES...)
883 For ENTRIES-BY-PROJECT, there is no DAY-LENGTH member. It is simply a
884 list of TIME-ENTRIES. Note that if DAY-LENGTH is nil, it means
885 whatever is the default should be used.
887 A TIME-ENTRY is a recorded time interval. It has the following format
888 \(although generally one does not have to manipulate these entries
889 directly; see below):
891 (BEGIN-TIME END-TIME PROJECT [COMMENT] [FINAL-P])
893 Anyway, suffice it to say there are a lot of structures. Typically
894 the user is expected to manipulate to the day(s) or project(s) that he
895 or she wants, at which point the following helper functions may be
898 timeclock-day-required
905 timeclock-day-projects
907 timeclock-day-list-required
908 timeclock-day-list-length
909 timeclock-day-list-debt
910 timeclock-day-list-begin
911 timeclock-day-list-end
912 timeclock-day-list-span
913 timeclock-day-list-break
914 timeclock-day-list-projects
916 timeclock-entry-length
917 timeclock-entry-begin
919 timeclock-entry-project
920 timeclock-entry-comment
922 timeclock-entry-list-length
923 timeclock-entry-list-begin
924 timeclock-entry-list-end
925 timeclock-entry-list-span
926 timeclock-entry-list-break
927 timeclock-entry-list-projects
929 A few comments should make the use of the above functions obvious:
931 `required' is the amount of time that must be spent during a day, or
932 sequence of days, in order to have no debt.
934 `length' is the actual amount of time that was spent.
936 `debt' is the difference between required time and length. A
937 negative debt signifies overtime.
939 `begin' is the earliest moment at which work began.
941 `end' is the final moment work was done.
943 `span' is the difference between begin and end.
945 `break' is the difference between span and length.
947 `project' is the project that was worked on, and `projects' is a
948 list of all the projects that were worked on during a given period.
950 `comment', where it applies, could mean anything.
952 There are a few more functions available, for locating day and entry
955 timeclock-day-alist LOG-DATA
956 timeclock-project-alist LOG-DATA
957 timeclock-current-debt LOG-DATA
959 See the documentation for the given function if more info is needed."
960 (let* ((log-data (list 0.0 nil nil
))
962 (todays-date (timeclock-time-to-date now
))
963 last-date-limited last-date-seconds last-date
964 (line 0) last beg day entry event
)
966 (insert-file-contents (or filename timeclock-file
))
968 (goto-char (point-max))
969 (unless (re-search-backward "^b\\s-+" nil t
)
970 (goto-char (point-min))))
971 (while (or (setq event
(timeclock-read-moment))
973 (setq last t event
(list "o" now
))))
974 (setq line
(1+ line
))
975 (cond ((equal (car event
) "b")
976 (setcar log-data
(string-to-number (nth 2 event
))))
977 ((equal (car event
) "h")
978 (setq last-date-limited
(timeclock-time-to-date (cadr event
))
979 last-date-seconds
(* (string-to-number (nth 2 event
))
981 ((equal (car event
) "i")
983 (error "Error in format of timelog file, line %d" line
)
985 (setq entry
(list (cadr event
) nil
986 (and (> (length (nth 2 event
)) 0)
988 (let ((date (timeclock-time-to-date (cadr event
))))
990 (not (equal date last-date
)))
992 (setcar (cdr log-data
)
993 (cons (cons last-date day
)
995 (setq day
(list (and last-date-limited
996 last-date-seconds
))))
998 (setq day
(list (and last-date-limited
999 last-date-seconds
)))))
1000 (setq last-date date
1001 last-date-limited nil
)))
1002 ((equal (downcase (car event
)) "o")
1004 (error "Error in format of timelog file, line %d" line
)
1006 (setcar (cdr entry
) (cadr event
))
1007 (let ((desc (and (> (length (nth 2 event
)) 0)
1010 (nconc entry
(list (nth 2 event
))))
1011 (if (equal (car event
) "O")
1012 (nconc entry
(if desc
1015 (nconc day
(list entry
))
1016 (setq desc
(nth 2 entry
))
1017 (let ((proj (assoc desc
(nth 2 log-data
))))
1019 (setcar (cddr log-data
)
1020 (cons (cons desc
(list entry
))
1021 (car (cddr log-data
))))
1022 (nconc (cdr proj
) (list entry
)))))))
1025 (setcar (cdr log-data
)
1026 (cons (cons last-date day
)
1030 (defun timeclock-find-discrep ()
1031 "Calculate time discrepancies, in seconds.
1032 The result is a three element list, containing the total time
1033 discrepancy, today's discrepancy, and the time worked today."
1034 ;; This is not implemented in terms of the functions above, because
1035 ;; it's a bit wasteful to read all of that data in, just to throw
1036 ;; away more than 90% of the information afterwards.
1038 ;; If it were implemented using those functions, it would look
1039 ;; something like this:
1040 ;; (let ((days (timeclock-day-alist (timeclock-log-data)))
1043 ;; (setq total (+ total (- (timeclock-day-length (cdar days))
1044 ;; (timeclock-day-required (cdar days))))
1045 ;; days (cdr days)))
1047 (let* ((now (current-time))
1048 (todays-date (timeclock-time-to-date now
))
1049 (first t
) (accum 0) (elapsed 0)
1050 event beg last-date avg
1051 last-date-limited last-date-seconds
)
1052 (unless timeclock-discrepancy
1053 (when (file-readable-p timeclock-file
)
1054 (setq timeclock-project-list nil
1055 timeclock-last-project nil
1056 timeclock-reason-list nil
1057 timeclock-elapsed
0)
1059 (insert-file-contents timeclock-file
)
1060 (goto-char (point-max))
1061 (unless (re-search-backward "^b\\s-+" nil t
)
1062 (goto-char (point-min)))
1063 (while (setq event
(timeclock-read-moment))
1064 (cond ((equal (car event
) "b")
1065 (setq accum
(string-to-number (nth 2 event
))))
1066 ((equal (car event
) "h")
1067 (setq last-date-limited
1068 (timeclock-time-to-date (cadr event
))
1070 (* (string-to-number (nth 2 event
)) 3600.0)))
1071 ((equal (car event
) "i")
1072 (when (and (nth 2 event
)
1073 (> (length (nth 2 event
)) 0))
1074 (add-to-list 'timeclock-project-list
(nth 2 event
))
1075 (setq timeclock-last-project
(nth 2 event
)))
1076 (let ((date (timeclock-time-to-date (cadr event
))))
1078 (not (equal date last-date
))
1081 accum
(- accum
(if last-date-limited
1083 timeclock-workday
))))
1084 (setq last-date date
1085 last-date-limited nil
)
1087 (error "Error in format of timelog file!")
1088 (setq beg
(timeclock-time-to-seconds (cadr event
))))))
1089 ((equal (downcase (car event
)) "o")
1090 (if (and (nth 2 event
)
1091 (> (length (nth 2 event
)) 0))
1092 (add-to-list 'timeclock-reason-list
(nth 2 event
)))
1094 (error "Error in format of timelog file!")
1095 (setq timeclock-last-period
1096 (- (timeclock-time-to-seconds (cadr event
)) beg
)
1097 accum
(+ timeclock-last-period accum
)
1099 (if (equal last-date todays-date
)
1100 (setq timeclock-elapsed
1101 (+ timeclock-last-period timeclock-elapsed
)))))
1102 (setq timeclock-last-event event
1103 timeclock-last-event-workday
1104 (if (equal (timeclock-time-to-date now
) last-date-limited
)
1108 (setq timeclock-discrepancy accum
))))
1109 (unless timeclock-last-event-workday
1110 (setq timeclock-last-event-workday timeclock-workday
))
1111 (setq accum
(or timeclock-discrepancy
0)
1112 elapsed
(or timeclock-elapsed elapsed
))
1113 (if timeclock-last-event
1114 (if (equal (car timeclock-last-event
) "i")
1115 (let ((last-period (timeclock-last-period now
)))
1116 (setq accum
(+ accum last-period
)
1117 elapsed
(+ elapsed last-period
)))
1118 (if (not (equal (timeclock-time-to-date
1119 (cadr timeclock-last-event
))
1120 (timeclock-time-to-date now
)))
1121 (setq accum
(- accum timeclock-last-event-workday
)))))
1122 (list accum
(- elapsed timeclock-last-event-workday
)
1125 ;;; A reporting function that uses timeclock-log-data
1127 (defun timeclock-time-less-p (t1 t2
)
1128 "Say whether time T1 is less than time T2."
1129 (or (< (car t1
) (car t2
))
1130 (and (= (car t1
) (car t2
))
1131 (< (nth 1 t1
) (nth 1 t2
)))))
1133 (defun timeclock-day-base (&optional time
)
1134 "Given a time within a day, return 0:0:0 within that day.
1135 If optional argument TIME is non-nil, use that instead of the current time."
1136 (let ((decoded (decode-time (or time
(current-time)))))
1137 (setcar (nthcdr 0 decoded
) 0)
1138 (setcar (nthcdr 1 decoded
) 0)
1139 (setcar (nthcdr 2 decoded
) 0)
1140 (apply 'encode-time decoded
)))
1142 (defun timeclock-geometric-mean (l)
1143 "Compute the geometric mean of the list L."
1147 (setq total
(+ total
(car l
))
1154 (defun timeclock-generate-report (&optional html-p
)
1155 "Generate a summary report based on the current timelog file.
1156 By default, the report is in plain text, but if the optional argument
1157 HTML-P is non-nil html markup is added."
1159 (let ((log (timeclock-log-data))
1160 (today (timeclock-day-base)))
1161 (if html-p
(insert "<p>"))
1162 (insert "Currently ")
1163 (let ((project (nth 2 timeclock-last-event
))
1164 (begin (nth 1 timeclock-last-event
))
1166 (if (timeclock-currently-in-p)
1168 (if (or (null project
) (= (length project
) 0))
1169 (progn (insert "Done Working Today")
1173 (insert " since " (format-time-string "%Y/%m/%d %-I:%M %p" begin
))
1175 (insert "<br>\n<b>")
1177 (if (timeclock-currently-in-p)
1178 (insert "Working on "))
1180 (insert project
"</b><br>\n")
1181 (insert project
"*\n"))
1182 (let ((proj-data (cdr (assoc project
(timeclock-project-alist log
))))
1183 (two-weeks-ago (timeclock-seconds-to-time
1184 (- (timeclock-time-to-seconds today
)
1186 two-week-len today-len
)
1188 (if (not (timeclock-time-less-p
1189 (timeclock-entry-begin (car proj-data
)) today
))
1190 (setq today-len
(timeclock-entry-list-length proj-data
)
1192 (if (and (null two-week-len
)
1193 (not (timeclock-time-less-p
1194 (timeclock-entry-begin (car proj-data
))
1196 (setq two-week-len
(timeclock-entry-list-length proj-data
)))
1197 (setq proj-data
(cdr proj-data
))))
1198 (if (null two-week-len
)
1199 (setq two-week-len today-len
))
1200 (if html-p
(insert "<p>"))
1202 (insert "\nTime spent on this task today: "
1203 (timeclock-seconds-to-string today-len
)
1204 ". In the last two weeks: "
1205 (timeclock-seconds-to-string two-week-len
))
1207 (insert "\nTime spent on this task in the last two weeks: "
1208 (timeclock-seconds-to-string two-week-len
))))
1209 (if html-p
(insert "<br>"))
1211 (timeclock-seconds-to-string (timeclock-workday-elapsed))
1213 (timeclock-seconds-to-string (timeclock-workday-remaining))
1214 " remaining, done at "
1215 (timeclock-when-to-leave-string) "\n")))
1216 (if html-p
(insert "<p>"))
1217 (insert "\nThere have been "
1219 (length (timeclock-day-alist log
)))
1220 " days of activity, starting "
1221 (caar (last (timeclock-day-alist log
))))
1222 (if html-p
(insert "</p>"))
1226 <td width=\"25\"><br></td><td>
1227 <table border=1 cellpadding=3>
1228 <tr><th><i>Statistics</i></th>
1235 (let* ((day-list (timeclock-day-list))
1236 (thirty-days-ago (timeclock-seconds-to-time
1237 (- (timeclock-time-to-seconds today
)
1239 (three-months-ago (timeclock-seconds-to-time
1240 (- (timeclock-time-to-seconds today
)
1242 (six-months-ago (timeclock-seconds-to-time
1243 (- (timeclock-time-to-seconds today
)
1245 (one-year-ago (timeclock-seconds-to-time
1246 (- (timeclock-time-to-seconds today
)
1248 (time-in (vector (list t
) (list t
) (list t
) (list t
) (list t
)))
1249 (time-out (vector (list t
) (list t
) (list t
) (list t
) (list t
)))
1250 (breaks (vector (list t
) (list t
) (list t
) (list t
) (list t
)))
1251 (workday (vector (list t
) (list t
) (list t
) (list t
) (list t
)))
1252 (lengths (vector '(0 0) thirty-days-ago three-months-ago
1253 six-months-ago one-year-ago
)))
1254 ;; collect statistics from complete timelog
1258 (unless (timeclock-time-less-p
1259 (timeclock-day-begin (car day-list
))
1261 (let ((base (timeclock-time-to-seconds
1263 (timeclock-day-begin (car day-list
))))))
1264 (nconc (aref time-in i
)
1265 (list (- (timeclock-time-to-seconds
1266 (timeclock-day-begin (car day-list
)))
1268 (let ((span (timeclock-day-span (car day-list
)))
1269 (len (timeclock-day-length (car day-list
)))
1270 (req (timeclock-day-required (car day-list
))))
1271 ;; If the day's actual work length is less than
1272 ;; 70% of its span, then likely the exit time
1273 ;; and break amount are not worthwhile adding to
1275 (when (and (> span
0)
1276 (> (/ (float len
) (float span
)) 0.70))
1277 (nconc (aref time-out i
)
1278 (list (- (timeclock-time-to-seconds
1279 (timeclock-day-end (car day-list
)))
1281 (nconc (aref breaks i
) (list (- span len
))))
1283 (setq len
(+ len
(- timeclock-workday req
))))
1284 (nconc (aref workday i
) (list len
)))))
1286 (setq day-list
(cdr day-list
)))
1287 ;; average statistics
1290 (aset time-in i
(timeclock-geometric-mean
1291 (cdr (aref time-in i
))))
1292 (aset time-out i
(timeclock-geometric-mean
1293 (cdr (aref time-out i
))))
1294 (aset breaks i
(timeclock-geometric-mean
1295 (cdr (aref breaks i
))))
1296 (aset workday i
(timeclock-geometric-mean
1297 (cdr (aref workday i
))))
1299 ;; Output the HTML table
1301 (insert "<td align=\"center\">Time in</td>\n")
1304 (insert "<td align=\"right\">"
1305 (timeclock-seconds-to-string (aref time-in i
))
1311 (insert "<td align=\"center\">Time out</td>\n")
1314 (insert "<td align=\"right\">"
1315 (timeclock-seconds-to-string (aref time-out i
))
1321 (insert "<td align=\"center\">Break</td>\n")
1324 (insert "<td align=\"right\">"
1325 (timeclock-seconds-to-string (aref breaks i
))
1331 (insert "<td align=\"center\">Workday</td>\n")
1334 (insert "<td align=\"right\">"
1335 (timeclock-seconds-to-string (aref workday i
))
1340 <td colspan=\"6\" align=\"center\">
1341 <i>These are approximate figures</i></td>
1346 ;;; A helpful little function
1348 (defun timeclock-visit-timelog ()
1349 "Open the file named by `timeclock-file' in another window."
1351 (find-file-other-window timeclock-file
))
1353 (provide 'timeclock
)
1355 (run-hooks 'timeclock-load-hook
)
1357 ;; make sure we know the list of reasons, projects, and have computed
1358 ;; the last event and current discrepancy.
1359 (if (file-readable-p timeclock-file
)
1360 (timeclock-reread-log))
1362 ;;; timeclock.el ends here