aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/Google.Protobuf/WellKnownTypes/DurationPartial.cs
diff options
context:
space:
mode:
authorJon Skeet <jonskeet@google.com>2016-01-15 11:39:27 +0000
committerJon Skeet <jonskeet@google.com>2016-01-15 11:39:27 +0000
commit1fc485928fc7a6483b700867f1a6cb2acfa8da5d (patch)
tree7175437a631dcefc48d1bf91f0bbe81c632295e2 /csharp/src/Google.Protobuf/WellKnownTypes/DurationPartial.cs
parentc74676f07037acca34e9df0fb868b29afae15ac9 (diff)
downloadprotobuf-1fc485928fc7a6483b700867f1a6cb2acfa8da5d.tar.gz
protobuf-1fc485928fc7a6483b700867f1a6cb2acfa8da5d.tar.bz2
protobuf-1fc485928fc7a6483b700867f1a6cb2acfa8da5d.zip
Fixes to JSON timestamp/duration representations
Diffstat (limited to 'csharp/src/Google.Protobuf/WellKnownTypes/DurationPartial.cs')
-rw-r--r--csharp/src/Google.Protobuf/WellKnownTypes/DurationPartial.cs23
1 files changed, 23 insertions, 0 deletions
diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/DurationPartial.cs b/csharp/src/Google.Protobuf/WellKnownTypes/DurationPartial.cs
index 324f48fc..b8eba9d3 100644
--- a/csharp/src/Google.Protobuf/WellKnownTypes/DurationPartial.cs
+++ b/csharp/src/Google.Protobuf/WellKnownTypes/DurationPartial.cs
@@ -57,15 +57,38 @@ namespace Google.Protobuf.WellKnownTypes
/// </summary>
public const long MinSeconds = -315576000000L;
+ internal const int MaxNanoseconds = NanosecondsPerSecond - 1;
+ internal const int MinNanoseconds = -NanosecondsPerSecond + 1;
+
+ internal static bool IsNormalized(long seconds, int nanoseconds)
+ {
+ // Simple boundaries
+ if (seconds < MinSeconds || seconds > MaxSeconds ||
+ nanoseconds < MinNanoseconds || nanoseconds > MaxNanoseconds)
+ {
+ return false;
+ }
+ // We only have a problem is one is strictly negative and the other is
+ // strictly positive.
+ return Math.Sign(seconds) * Math.Sign(nanoseconds) != -1;
+ }
+
+
/// <summary>
/// Converts this <see cref="Duration"/> to a <see cref="TimeSpan"/>.
/// </summary>
/// <remarks>If the duration is not a precise number of ticks, it is truncated towards 0.</remarks>
/// <returns>The value of this duration, as a <c>TimeSpan</c>.</returns>
+ /// <exception cref="InvalidOperationException">This value isn't a valid normalized duration, as
+ /// described in the documentation.</exception>
public TimeSpan ToTimeSpan()
{
checked
{
+ if (!IsNormalized(Seconds, Nanos))
+ {
+ throw new InvalidOperationException("Duration was not a valid normalized duration");
+ }
long ticks = Seconds * TimeSpan.TicksPerSecond + Nanos / NanosecondsPerTick;
return TimeSpan.FromTicks(ticks);
}