help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Elisp - Function returning a list


From: Emanuel Berg
Subject: Re: Elisp - Function returning a list
Date: Wed, 16 Dec 2020 05:48:18 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Yuri Khan wrote:

>> For me 800 is 8 O'Clock
>
> You cannot just do decimal arithmetics on times written in
> that format.
>
> Start: 800  End: 1100  Skip: 45
> Expected: 800 845 930 1015 1100
> Actual: 800 845 890 935 980 1025 1070

Arithmetics on time? Is that the purpose?

If so, check out this:

;;; -*- lexical-binding: t -*-
;;;
;;; this file:
;;;   http://user.it.uu.se/~embe8573/emacs-init/time-cmp.el
;;;   https://dataswamp.org/~incal/emacs-init/time-cmp.el

(defun wall-clock-time (h1 m1 s1 h2 m2 s2)
  (let*((d   08) ; arbitrary day to use below, any would do
        (m   05) ; actually something cool happened that day
        (y 1978) ; in the history of climbing
        (total-seconds-1 (float-time (encode-time s1 m1 h1 d m y)))
        (total-seconds-2 (float-time (encode-time s2 m2 h2 d m y)))
        (s-diff (- total-seconds-2 total-seconds-1)) )
    (format-seconds "%.2h:%.2m:%.2s" s-diff) ))
(defalias 'wct #'wall-clock-time)
;; (wct 09 35 10 23 00 00) ; 13:24:50
;; (wct 09 35 10 09 35 20) ; 00:00:10

(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 "%yy %dd" seconds-diff)))
;;   (time-between-times 1958  4 13 1958 8 30) ; Tahiti Nui 2 -> 3,
;;                                             ; i.e. 0y 139d

(defun get-time-since (year month day)
  (interactive "nyear: \nnmonth: \nnday: ")
  (message "%s"
           (format-seconds
            "%yy %dd"
            (float-time
             (time-since (encode-time 0 0 0 day month year)) ))))
;; (get-time-since 2011 09 27) ; 8y 228d @ 2020-05-10

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]