emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[nongnu] elpa/subed 07567d51ff 2/2: subed-to-msecs: New function to conv


From: ELPA Syncer
Subject: [nongnu] elpa/subed 07567d51ff 2/2: subed-to-msecs: New function to convert timestamps
Date: Tue, 23 Jan 2024 10:01:27 -0500 (EST)

branch: elpa/subed
commit 07567d51ffc78d07528a6e69f2527f2765cbe3bc
Author: Sacha Chua <sacha@sachachua.com>
Commit: Sacha Chua <sacha@sachachua.com>

    subed-to-msecs: New function to convert timestamps
    
    * subed/subed-common.el (subed-to-msecs): New
    function to convert timestamp as string or number
    to msecs.
---
 subed/subed-common.el   | 12 ++++++++++--
 tests/test-subed-ass.el |  8 ++++++++
 tests/test-subed-srt.el |  9 ++++++++-
 tests/test-subed-tsv.el |  6 ++++++
 tests/test-subed-vtt.el |  9 ++++++++-
 5 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/subed/subed-common.el b/subed/subed-common.el
index 66e0053e9f..08bc6c804a 100644
--- a/subed/subed-common.el
+++ b/subed/subed-common.el
@@ -185,6 +185,13 @@ Return nil if TIME-STRING doesn't match the pattern.")
 (subed-define-generic-function msecs-to-timestamp (msecs)
   "Convert MSECS to string in the subtitle's timestamp format.")
 
+(defun subed-to-msecs (time-string)
+  "Convert TIME-STRING to milliseconds."
+  (or (and (stringp time-string) (subed-timestamp-to-msecs time-string))
+      (cond
+       ((numberp time-string) time-string)
+       ((string-match "^[0-9\\.]+$" time-string) (string-to-number 
time-string)))))
+
 (subed-define-generic-function subtitle-id ()
   "Return the ID of the subtitle at point or nil if there is no ID.")
 
@@ -1266,9 +1273,10 @@ To shift to a specific timestamp, use 
`subed-shift-subtitles-to-start-at-timesta
 
 (defun subed-shift-subtitles-to-start-at-timestamp (timestamp)
   "Move this and following subtitles so that the current one starts at 
TIMESTAMP.
-To shift by a millisecond offset, use `subed-shift-subtitles'."
+To shift by a millisecond offset, use `subed-shift-subtitles'.
+If TIMESTAMP is a number or a numeric string, treat it as the time in 
milliseconds."
   (interactive (list (read-string "New start: ")))
-  (subed-shift-subtitles (- (subed-timestamp-to-msecs timestamp) 
(subed-subtitle-msecs-start))))
+  (subed-shift-subtitles (- (subed-to-msecs timestamp) 
(subed-subtitle-msecs-start))))
 
 (defun subed-shift-subtitle-forward (&optional arg)
   "Shift subtitle `subed-milliseconds-adjust' forward.
diff --git a/tests/test-subed-ass.el b/tests/test-subed-ass.el
index 481023dd7f..b1b1242538 100644
--- a/tests/test-subed-ass.el
+++ b/tests/test-subed-ass.el
@@ -57,6 +57,14 @@ Dialogue: 0,0:00:17.00,0:00:19.80,Default,,0,0,0,,I hope it 
works.
          (insert mock-ass-data)
          (subed-jump-to-subtitle-text "0:00:14.00")
          (expect (subed-subtitle-text) :to-equal "This is a test.")))))
+  (describe "Converting to msecs"
+    (it "works with numbers."
+      (expect (with-temp-ass-buffer (subed-to-msecs 5123)) :to-equal 5123))
+    (it "works with numbers as strings."
+      (expect (with-temp-ass-buffer (subed-to-msecs "5123")) :to-equal 5123))
+    (it "works with timestamps."
+      (expect (with-temp-ass-buffer
+               (subed-to-msecs "00:00:05.12")) :to-equal 5120)))
   (describe "Jumping"
     (describe "to current subtitle timestamp"
       (it "can handle different formats of timestamps."
diff --git a/tests/test-subed-srt.el b/tests/test-subed-srt.el
index d01d05eb98..3c4277a167 100644
--- a/tests/test-subed-srt.el
+++ b/tests/test-subed-srt.el
@@ -196,7 +196,14 @@ Baz.
          (expect (subed-subtitle-relative-point) :to-equal nil)))
       )
     )
-
+  (describe "Converting to msecs"
+    (it "works with numbers."
+      (expect (with-temp-srt-buffer (subed-to-msecs 5123)) :to-equal 5123))
+    (it "works with numbers as strings."
+      (expect (with-temp-srt-buffer (subed-to-msecs "5123")) :to-equal 5123))
+    (it "works with timestamps."
+      (with-temp-srt-buffer
+       (expect (subed-to-msecs "00:00:05,124") :to-equal 5124))))
   (describe "Jumping"
     (describe "to current subtitle ID"
       (it "returns ID's point when point is already on the ID."
diff --git a/tests/test-subed-tsv.el b/tests/test-subed-tsv.el
index 546d39de20..50c99d28f5 100644
--- a/tests/test-subed-tsv.el
+++ b/tests/test-subed-tsv.el
@@ -44,6 +44,12 @@
          (insert mock-tsv-data)
          (subed-jump-to-subtitle-text "14.000000")
          (expect (subed-subtitle-text) :to-equal "This is a test.")))))
+  (describe "Converting to msecs"
+    (it "works with numbers, although these use seconds because that's what 
TSV uses."
+      (expect (with-temp-tsv-buffer
+               (floor (subed-to-msecs "5.123"))) :to-equal 5123))
+    (it "works with numbers."
+      (expect (subed-to-msecs 5123) :to-equal 5123)))
   (describe "Jumping"
     (describe "to current subtitle timestamp"
       (it "can handle different formats of timestamps."
diff --git a/tests/test-subed-vtt.el b/tests/test-subed-vtt.el
index a93cf347c9..c0b4196b9f 100644
--- a/tests/test-subed-vtt.el
+++ b/tests/test-subed-vtt.el
@@ -166,7 +166,14 @@ Baz.
          (expect (subed-subtitle-relative-point) :to-equal nil)))
       )
     )
-
+  (describe "Converting to msecs"
+    (it "works with numbers."
+      (expect (with-temp-vtt-buffer (subed-to-msecs 5123)) :to-equal 5123))
+    (it "works with numbers as strings."
+      (expect (with-temp-vtt-buffer (subed-to-msecs "5123")) :to-equal 5123))
+    (it "works with timestamps."
+      (expect (with-temp-vtt-buffer
+               (subed-to-msecs "00:00:05.124")) :to-equal 5124)))
   (describe "Jumping"
     (describe "to current subtitle timestamp"
       (it "returns timestamp's point when point is already on the timestamp."



reply via email to

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