| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.Primitives |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | |
|
| | 8 | | static class Ticks |
| | 9 | | { |
| 0 | 10 | | public static long Now => DateTime.UtcNow.ToFileTimeUtc(); |
| | 11 | |
|
| | 12 | | public static long FromMilliseconds(int milliseconds) |
| | 13 | | { |
| 0 | 14 | | return checked(milliseconds * TimeSpan.TicksPerMillisecond); |
| | 15 | | } |
| | 16 | |
|
| | 17 | | public static int ToMilliseconds(long ticks) |
| | 18 | | { |
| 0 | 19 | | return checked((int)(ticks / TimeSpan.TicksPerMillisecond)); |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public static long FromTimeSpan(TimeSpan duration) |
| | 23 | | { |
| 0 | 24 | | return duration.Ticks; |
| | 25 | | } |
| | 26 | |
|
| | 27 | | public static TimeSpan ToTimeSpan(long ticks) |
| | 28 | | { |
| 0 | 29 | | return new TimeSpan(ticks); |
| | 30 | | } |
| | 31 | |
|
| | 32 | | public static long Add(long firstTicks, long secondTicks) |
| | 33 | | { |
| 0 | 34 | | if (firstTicks == long.MaxValue || firstTicks == long.MinValue) |
| | 35 | | { |
| 0 | 36 | | return firstTicks; |
| | 37 | | } |
| | 38 | |
|
| 0 | 39 | | if (secondTicks == long.MaxValue || secondTicks == long.MinValue) |
| | 40 | | { |
| 0 | 41 | | return secondTicks; |
| | 42 | | } |
| | 43 | |
|
| 0 | 44 | | if (firstTicks >= 0 && long.MaxValue - firstTicks <= secondTicks) |
| | 45 | | { |
| 0 | 46 | | return long.MaxValue - 1; |
| | 47 | | } |
| | 48 | |
|
| 0 | 49 | | if (firstTicks <= 0 && long.MinValue - firstTicks >= secondTicks) |
| | 50 | | { |
| 0 | 51 | | return long.MinValue + 1; |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | return checked(firstTicks + secondTicks); |
| | 55 | | } |
| | 56 | | } |
| | 57 | | } |