Skip to content

Commit 5eddd17

Browse files
committed
Introduces DateMath utility for date math parsing
Adds a `DateMath` utility class for parsing Elasticsearch date math expressions, offering standalone date math functionality without range capabilities. This utility supports parsing expressions with `now`, explicit dates, operations, and time units, providing a simpler API for direct parsing operations. Includes comprehensive unit tests for various parsing scenarios, edge cases, and timezone handling.
1 parent db06e16 commit 5eddd17

File tree

4 files changed

+956
-281
lines changed

4 files changed

+956
-281
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,37 @@ Examples:
7070
- `2025-01-01T01:25:35Z||+3d/d` - January 4th, 2025 (start of day) in UTC
7171
- `2023-06-15T14:30:00+05:00||+1M-2d` - One month minus 2 days from the specified date/time in +05:00 timezone
7272

73+
### DateMath Utility
74+
75+
For applications that need standalone date math parsing without the range functionality, the `DateMath` utility class provides direct access to Elasticsearch date math expression parsing. Check out our [unit tests](https://github.com/exceptionless/Exceptionless.DateTimeExtensions/blob/main/tests/Exceptionless.DateTimeExtensions.Tests/DateMathTests.cs) for more usage samples.
76+
77+
```csharp
78+
using Exceptionless.DateTimeExtensions;
79+
80+
// Parse date math expressions with standard .NET conventions
81+
var baseTime = DateTimeOffset.Now;
82+
83+
// Parse method - throws ArgumentException on invalid input
84+
var result = DateMath.Parse("now+1h", baseTime);
85+
var rounded = DateMath.Parse("now-1d/d", baseTime, isUpperLimit: false); // Start of yesterday
86+
87+
// TryParse method - returns bool for success/failure
88+
if (DateMath.TryParse("2023.06.15||+1M/d", baseTime, false, out var parsed)) {
89+
// Successfully parsed: June 15, 2023 + 1 month, rounded to start of day
90+
Console.WriteLine($"Parsed: {parsed:O}");
91+
}
92+
93+
// Upper limit behavior affects rounding
94+
var startOfDay = DateMath.Parse("now/d", baseTime, isUpperLimit: false); // 00:00:00
95+
var endOfDay = DateMath.Parse("now/d", baseTime, isUpperLimit: true); // 23:59:59.999
96+
97+
// Explicit dates with timezone preservation
98+
var utcResult = DateMath.Parse("2025-01-01T01:25:35Z||+3d/d", baseTime);
99+
var offsetResult = DateMath.Parse("2023-06-15T14:30:00+05:00||+1M", baseTime);
100+
```
101+
102+
The `DateMath` utility supports the same comprehensive syntax as `DateTimeRange` but provides a simpler API for direct parsing operations.
103+
73104
### TimeUnit
74105

75106
Quickly work with time units. . Check out our [unit tests](https://github.com/exceptionless/Exceptionless.DateTimeExtensions/blob/main/tests/Exceptionless.DateTimeExtensions.Tests/TimeUnitTests.cs) for more usage samples.

0 commit comments

Comments
 (0)