Skip to content

Commit 4e312da

Browse files
committed
Use shared regex
1 parent ee0c328 commit 4e312da

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/Exceptionless.DateTimeExtensions/DateMath.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public static class DateMath
3131
// Pre-compiled regex for operation parsing to avoid repeated compilation
3232
private static readonly Regex _operationRegex = new(@"([+\-/])(\d*)([yMwdhHms])", RegexOptions.Compiled);
3333

34+
// Pre-compiled regex for offset parsing to avoid repeated compilation
35+
private static readonly Regex _offsetRegex = new(@"(Z|[+-]\d{2}:\d{2})$", RegexOptions.Compiled);
36+
3437
/// <summary>
3538
/// Parses a date math expression and returns the resulting DateTimeOffset.
3639
/// </summary>
@@ -232,7 +235,7 @@ public static bool IsValidExpression(string expression)
232235
/// <returns><see langword="true"/> when the expression is successfully parsed as an explicit date; otherwise, <see langword="false"/>.</returns>
233236
private static bool TryParseFallbackDate(string expression, TimeZoneInfo defaultTimeZone, bool isUpperLimit, out DateTimeOffset result)
234237
{
235-
if (Regex.IsMatch(expression, @"(Z|[+-]\d{2}:\d{2})$") && DateTimeOffset.TryParse(expression, out DateTimeOffset explicitDate))
238+
if (_offsetRegex.IsMatch(expression) && DateTimeOffset.TryParse(expression, out DateTimeOffset explicitDate))
236239
{
237240
result = explicitDate;
238241

@@ -270,7 +273,7 @@ private static bool TryParseFallbackDate(string expression, TimeZoneInfo default
270273
/// <returns><see langword="true"/> when the expression is successfully parsed as an explicit date; otherwise, <see langword="false"/>.</returns>
271274
private static bool TryParseFallbackDate(string expression, TimeSpan offset, bool isUpperLimit, out DateTimeOffset result)
272275
{
273-
if (Regex.IsMatch(expression, @"(Z|[+-]\d{2}:\d{2})$") && DateTimeOffset.TryParse(expression, out DateTimeOffset explicitDate))
276+
if (_offsetRegex.IsMatch(expression) && DateTimeOffset.TryParse(expression, out DateTimeOffset explicitDate))
274277
{
275278
result = explicitDate;
276279

0 commit comments

Comments
 (0)