@@ -31,6 +31,9 @@ public static class DateMath
31
31
// Pre-compiled regex for operation parsing to avoid repeated compilation
32
32
private static readonly Regex _operationRegex = new ( @"([+\-/])(\d*)([yMwdhHms])" , RegexOptions . Compiled ) ;
33
33
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
+
34
37
/// <summary>
35
38
/// Parses a date math expression and returns the resulting DateTimeOffset.
36
39
/// </summary>
@@ -232,7 +235,7 @@ public static bool IsValidExpression(string expression)
232
235
/// <returns><see langword="true"/> when the expression is successfully parsed as an explicit date; otherwise, <see langword="false"/>.</returns>
233
236
private static bool TryParseFallbackDate ( string expression , TimeZoneInfo defaultTimeZone , bool isUpperLimit , out DateTimeOffset result )
234
237
{
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 ) )
236
239
{
237
240
result = explicitDate ;
238
241
@@ -270,7 +273,7 @@ private static bool TryParseFallbackDate(string expression, TimeZoneInfo default
270
273
/// <returns><see langword="true"/> when the expression is successfully parsed as an explicit date; otherwise, <see langword="false"/>.</returns>
271
274
private static bool TryParseFallbackDate ( string expression , TimeSpan offset , bool isUpperLimit , out DateTimeOffset result )
272
275
{
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 ) )
274
277
{
275
278
result = explicitDate ;
276
279
0 commit comments