Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
ac5ec5f
Add minus operator to DatePeriod and DateTimePeriod
PeterAttardo Mar 2, 2022
a962bde
Add unary minus operator to DatePeriod and DateTimePeriod
PeterAttardo Mar 2, 2022
740434f
Add LocalDateRange class and associated tests. Exists to enable Kotlin
PeterAttardo Mar 2, 2022
2ea4cdb
Add tests for minus and unaryMinus operators for DatePeriod and
PeterAttardo Mar 2, 2022
d3ab645
Merge remote-tracking branch 'origin/master' into local-date-range
PeterAttardo Sep 3, 2024
59bd4fd
Rework LocalDateRange to match proposed spec
PeterAttardo Sep 4, 2024
e249b20
Revert "Add tests for minus and unaryMinus operators for DatePeriod and"
PeterAttardo Sep 5, 2024
2e0b957
Revert "Add unary minus operator to DatePeriod and DateTimePeriod"
PeterAttardo Sep 5, 2024
375b6da
Revert "Add minus operator to DatePeriod and DateTimePeriod"
PeterAttardo Sep 5, 2024
9a7c507
Make LocalDateProgressionIterator private
PeterAttardo Sep 5, 2024
33222c6
Specify that steps are in days in string representation of LocalDateP…
PeterAttardo Sep 5, 2024
372fa1b
Add deprecation warning to endExclusive of LocalDateRange
PeterAttardo Sep 5, 2024
72fa3a5
Remove downUntil from LocalDateRange
PeterAttardo Sep 5, 2024
df405c7
Simplify LocalDateProgression.random()
PeterAttardo Sep 5, 2024
f2d2566
Use safeMultiply in LocalDateRange
PeterAttardo Sep 5, 2024
fd1179a
Make fromClosedRange internal
PeterAttardo Sep 5, 2024
01455c6
Simplify equals method in LocalDateProgression
PeterAttardo Sep 5, 2024
358e60d
Add to/from epoch days in Long to LocalDate
PeterAttardo Sep 9, 2024
4911221
Correct LocalDateRangeTest
PeterAttardo Sep 9, 2024
430bfdc
Switch from IntProgression to LongProgression in LocalDateProgression
PeterAttardo Sep 9, 2024
77c9ab1
Delete downUntil test
PeterAttardo Sep 9, 2024
c04b640
Samples and documentation for LocalDateRange
PeterAttardo Sep 9, 2024
bb7991f
Simplify LocalDateRange.EMPTY
PeterAttardo Sep 10, 2024
7bff3cc
Fix logic of LongProgression.contains
PeterAttardo Sep 10, 2024
c8f88ab
Move LocalDate range operators from extension functions to class methods
PeterAttardo Sep 10, 2024
22d263c
Add tests for LocalDateRange
PeterAttardo Sep 10, 2024
0042bfa
Return EMPTY in case of rangeUntil LocalDate.MIN
PeterAttardo Sep 16, 2024
fad37f9
Add tests for non-1 step in LocalDateProgression.getSize()
PeterAttardo Sep 16, 2024
d0265f2
Add test for LocalDateProgression.random()
PeterAttardo Sep 16, 2024
ca43825
Merge remote-tracking branch 'origin/master' into local-date-range
PeterAttardo Sep 16, 2024
66a058b
Update core/commonKotlin/src/LocalDate.kt
PeterAttardo Oct 5, 2024
4255bf3
Reduce duplication
PeterAttardo Oct 5, 2024
0880589
Update LocalDateRangeSamples
PeterAttardo Oct 5, 2024
56ca1ae
Update core/commonJs/src/LocalDate.kt
PeterAttardo Oct 7, 2024
63a4c42
Update core/commonKotlin/src/LocalDate.kt
PeterAttardo Oct 7, 2024
614038d
Update core/common/test/LocalDateRangeTest.kt
PeterAttardo Oct 15, 2024
b5db45c
Update core/common/test/LocalDateRangeTest.kt
PeterAttardo Oct 15, 2024
cc9d7a1
Update core/common/test/LocalDateRangeTest.kt
PeterAttardo Oct 15, 2024
a4a2ead
Update core/common/test/LocalDateRangeTest.kt
PeterAttardo Oct 15, 2024
9ba1ac1
Update core/common/src/LocalDateRange.kt
PeterAttardo Oct 15, 2024
53f6227
Fix LocalDateRange test
PeterAttardo Oct 15, 2024
f4b5bc0
Add tests to last() for LocalDateRangeTest
PeterAttardo Oct 15, 2024
89101cb
Add documentation to LocalDateRange.EMPTY
PeterAttardo Oct 15, 2024
fef8e59
Update core/common/src/LocalDateRange.kt
PeterAttardo Oct 16, 2024
5e97217
Update core/common/src/LocalDateRange.kt
PeterAttardo Oct 16, 2024
1f2ac18
Update core/common/src/LocalDateRange.kt
PeterAttardo Oct 16, 2024
37a213e
Update core/common/src/LocalDateRange.kt
PeterAttardo Oct 16, 2024
28e1806
Word choice in comments
PeterAttardo Oct 16, 2024
d161989
Add safeMultiplyOrClamp
PeterAttardo Oct 16, 2024
dbac356
Clamp steps that overflow Long
PeterAttardo Oct 16, 2024
56c042b
Unused variables
PeterAttardo Oct 16, 2024
163f585
Import clampToInt for js
PeterAttardo Oct 17, 2024
405f034
Fix getSize test in LocalDateRangeTest
PeterAttardo Oct 17, 2024
179478e
Make LocalDateRangeTest stricter
PeterAttardo Oct 22, 2024
b16b329
Add equals and hashcode tests to LocalDateRangeTest
PeterAttardo Oct 22, 2024
fe39e5c
Fix whitespace
PeterAttardo Nov 1, 2024
6f67327
Unify parameter name for LocalDateRange.contains() and LocalDateProgr…
PeterAttardo Nov 1, 2024
d29d53d
Merge remote-tracking branch 'upstream/master' into local-date-range
PeterAttardo Nov 1, 2024
b6d56ce
Add type checks for unexpected variance in LocalDateProgression
PeterAttardo Nov 1, 2024
c58c61f
Switch LocalDateRange.random() to throw NoSuchElementException instea…
PeterAttardo Nov 4, 2024
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
25 changes: 25 additions & 0 deletions core/common/src/DateTimePeriod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,19 @@ public operator fun DateTimePeriod.plus(other: DateTimePeriod): DateTimePeriod =
safeAdd(totalNanoseconds, other.totalNanoseconds),
)

