@@ -225,54 +225,20 @@ public static bool IsValidExpression(string expression)
225
225
return TryParseFallbackDate ( expression , TimeZoneInfo . Local , false , out _ ) ;
226
226
}
227
227
228
- /// <summary>
229
- /// Attempts to parse the expression as an explicit date when date math parsing fails, using the provided timezone for missing offsets.
230
- /// </summary>
231
- /// <param name="expression">The original expression to interpret as an explicit date.</param>
232
- /// <param name="defaultTimeZone">The timezone applied when the expression lacks explicit offset information.</param>
233
- /// <param name="isUpperLimit">Whether the value should be treated as an upper bound, rounding end-of-day when applicable.</param>
234
- /// <param name="result">Receives the parsed <see cref="DateTimeOffset"/> when parsing succeeds.</param>
235
- /// <returns><see langword="true"/> when the expression is successfully parsed as an explicit date; otherwise, <see langword="false"/>.</returns>
236
228
private static bool TryParseFallbackDate ( string expression , TimeZoneInfo defaultTimeZone , bool isUpperLimit , out DateTimeOffset result )
237
229
{
238
- if ( _offsetRegex . IsMatch ( expression ) && DateTimeOffset . TryParse ( expression , out DateTimeOffset explicitDate ) )
239
- {
240
- result = explicitDate ;
241
-
242
- if ( result . TimeOfDay == TimeSpan . Zero && isUpperLimit )
243
- {
244
- // If time is exactly midnight, and it's an upper limit, set to end of day
245
- result = result . EndOfDay ( ) ;
246
- }
247
-
248
- return true ;
249
- }
250
-
251
- if ( DateTime . TryParse ( expression , out DateTime dt ) )
252
- {
253
- result = new DateTimeOffset ( dt , defaultTimeZone . GetUtcOffset ( dt ) ) ;
254
-
255
- if ( result . TimeOfDay == TimeSpan . Zero && isUpperLimit )
256
- {
257
- // If time is exactly midnight, and it's an upper limit, set to end of day
258
- result = result . EndOfDay ( ) ;
259
- }
260
- return true ;
261
- }
262
-
263
- return false ;
230
+ return TryParseFallbackDateCore ( expression , isUpperLimit , out result , defaultTimeZone . GetUtcOffset ) ;
264
231
}
265
232
266
- /// <summary>
267
- /// Attempts to parse the expression as an explicit date when date math parsing fails, using the provided offset for missing timezone information.
268
- /// </summary>
269
- /// <param name="expression">The original expression to interpret as an explicit date.</param>
270
- /// <param name="offset">The fallback UTC offset applied when the expression omits timezone data.</param>
271
- /// <param name="isUpperLimit">Whether the value should be treated as an upper bound, rounding to the end of day when appropriate.</param>
272
- /// <param name="result">Receives the parsed <see cref="DateTimeOffset"/> when parsing succeeds.</param>
273
- /// <returns><see langword="true"/> when the expression is successfully parsed as an explicit date; otherwise, <see langword="false"/>.</returns>
274
233
private static bool TryParseFallbackDate ( string expression , TimeSpan offset , bool isUpperLimit , out DateTimeOffset result )
275
234
{
235
+ return TryParseFallbackDateCore ( expression , isUpperLimit , out result , _ => offset ) ;
236
+ }
237
+
238
+ private static bool TryParseFallbackDateCore ( string expression , bool isUpperLimit , out DateTimeOffset result , Func < DateTime , TimeSpan > offsetResolver )
239
+ {
240
+ result = default ;
241
+
276
242
if ( _offsetRegex . IsMatch ( expression ) && DateTimeOffset . TryParse ( expression , out DateTimeOffset explicitDate ) )
277
243
{
278
244
result = explicitDate ;
@@ -288,13 +254,14 @@ private static bool TryParseFallbackDate(string expression, TimeSpan offset, boo
288
254
289
255
if ( DateTime . TryParse ( expression , out DateTime dt ) )
290
256
{
291
- result = new DateTimeOffset ( dt , offset ) ;
257
+ result = new DateTimeOffset ( dt , offsetResolver ( dt ) ) ;
292
258
293
259
if ( result . TimeOfDay == TimeSpan . Zero && isUpperLimit )
294
260
{
295
261
// If time is exactly midnight, and it's an upper limit, set to end of day
296
262
result = result . EndOfDay ( ) ;
297
263
}
264
+
298
265
return true ;
299
266
}
300
267
0 commit comments