|
| 1 | +/*------------------------------------------------------------------------------ |
| 2 | + * Copied from https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/6.22.0/src/Microsoft.IdentityModel.Tokens/DateTimeUtil.cs |
| 3 | +------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +using System; |
| 6 | + |
| 7 | +namespace Microsoft.Azure.SignalR |
| 8 | +{ |
| 9 | + internal static class DateTimeUtil |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Add a DateTime and a TimeSpan. |
| 13 | + /// The maximum time is DateTime.MaxTime. It is not an error if time + timespan > MaxTime. |
| 14 | + /// Just return MaxTime. |
| 15 | + /// </summary> |
| 16 | + /// <param name="time">Initial <see cref="DateTime"/> value.</param> |
| 17 | + /// <param name="timespan"><see cref="TimeSpan"/> to add.</param> |
| 18 | + /// <returns><see cref="DateTime"/> as the sum of time and timespan.</returns> |
| 19 | + public static DateTime Add(DateTime time, TimeSpan timespan) |
| 20 | + { |
| 21 | + if (timespan == TimeSpan.Zero) |
| 22 | + { |
| 23 | + return time; |
| 24 | + } |
| 25 | + |
| 26 | + if (timespan > TimeSpan.Zero && DateTime.MaxValue - time <= timespan) |
| 27 | + { |
| 28 | + return GetMaxValue(time.Kind); |
| 29 | + } |
| 30 | + |
| 31 | + if (timespan < TimeSpan.Zero && DateTime.MinValue - time >= timespan) |
| 32 | + { |
| 33 | + return GetMinValue(time.Kind); |
| 34 | + } |
| 35 | + |
| 36 | + return time + timespan; |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets the Maximum value for a DateTime specifying kind. |
| 41 | + /// </summary> |
| 42 | + /// <param name="kind">DateTimeKind to use.</param> |
| 43 | + /// <returns>DateTime of specified kind.</returns> |
| 44 | + public static DateTime GetMaxValue(DateTimeKind kind) |
| 45 | + { |
| 46 | + if (kind == DateTimeKind.Unspecified) |
| 47 | + return new DateTime(DateTime.MaxValue.Ticks, DateTimeKind.Utc); |
| 48 | + |
| 49 | + return new DateTime(DateTime.MaxValue.Ticks, kind); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Gets the Minimum value for a DateTime specifying kind. |
| 54 | + /// </summary> |
| 55 | + /// <param name="kind">DateTimeKind to use.</param> |
| 56 | + /// <returns>DateTime of specified kind.</returns> |
| 57 | + public static DateTime GetMinValue(DateTimeKind kind) |
| 58 | + { |
| 59 | + if (kind == DateTimeKind.Unspecified) |
| 60 | + return new DateTime(DateTime.MinValue.Ticks, DateTimeKind.Utc); |
| 61 | + |
| 62 | + return new DateTime(DateTime.MinValue.Ticks, kind); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Ensures that DataTime is UTC. |
| 67 | + /// </summary> |
| 68 | + /// <param name="value"><see cref="DateTime"/>to convert.</param> |
| 69 | + /// <returns></returns> |
| 70 | + public static DateTime? ToUniversalTime(DateTime? value) |
| 71 | + { |
| 72 | + if (value == null || value.Value.Kind == DateTimeKind.Utc) |
| 73 | + return value; |
| 74 | + |
| 75 | + return ToUniversalTime(value.Value); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Ensures that DateTime is UTC. |
| 80 | + /// </summary> |
| 81 | + /// <param name="value"><see cref="DateTime"/>to convert.</param> |
| 82 | + /// <returns></returns> |
| 83 | + public static DateTime ToUniversalTime(DateTime value) |
| 84 | + { |
| 85 | + |
| 86 | + if (value.Kind == DateTimeKind.Utc) |
| 87 | + return value; |
| 88 | + |
| 89 | + return value.ToUniversalTime(); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments