| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | |
| | | 6 | | namespace Azure.Messaging.EventHubs.Core |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// The set of extensions for the <see cref="TimeSpan" /> |
| | | 10 | | /// class. |
| | | 11 | | /// </summary> |
| | | 12 | | /// |
| | | 13 | | internal static class TimeSpanExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Calculates the duration remaining in a given period, with consideration to |
| | | 17 | | /// the amount of time that has already elapsed. |
| | | 18 | | /// </summary> |
| | | 19 | | /// |
| | | 20 | | /// <param name="instance">The instance that this method was invoked on.</param> |
| | | 21 | | /// <param name="elapsed">The amount of time that has already elapsed.</param> |
| | | 22 | | /// |
| | | 23 | | /// <returns>The amount of time remaining in the time period.</returns> |
| | | 24 | | /// |
| | | 25 | | public static TimeSpan CalculateRemaining(this TimeSpan instance, |
| | | 26 | | TimeSpan elapsed) |
| | | 27 | | { |
| | 0 | 28 | | if ((instance == TimeSpan.Zero) || (elapsed >= instance)) |
| | | 29 | | { |
| | 0 | 30 | | return TimeSpan.Zero; |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | if (elapsed == TimeSpan.Zero) |
| | | 34 | | { |
| | 0 | 35 | | return instance; |
| | | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | return TimeSpan.FromMilliseconds(Math.Max(instance.TotalMilliseconds - elapsed.TotalMilliseconds, 0)); |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | } |