Skip to content

Commit e246279

Browse files
committed
Refactors time unit parsing and wildcard tests
Improves time unit parsing by using a more concise loop and clarifies variable types. Refactors wildcard tests for better readability and consistency by using `string[]` for test inputs and `int` for index variables.
1 parent 3555cfe commit e246279

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/Exceptionless.DateTimeExtensions/TimeUnit.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,31 @@ public static bool TryParse(string value, out TimeSpan? time)
2929
private static TimeSpan? ParseTime(string value)
3030
{
3131
// bail if we have any weird characters
32-
for (int i = 0; i < value.Length; i++)
33-
if (!Char.IsLetterOrDigit(value[i]) && value[i] != '-' && value[i] != '+' && !Char.IsWhiteSpace(value[i]))
32+
foreach (char c in value)
33+
if (!Char.IsLetterOrDigit(c) && c != '-' && c != '+' && !Char.IsWhiteSpace(c))
3434
return null;
3535

3636
// compare using the original value as uppercase M could mean months.
37-
var normalized = value.Trim();
38-
if (value.EndsWith("m") && Int32.TryParse(normalized.Substring(0, normalized.Length - 1), out var minutes))
37+
string normalized = value.Trim();
38+
if (value.EndsWith("m") && Int32.TryParse(normalized.Substring(0, normalized.Length - 1), out int minutes))
3939
return new TimeSpan(0, minutes, 0);
4040

41-
if (normalized.EndsWith("h") && Int32.TryParse(normalized.Substring(0, normalized.Length - 1), out var hours))
41+
if (normalized.EndsWith("h") && Int32.TryParse(normalized.Substring(0, normalized.Length - 1), out int hours))
4242
return new TimeSpan(hours, 0, 0);
4343

44-
if (normalized.EndsWith("d") && Int32.TryParse(normalized.Substring(0, normalized.Length - 1), out var days))
44+
if (normalized.EndsWith("d") && Int32.TryParse(normalized.Substring(0, normalized.Length - 1), out int days))
4545
return new TimeSpan(days, 0, 0, 0);
4646

47-
if (normalized.EndsWith("nanos", StringComparison.OrdinalIgnoreCase) && Int32.TryParse(normalized.Substring(0, normalized.Length - 5), out var nanoseconds))
47+
if (normalized.EndsWith("nanos", StringComparison.OrdinalIgnoreCase) && Int32.TryParse(normalized.Substring(0, normalized.Length - 5), out int nanoseconds))
4848
return new TimeSpan((int)Math.Round(nanoseconds / 100d));
4949

50-
if (normalized.EndsWith("micros", StringComparison.OrdinalIgnoreCase) && Int32.TryParse(normalized.Substring(0, normalized.Length - 6), out var microseconds))
50+
if (normalized.EndsWith("micros", StringComparison.OrdinalIgnoreCase) && Int32.TryParse(normalized.Substring(0, normalized.Length - 6), out int microseconds))
5151
return new TimeSpan(microseconds * 10);
5252

53-
if (normalized.EndsWith("ms") && Int32.TryParse(normalized.Substring(0, normalized.Length - 2), out var milliseconds))
53+
if (normalized.EndsWith("ms") && Int32.TryParse(normalized.Substring(0, normalized.Length - 2), out int milliseconds))
5454
return new TimeSpan(0, 0, 0, 0, milliseconds);
5555

56-
if (normalized.EndsWith("s") && Int32.TryParse(normalized.Substring(0, normalized.Length - 1), out var seconds))
56+
if (normalized.EndsWith("s") && Int32.TryParse(normalized.Substring(0, normalized.Length - 1), out int seconds))
5757
return new TimeSpan(0, 0, seconds);
5858

5959
return null;

tests/Exceptionless.DateTimeExtensions.Tests/FormatParsers/PartParsers/WildcardPartParserTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public void RegexPatternTest()
7171
_logger.LogInformation("Regex pattern: {Pattern}", regex);
7272

7373
// Test various inputs
74-
var testInputs = new[] { "*", " * ", " * ", "blah", "2012", "**", "* *", "" };
74+
string[] testInputs = new[] { "*", " * ", " * ", "blah", "2012", "**", "* *", "" };
7575

76-
foreach (var input in testInputs)
76+
foreach (string input in testInputs)
7777
{
7878
var match = regex.Match(input);
7979
_logger.LogInformation("Input: '{Input}' -> Success: {Success}, Value: '{Value}', Index: {Index}, Length: {Length}", input, match.Success, match.Value, match.Index, match.Length);
@@ -86,9 +86,9 @@ public void TestInTwoPartContext()
8686
var parser = new WildcardPartParser();
8787

8888
// Test how it behaves in a two-part parsing context
89-
var inputs = new[] { "* TO 2013", "2012 TO *", "[* TO 2013]", "{2012 TO *}" };
89+
string[] inputs = new[] { "* TO 2013", "2012 TO *", "[* TO 2013]", "{2012 TO *}" };
9090

91-
foreach (var input in inputs)
91+
foreach (string input in inputs)
9292
{
9393
_logger.LogInformation("Testing two-part context for: '{Input}'", input);
9494

@@ -104,10 +104,10 @@ public void TestInTwoPartContext()
104104
}
105105

106106
// Find TO and test parsing after it
107-
var toIndex = input.IndexOf(" TO ", StringComparison.OrdinalIgnoreCase);
107+
int toIndex = input.IndexOf(" TO ", StringComparison.OrdinalIgnoreCase);
108108
if (toIndex >= 0)
109109
{
110-
var afterTo = toIndex + 4;
110+
int afterTo = toIndex + 4;
111111
match = parser.Regex.Match(input, afterTo);
112112
_logger.LogInformation(" After TO at position {Position}: Success: {Success}, Value: '{Value}', Index: {Index}, Length: {Length}", afterTo, match.Success, match.Value, match.Index, match.Length);
113113
}

0 commit comments

Comments
 (0)