timeclock.el 51 KB

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