|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Globalization; |
| 8 | +using System.Text; |
| 9 | +using System.Text.Json; |
| 10 | +using System.Text.Json.Serialization; |
| 11 | +using System.Text.RegularExpressions; |
| 12 | + |
| 13 | +namespace Elastic.Clients.Elasticsearch; |
| 14 | + |
| 15 | +[JsonConverter(typeof(DateMathConverter))] |
| 16 | +public abstract class DateMath |
| 17 | +{ |
| 18 | + private static readonly Regex DateMathRegex = |
| 19 | + new(@"^(?<anchor>now|.+(?:\|\||$))(?<ranges>(?:(?:\+|\-)[^\/]*))?(?<rounding>\/(?:y|M|w|d|h|m|s))?$"); |
| 20 | + |
| 21 | + public static DateMathExpression Now => new("now"); |
| 22 | + |
| 23 | + internal DateMath(string anchor) => Anchor = anchor; |
| 24 | + internal DateMath(DateTime anchor) => Anchor = anchor; |
| 25 | + internal DateMath(Union<DateTime, string> anchor, DateMathTime range, DateMathOperation operation) |
| 26 | + { |
| 27 | + anchor.ThrowIfNull(nameof(anchor)); |
| 28 | + range.ThrowIfNull(nameof(range)); |
| 29 | + operation.ThrowIfNull(nameof(operation)); |
| 30 | + Anchor = anchor; |
| 31 | + Ranges.Add(Tuple.Create(operation, range)); |
| 32 | + } |
| 33 | + |
| 34 | + public Union<DateTime, string> Anchor { get; } |
| 35 | + public IList<Tuple<DateMathOperation, DateMathTime>> Ranges { get; } = new List<Tuple<DateMathOperation, DateMathTime>>(); |
| 36 | + public DateMathTimeUnit? Round { get; protected set; } |
| 37 | + |
| 38 | + public static DateMathExpression Anchored(DateTime anchor) => new(anchor); |
| 39 | + |
| 40 | + public static DateMathExpression Anchored(string dateAnchor) => new(dateAnchor); |
| 41 | + |
| 42 | + public static implicit operator DateMath(DateTime dateTime) => Anchored(dateTime); |
| 43 | + |
| 44 | + public static implicit operator DateMath(string dateMath) => FromString(dateMath); |
| 45 | + |
| 46 | + public static DateMath FromString(string dateMath) |
| 47 | + { |
| 48 | + if (dateMath == null) return null; |
| 49 | + |
| 50 | + var match = DateMathRegex.Match(dateMath); |
| 51 | + if (!match.Success) throw new ArgumentException($"Cannot create a {nameof(DateMathExpression)} out of '{dateMath}'"); |
| 52 | + |
| 53 | + var math = new DateMathExpression(match.Groups["anchor"].Value); |
| 54 | + |
| 55 | + if (match.Groups["ranges"].Success) |
| 56 | + { |
| 57 | + var rangeString = match.Groups["ranges"].Value; |
| 58 | + do |
| 59 | + { |
| 60 | + var nextRangeStart = rangeString.Substring(1).IndexOfAny(new[] { '+', '-', '/' }); |
| 61 | + if (nextRangeStart == -1) nextRangeStart = rangeString.Length - 1; |
| 62 | + var unit = rangeString.Substring(1, nextRangeStart); |
| 63 | + if (rangeString.StartsWith("+", StringComparison.Ordinal)) |
| 64 | + { |
| 65 | + math = math.Add(unit); |
| 66 | + rangeString = rangeString.Substring(nextRangeStart + 1); |
| 67 | + } |
| 68 | + else if (rangeString.StartsWith("-", StringComparison.Ordinal)) |
| 69 | + { |
| 70 | + math = math.Subtract(unit); |
| 71 | + rangeString = rangeString.Substring(nextRangeStart + 1); |
| 72 | + } |
| 73 | + else rangeString = null; |
| 74 | + } while (!rangeString.IsNullOrEmpty()); |
| 75 | + } |
| 76 | + |
| 77 | + if (match.Groups["rounding"].Success) |
| 78 | + { |
| 79 | + var rounding = match.Groups["rounding"].Value.Substring(1).ToEnum<DateMathTimeUnit>(StringComparison.Ordinal); |
| 80 | + if (rounding.HasValue) |
| 81 | + return math.RoundTo(rounding.Value); |
| 82 | + } |
| 83 | + return math; |
| 84 | + } |
| 85 | + |
| 86 | + internal static bool IsValidDateMathString(string dateMath) => dateMath != null && DateMathRegex.IsMatch(dateMath); |
| 87 | + |
| 88 | + internal bool IsValid => Anchor.Match(_ => true, s => !s.IsNullOrEmpty()); |
| 89 | + |
| 90 | + public override string ToString() |
| 91 | + { |
| 92 | + if (!IsValid) return string.Empty; |
| 93 | + |
| 94 | + var separator = Round.HasValue || Ranges.HasAny() ? "||" : string.Empty; |
| 95 | + |
| 96 | + var sb = new StringBuilder(); |
| 97 | + var anchor = Anchor.Match( |
| 98 | + d => ToMinThreeDecimalPlaces(d) + separator, |
| 99 | + s => s == "now" || s.EndsWith("||", StringComparison.Ordinal) ? s : s + separator |
| 100 | + ); |
| 101 | + sb.Append(anchor); |
| 102 | + foreach (var r in Ranges) |
| 103 | + { |
| 104 | + sb.Append(r.Item1.GetStringValue()); |
| 105 | + //date math does not support fractional time units so e.g TimeSpan.FromHours(25) should not yield 1.04d |
| 106 | + sb.Append(r.Item2); |
| 107 | + } |
| 108 | + if (Round.HasValue) |
| 109 | + sb.Append("/" + Round.Value.GetStringValue()); |
| 110 | + |
| 111 | + return sb.ToString(); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Formats a <see cref="DateTime"/> to have a minimum of 3 decimal places if there are sub second values |
| 116 | + /// </summary> |
| 117 | + private static string ToMinThreeDecimalPlaces(DateTime dateTime) |
| 118 | + { |
| 119 | + var builder = new StringBuilder(33); |
| 120 | + var format = dateTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFF", CultureInfo.InvariantCulture); |
| 121 | + builder.Append(format); |
| 122 | + |
| 123 | + if (format.Length > 20 && format.Length < 23) |
| 124 | + { |
| 125 | + var diff = 23 - format.Length; |
| 126 | + for (var i = 0; i < diff; i++) |
| 127 | + builder.Append('0'); |
| 128 | + } |
| 129 | + |
| 130 | + switch (dateTime.Kind) |
| 131 | + { |
| 132 | + case DateTimeKind.Local: |
| 133 | + var offset = TimeZoneInfo.Local.GetUtcOffset(dateTime); |
| 134 | + if (offset >= TimeSpan.Zero) |
| 135 | + builder.Append('+'); |
| 136 | + else |
| 137 | + { |
| 138 | + builder.Append('-'); |
| 139 | + offset = offset.Negate(); |
| 140 | + } |
| 141 | + |
| 142 | + AppendTwoDigitNumber(builder, offset.Hours); |
| 143 | + builder.Append(':'); |
| 144 | + AppendTwoDigitNumber(builder, offset.Minutes); |
| 145 | + break; |
| 146 | + case DateTimeKind.Utc: |
| 147 | + builder.Append('Z'); |
| 148 | + break; |
| 149 | + } |
| 150 | + |
| 151 | + return builder.ToString(); |
| 152 | + } |
| 153 | + |
| 154 | + private static void AppendTwoDigitNumber(StringBuilder result, int val) |
| 155 | + { |
| 156 | + result.Append((char)('0' + (val / 10))); |
| 157 | + result.Append((char)('0' + (val % 10))); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +internal sealed class DateMathConverter : JsonConverter<DateMath> |
| 162 | +{ |
| 163 | + public override DateMath? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 164 | + { |
| 165 | + if (reader.TokenType != JsonTokenType.String) |
| 166 | + return null; |
| 167 | + |
| 168 | + // TODO: Performance - Review potential to avoid allocation on DateTime path and use Span<byte> |
| 169 | + |
| 170 | + var value = reader.GetString(); |
| 171 | + reader.Read(); |
| 172 | + |
| 173 | + if (!value.Contains("|") && DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var dateTime)) |
| 174 | + return DateMath.Anchored(dateTime); |
| 175 | + |
| 176 | + return DateMath.Anchored(value); |
| 177 | + } |
| 178 | + |
| 179 | + public override void Write(Utf8JsonWriter writer, DateMath value, JsonSerializerOptions options) |
| 180 | + { |
| 181 | + if (value is null) |
| 182 | + { |
| 183 | + writer.WriteNullValue(); |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + writer.WriteStringValue(value.ToString()); |
| 188 | + } |
| 189 | +} |
0 commit comments