3

I have a time-stamp of a video formatted as "00:00:00,000" such that "%H:%M:%s,%3N".

I am want to eventually use this timestamp as part of an in-interval-p function where I take two timestamps and a current timestamp and check if I'm currently between the two. The best idea I have is to convert these timestamps to cumulative seconds.

I'm not sure if this is the best way to do it, but here is what I currently have

(require 'dash)
(require 's)

(defvar time-stamp "00:00:52,010")

(s-split "[:|,]" time-stamp)
;; ("00" "00" "52" "010")

What I would like to do, but not sure how, is something like

(map '((lambda (x) (* x 3600.0))
       (lambda (x) (* x 60.0))
       (lambda (x) (x))
       (lambda (x) (/ (float x) 1000.0))) 
  (s-split "[:|,]" time-stamp))

;; (0.0 0.0 52 0.01)

Is it possible to apply a different function at each index of a list? I would then sum the list to get 52.01 seconds.

2 Answers 2

2

There is a function in the "dash" library you are using.

(-zip-with #'funcall
           '((lambda (x) (* x 3600.0))
             (lambda (x) (* x 60.0))
             (lambda (x) x)
             (lambda (x) (/ (float x) 1000.0)))
           (mapcar #'string-to-number (s-split "[:|,]" time-stamp)))

Note that s-split returns strings but you want to work with numbers, so you can use string-to-number for that.

2
  • Note: your 4 quoted lambdas can't be compiled (and can't use lexical scoping). Better construct your list with list or with ..`(..,(lambda ..) ,(lambda ..) ..)..
    – Stefan
    Commented Feb 1, 2020 at 16:35
  • You can also use cl-mapcar from cl-lib instead of -zip-with.
    – Stefan
    Commented Feb 1, 2020 at 16:39
4

I think you've overcomplicated the problem.

You're obtaining a list comprising hours, minutes, seconds, and microseconds, so I think your code would be simpler and more readable if you expressed that explicitly:

(require 'cl-lib)

(defvar time-stamp "00:00:52,010")

(cl-destructuring-bind (hours minutes seconds microseconds)
    (mapcar #'string-to-number (split-string time-stamp "[:,]"))
  (+ (* hours 3600)
     (* minutes 60)
     seconds
     (/ (float microseconds) 1000.0)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.