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