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