Skip to content

Commit 90014d5

Browse files
authored
Add IComparable and IEquatable to WellKnownTypes.Timestamp (#1165)
1 parent 21d5c30 commit 90014d5

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

‎src/protobuf-net.Core/WellKnownTypes/Timestamp.cs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace ProtoBuf.WellKnownTypes
4747
/// </summary>
4848
[ProtoContract(Name = ".google.protobuf.Timestamp", Serializer = typeof(PrimaryTypeProvider), Origin = "google/protobuf/timestamp.proto")]
4949
[StructLayout(LayoutKind.Auto)]
50-
public readonly struct Timestamp
50+
public readonly struct Timestamp : IComparable<Timestamp>, IEquatable<Timestamp>
5151
{
5252
/// <summary>
5353
/// Represents seconds of UTC time since Unix epoch
@@ -89,6 +89,18 @@ public Timestamp Normalize()
8989
/// <summary>Converts a Timestamp to a DateTime</summary>
9090
public DateTime AsDateTime() => TimestampEpoch.AddTicks(PrimaryTypeProvider.ToTicks(Seconds, Nanoseconds));
9191

92+
public int CompareTo(Timestamp other)
93+
{
94+
if (Seconds != other.Seconds)
95+
{
96+
return Seconds.CompareTo(other.Seconds);
97+
}
98+
99+
return Nanoseconds.CompareTo(other.Nanoseconds);
100+
}
101+
102+
public bool Equals(Timestamp other) => Seconds == other.Seconds && Nanoseconds == other.Nanoseconds;
103+
92104
/// <summary>Converts a Timestamp to a DateTime</summary>
93105
public static implicit operator DateTime(Timestamp value) => value.AsDateTime();
94106

0 commit comments

Comments
 (0)