Skip to content

Commit 2e6750a

Browse files
authored
Implement compare functionality and some more traits (#82)
This adds some `compare` functions to `Date` and `DateTime`, and Ordering for `ZonedDateTime`. The compare functions are notably different from `Eq` and `PartialEq` as they compare the internal ISO representation ignoring the Calendar
1 parent 44ed454 commit 2e6750a

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/components/date.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ pub struct Date {
3131
calendar: Calendar,
3232
}
3333

34+
impl Ord for Date {
35+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
36+
self.iso.cmp(&other.iso)
37+
}
38+
}
39+
40+
impl PartialOrd for Date {
41+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
42+
Some(self.cmp(other))
43+
}
44+
}
45+
3446
// ==== Private API ====
3547

3648
impl Date {

src/components/datetime.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ pub struct DateTime {
2828
calendar: Calendar,
2929
}
3030

31+
impl Ord for DateTime {
32+
fn cmp(&self, other: &Self) -> Ordering {
33+
self.iso.cmp(&other.iso)
34+
}
35+
}
36+
37+
impl PartialOrd for DateTime {
38+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
39+
Some(self.cmp(other))
40+
}
41+
}
42+
3143
// ==== Private DateTime API ====
3244

3345
impl DateTime {

src/components/tz.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ pub const TIME_ZONE_PROPERTIES: [&str; 3] =
1313
["getOffsetNanosecondsFor", "getPossibleInstantsFor", "id"];
1414

1515
/// A Temporal `TimeZone`.
16-
#[derive(Debug, Clone)]
17-
#[allow(unused)]
16+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
1817
pub struct TimeZone {
1918
pub(crate) iana: Option<String>, // TODO: ICU4X IANA TimeZone support.
2019
pub(crate) offset: Option<i16>,

src/components/zoneddatetime.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,25 @@ use super::calendar::CalendarDateLike;
1212

1313
/// The native Rust implementation of `Temporal.ZonedDateTime`.
1414
#[non_exhaustive]
15-
#[derive(Debug, Clone)]
15+
#[derive(Debug, Clone, PartialEq, Eq)]
1616
pub struct ZonedDateTime {
1717
instant: Instant,
1818
calendar: Calendar,
1919
tz: TimeZone,
2020
}
2121

22+
impl Ord for ZonedDateTime {
23+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
24+
self.instant.cmp(&other.instant)
25+
}
26+
}
27+
28+
impl PartialOrd for ZonedDateTime {
29+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
30+
Some(self.cmp(other))
31+
}
32+
}
33+
2234
// ==== Private API ====
2335

2436
impl ZonedDateTime {

0 commit comments

Comments
 (0)