| | | 1 | | using NodaTime; |
| | | 2 | | using NodaTime.Text; |
| | | 3 | | |
| | | 4 | | namespace Orchestrator.Commands.Observability; |
| | | 5 | | |
| | | 6 | | internal static class EvaluationTimeParser |
| | | 7 | | { |
| | 1 | 8 | | private static readonly ZonedDateTimePattern EvaluationTimePattern = |
| | 1 | 9 | | ZonedDateTimePattern.GeneralFormatOnlyIso.WithZoneProvider(DateTimeZoneProviders.Tzdb); |
| | | 10 | | |
| | | 11 | | private const string ExampleValue = "2026-03-15T12:00:00 Europe/Berlin (+01)"; |
| | | 12 | | |
| | | 13 | | public static DateTimeOffset? ParseOrNull(string? value) |
| | | 14 | | { |
| | 1 | 15 | | if (string.IsNullOrWhiteSpace(value)) |
| | | 16 | | { |
| | 1 | 17 | | return null; |
| | | 18 | | } |
| | | 19 | | |
| | 1 | 20 | | return Parse(value); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | public static DateTimeOffset Parse(string value) |
| | | 24 | | { |
| | 1 | 25 | | ArgumentException.ThrowIfNullOrWhiteSpace(value); |
| | | 26 | | |
| | 1 | 27 | | value = Normalize(value); |
| | | 28 | | |
| | | 29 | | try |
| | | 30 | | { |
| | 1 | 31 | | return EvaluationTimePattern.Parse(value).GetValueOrThrow().ToDateTimeOffset(); |
| | | 32 | | } |
| | 1 | 33 | | catch (UnparsableValueException ex) |
| | | 34 | | { |
| | 1 | 35 | | throw new ArgumentException( |
| | 1 | 36 | | $"Evaluation time must use NodaTime's invariant ZonedDateTime 'G' pattern, for example '{ExampleValue}'. |
| | 1 | 37 | | ex); |
| | | 38 | | } |
| | 1 | 39 | | } |
| | | 40 | | |
| | | 41 | | private static string Normalize(string value) |
| | | 42 | | { |
| | 1 | 43 | | value = value.Trim(); |
| | 1 | 44 | | if (value.Length >= 2 && value[0] == '"' && value[^1] == '"') |
| | | 45 | | { |
| | 1 | 46 | | value = value[1..^1]; |
| | | 47 | | } |
| | | 48 | | |
| | 1 | 49 | | return value; |
| | | 50 | | } |
| | | 51 | | } |