| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.Threading; |
| | | 4 | | |
| | | 5 | | namespace AmbientServices.Utilities; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A static class that contains utility functions for <see cref="System.TimeSpan"/>. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// <pitch>Precise conversion between the library's three time representations — <see cref="TimeSpan"/> ticks, <see cref |
| | | 12 | | /// <pledge>Tick-rate conversions are exact whenever the integer math fits in a <see cref="long"/> and lose at most doub |
| | | 13 | | /// <plan>The static initializer reduces the <see cref="TimeSpan"/> and <see cref="Stopwatch.Frequency"/> tick rates by |
| | | 14 | | /// </remarks> |
| | | 15 | | internal static class TimeSpanUtilities |
| | | 16 | | { |
| | | 17 | | internal static readonly long TimeSpanToStopwatchMultiplier; |
| | | 18 | | internal static readonly long TimeSpanToStopwatchDivisor; |
| | | 19 | | private static readonly double TimeSpanToStopwatchRatio; |
| | | 20 | | internal static readonly long StopwatchToTimeSpanMultiplier; |
| | | 21 | | internal static readonly long StopwatchToTimeSpanDivisor; |
| | | 22 | | private static readonly double StopwatchToTimeSpanRatio; |
| | | 23 | | private static readonly long BaselineStopwatchTimestamp; |
| | | 24 | | private static readonly long BaselineDateTimeTicks; |
| | | 25 | | #pragma warning disable CA1810 // it should be faster in this case to do it this way because the values depend on each |
| | | 26 | | static TimeSpanUtilities() |
| | | 27 | | #pragma warning restore CA1810 |
| | | 28 | | { |
| | 3 | 29 | | ulong timeSpanTicksPerSecond = TimeSpan.TicksPerSecond; |
| | 3 | 30 | | ulong stopwatchTicksPerSecond = (ulong)Stopwatch.Frequency; |
| | | 31 | | // adjust the multipliers and divisors to reduce the range of values that will cause overflow during conversion |
| | 3 | 32 | | ulong gcd = GCD(timeSpanTicksPerSecond, stopwatchTicksPerSecond); |
| | 3 | 33 | | TimeSpanToStopwatchMultiplier = (long)(stopwatchTicksPerSecond / gcd); |
| | 3 | 34 | | TimeSpanToStopwatchDivisor = (long)(timeSpanTicksPerSecond / gcd); |
| | 3 | 35 | | TimeSpanToStopwatchRatio = TimeSpanToStopwatchMultiplier / (double)TimeSpanToStopwatchDivisor; |
| | 3 | 36 | | StopwatchToTimeSpanMultiplier = (long)(timeSpanTicksPerSecond / gcd); |
| | 3 | 37 | | StopwatchToTimeSpanDivisor = (long)(stopwatchTicksPerSecond / gcd); |
| | 3 | 38 | | StopwatchToTimeSpanRatio = StopwatchToTimeSpanMultiplier / (double)StopwatchToTimeSpanDivisor; |
| | 3 | 39 | | BaselineStopwatchTimestamp = Stopwatch.GetTimestamp(); // note that it doesn't matter if AmbientClock is in |
| | | 40 | | #if DEBUG // delay this a little bit just in case it makes a difference |
| | | 41 | | System.Threading.Thread.Sleep(73); |
| | | 42 | | #endif |
| | | 43 | | // we can't use AmbientClock here because it probably hasn't been fully initialized yet |
| | 3 | 44 | | BaselineDateTimeTicks = DateTime.UtcNow.Ticks; |
| | | 45 | | // make sure that nobody else gets times before these |
| | 3 | 46 | | System.Threading.Thread.MemoryBarrier(); |
| | 3 | 47 | | TimeSpanStopwatchConversionLeastCommonMultiple = TimeSpanToStopwatchMultiplier * TimeSpanToStopwatchDivisor; |
| | 3 | 48 | | } |
| | | 49 | | internal static ulong GCD(ulong a, ulong b) |
| | | 50 | | { |
| | 3 | 51 | | while (a != 0 && b != 0) |
| | | 52 | | { |
| | 3 | 53 | | if (a > b) a %= b; else b %= a; |
| | | 54 | | } |
| | 3 | 55 | | return a | b; |
| | | 56 | | } |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the smallest number of ticks than can be successfully roundtripped between stopwatch ticks and timespan tic |
| | | 59 | | /// </summary> |
| | | 60 | | internal static long TimeSpanStopwatchConversionLeastCommonMultiple { get; private set; } |
| | | 61 | | /// <summary> |
| | | 62 | | /// Converts <see cref="TimeSpan"/> ticks to <see cref="System.Diagnostics.Stopwatch"/> ticks as accurately as possi |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="timeSpanTicks">The number of <see cref="TimeSpan"/> ticks.</param> |
| | | 65 | | /// <returns>The equivalent number of <see cref="System.Diagnostics.Stopwatch"/> ticks</returns> |
| | | 66 | | public static long TimeSpanTicksToStopwatchTicks(long timeSpanTicks) |
| | | 67 | | { |
| | | 68 | | try |
| | | 69 | | { |
| | | 70 | | checked |
| | | 71 | | { |
| | 3 | 72 | | return timeSpanTicks * TimeSpanToStopwatchMultiplier / TimeSpanToStopwatchDivisor; |
| | | 73 | | } |
| | | 74 | | } |
| | | 75 | | // Coverage note: the testability of this code depends on the values of system constants which cannot be altered |
| | 2 | 76 | | catch (OverflowException) |
| | | 77 | | { |
| | 2 | 78 | | return (long)(timeSpanTicks * TimeSpanToStopwatchRatio); |
| | | 79 | | } |
| | 3 | 80 | | } |
| | | 81 | | /// <summary> |
| | | 82 | | /// Converts <see cref="System.Diagnostics.Stopwatch"/> ticks to <see cref="TimeSpan"/> ticks as accurately as possi |
| | | 83 | | /// </summary> |
| | | 84 | | /// <param name="timeSpanTicks">The number of <see cref="System.Diagnostics.Stopwatch"/> ticks.</param> |
| | | 85 | | /// <returns>The equivalent number of <see cref="TimeSpan"/> ticks</returns> |
| | | 86 | | public static long StopwatchTicksToTimeSpanTicks(long timeSpanTicks) |
| | | 87 | | { |
| | | 88 | | try |
| | | 89 | | { |
| | | 90 | | checked |
| | | 91 | | { |
| | 3 | 92 | | return timeSpanTicks * StopwatchToTimeSpanMultiplier / StopwatchToTimeSpanDivisor; |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | // Coverage note: the testability of this code depends on the values of system constants which cannot be altered |
| | 0 | 96 | | catch (OverflowException) |
| | | 97 | | { |
| | 0 | 98 | | return (long)(timeSpanTicks * StopwatchToTimeSpanRatio); |
| | | 99 | | } |
| | 3 | 100 | | } |
| | | 101 | | /// <summary> |
| | | 102 | | /// Converts a stopwatch timestamp to UTC <see cref="DateTime"/> ticks. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="stopwatchTimestamp">A timestamp gathered from <see cref="Stopwatch.GetTimestamp()"/>.</param> |
| | | 105 | | /// <returns>The number of UTC <see cref="DateTime "/>ticks.</returns> |
| | | 106 | | public static long StopwatchTimestampToDateTime(long stopwatchTimestamp) |
| | | 107 | | { |
| | 3 | 108 | | long stopwatchTicksAgo = stopwatchTimestamp - BaselineStopwatchTimestamp; |
| | 3 | 109 | | long dateTimeTicksAgo = StopwatchTicksToTimeSpanTicks(stopwatchTicksAgo); |
| | 3 | 110 | | return BaselineDateTimeTicks + dateTimeTicksAgo; |
| | | 111 | | } |
| | | 112 | | /// <summary> |
| | | 113 | | /// Converts UTC <see cref="DateTime"/> ticks to a stopwatch timestamp. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="dateTimeTicks">Ticks from a UTC <see cref="DateTime"/>.</param> |
| | | 116 | | /// <returns>The corresponding <see cref="Stopwatch"/> timestamp.</returns> |
| | | 117 | | public static long DateTimeToStopwatchTimestamp(long dateTimeTicks) |
| | | 118 | | { |
| | 2 | 119 | | long dateTimeTicksAgo = dateTimeTicks - BaselineDateTimeTicks; |
| | 2 | 120 | | long stopwatchTicksAgo = TimeSpanTicksToStopwatchTicks(dateTimeTicksAgo); |
| | 2 | 121 | | return BaselineStopwatchTimestamp + stopwatchTicksAgo; |
| | | 122 | | } |
| | | 123 | | } |