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