timeclock.el 50 KB

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