Loading src/VideoSlice.Cli/SimpleTimeSpanExpress.cs +42 −12 Original line number Diff line number Diff line namespace VideoSlice.Cli; using System.Text; namespace VideoSlice.Cli; public static class SimpleTimeSpanExpress { Loading @@ -9,16 +11,25 @@ public static class SimpleTimeSpanExpress /// <returns>时间表达式</returns> public static string ToString(TimeSpan timeSpan) { var result = string.Empty; var len = GetStringLengthFast(timeSpan.Days) + 1 + GetStringLengthFast(timeSpan.Hours) + 1 + GetStringLengthFast(timeSpan.Minutes) + 1 + GetStringLengthFast(timeSpan.Seconds) + 1 + GetStringLengthFast(timeSpan.Milliseconds) + 2 + GetStringLengthFast(timeSpan.Microseconds) + 2 + 4; var sb = new StringBuilder(len); result += Append(timeSpan.Days, "d"); result += Append(timeSpan.Hours, "h"); result += Append(timeSpan.Minutes, "m"); result += Append(timeSpan.Seconds, "s"); result += Append(timeSpan.Milliseconds, "ms"); result += Append(timeSpan.Microseconds, "us"); Append(timeSpan.Days, "d", sb); Append(timeSpan.Hours, "h", sb); Append(timeSpan.Minutes, "m", sb); Append(timeSpan.Seconds, "s", sb); Append(timeSpan.Milliseconds, "ms", sb); Append(timeSpan.Microseconds, "us", sb); return result; return sb.ToString(); } /// <summary> Loading @@ -40,9 +51,9 @@ public static class SimpleTimeSpanExpress return timespan; } private static string Append(int value, string unit) private static void Append(int value, string unit, StringBuilder sb) { return value > 0 ? $"{value}{unit}" : string.Empty; sb.Append($"{value}{unit}"); } public static int Parse(string s, string unit) Loading Loading @@ -80,4 +91,23 @@ public static class SimpleTimeSpanExpress return new string(arr, arr.Length - count, count); } private static int GetStringLengthFast(int value) { long v = value < 0 ? -(long)value : value; return v switch { < 10 => value < 0 ? 2 : 1, < 100 => value < 0 ? 3 : 2, < 1000 => value < 0 ? 4 : 3, < 10000 => value < 0 ? 5 : 4, < 100000 => value < 0 ? 6 : 5, < 1000000 => value < 0 ? 7 : 6, < 10000000 => value < 0 ? 8 : 7, < 100000000 => value < 0 ? 9 : 8, < 1000000000 => value < 0 ? 10 : 9, _ => value < 0 ? 11 : 10 }; } } No newline at end of file src/VideoSlice.Tests/SimpleTimeSpanExpressTest.cs +14 −0 Original line number Diff line number Diff line Loading @@ -23,4 +23,18 @@ public class SimpleTimeSpanExpressTest var time = "1d2h3m4s5ms6us"; Assert.Equal(new TimeSpan(1, 2, 3, 4, 5, 6), SimpleTimeSpanExpress.ToTimeSpan(time)); } [Fact] public void Should_ToString_Not_Full() { var time = new TimeSpan(1, 2, 0, 4, 0, 0); Assert.Equal("1d2h0m4s", SimpleTimeSpanExpress.ToString(time)); } [Fact] public void Should_ToTimeSpan_Not_Full() { var time = "1d2h0m4s"; Assert.Equal(new TimeSpan(1, 2, 0, 4, 0, 0), SimpleTimeSpanExpress.ToTimeSpan(time)); } } No newline at end of file Loading
src/VideoSlice.Cli/SimpleTimeSpanExpress.cs +42 −12 Original line number Diff line number Diff line namespace VideoSlice.Cli; using System.Text; namespace VideoSlice.Cli; public static class SimpleTimeSpanExpress { Loading @@ -9,16 +11,25 @@ public static class SimpleTimeSpanExpress /// <returns>时间表达式</returns> public static string ToString(TimeSpan timeSpan) { var result = string.Empty; var len = GetStringLengthFast(timeSpan.Days) + 1 + GetStringLengthFast(timeSpan.Hours) + 1 + GetStringLengthFast(timeSpan.Minutes) + 1 + GetStringLengthFast(timeSpan.Seconds) + 1 + GetStringLengthFast(timeSpan.Milliseconds) + 2 + GetStringLengthFast(timeSpan.Microseconds) + 2 + 4; var sb = new StringBuilder(len); result += Append(timeSpan.Days, "d"); result += Append(timeSpan.Hours, "h"); result += Append(timeSpan.Minutes, "m"); result += Append(timeSpan.Seconds, "s"); result += Append(timeSpan.Milliseconds, "ms"); result += Append(timeSpan.Microseconds, "us"); Append(timeSpan.Days, "d", sb); Append(timeSpan.Hours, "h", sb); Append(timeSpan.Minutes, "m", sb); Append(timeSpan.Seconds, "s", sb); Append(timeSpan.Milliseconds, "ms", sb); Append(timeSpan.Microseconds, "us", sb); return result; return sb.ToString(); } /// <summary> Loading @@ -40,9 +51,9 @@ public static class SimpleTimeSpanExpress return timespan; } private static string Append(int value, string unit) private static void Append(int value, string unit, StringBuilder sb) { return value > 0 ? $"{value}{unit}" : string.Empty; sb.Append($"{value}{unit}"); } public static int Parse(string s, string unit) Loading Loading @@ -80,4 +91,23 @@ public static class SimpleTimeSpanExpress return new string(arr, arr.Length - count, count); } private static int GetStringLengthFast(int value) { long v = value < 0 ? -(long)value : value; return v switch { < 10 => value < 0 ? 2 : 1, < 100 => value < 0 ? 3 : 2, < 1000 => value < 0 ? 4 : 3, < 10000 => value < 0 ? 5 : 4, < 100000 => value < 0 ? 6 : 5, < 1000000 => value < 0 ? 7 : 6, < 10000000 => value < 0 ? 8 : 7, < 100000000 => value < 0 ? 9 : 8, < 1000000000 => value < 0 ? 10 : 9, _ => value < 0 ? 11 : 10 }; } } No newline at end of file
src/VideoSlice.Tests/SimpleTimeSpanExpressTest.cs +14 −0 Original line number Diff line number Diff line Loading @@ -23,4 +23,18 @@ public class SimpleTimeSpanExpressTest var time = "1d2h3m4s5ms6us"; Assert.Equal(new TimeSpan(1, 2, 3, 4, 5, 6), SimpleTimeSpanExpress.ToTimeSpan(time)); } [Fact] public void Should_ToString_Not_Full() { var time = new TimeSpan(1, 2, 0, 4, 0, 0); Assert.Equal("1d2h0m4s", SimpleTimeSpanExpress.ToString(time)); } [Fact] public void Should_ToTimeSpan_Not_Full() { var time = "1d2h0m4s"; Assert.Equal(new TimeSpan(1, 2, 0, 4, 0, 0), SimpleTimeSpanExpress.ToTimeSpan(time)); } } No newline at end of file