-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace CLI seconds with FFmpeg-style time duration parsing (#1032)
* Create Time struct for parsing various time strings This enables the use of "12", "12s", "12000ms", "0:00:12", "0.2m" to get a value of 12 seconds * Create tests * Implement Time in CLI * I hate working with CommandLineParser * Update documentation * Use decimal instead of double to avoid rounding errors * Rename Time -> TimeDuration * Add more tests
- Loading branch information
Showing
14 changed files
with
241 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using TwitchDownloaderCLI.Models; | ||
using static System.TimeSpan; | ||
|
||
namespace TwitchDownloaderCLI.Tests.ModelTests | ||
{ | ||
public class TimeDurationTests | ||
{ | ||
[Theory] | ||
[InlineData("200ms", 200 * TicksPerMillisecond)] | ||
[InlineData("55", 55 * TicksPerSecond)] | ||
[InlineData("0.2", 2 * TicksPerSecond / 10)] | ||
[InlineData("23.189", 23189 * TicksPerSecond / 1000)] | ||
[InlineData("55s", 55 * TicksPerSecond)] | ||
[InlineData("17m", 17 * TicksPerMinute)] | ||
[InlineData("31h", 31 * TicksPerHour)] | ||
[InlineData("0:09:27", 9 * TicksPerMinute + 27 * TicksPerSecond)] | ||
[InlineData("11:30", 11 * TicksPerHour + 30 * TicksPerMinute)] | ||
[InlineData("12:03:45", 12 * TicksPerHour + 3 * TicksPerMinute + 45 * TicksPerSecond)] | ||
public void CorrectlyParsesTimeStrings(string input, long expectedTicks) | ||
{ | ||
var expected = new TimeDuration(expectedTicks); | ||
|
||
var actual = new TimeDuration(input); | ||
|
||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
[InlineData("100 s")] | ||
[InlineData("123d")] | ||
[InlineData("0:12345")] | ||
public void ThrowsOnBadFormat(string input) | ||
{ | ||
Assert.ThrowsAny<Exception>(() => | ||
{ | ||
_ = TimeDuration.Parse(input); | ||
}); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
TwitchDownloaderCLI.Tests/TwitchDownloaderCLI.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"/> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/> | ||
<PackageReference Include="xunit" Version="2.5.3"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\TwitchDownloaderCLI\TwitchDownloaderCLI.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace TwitchDownloaderCLI.Models | ||
{ | ||
[DebuggerDisplay("{_timeSpan}")] | ||
public readonly record struct TimeDuration | ||
{ | ||
private readonly TimeSpan _timeSpan; | ||
|
||
/// <summary> | ||
/// Constructor used by CommandLineParser | ||
/// </summary> | ||
public TimeDuration(string str) | ||
{ | ||
this = Parse(str); | ||
} | ||
|
||
public TimeDuration(TimeSpan timeSpan) | ||
{ | ||
_timeSpan = timeSpan; | ||
} | ||
|
||
public TimeDuration(long ticks) | ||
{ | ||
_timeSpan = TimeSpan.FromTicks(ticks); | ||
} | ||
|
||
public static TimeDuration Parse(string str) | ||
{ | ||
if (string.IsNullOrWhiteSpace(str)) | ||
{ | ||
throw new FormatException(); | ||
} | ||
|
||
if (str.Contains(':')) | ||
{ | ||
var timeSpan = TimeSpan.Parse(str); | ||
return new TimeDuration(timeSpan); | ||
} | ||
|
||
var multiplier = GetMultiplier(str, out var span); | ||
if (decimal.TryParse(span, NumberStyles.Number, null, out var result)) | ||
{ | ||
var ticks = (long)(result * multiplier); | ||
return new TimeDuration(ticks); | ||
} | ||
|
||
throw new FormatException(); | ||
} | ||
|
||
private static long GetMultiplier(string input, out ReadOnlySpan<char> trimmedInput) | ||
{ | ||
if (char.IsDigit(input[^1])) | ||
{ | ||
trimmedInput = input.AsSpan(); | ||
return TimeSpan.TicksPerSecond; | ||
} | ||
|
||
if (Regex.IsMatch(input, @"\dms$", RegexOptions.RightToLeft)) | ||
{ | ||
trimmedInput = input.AsSpan()[..^2]; | ||
return TimeSpan.TicksPerMillisecond; | ||
} | ||
|
||
if (Regex.IsMatch(input, @"\ds$", RegexOptions.RightToLeft)) | ||
{ | ||
trimmedInput = input.AsSpan()[..^1]; | ||
return TimeSpan.TicksPerSecond; | ||
} | ||
|
||
if (Regex.IsMatch(input, @"\dm$", RegexOptions.RightToLeft)) | ||
{ | ||
trimmedInput = input.AsSpan()[..^1]; | ||
return TimeSpan.TicksPerMinute; | ||
} | ||
|
||
if (Regex.IsMatch(input, @"\dh$", RegexOptions.RightToLeft)) | ||
{ | ||
trimmedInput = input.AsSpan()[..^1]; | ||
return TimeSpan.TicksPerHour; | ||
} | ||
|
||
throw new FormatException(); | ||
} | ||
|
||
public static implicit operator TimeSpan(TimeDuration timeDuration) => timeDuration._timeSpan; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.