From 96833b8f4b1897ae2a4ddf21ec9d1c5e6eabc8c7 Mon Sep 17 00:00:00 2001 From: Warren Falk Date: Mon, 9 Jul 2018 17:26:36 -0400 Subject: implement IComparable and comparison operators on Timestamp (#4318) --- .../WellKnownTypes/TimestampPartial.cs | 105 ++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) (limited to 'csharp/src/Google.Protobuf/WellKnownTypes/TimestampPartial.cs') diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/TimestampPartial.cs b/csharp/src/Google.Protobuf/WellKnownTypes/TimestampPartial.cs index aa403473..a9251974 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/TimestampPartial.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/TimestampPartial.cs @@ -36,7 +36,7 @@ using System.Text; namespace Google.Protobuf.WellKnownTypes { - public partial class Timestamp : ICustomDiagnosticMessage + public partial class Timestamp : ICustomDiagnosticMessage, IComparable { private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // Constants determined programmatically, but then hard-coded so they can be constant expressions. @@ -222,6 +222,109 @@ namespace Google.Protobuf.WellKnownTypes } } + /// + /// Given another timestamp, returns 0 if the timestamps are equivalent, -1 if this timestamp precedes the other, and 1 otherwise + /// + /// + /// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results. + /// + /// Timestamp to compare + /// an integer indicating whether this timestamp precedes or follows the other + public int CompareTo(Timestamp other) + { + return other == null ? 1 + : Seconds < other.Seconds ? -1 + : Seconds > other.Seconds ? 1 + : Nanos < other.Nanos ? -1 + : Nanos > other.Nanos ? 1 + : 0; + } + + /// + /// Compares two timestamps and returns whether the first is less than (chronologically precedes) the second + /// + /// + /// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results. + /// + /// + /// + /// true if a precedes b + public static bool operator <(Timestamp a, Timestamp b) + { + return a.CompareTo(b) < 0; + } + + /// + /// Compares two timestamps and returns whether the first is greater than (chronologically follows) the second + /// + /// + /// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results. + /// + /// + /// + /// true if a follows b + public static bool operator >(Timestamp a, Timestamp b) + { + return a.CompareTo(b) > 0; + } + + /// + /// Compares two timestamps and returns whether the first is less than (chronologically precedes) the second + /// + /// + /// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results. + /// + /// + /// + /// true if a precedes b + public static bool operator <=(Timestamp a, Timestamp b) + { + return a.CompareTo(b) <= 0; + } + + /// + /// Compares two timestamps and returns whether the first is greater than (chronologically follows) the second + /// + /// + /// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results. + /// + /// + /// + /// true if a follows b + public static bool operator >=(Timestamp a, Timestamp b) + { + return a.CompareTo(b) >= 0; + } + + + /// + /// Returns whether two timestamps are equivalent + /// + /// + /// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results. + /// + /// + /// + /// true if the two timestamps refer to the same nanosecond + public static bool operator ==(Timestamp a, Timestamp b) + { + return ReferenceEquals(a, b) || (a is null ? (b is null ? true : false) : a.Equals(b)); + } + + /// + /// Returns whether two timestamps differ + /// + /// + /// Make sure the timestamps are normalized. Comparing non-normalized timestamps is not specified and may give unexpected results. + /// + /// + /// + /// true if the two timestamps differ + public static bool operator !=(Timestamp a, Timestamp b) + { + return !(a == b); + } + /// /// Returns a string representation of this for diagnostic purposes. /// -- cgit v1.2.3