/**
* Subtracts one [DateTimePeriod] instance from another.
*
* @throws DateTimeArithmeticException if arithmetic overflow happens.
*/
public operator fun DateTimePeriod.minus(other: DateTimePeriod) : DateTimePeriod = buildDateTimePeriod(
safeAdd(totalMonths, -other.totalMonths),
safeAdd(days, -other.days),
safeAdd(totalNanoseconds, -other.totalNanoseconds)
)

public operator fun DateTimePeriod.unaryMinus(): DateTimePeriod = buildDateTimePeriod(-totalMonths, -days, -totalNanoseconds)

/**
* Adds two [DatePeriod] instances.
*
Expand All @@ -457,3 +470,15 @@ public operator fun DatePeriod.plus(other: DatePeriod): DatePeriod = DatePeriod(
safeAdd(totalMonths, other.totalMonths),
safeAdd(days, other.days),
)

/**
* Subtracts one [DatePeriod] instance from another.
*
* @throws DateTimeArithmeticException if arithmetic overflow happens.
*/
public operator fun DatePeriod.minus(other: DatePeriod): DatePeriod = DatePeriod(
safeAdd(totalMonths, -other.totalMonths),
safeAdd(days, -other.days)
)

public operator fun DatePeriod.unaryMinus(): DatePeriod = DatePeriod(-totalMonths, -days)
109 changes: 109 additions & 0 deletions core/common/src/LocalDateRange.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2019-2022 JetBrains s.r.o.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/

package kotlinx.datetime

public abstract class LocalDateIterator : Iterator<LocalDate> {
final override fun next(): LocalDate = nextLocalDate()
public abstract fun nextLocalDate(): LocalDate
}

