Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 31 additions & 1 deletion temporal_capi/src/plain_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod ffi {
use crate::plain_time::ffi::PlainTime;
use crate::plain_year_month::ffi::PlainYearMonth;
use diplomat_runtime::{DiplomatOption, DiplomatStrSlice, DiplomatWrite};
use std::cmp::Ordering;
use std::fmt::Write;

#[diplomat::opaque]
Expand Down Expand Up @@ -158,7 +159,36 @@ pub mod ffi {
pub fn equals(&self, other: &Self) -> bool {
self.0 == other.0
}


pub fn compare(one: &Self, two: &Self) -> i32 {
Self::compare_iso_date(
one.iso_year(),
one.iso_month(),
one.iso_day(),
two.iso_year(),
two.iso_month(),
two.iso_day(),
)
}

pub fn compare_iso_date(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the library's compare function. Don't write your own. only write a thing wrapper.

year1: i32,
month1: u8,
day1: u8,
year2: i32,
month2: u8,
day2: u8,
) -> i32 {
match (year1.cmp(&year2), month1.cmp(&month2), day1.cmp(&day2)) {
(Ordering::Greater, _, _) => 1,
(Ordering::Less, _, _) => -1,
(_, Ordering::Greater, _) => 1,
(_, Ordering::Less, _) => -1,
(_, _, Ordering::Greater) => 1,
(_, _, Ordering::Less) => -1,
_ => 0,
}
}
pub fn year(&self) -> i32 {
self.0.year()
}
Expand Down
66 changes: 65 additions & 1 deletion temporal_capi/src/plain_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,71 @@ pub mod ffi {
pub fn equals(&self, other: &Self) -> bool {
self.0 == other.0
}


pub fn compare(one: &Self, two: &Self) -> i32 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: I believe there should be a simpler way to implement these.

The built-ins that have an IsoDate internally should have a method called compare_iso that does this work for you.

The exception to this I believe is the time oriented types, which should implement PartialOrd and Ord. In these cases, you can use Ord's method cmp and cast Ordering into an i8. Although, ZonedDateTime implements a compare_instant method instead of an Ord implementation.

Self::compare_iso_plain_date_time(
one.iso_year(),
one.iso_month(),
one.iso_day(),
one.hour(),
one.minute(),
one.second(),
one.millisecond(),
one.microsecond(),
one.nanosecond(),
two.iso_year(),
two.iso_month(),
two.iso_day(),
two.hour(),
two.minute(),
two.second(),
two.millisecond(),
two.microsecond(),
two.nanosecond(),
)
}

pub fn compare_iso_plain_date_time(
year1: i32,
month1: u8,
day1: u8,
hour1: u8,
minute1: u8,
second1: u8,
millisecond1: u16,
microsecond1: u16,
nanosecond1: u16,
year2: i32,
month2: u8,
day2: u8,
hour2: u8,
minute2: u8,
second2: u8,
millisecond2: u16,
microsecond2: u16,
nanosecond2: u16,
) -> i32 {
let comparisons = [
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: no, this should just use (year1, month1, ...).cmp((year2, month2, ...))

year1.cmp(&year2),
month1.cmp(&month2),
day1.cmp(&day2),
hour1.cmp(&hour2),
minute1.cmp(&minute2),
second1.cmp(&second2),
millisecond1.cmp(&millisecond2),
microsecond1.cmp(&microsecond2),
nanosecond1.cmp(&nanosecond2),
];
comparisons
.iter()
.find(|&&ord| ord != std::cmp::Ordering::Equal)
.map_or(0, |ord| match ord {
std::cmp::Ordering::Greater => 1,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: Ordering is repr(i8), so you should be able to cast it. Somthing like: i32::from(ord as i8)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also diplomat supports returning Ordering over FFI, since it supports custom comparators.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooooooooh, good to know!

std::cmp::Ordering::Less => -1,
std::cmp::Ordering::Equal => 0,
})
}

pub fn round(&self, options: RoundingOptions) -> Result<Box<Self>, TemporalError> {
self.0
.round(options.try_into()?)
Expand Down
26 changes: 25 additions & 1 deletion temporal_capi/src/plain_month_day.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod ffi {
use crate::plain_date::ffi::{PartialDate, PlainDate};

use diplomat_runtime::DiplomatWrite;
use std::cmp::Ordering;
use std::fmt::Write;

#[diplomat::opaque]
Expand Down Expand Up @@ -47,7 +48,30 @@ pub mod ffi {
pub fn equals(&self, other: &Self) -> bool {
self.0 == other.0
}


pub fn compare(one: &Self, two: &Self) -> i32 {
Self::compare_iso_month_day(
one.iso_month(),
one.iso_day(),
two.iso_month(),
two.iso_day(),
)
}

/// Compares two ISO month-day pairs and returns -1, 0, or 1.
pub fn compare_iso_month_day(month1: u8, day1: u8, month2: u8, day2: u8) -> i32 {
if month1 > month2 {
1
} else if month1 < month2 {
-1
} else if day1 > day2 {
1
} else if day1 < day2 {
-1
} else {
0
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: PlainMonthDay does not have a compare function

}
pub fn iso_year(&self) -> i32 {
self.0.iso_year()
}
Expand Down
48 changes: 48 additions & 0 deletions temporal_capi/src/plain_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,54 @@ pub mod ffi {
pub fn equals(&self, other: &Self) -> bool {
self.0 == other.0
}
pub fn compare(one: &Self, two: &Self) -> i32 {
Self::compare_iso_time(
one.hour(),
one.minute(),
one.second(),
one.millisecond(),
one.microsecond(),
one.nanosecond(),
two.hour(),
two.minute(),
two.second(),
two.millisecond(),
two.microsecond(),
two.nanosecond(),
)
}

pub fn compare_iso_time(
hour1: u8,
minute1: u8,
second1: u8,
millisecond1: u16,
microsecond1: u16,
nanosecond1: u16,
hour2: u8,
minute2: u8,
second2: u8,
millisecond2: u16,
microsecond2: u16,
nanosecond2: u16,
) -> i32 {
let comparisons = [
hour1.cmp(&hour2),
minute1.cmp(&minute2),
second1.cmp(&second2),
millisecond1.cmp(&millisecond2),
microsecond1.cmp(&microsecond2),
nanosecond1.cmp(&nanosecond2),
];
comparisons
.iter()
.find(|&&ord| ord != std::cmp::Ordering::Equal)
.map_or(0, |ord| match ord {
std::cmp::Ordering::Greater => 1,
std::cmp::Ordering::Less => -1,
std::cmp::Ordering::Equal => 0,
})
}
pub fn round(
&self,
smallest_unit: Unit,
Expand Down
22 changes: 22 additions & 0 deletions temporal_capi/src/plain_year_month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ pub mod ffi {
pub fn equals(&self, other: &Self) -> bool {
self.0 == other.0
}
pub fn compare(one: &Self, two: &Self) -> i32 {
Self::compare_iso_year_month(
one.iso_year(),
one.iso_month(),
two.iso_year(),
two.iso_month(),
)
}

pub fn compare_iso_year_month(year1: i32, month1: u8, year2: i32, month2: u8) -> i32 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: write this as a match on (year1, month1).cmp((year2, month2)) instead

And link to the spec entry

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, thought: I don't think it would be a bad idea to just have a method return std::cmp::Ordering here, that converts to the integer anyway, and makes it easier to add comparator support later

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should i do this for all the new compare funcitons?

if year1 > year2 {
1
} else if year1 < year2 {
-1
} else if month1 > month2 {
1
} else if month1 < month2 {
-1
} else {
0
}
}
pub fn to_plain_date(&self) -> Result<Box<PlainDate>, TemporalError> {
self.0
.to_plain_date()
Expand Down
Loading