Skip to content

Commit dc02960

Browse files
committed
Refactor method
1 parent 4e312da commit dc02960

File tree

1 file changed

+10
-43
lines changed

1 file changed

+10
-43
lines changed

src/Exceptionless.DateTimeExtensions/DateMath.cs

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -225,54 +225,20 @@ public static bool IsValidExpression(string expression)
225225
return TryParseFallbackDate(expression, TimeZoneInfo.Local, false, out _);
226226
}
227227

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>
236228
private static bool TryParseFallbackDate(string expression, TimeZoneInfo defaultTimeZone, bool isUpperLimit, out DateTimeOffset result)
237229
{
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);
264231
}
265232

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>
274233
private static bool TryParseFallbackDate(string expression, TimeSpan offset, bool isUpperLimit, out DateTimeOffset result)
275234
{
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+
276242
if (_offsetRegex.IsMatch(expression) && DateTimeOffset.TryParse(expression, out DateTimeOffset explicitDate))
277243
{
278244
result = explicitDate;
@@ -288,13 +254,14 @@ private static bool TryParseFallbackDate(string expression, TimeSpan offset, boo
288254

289255
if (DateTime.TryParse(expression, out DateTime dt))
290256
{
291-
result = new DateTimeOffset(dt, offset);
257+
result = new DateTimeOffset(dt, offsetResolver(dt));
292258

293259
if (result.TimeOfDay == TimeSpan.Zero && isUpperLimit)
294260
{
295261
// If time is exactly midnight, and it's an upper limit, set to end of day
296262
result = result.EndOfDay();
297263
}
264+
298265
return true;
299266
}
300267

0 commit comments

Comments
 (0)