Jon Skeet | 9629797 | 2015-07-30 09:29:52 +0100 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | // Protocol Buffers - Google's data interchange format |
| 3 | // Copyright 2015 Google Inc. All rights reserved. |
| 4 | // https://developers.google.com/protocol-buffers/ |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | #endregion |
| 32 | |
| 33 | using System; |
| 34 | |
| 35 | namespace Google.Protobuf.WellKnownTypes |
| 36 | { |
| 37 | // Manually-written partial class for the Duration well-known type, |
| 38 | // providing a conversion to TimeSpan and convenience operators. |
| 39 | public partial class Duration |
| 40 | { |
Jon Skeet | 811fc89 | 2015-08-04 15:58:39 +0100 | [diff] [blame] | 41 | /// <summary> |
| 42 | /// The number of nanoseconds in a second. |
| 43 | /// </summary> |
Jon Skeet | 9629797 | 2015-07-30 09:29:52 +0100 | [diff] [blame] | 44 | public const int NanosecondsPerSecond = 1000000000; |
Jon Skeet | 811fc89 | 2015-08-04 15:58:39 +0100 | [diff] [blame] | 45 | /// <summary> |
| 46 | /// The number of nanoseconds in a BCL tick (as used by <see cref="TimeSpan"/> and <see cref="DateTime"/>). |
| 47 | /// </summary> |
Jon Skeet | 9629797 | 2015-07-30 09:29:52 +0100 | [diff] [blame] | 48 | public const int NanosecondsPerTick = 100; |
| 49 | |
| 50 | /// <summary> |
Jon Skeet | fb24882 | 2015-09-04 12:41:14 +0100 | [diff] [blame^] | 51 | /// The maximum permitted number of seconds. |
| 52 | /// </summary> |
| 53 | public const long MaxSeconds = 315576000000L; |
| 54 | |
| 55 | /// <summary> |
| 56 | /// The minimum permitted number of seconds. |
| 57 | /// </summary> |
| 58 | public const long MinSeconds = -315576000000L; |
| 59 | |
| 60 | /// <summary> |
Jon Skeet | 9629797 | 2015-07-30 09:29:52 +0100 | [diff] [blame] | 61 | /// Converts this <see cref="Duration"/> to a <see cref="TimeSpan"/>. |
| 62 | /// </summary> |
| 63 | /// <remarks>If the duration is not a precise number of ticks, it is truncated towards 0.</remarks> |
| 64 | /// <returns>The value of this duration, as a <c>TimeSpan</c>.</returns> |
| 65 | public TimeSpan ToTimeSpan() |
| 66 | { |
| 67 | checked |
| 68 | { |
| 69 | long ticks = Seconds * TimeSpan.TicksPerSecond + Nanos / NanosecondsPerTick; |
| 70 | return TimeSpan.FromTicks(ticks); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// <summary> |
| 75 | /// Converts the given <see cref="TimeSpan"/> to a <see cref="Duration"/>. |
| 76 | /// </summary> |
| 77 | /// <param name="timeSpan">The <c>TimeSpan</c> to convert.</param> |
| 78 | /// <returns>The value of the given <c>TimeSpan</c>, as a <c>Duration</c>.</returns> |
| 79 | public static Duration FromTimeSpan(TimeSpan timeSpan) |
| 80 | { |
| 81 | checked |
| 82 | { |
| 83 | long ticks = timeSpan.Ticks; |
| 84 | long seconds = ticks / TimeSpan.TicksPerSecond; |
| 85 | int nanos = (int) (ticks % TimeSpan.TicksPerSecond) * NanosecondsPerTick; |
| 86 | return new Duration { Seconds = seconds, Nanos = nanos }; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// <summary> |
| 91 | /// Returns the result of negating the duration. For example, the negation of 5 minutes is -5 minutes. |
| 92 | /// </summary> |
| 93 | /// <param name="value">The duration to negate. Must not be null.</param> |
| 94 | /// <returns>The negated value of this duration.</returns> |
| 95 | public static Duration operator -(Duration value) |
| 96 | { |
| 97 | Preconditions.CheckNotNull(value, "value"); |
| 98 | checked |
| 99 | { |
| 100 | return Normalize(-value.Seconds, -value.Nanos); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// <summary> |
| 105 | /// Adds the two specified <see cref="Duration"/> values together. |
| 106 | /// </summary> |
| 107 | /// <param name="lhs">The first value to add. Must not be null.</param> |
| 108 | /// <param name="rhs">The second value to add. Must not be null.</param> |
| 109 | /// <returns></returns> |
| 110 | public static Duration operator +(Duration lhs, Duration rhs) |
| 111 | { |
| 112 | Preconditions.CheckNotNull(lhs, "lhs"); |
| 113 | Preconditions.CheckNotNull(rhs, "rhs"); |
| 114 | checked |
| 115 | { |
| 116 | return Normalize(lhs.Seconds + rhs.Seconds, lhs.Nanos + rhs.Nanos); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// <summary> |
| 121 | /// Subtracts one <see cref="Duration"/> from another. |
| 122 | /// </summary> |
| 123 | /// <param name="lhs">The duration to subtract from. Must not be null.</param> |
| 124 | /// <param name="rhs">The duration to subtract. Must not be null.</param> |
| 125 | /// <returns>The difference between the two specified durations.</returns> |
| 126 | public static Duration operator -(Duration lhs, Duration rhs) |
| 127 | { |
| 128 | Preconditions.CheckNotNull(lhs, "lhs"); |
| 129 | Preconditions.CheckNotNull(rhs, "rhs"); |
| 130 | checked |
| 131 | { |
| 132 | return Normalize(lhs.Seconds - rhs.Seconds, lhs.Nanos - rhs.Nanos); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /// <summary> |
| 137 | /// Creates a duration with the normalized values from the given number of seconds and |
| 138 | /// nanoseconds, conforming with the description in the proto file. |
| 139 | /// </summary> |
| 140 | internal static Duration Normalize(long seconds, int nanoseconds) |
| 141 | { |
| 142 | // Ensure that nanoseconds is in the range (-1,000,000,000, +1,000,000,000) |
| 143 | int extraSeconds = nanoseconds / NanosecondsPerSecond; |
| 144 | seconds += extraSeconds; |
| 145 | nanoseconds -= extraSeconds * NanosecondsPerSecond; |
| 146 | |
| 147 | // Now make sure that Sign(seconds) == Sign(nanoseconds) if Sign(seconds) != 0. |
| 148 | if (seconds < 0 && nanoseconds > 0) |
| 149 | { |
| 150 | seconds += 1; |
| 151 | nanoseconds -= NanosecondsPerSecond; |
| 152 | } |
| 153 | else if (seconds > 0 && nanoseconds < 0) |
| 154 | { |
| 155 | seconds -= 1; |
| 156 | nanoseconds += NanosecondsPerSecond; |
| 157 | } |
| 158 | return new Duration { Seconds = seconds, Nanos = nanoseconds }; |
| 159 | } |
| 160 | } |
| 161 | } |