| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | |
| | | 6 | | namespace CoreWCF.IdentityModel |
| | | 7 | | { |
| | | 8 | | internal static class DateTimeUtil |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Add a DateTime and a TimeSpan. |
| | | 12 | | /// The maximum time is DateTime.MaxTime. It is not an error if time + timespan > MaxTime. |
| | | 13 | | /// Just return MaxTime. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="time">Initial <see cref="DateTime"/> value.</param> |
| | | 16 | | /// <param name="timespan"><see cref="TimeSpan"/> to add.</param> |
| | | 17 | | /// <returns></returns> |
| | | 18 | | public static DateTime Add(DateTime time, TimeSpan timespan) |
| | | 19 | | { |
| | 0 | 20 | | if (timespan >= TimeSpan.Zero && DateTime.MaxValue - time <= timespan) |
| | | 21 | | { |
| | 0 | 22 | | return GetMaxValue(time.Kind); |
| | | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | if (timespan <= TimeSpan.Zero && DateTime.MinValue - time >= timespan) |
| | | 26 | | { |
| | 0 | 27 | | return GetMinValue(time.Kind); |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | return time + timespan; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Add a DateTime and a non-negative TimeSpan. |
| | | 35 | | /// The maximum time is DateTime.MaxTime. It is not an error if time + timespan > MaxTime. |
| | | 36 | | /// Just return MaxTime. If TimeSpan is < TimeSpan.Zero, throw exception. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="time"></param> |
| | | 39 | | /// <param name="timespan"></param> |
| | | 40 | | /// <returns></returns> |
| | | 41 | | public static DateTime AddNonNegative(DateTime time, TimeSpan timespan) |
| | | 42 | | { |
| | 0 | 43 | | if (timespan <= TimeSpan.Zero) |
| | | 44 | | { |
| | 0 | 45 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID2 |
| | | 46 | | } |
| | 0 | 47 | | return Add(time, timespan); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public static DateTime GetMaxValue(DateTimeKind kind) |
| | | 51 | | { |
| | 0 | 52 | | return new DateTime(DateTime.MaxValue.Ticks, kind); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public static DateTime GetMinValue(DateTimeKind kind) |
| | | 56 | | { |
| | 0 | 57 | | return new DateTime(DateTime.MinValue.Ticks, kind); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public static DateTime? ToUniversalTime(DateTime? value) |
| | | 61 | | { |
| | 0 | 62 | | if (null == value || value.Value.Kind == DateTimeKind.Utc) |
| | | 63 | | { |
| | 0 | 64 | | return value; |
| | | 65 | | } |
| | 0 | 66 | | return ToUniversalTime(value.Value); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public static DateTime ToUniversalTime(DateTime value) |
| | | 70 | | { |
| | | 71 | | |
| | 0 | 72 | | if (value.Kind == DateTimeKind.Utc) |
| | | 73 | | { |
| | 0 | 74 | | return value; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | return value.ToUniversalTime(); |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | } |