elisp - using date-time in emacs spreadsheets -
elisp - using date-time in emacs spreadsheets -
i'm starting utilize ses-mode in emacs, , plan utilize timestamps, not manage have them parsed in way can use.
i'm taking measurements on 3 days of week, distances between 1 measurement , other either 2 or 3 days. chose utilize ses-mode in emacs because runs on of computers, including phone.
my spreadsheet contains datestamp, conductivity, temperature, , gallon count, couple of subsequent lines this:
2014-10-03 2.95 33.4 4031070 2014-10-06 3.07 33.5 4086930 2014-10-08 2.97 33.6 4119590
i add together 2 more columns, first difference of days between readings, sec "gallon-per-day" value.
i not manage have string timestamp parsed format can computations, staying within simple emacs spreadsheet (ses).
i've tried date-to-time
, returns same value (14445 17280)
.
parse-time-string
gives me 9-tuple can't straight pass format-time-string
.
the function encode-time
helps:
(let ((l (parse-time-string "2014-09-12"))) (format-time-string "%d %m %y" (encode-time 0 0 0 (nth 3 l) (nth 4 l) (nth 5 l))))
the next version uses cl-flet
avoid doubling of code if encoding needed multiple times. if need encoding in other functions can utilize defun
instead of cl-flet
.
(eval-when (compile) (require 'cl)) ;; cl-flet (let ((a2 "2014-10-08") ;; testing (a1 "2014-10-03")) ;; testing (cl-flet ((encode (str) (let ((l (parse-time-string str))) (encode-time 0 0 0 (nth 3 l) (nth 4 l) (nth 5 l))))) (let* ((t-prev (encode a1)) (t-this (encode a2))) (/ (time-to-seconds (time-subtract t-this t-prev)) (* 24 60 60)))))
as function:
class="lang-lisp prettyprint-override">(eval-when (compile) (require 'cl)) ;; cl-flet (defun day-diff (date1 date2) "calculate difference of dates in days between date1-str , date2-str." (interactive "sdate1:\nsdate2:") (cl-flet ((encode (str) (let ((l (parse-time-string str))) (encode-time 0 0 0 (nth 3 l) (nth 4 l) (nth 5 l))))) (setq date1 (encode date1) date2 (encode date2)) (let ((ret (/ (time-to-seconds (time-subtract date1 date2)) (* 24 60 60)))) (when (called-interactively-p 'any) (message "day difference: %s" ret)) ret))) (put 'day-diff 'safe-function t)
an alternative using calc
be:
(require 'calc) (defun day-diff (date1 date2) "calculate difference of dates in days between date1-str , date2-str." (interactive "sdate1:\nsdate2:") (let ((ret (string-to-number (calc-eval (format "<%s>-<%s>" date1 date2))))) (when (called-interactively-p 'any) (message "day difference: %s" ret)) ret))
if omit nice-to-have features becomes simple cell formula: (string-to-number (calc-eval (format "<%s>-<%s>" a1 a2)))
.
if want save stuff in spreadsheet can set defun in table cell a1
. more simple example:
(progn (defun day-diff (date1 date2) (string-to-number (calc-eval (format "<%s>-<%s>" date1 date2)))) (put 'day 'safe-function t) "actual header")
to have more convenient editing possibility can switch m-x lisp-mode
. there find
^l (ses-cell a1 "actual header" (progn (defun day-diff (date1 date2) (string-to-number (calc-eval (format "<%s>-<%s>" date1 date2)))) (put 'day 'safe-function t) "actual header") nil nil)
which can edit. not insert linebreaks! ses
identifies cell-positions line numbers in file!
another nice alternative set definition of function file-local variable list. switch lisp-interaction mode m-x lisp-interaction-mode
. go end of file. there find lines:
;; local variables: ;; mode: ses ;; end:
add function definition eval
list:
;; local variables: ;; mode: ses ;; eval: ;; (progn ;; (defun day-diff (date1 date2) ;; (string-to-number (calc-eval (format "<%s>-<%s>" date1 date2)))) ;; (put 'day-diff 'safe-function t)) ;; end:
you can add together progn
without comment characters ;
. in case indentation works. afterwards can phone call comment-region
progn
.
you can save file , run m-x normal-mode
. afterwards function defined , can utilize in spreadsheet.
emacs elisp spreadsheet ses
Comments
Post a Comment