Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ord and partialOrd to Timestamp #994

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions prost-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,15 @@ impl std::hash::Hash for Timestamp {
}
}

impl Ord for Timestamp {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.seconds.cmp(&other.seconds) {
std::cmp::Ordering::Equal => self.nanos.cmp(&other.nanos),
ordering => ordering,
}
}
}

#[cfg(feature = "std")]
impl From<std::time::SystemTime> for Timestamp {
fn from(system_time: std::time::SystemTime) -> Timestamp {
Expand Down Expand Up @@ -907,4 +916,38 @@ mod tests {
// Must contain at least one "/" character.
assert_eq!(TypeUrl::new("google.protobuf.Duration"), None);
}

#[test]
fn check_timestamp_ord() {
let earlier_timestamp = Timestamp {
seconds: 10,
nanos: 500,
};
let later_timestamp = Timestamp {
seconds: 10,
nanos: 700,
};
let equal_timestamp = Timestamp {
seconds: 10,
nanos: 500,
};

assert!(earlier_timestamp < later_timestamp);
assert!(earlier_timestamp <= later_timestamp);
assert!(later_timestamp > earlier_timestamp);
assert!(later_timestamp >= earlier_timestamp);

assert_eq!(
earlier_timestamp.cmp(&later_timestamp),
std::cmp::Ordering::Less
);
assert_eq!(
later_timestamp.cmp(&earlier_timestamp),
std::cmp::Ordering::Greater
);
assert_eq!(
equal_timestamp.cmp(&earlier_timestamp),
std::cmp::Ordering::Equal
);
}
}
2 changes: 1 addition & 1 deletion prost-types/src/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2293,7 +2293,7 @@ impl NullValue {
/// the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use
/// the Joda Time's [`ISODateTimeFormat.dateTime()`](<http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%2D%2D>) to obtain a formatter capable of generating timestamps in this format.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
#[derive(Clone, PartialEq, PartialOrd,::prost::Message)]
pub struct Timestamp {
/// Represents seconds of UTC time since Unix epoch
/// 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
Expand Down
Loading