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 | public partial class Timestamp |
| 38 | { |
| 39 | private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); |
| 40 | private static readonly long BclSecondsAtUnixEpoch = UnixEpoch.Ticks / TimeSpan.TicksPerSecond; |
Jon Skeet | fb24882 | 2015-09-04 12:41:14 +0100 | [diff] [blame^] | 41 | internal static readonly long UnixSecondsAtBclMinValue = -BclSecondsAtUnixEpoch; |
| 42 | internal static readonly long UnixSecondsAtBclMaxValue = (DateTime.MaxValue.Ticks / TimeSpan.TicksPerSecond) - BclSecondsAtUnixEpoch; |
Jon Skeet | 9629797 | 2015-07-30 09:29:52 +0100 | [diff] [blame] | 43 | |
| 44 | /// <summary> |
| 45 | /// Returns the difference between one <see cref="Timestamp"/> and another, as a <see cref="Duration"/>. |
| 46 | /// </summary> |
| 47 | /// <param name="lhs">The timestamp to subtract from. Must not be null.</param> |
| 48 | /// <param name="rhs">The timestamp to subtract. Must not be null.</param> |
| 49 | /// <returns>The difference between the two specified timestamps.</returns> |
| 50 | public static Duration operator -(Timestamp lhs, Timestamp rhs) |
| 51 | { |
| 52 | Preconditions.CheckNotNull(lhs, "lhs"); |
| 53 | Preconditions.CheckNotNull(rhs, "rhs"); |
| 54 | checked |
| 55 | { |
| 56 | return Duration.Normalize(lhs.Seconds - rhs.Seconds, lhs.Nanos - rhs.Nanos); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// <summary> |
| 61 | /// Adds a <see cref="Duration"/> to a <see cref="Timestamp"/>, to obtain another <c>Timestamp</c>. |
| 62 | /// </summary> |
| 63 | /// <param name="lhs">The timestamp to add the duration to. Must not be null.</param> |
| 64 | /// <param name="rhs">The duration to add. Must not be null.</param> |
| 65 | /// <returns>The result of adding the duration to the timestamp.</returns> |
| 66 | public static Timestamp operator +(Timestamp lhs, Duration rhs) |
| 67 | { |
| 68 | Preconditions.CheckNotNull(lhs, "lhs"); |
| 69 | Preconditions.CheckNotNull(rhs, "rhs"); |
| 70 | checked |
| 71 | { |
| 72 | return Normalize(lhs.Seconds + rhs.Seconds, lhs.Nanos + rhs.Nanos); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /// <summary> |
| 77 | /// Subtracts a <see cref="Duration"/> from a <see cref="Timestamp"/>, to obtain another <c>Timestamp</c>. |
| 78 | /// </summary> |
| 79 | /// <param name="lhs">The timestamp to subtract the duration from. Must not be null.</param> |
| 80 | /// <param name="rhs">The duration to subtract.</param> |
| 81 | /// <returns>The result of subtracting the duration from the timestamp.</returns> |
| 82 | public static Timestamp operator -(Timestamp lhs, Duration rhs) |
| 83 | { |
| 84 | Preconditions.CheckNotNull(lhs, "lhs"); |
| 85 | Preconditions.CheckNotNull(rhs, "rhs"); |
| 86 | checked |
| 87 | { |
| 88 | return Normalize(lhs.Seconds - rhs.Seconds, lhs.Nanos - rhs.Nanos); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// <summary> |
| 93 | /// Converts this timestamp into a <see cref="DateTime"/>. |
| 94 | /// </summary> |
| 95 | /// <remarks> |
| 96 | /// The resulting <c>DateTime</c> will always have a <c>Kind</c> of <c>Utc</c>. |
| 97 | /// If the timestamp is not a precise number of ticks, it will be truncated towards the start |
| 98 | /// of time. For example, a timestamp with a <see cref="Nanos"/> value of 99 will result in a |
| 99 | /// <see cref="DateTime"/> value precisely on a second. |
| 100 | /// </remarks> |
| 101 | /// <returns>This timestamp as a <c>DateTime</c>.</returns> |
| 102 | public DateTime ToDateTime() |
| 103 | { |
| 104 | return UnixEpoch.AddSeconds(Seconds).AddTicks(Nanos / Duration.NanosecondsPerTick); |
| 105 | } |
| 106 | |
| 107 | /// <summary> |
| 108 | /// Converts this timestamp into a <see cref="DateTimeOffset"/>. |
| 109 | /// </summary> |
| 110 | /// <remarks> |
| 111 | /// The resulting <c>DateTimeOffset</c> will always have an <c>Offset</c> of zero. |
| 112 | /// If the timestamp is not a precise number of ticks, it will be truncated towards the start |
| 113 | /// of time. For example, a timestamp with a <see cref="Nanos"/> value of 99 will result in a |
| 114 | /// <see cref="DateTimeOffset"/> value precisely on a second. |
| 115 | /// </remarks> |
| 116 | /// <returns>This timestamp as a <c>DateTimeOffset</c>.</returns> |
| 117 | public DateTimeOffset ToDateTimeOffset() |
| 118 | { |
| 119 | return new DateTimeOffset(ToDateTime(), TimeSpan.Zero); |
| 120 | } |
| 121 | |
| 122 | /// <summary> |
| 123 | /// Converts the specified <see cref="DateTime"/> to a <see cref="Timestamp"/>. |
| 124 | /// </summary> |
| 125 | /// <param name="dateTime"></param> |
| 126 | /// <exception cref="ArgumentException">The <c>Kind</c> of <paramref name="dateTime"/> is not <c>DateTimeKind.Utc</c>.</exception> |
| 127 | /// <returns>The converted timestamp.</returns> |
| 128 | public static Timestamp FromDateTime(DateTime dateTime) |
| 129 | { |
| 130 | if (dateTime.Kind != DateTimeKind.Utc) |
| 131 | { |
| 132 | throw new ArgumentException("Conversion from DateTime to Timestamp requires the DateTime kind to be Utc", "dateTime"); |
| 133 | } |
| 134 | // Do the arithmetic using DateTime.Ticks, which is always non-negative, making things simpler. |
| 135 | long secondsSinceBclEpoch = dateTime.Ticks / TimeSpan.TicksPerSecond; |
| 136 | int nanoseconds = (int) (dateTime.Ticks % TimeSpan.TicksPerSecond) * Duration.NanosecondsPerTick; |
| 137 | return new Timestamp { Seconds = secondsSinceBclEpoch - BclSecondsAtUnixEpoch, Nanos = nanoseconds }; |
| 138 | } |
| 139 | |
| 140 | /// <summary> |
Jon Skeet | 811fc89 | 2015-08-04 15:58:39 +0100 | [diff] [blame] | 141 | /// Converts the given <see cref="DateTimeOffset"/> to a <see cref="Timestamp"/> |
Jon Skeet | 9629797 | 2015-07-30 09:29:52 +0100 | [diff] [blame] | 142 | /// </summary> |
Jon Skeet | 811fc89 | 2015-08-04 15:58:39 +0100 | [diff] [blame] | 143 | /// <remarks>The offset is taken into consideration when converting the value (so the same instant in time |
| 144 | /// is represented) but is not a separate part of the resulting value. In other words, there is no |
| 145 | /// roundtrip operation to retrieve the original <c>DateTimeOffset</c>.</remarks> |
| 146 | /// <param name="dateTimeOffset">The date and time (with UTC offset) to convert to a timestamp.</param> |
Jon Skeet | 9629797 | 2015-07-30 09:29:52 +0100 | [diff] [blame] | 147 | /// <returns>The converted timestamp.</returns> |
| 148 | public static Timestamp FromDateTimeOffset(DateTimeOffset dateTimeOffset) |
| 149 | { |
| 150 | // We don't need to worry about this having negative ticks: DateTimeOffset is constrained to handle |
| 151 | // values whose *UTC* value is in the range of DateTime. |
| 152 | return FromDateTime(dateTimeOffset.UtcDateTime); |
| 153 | } |
| 154 | |
Jon Skeet | 16e272e | 2015-07-31 13:22:15 +0100 | [diff] [blame] | 155 | internal static Timestamp Normalize(long seconds, int nanoseconds) |
Jon Skeet | 9629797 | 2015-07-30 09:29:52 +0100 | [diff] [blame] | 156 | { |
| 157 | int extraSeconds = nanoseconds / Duration.NanosecondsPerSecond; |
| 158 | seconds += extraSeconds; |
| 159 | nanoseconds -= extraSeconds * Duration.NanosecondsPerSecond; |
| 160 | |
| 161 | if (nanoseconds < 0) |
| 162 | { |
| 163 | nanoseconds += Duration.NanosecondsPerSecond; |
| 164 | seconds--; |
| 165 | } |
| 166 | return new Timestamp { Seconds = seconds, Nanos = nanoseconds }; |
| 167 | } |
| 168 | } |
| 169 | } |