Skip to content

Local date range #189

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

Closed
wants to merge 60 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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 @@ -583,6 +583,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 @@ -595,3 +608,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)
118 changes: 118 additions & 0 deletions core/common/src/LocalDateRange.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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

import kotlin.random.Random
import kotlin.random.nextInt

internal class LocalDateProgressionIterator(private val iterator: IntIterator) : Iterator<LocalDate> {
override fun hasNext(): Boolean = iterator.hasNext()
override fun next(): LocalDate = LocalDate.fromEpochDays(iterator.next())
}

public open class LocalDateProgression
internal constructor(internal val intProgression: IntProgression) : Iterable<LocalDate> {

internal constructor(
start: LocalDate,
endInclusive: LocalDate,
step: Int
) : this(IntProgression.fromClosedRange(start.toEpochDays(), endInclusive.toEpochDays(), step))

public val first: LocalDate = LocalDate.fromEpochDays(intProgression.first)
public val last: LocalDate = LocalDate.fromEpochDays(intProgression.last)

override fun iterator(): Iterator<LocalDate> = LocalDateProgressionIterator(intProgression.iterator())

public open fun isEmpty(): Boolean = intProgression.isEmpty()

override fun toString(): String = if (intProgression.step > 0) "$first..$last step ${intProgression.step}" else "$first downTo $last step ${intProgression.step}"

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

other as LocalDateProgression

return intProgression == other.intProgression
}

override fun hashCode(): Int {
return intProgression.hashCode()
}


public companion object {
public fun fromClosedRange(
rangeStart: LocalDate,
rangeEnd: LocalDate,
stepValue: Int,
stepUnit: DateTimeUnit.DayBased
): LocalDateProgression = LocalDateProgression(rangeStart, rangeEnd, stepValue * stepUnit.days)
}
}

public class LocalDateRange(start: LocalDate, endInclusive: LocalDate) : LocalDateProgression(start, endInclusive, 1), ClosedRange<LocalDate>, OpenEndRange<LocalDate> {
override val start: LocalDate get() = first
override val endInclusive: LocalDate get() = last
override val endExclusive: LocalDate get() = endInclusive.plus(1, DateTimeUnit.DAY)

@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)
}
}

public fun LocalDateProgression.first(): LocalDate {
if (isEmpty())
throw NoSuchElementException("Progression $this is empty.")
return this.first
}
public fun LocalDateProgression.last(): LocalDate {
if (isEmpty())
throw NoSuchElementException("Progression $this is empty.")
return this.last
}
public fun LocalDateProgression.firstOrNull(): LocalDate? = if (isEmpty()) null else this.first
public fun LocalDateProgression.lastOrNull(): LocalDate? = if (isEmpty()) null else this.last

public fun LocalDateProgression.reversed(): LocalDateProgression = LocalDateProgression(intProgression.reversed())

public fun LocalDateProgression.step(value: Int, unit: DateTimeUnit.DayBased) : LocalDateProgression = LocalDateProgression(intProgression.step(value * unit.days))
public fun LocalDateProgression.step(value: Long, unit: DateTimeUnit.DayBased) : LocalDateProgression = step(value.toInt(), unit)

public infix fun LocalDate.downTo(that: LocalDate) : LocalDateProgression = LocalDateProgression.fromClosedRange(this, that, -1, DateTimeUnit.DAY)
public infix fun LocalDate.downUntil(that: LocalDate) : LocalDateProgression = downTo(that.plus(1, DateTimeUnit.DAY))

public operator fun LocalDate.rangeTo(that: LocalDate): LocalDateRange = LocalDateRange(this, that)
public operator fun LocalDate.rangeUntil(that: LocalDate) : LocalDateRange = rangeTo(that.minus(1, DateTimeUnit.DAY))

public fun LocalDateProgression.random(randomIntFunction: (IntRange) -> Int) : LocalDate = intProgression.random(randomIntFunction)
.let(LocalDate.Companion::fromEpochDays)

public fun LocalDateProgression.random(random: Random = Random) : LocalDate = intProgression.random(random).let(LocalDate.Companion::fromEpochDays)

public fun LocalDateProgression.randomOrNull(randomIntFunction: (range: IntRange) -> Int) : LocalDate? = intProgression.randomOrNull(randomIntFunction)
?.let(LocalDate.Companion::fromEpochDays)

public fun LocalDateProgression.randomOrNull(random: Random = Random) : LocalDate? = intProgression.randomOrNull(random)
?.let(LocalDate.Companion::fromEpochDays)

public inline fun IntProgression.random(func: (IntRange) -> Int) : Int = func(0..(last - first) / step) * step + first

public fun IntProgression.random(random: Random = Random) : Int = random(random::nextInt)

public inline fun IntProgression.randomOrNull(randomIntFunction: (range: IntRange) -> Int) : Int? = if (isEmpty()) null else random(randomIntFunction)

public fun IntProgression.randomOrNull(random: Random = Random) : Int? = if (isEmpty()) null else random(random)
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
165 changes: 165 additions & 0 deletions core/common/test/LocalDateRangeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* 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.test

