From 68484efba39bd12d1e3c4121c648c2641dd534db Mon Sep 17 00:00:00 2001 From: Chandu Tennety Date: Thu, 18 May 2017 15:21:44 -0400 Subject: [PATCH] Allows timecode format hh:mm:ss to be valid (#24) --- lib/srt/parser.rb | 2 +- spec/parser_spec.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/srt/parser.rb b/lib/srt/parser.rb index 8357ccc..7fdbffe 100644 --- a/lib/srt/parser.rb +++ b/lib/srt/parser.rb @@ -12,7 +12,7 @@ def id(id_string) end def timecode(timecode_string) - mres = timecode_string.match(/(?\d+):(?\d+):(?\d+)[,.](?\d+)/) + mres = timecode_string.match(/(?\d+):(?\d+):(?\d+)[,.]?(?\d+)?/) mres ? "#{mres["h"].to_i * 3600 + mres["m"].to_i * 60 + mres["s"].to_i}.#{mres["ms"]}".to_f : nil end diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb index a033832..5b73e10 100644 --- a/spec/parser_spec.rb +++ b/spec/parser_spec.rb @@ -14,6 +14,11 @@ it "should convert the SRT timecode format to a float representing seconds" do expect(subject.timecode("01:03:44,200")).to eq(3824.2) end + + it "should handle timecodes with no milliseconds component" do + expect(subject.timecode("01:03:44")).not_to be_nil + expect(subject.timecode("01:03:44")).to eq(3824.0) + end end describe ".timespan" do