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