import kotlinx.datetime.*
import kotlin.random.Random
import kotlin.random.nextInt
import kotlin.test.*

class LocalDateRangeTest {
val Dec_24_1900 = LocalDate(1900, 12, 24)
val Dec_30_1999 = LocalDate(1999, 12, 30)
val Jan_01_2000 = LocalDate(2000, 1, 1)
val Jan_02_2000 = LocalDate(2000, 1, 2)
val Jan_05_2000 = LocalDate(2000, 1, 5)
val Jan_24_2000 = LocalDate(2000, 1, 24)
val Dec_24_2000 = LocalDate(2000, 12, 24)

@Test
fun emptyRange() {
assertTrue { (Jan_05_2000..Jan_01_2000).isEmpty() }
assertTrue { (Jan_01_2000 downTo Jan_05_2000).isEmpty() }
assertTrue { LocalDateRange.EMPTY.isEmpty() }
}

@Test
fun forwardRange() {
assertContentEquals(
(1..5).map { LocalDate(2000, 1, it) },
Jan_01_2000..Jan_05_2000
)
assertContentEquals(
(1..<5).map { LocalDate(2000, 1, it) },
Jan_01_2000..<Jan_05_2000
)
assertContentEquals(
listOf(Jan_01_2000),
Jan_01_2000..Jan_01_2000
)
assertContentEquals(
listOf(
LocalDate(1999, 12, 30),
LocalDate(1999, 12, 31),
LocalDate(2000, 1, 1),
LocalDate(2000, 1, 2)
),
Dec_30_1999..Jan_02_2000
)
}

@Test
fun backwardRange() {
assertContentEquals(
(5 downTo 1).map { LocalDate(2000, 1, it) },
Jan_05_2000 downTo Jan_01_2000
)
assertContentEquals(
(5 downTo 2).map { LocalDate(2000, 1, it) },
Jan_05_2000 downUntil Jan_01_2000
)
assertContentEquals(
listOf(Jan_01_2000),
Jan_01_2000 downTo Jan_01_2000
)
assertContentEquals(
listOf(
LocalDate(2000, 1, 2),
LocalDate(2000, 1, 1),
LocalDate(1999, 12, 31),
LocalDate(1999, 12, 30),
),
Jan_02_2000 downTo Dec_30_1999
)
}

@Test
fun step() {
assertContentEquals(
(1..24 step 2).map { LocalDate(2000, 1, it) },
(Jan_01_2000..Jan_24_2000).step(2, DateTimeUnit.DAY)
)
assertContentEquals(
(24 downTo 1 step 2).map { LocalDate(2000, 1, it) },
(Jan_24_2000 downTo Jan_01_2000).step(2, DateTimeUnit.DAY)
)
}

@Test
fun string() {
assertEquals(
"2000-01-01..2000-01-05",
(Jan_01_2000..Jan_05_2000).toString()
)
assertEquals(
"2000-01-05 downTo 2000-01-01 step -1",
(Jan_05_2000 downTo Jan_01_2000).toString()
)
assertEquals(
"2000-01-01..2000-01-05 step 1",
LocalDateProgression.fromClosedRange(Jan_01_2000, Jan_05_2000, 1, DateTimeUnit.DAY).toString()
)
assertEquals(
"2000-01-05 downTo 2000-01-01 step -1",
LocalDateProgression.fromClosedRange(Jan_05_2000, Jan_01_2000, -1, DateTimeUnit.DAY).toString()
)
}

@Test
fun random() {
assertEquals(
Jan_01_2000,
(Jan_01_2000..Jan_01_2000).random()
)

assertEquals(
Jan_01_2000,
(Jan_01_2000 downTo Jan_01_2000).random()
)

assertFails {
(Jan_02_2000..Jan_01_2000).random()
}

assertNull((Jan_02_2000..Jan_01_2000).randomOrNull())

val seed = 123456
val expectedRand = Random(seed)
val actualRand = Random(seed)

repeat(20) {
assertEquals(
expectedRand.nextInt(0..23).let { Jan_01_2000.plus(it, DateTimeUnit.DAY) },
(Jan_01_2000..Jan_24_2000).random(actualRand)
)
}

repeat(20) {
assertEquals(
expectedRand.nextInt(0..23).let { Jan_24_2000.minus(it, DateTimeUnit.DAY) },
(Jan_24_2000 downTo Jan_01_2000).random(actualRand)
)
}

listOf(1, 2, 5, 30).forEach { step ->
repeat(20) {
val range = (0..23 step step)
assertEquals(
expectedRand.nextInt(0..range.last / step).let { Jan_01_2000.plus(it * step, DateTimeUnit.DAY) },
(Jan_01_2000..Jan_24_2000).step(step, DateTimeUnit.DAY).random(actualRand)
)
}

repeat(20) {
val range = (0..23 step step)
assertEquals(
expectedRand.nextInt(0..range.last / step).let { Jan_24_2000.minus(it * step, DateTimeUnit.DAY) },
(Jan_24_2000 downTo Jan_01_2000).step(step, DateTimeUnit.DAY).random(actualRand)
)
}
}
}
}