internal class LocalDateProgressionIterator(first: LocalDate, last: LocalDate, val step: DatePeriod) : LocalDateIterator() {
private val finalElement: LocalDate = last
private val increasing = step.positive()
private var hasNext: Boolean = if (increasing) first <= last else first >= last
private var next: LocalDate = if (hasNext) first else finalElement

override fun hasNext(): Boolean = hasNext

override fun nextLocalDate(): LocalDate {
if(!hasNext) throw NoSuchElementException()
val value = next
next += step
/**
* Some [DatePeriod]s with opposite-signed constituent parts can get stuck in an infinite loop rather than progressing toward the far future or far past.
* A period of P1M-28D for example, when added to any date in February, will return that same date, thus leading to a loop.
*/
if(next == value) throw IllegalStateException("Progression has hit an infinite loop. Check to ensure that the the values for total months and days in the provided step DatePeriod are not equal and opposite for certain month(s).")
if ((increasing && next > finalElement) || (!increasing && next < finalElement)) {
hasNext = false
}
return value
}
}

public open class LocalDateProgression
internal constructor
(
start: LocalDate,
endInclusive: LocalDate,
public val step: DatePeriod
) : Iterable<LocalDate> {
init {
if(!step.positive() && !step.negative()) throw IllegalArgumentException("Provided step DatePeriod is of size zero (or equivalent over an arbitrarily long timeline)")
}
public val first: LocalDate = start
public val last: LocalDate = endInclusive

override fun iterator(): LocalDateIterator = LocalDateProgressionIterator(first, last, step)

public open fun isEmpty(): Boolean = if (step.positive()) first > last else first < last


override fun toString(): String = if (step.positive()) "$first..$last step $step" else "$first downTo $last step $step"

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false

other as LocalDateProgression

if (first != other.first) return false
if (last != other.last) return false
if (step != other.step) return false

return true
}

override fun hashCode(): Int {
var result = first.hashCode()
result = 31 * result + last.hashCode()
result = 31 * result + step.hashCode()
return result
}

public companion object {
public fun fromClosedRange(rangeStart: LocalDate, rangeEnd: LocalDate, step: DatePeriod): LocalDateProgression = LocalDateProgression(rangeStart, rangeEnd, step)
}
}

public class LocalDateRange(start: LocalDate, endInclusive: LocalDate) : LocalDateProgression(start, endInclusive, DatePeriod(days = 1)), ClosedRange<LocalDate> {
override val start: LocalDate get() = first
override val endInclusive: LocalDate get() = last

@Suppress("ConvertTwoComparisonsToRangeCheck")
override fun contains(value: LocalDate): Boolean = first <= value && value <= last

override fun isEmpty(): Boolean = first > last

override fun toString(): String = "$first..$last"

public companion object {
private val DATE_ONE = LocalDate(1, 1, 1)
private val DATE_TWO = LocalDate(1, 1, 2)
public val EMPTY: LocalDateRange = LocalDateRange(DATE_TWO, DATE_ONE)
}
}

/**
* On an arbitrarily long timeline, the average month will be 30.436875 days long (146097 days over 400 years).
*/
public fun DatePeriod.positive() : Boolean = totalMonths * 30.436875 + days > 0
public fun DatePeriod.negative() : Boolean = totalMonths * 30.436875 + days < 0

public infix fun LocalDateProgression.step(step: DatePeriod) : LocalDateProgression = LocalDateProgression.fromClosedRange(first, last, step)
public infix fun LocalDate.downTo(that: LocalDate) : LocalDateProgression = LocalDateProgression.fromClosedRange(this, that, DatePeriod(days = -1))

public operator fun LocalDate.rangeTo(that: LocalDate): LocalDateRange = LocalDateRange(this, that)
10 changes: 10 additions & 0 deletions core/common/test/DateTimePeriodTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,19 @@ class DateTimePeriodTest {
assertEquals(DatePeriod(years = 2, months = 12), dp1 + dp1)
assertEquals(DateTimePeriod(years = 1, months = 6, days = 3), p2 + dp1)

assertEquals(p1, DateTimePeriod(years=10, days=3, hours=2) - p2 -p3)
assertEquals(p1, DatePeriod(years = 11, months = 6) - dp1)
assertEquals(dp1, DatePeriod(years = 2, months = 12) - dp1)
assertEquals(p2, DateTimePeriod(years = 1, months = 6, days = 3) - dp1)

val dp2 = dp1 + p3 + p4
val dp3 = dp2 - p3 -p4
assertEquals(dp1, dp2)
assertEquals(dp1, dp3)
assertTrue(dp2 is DatePeriod)
assertTrue(dp3 is DatePeriod)
assertEquals(DateTimePeriod(years = -10, days=-3, hours = -2), -(p1 + p2 + p3))
assertEquals(DatePeriod(years = -11, months = -6), -(dp1 + p1))
}

@Test
Expand Down
Loading