| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | |
|
| | 6 | | namespace Azure.Messaging.ServiceBus.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( |
| | 26 | | this TimeSpan instance, |
| | 27 | | TimeSpan elapsed) |
| | 28 | | { |
| 0 | 29 | | if ((instance == TimeSpan.Zero) || (elapsed >= instance)) |
| | 30 | | { |
| 0 | 31 | | return TimeSpan.Zero; |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | if (elapsed == TimeSpan.Zero) |
| | 35 | | { |
| 0 | 36 | | return instance; |
| | 37 | | } |
| | 38 | |
|
| 0 | 39 | | return TimeSpan.FromMilliseconds(Math.Max(instance.TotalMilliseconds - elapsed.TotalMilliseconds, 0)); |
| | 40 | | } |
| | 41 | | } |
| | 42 | | } |