From af54326102d8e3e8a41c9a76671ab7b2c793bf84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lumi=C3=A8re=20=C3=89lev=C3=A9?= <88174309+PoneyClairDeLune@users.noreply.github.com> Date: Tue, 20 Aug 2024 09:32:36 +0000 Subject: [PATCH] Normalizes edge cases for parsing milliseconds. --- src/libse/Common/TimeCode.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/libse/Common/TimeCode.cs b/src/libse/Common/TimeCode.cs index 11cfc4ac7..de50c43fa 100644 --- a/src/libse/Common/TimeCode.cs +++ b/src/libse/Common/TimeCode.cs @@ -24,6 +24,19 @@ namespace Nikse.SubtitleEdit.Core.Common { if (int.TryParse(parts[0], out var hours) && int.TryParse(parts[1], out var minutes) && int.TryParse(parts[2], out var seconds) && int.TryParse(parts[3], out var milliseconds)) { + // Fixes edge cases where the millisecond portion doesn't have three digits + if (parts[3].Length < 3) + { + for (int msLength = parts[3].Length; msLength < 3; msLength ++) + { + milliseconds *= 10; + } + } else { + for (int msLength = parts[3].Length; msLength > 3; msLength --) + { + milliseconds /= 10; + } + } return new TimeSpan(0, hours, minutes, seconds, milliseconds).TotalMilliseconds; } } @@ -32,6 +45,19 @@ namespace Nikse.SubtitleEdit.Core.Common { if (int.TryParse(parts[0], out var minutes) && int.TryParse(parts[1], out var seconds) && int.TryParse(parts[2], out var milliseconds)) { + // Fixes edge cases where the millisecond portion doesn't have three digits + if (parts[3].Length < 3) + { + for (int msLength = parts[3].Length; msLength < 3; msLength ++) + { + milliseconds *= 10; + } + } else { + for (int msLength = parts[3].Length; msLength > 3; msLength --) + { + milliseconds /= 10; + } + } return new TimeSpan(0, 0, minutes, seconds, milliseconds).TotalMilliseconds; } }