Guest User

Untitled

a guest
Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. ;; use appt-convert-time instead if appt is loaded
  2. (defun iy/convert-time (time2conv)
  3. "Convert hour:min[am/pm] format TIME2CONV to minutes from midnight.
  4. A period (.) can be used instead of a colon (:) to separate the
  5. hour and minute parts."
  6. ;; Formats that should be accepted:
  7. ;; 10:00 10.00 10h00 10h 10am 10:00am 10.00am
  8. (let ((min (if (string-match "[h:.]\\([0-9][0-9]\\)" time2conv)
  9. (string-to-number (match-string 1 time2conv))
  10. 0))
  11. (hr (if (string-match "[0-9]*[0-9]" time2conv)
  12. (string-to-number (match-string 0 time2conv))
  13. 0)))
  14. ;; Convert the time appointment time into 24 hour time.
  15. (cond ((and (string-match "pm" time2conv) (< hr 12))
  16. (setq hr (+ 12 hr)))
  17. ((and (string-match "am" time2conv) (= hr 12))
  18. (setq hr 0)))
  19. ;; Convert the actual time into minutes.
  20. (+ (* hr 60) min)))
  21.  
  22. (defun iy/org-get-entries (files date)
  23. (let (entry entries)
  24. (dolist (file files)
  25. (catch 'nextfile
  26. (org-check-agenda-file file)
  27. (setq entry (org-agenda-get-day-entries
  28. file date :scheduled :timestamp))
  29. (setq entries (append entry entries))))
  30. entries))
  31.  
  32. (defun sacha/org-calculate-free-time (date start-time end-of-day)
  33. "Return a cons cell of the form (TASK-TIME . FREE-TIME) for DATE, given START-TIME and END-OF-DAY.
  34. DATE is a list of the form (MONTH DAY YEAR).
  35. START-TIME and END-OF-DAY are the number of minutes past midnight."
  36. (save-window-excursion
  37. (let* ((entries (iy/org-get-entries (org-agenda-files) date))
  38. (total-unscheduled 0)
  39. (total-gap 0)
  40. (last-timestamp start-time)
  41. scheduled-entries)
  42. ;; For each item on the list
  43. (dolist (entry entries)
  44. (let ((time (get-text-property 1 'time entry))
  45. (effort (org-get-effort (get-text-property 1 'org-hd-marker entry))))
  46. (cond
  47. ((and time
  48. (string-match "\\([^-]+\\)-\\([^-]+\\)" time))
  49. (push (cons
  50. (save-match-data (iy/convert-time (match-string 1 time)))
  51. (save-match-data (iy/convert-time (match-string 2 time))))
  52. scheduled-entries))
  53. ((and time
  54. (string-match "\\([^-]+\\)\\.+" time)
  55. (not (eq effort nil)))
  56. (let ((start (save-match-data (iy/convert-time (match-string 1 time))))
  57. (effort (save-match-data (iy/convert-time effort))))
  58. (push (cons start (+ start effort)) scheduled-entries)))
  59. ((not (eq effort nil))
  60. (setq total-unscheduled (+ (iy/convert-time effort)
  61. total-unscheduled))))))
  62. ;; Sort the scheduled entries by time
  63. (setq scheduled-entries (sort scheduled-entries (lambda (a b) (< (car a) (car b)))))
  64. (while scheduled-entries
  65. (let ((start (car (car scheduled-entries)))
  66. (end (cdr (car scheduled-entries))))
  67. (cond
  68. ;; are we in the middle of this timeslot?
  69. ((and (>= last-timestamp start)
  70. (<= last-timestamp end))
  71. ;; move timestamp later, no change to time
  72. (setq last-timestamp end))
  73. ;; are we completely before this timeslot?
  74. ((< last-timestamp start)
  75. ;; add gap to total, skip to the end
  76. (setq total-gap (+ (- start last-timestamp) total-gap))
  77. (setq last-timestamp end)))
  78. (setq scheduled-entries (cdr scheduled-entries))))
  79. (if (< last-timestamp end-of-day)
  80. (setq total-gap (+ (- end-of-day last-timestamp) total-gap)))
  81. (cons total-unscheduled total-gap))))
Add Comment
Please, Sign In to add comment