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