---
(setq d-emacs-start-time (current-time))
(if emacs-dialect-xemacs-p
(setq display-time-compatible t))
(defun d-seconds-of-time-difference (old-time new-time)
"Returns the number of seconds that separate two time-measurments,
as returned by the function `current-time'."
(d-assert (timep old-time))
(d-assert (timep new-time))
(let ((super-old (+ (* 65536.0 (car old-time)) (cadr old-time)))
(super-new (+ (* 65536.0 (car new-time)) (cadr new-time))))
(- super-new super-old)))
(defun say-number-in-words (value units &optional short)
(if short
(if (/= 0 value)
(concat
(int-to-string value)
(concat (substring units 0 1))))
(if (/= 0 value)
(concat
(int-to-string value)
(if (= 1 value)
(concat " " units " ")
(concat " " units "s "))))))
(defun d-seconds-to-readable-string (time-in-secs &optional no-seconds short)
"Converts TIME-IN-SECS to a readable value of years, weeks,
days, hours, minutes, seconds. Called with x nil nil this
function is the inverse of `timer-duration'. Assumes there are 60
seconds in 1 minute, 60 minutes in 1 hour, 24 hours in 1 day, 7
days in 1 week, 4 weeks in 1 month (this is an approximation) and
12 months in 1 year. Note: months are not returned because
months (m) conflict with minutes (also m)."
(let* ((secs-per-hour (* 60 60))
(secs-per-day (* 24 secs-per-hour))
(secs-per-week (* 7 secs-per-day))
(secs-per-year (* 365.25 secs-per-day))
(years (floor (/ time-in-secs secs-per-year)))
(time-in-secs (- time-in-secs (* secs-per-year years)))
(weeks (floor (/ time-in-secs secs-per-week)))
(time-in-secs (- time-in-secs (* secs-per-week weeks)))
(days (floor (/ time-in-secs secs-per-day)))
(time-in-secs (- time-in-secs (* secs-per-day days)))
(hours (floor (/ time-in-secs secs-per-hour)))
(time-in-secs (- time-in-secs (* secs-per-hour hours)))
(minutes (floor (/ time-in-secs 60)))
(time-in-secs (- time-in-secs (* 60 minutes)))
(seconds (round time-in-secs))
(string (concat (say-number-in-words years "year" short)
(say-number-in-words weeks "week" short)
(say-number-in-words days "day" short)
(say-number-in-words hours "hour" short)
(say-number-in-words minutes "minute" short)
(if (not no-seconds)
(say-number-in-words seconds "second" short)))))
(if (string= "" string)
"Zero time!"
(if short string (substring string 0 -1)))))
(d-quote
condition-case err
(progn
(setq display-time-string-forms
'(year
"-"
(format "%02d" (read month))
"-"
(format "%02d" (read day))
" "
dayname " "
24-hours ":" minutes))
(setq display-time-string-forms nil)
(display-time))
(error
(message "Cannot display time %s" (cdr err))))
(require 'timer)
(defun d-time--get-stamp (&optional time)
(interactive "Senter time string: ")
(let ((time-list (decode-time time)))
(setq yyyymmdd (format "%04d%02d%02d" (nth 5 time-list) (nth 4 time-list) (nth 3 time-list)))
(setq hhmmss (format "%02d%02d%02d" (nth 2 time-list) (nth 1 time-list) (nth 0 time-list)))
(setq dt-stamp (concat yyyymmdd "-" hhmmss))
dt-stamp
))
(defun d-time--decode-time-readable (&optional time)
(interactive)
(let* ((decoded (decode-time time))
(year (nth 5 decoded))
(month (nth 4 decoded))
(day (nth 3 decoded))
(hour (nth 2 decoded))
(minute (nth 1 decoded))
(second (nth 0 decoded))
(yyyymmdd (format "%04d%02d%02d" year month day))
(hhmmss (format "%02d%02d%02d" hour minute second)))
(concat yyyymmdd "-" hhmmss)))
(defun d-time--encode-time-readable (time)
(interactive)
(let* ((year (d-read-str (substring time 0 4)))
(month (d-read-str (substring time 4 6)))
(day (d-read-str (substring time 6 8)))
(hour (d-read-str (substring time 9 11)))
(minute (d-read-str (substring time 11 13)))
(second (d-read-str (substring time 13 15)))
(encoded (encode-time second minute hour day month year)))
encoded))
(defun d-time--frame-title ()
(let (time dow year month day hour minute second)
(setq time (decode-time (current-time)))
(setq dow (aref ["SUN" "MON" "TUE" "WED" "THU" "FRI" "SAT"] (nth 6 time)))
(setq year (nth 5 time))
(setq month (nth 4 time))
(setq day (nth 3 time))
(setq hour (nth 2 time))
(setq minute (nth 1 time))
(setq second (nth 0 time))
(setq frame-title-format (format "%02d:%02d:%02d %s %04d-%02d-%02d %s"
hour
minute
second
dow
year
month
day
(cond
((buffer-file-name)
(buffer-file-name))
((buffer-name)
(buffer-name))
(default-directory
default-directory)
)))))
(defun d-time--print-full-date-as-YYYYmmdd-HHMMSS (time &optional no-dow)
(let (time1 dow year month day hour minute second)
(setq time1 (decode-time (or time (current-time))))
(setq dow (aref ["SUN" "MON" "TUE" "WED" "THU" "FRI" "SAT"] (nth 6 time1)))
(setq year (nth 5 time1))
(setq month (nth 4 time1))
(setq day (nth 3 time1))
(setq hour (nth 2 time1))
(setq minute (nth 1 time1))
(setq second (nth 0 time1))
(format "%04d-%02d-%02d %s %02d-%02d-%02d"
year
month
day
(if no-dow "" dow) hour
minute
second
)))
(defun d-time--add-hours (t1 hours)
(progn
(setq t1 (current-time))
(setq t1 (decode-time t1))
(setq slot-seconds 0)
(setq slot-minutes 1)
(setq slot-hours 2)
(setq slot-days 3)
(setq slot-months 4)
(setq slot-years 5)
(incf (nth slot-hours t1) hours)
(setq t1 (apply 'encode-time t1))
(setq t1 (d-time--decode-time-readable t1)))
)
(byte-compile 'd-time--frame-title)
(run-with-timer 1 1 'd-time--frame-title)
(defun d-time--current-line-as-string ()
(buffer-substring-no-properties (point-at-bol)
(point-at-eol)))
(defun d-time--YYYYmmdd-HHMMSS-to-time (YYYYmmdd-HHMMSS)
(let (years months days hours minutes seconds)
(progn
(d-assert (= (length "YYYYMMDD-HHMMSS") (length YYYYmmdd-HHMMSS)))
(setq years (d-read-str (substring YYYYmmdd-HHMMSS 0 (length "YYYY"))))
(setq months (d-read-str (substring YYYYmmdd-HHMMSS (length "YYYY") (length "YYYYMM"))))
(setq days (d-read-str (substring YYYYmmdd-HHMMSS (length "YYYYMM") (length "YYYYMMDD"))))
(setq hours (d-read-str (substring YYYYmmdd-HHMMSS (length "YYYYMMDD-") (length "YYYYMMDD-HH"))))
(setq minutes (d-read-str (substring YYYYmmdd-HHMMSS (length "YYYYMMDD-HH") (length "YYYYMMDD-HHMM"))))
(setq seconds (d-read-str (substring YYYYmmdd-HHMMSS (length "YYYYMMDD-HHMM") (length "YYYYMMDD-HHMMSS"))))
(encode-time seconds minutes hours days months years)
)))
(defun d-time--YYYYmmdd-to-time (YYYYmmdd)
(let (years months days)
(progn
(assert (= (length "YYYYMMDD") (length YYYYmmdd)))
(setq years (d-read-str (substring YYYYmmdd 0 (length "YYYY"))))
(setq months (d-read-str (substring YYYYmmdd (length "YYYY") (length "YYYYmm"))))
(setq days (d-read-str (substring YYYYmmdd (length "YYYYmm") (length "YYYYmmdd"))))
(encode-time 0 0 0 days months years)
)))
(run-with-timer 1 1 'force-mode-line-update)
(defun load-file-most-recent (str)
(interactive "FEnter filename: ")
(let ((el-str (concat str ".el"))
(elc-str (concat str ".elc")))
(setq el-time (nth 5 (file-attributes el-str)))
(setq elc-time (nth 5 (file-attributes elc-str)))
(setq dif (d-seconds-of-time-difference el-time elc-time))
(if (<= dif 0)
(progn
(message "Loading file...%s" el-str)
(load-file el-str))
(message "Loading file...%s" elc-str)
(load-file elc-str)
)))
(defun time-between-times (year1 month1 day1
year2 month2 day2)
(let*
((seconds-then (float-time (encode-time 0 0 0 day1 month1 year1)))
(seconds-now (float-time (encode-time 0 0 0 day2 month2 year2)))
(seconds-diff (- seconds-now seconds-then)) )
(format-seconds "%Y, %D" seconds-diff))
)
(defun get-time-since (year month day)
(interactive "nyear: \nnmonth: \nnday: ")
(message "%s" (format-seconds
"%Y, %D"
(float-time
(time-since (encode-time 0 0 0 day month year)) ))))
(progn
(setq slot-seconds 0)
(setq slot-minutes 1)
(setq slot-hours 2)
(setq slot-days 3)
(setq slot-months 4)
(setq slot-years 5))
(defun timep (time)
(and (consp time)
(integerp (car time))
(integerp (cadr time))
(> (car time) 0)
(> (cadr time) 0)))
(defun d-add-seconds (secs-to-add &optional time)
(setq time (decode-time time))
(let ((secs (nth slot-seconds time))
(minutes (nth slot-minutes time))
(hours (nth slot-hours time))
(days (nth slot-days time))
(months (nth slot-months time))
(years (nth slot-years time)))
(incf secs secs-to-add)
(encode-time secs minutes hours days months years))
)
(provide 'd-time)