diff --git a/core/src/commonMain/kotlin/io/islandtime/ranges/DateTimeInterval.kt b/core/src/commonMain/kotlin/io/islandtime/ranges/DateTimeInterval.kt index 2a24eb06a..351c0345f 100644 --- a/core/src/commonMain/kotlin/io/islandtime/ranges/DateTimeInterval.kt +++ b/core/src/commonMain/kotlin/io/islandtime/ranges/DateTimeInterval.kt @@ -37,6 +37,11 @@ class DateTimeInterval( return start >= endExclusive } + override fun equals(other: Any?): Boolean { + return other is DateTimeInterval && (isEmpty() && other.isEmpty() || + start == other.start && endExclusive == other.endExclusive) + } + /** * Convert this interval to a string in ISO-8601 extended format. */ diff --git a/core/src/commonTest/kotlin/io/islandtime/ranges/DateTimeIntervalTest.kt b/core/src/commonTest/kotlin/io/islandtime/ranges/DateTimeIntervalTest.kt index ddcff3d4d..34880ab52 100644 --- a/core/src/commonTest/kotlin/io/islandtime/ranges/DateTimeIntervalTest.kt +++ b/core/src/commonTest/kotlin/io/islandtime/ranges/DateTimeIntervalTest.kt @@ -28,6 +28,23 @@ class DateTimeIntervalTest : AbstractIslandTimeTest() { assertFalse { DateTimeInterval.UNBOUNDED.hasBoundedEnd() } } + @Test + fun `equality works correctly`() { + assertEquals(DateTimeInterval.EMPTY, DateTimeInterval.EMPTY) + assertNotEquals(DateTimeInterval.UNBOUNDED, DateTimeInterval.EMPTY) + assertNotEquals(DateTimeInterval.EMPTY, DateTimeInterval.UNBOUNDED) + + val date1 = DateTime(2017, 1, 3, 30, 0, 0) + val date2 = DateTime(2017, 2, 3, 30, 0, 0) + val date3 = DateTime(2017, 3, 3, 30, 0, 0) + + assertEquals(DateTimeInterval.EMPTY, date2..date1) + assertNotEquals(date1..date2, date1..date3) + assertNotEquals(date1..date3, date1..date2) + assertNotEquals(date2..date3, date1..date3) + assertNotEquals(date1..date3, date2..date3) + } + @Test fun `inclusive end creation handles unbounded correctly`() { val start = Date(2019, Month.MARCH, 10) at Time.MIDNIGHT