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