Skip to content

Commit feebb44

Browse files
committed
Add YearMonth.days: range of dates in a given month
1 parent 4e63121 commit feebb44

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

core/common/src/YearMonth.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public constructor(year: Int, month: Int) : Comparable<YearMonth> {
115115
*
116116
* @sample kotlinx.datetime.test.samples.YearMonthSamples.days
117117
*/
118-
// val days: LocalDateRange get() = firstDay..lastDay // no ranges yet
118+
public val days: LocalDateRange
119119

120120
/**
121121
* Constructs a [YearMonth] instance from the given year-month components.

core/common/test/YearMonthTest.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,18 @@ class YearMonthTest {
165165
}
166166

167167
@Test
168-
fun firstAndLastDay() {
168+
fun daysRange() {
169+
@OptIn(ExperimentalStdlibApi::class)
169170
fun test(year: Int, month: Int) {
170171
val yearMonth = YearMonth(year, month)
171172
assertEquals(LocalDate(year, month, 1), yearMonth.firstDay)
172173
assertEquals(LocalDate(year, month, yearMonth.numberOfDays), yearMonth.lastDay)
173174
assertEquals(yearMonth.plusMonth().firstDay, yearMonth.lastDay.plus(1, DateTimeUnit.DAY))
175+
assertEquals(LocalDateRange(yearMonth.firstDay, yearMonth.lastDay), yearMonth.days)
176+
assertContains(yearMonth.days as OpenEndRange<LocalDate>, yearMonth.firstDay)
177+
assertContains(yearMonth.days as OpenEndRange<LocalDate>, yearMonth.lastDay)
178+
assertFalse(yearMonth.days.contains(yearMonth.firstDay.minus(1, DateTimeUnit.DAY)))
179+
assertFalse(yearMonth.days.contains(yearMonth.lastDay.plus(1, DateTimeUnit.DAY)))
174180
}
175181
for (month in 1..12) {
176182
for (year in 2000..2005) {

core/common/test/samples/YearMonthSamples.kt

+16
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ class YearMonthSamples {
7373
}
7474
}
7575

76+
@Test
77+
fun days() {
78+
// Getting the range of days in a YearMonth
79+
val yearMonth = YearMonth(2024, Month.APRIL)
80+
// The range consists of all the days in the month:
81+
check(yearMonth.days.size == 30)
82+
check(yearMonth.days.first() == LocalDate(2024, Month.APRIL, 1))
83+
check(yearMonth.days.last() == LocalDate(2024, Month.APRIL, 30))
84+
check(yearMonth.days.contains(LocalDate(2024, Month.APRIL, 15)))
85+
// The range allows iterating over the days:
86+
for (day in yearMonth.days) {
87+
check(day.month == Month.APRIL)
88+
check(day.year == 2024)
89+
}
90+
}
91+
7692
@Test
7793
fun compareToSample() {
7894
// Comparing YearMonth values

core/commonKotlin/src/YearMonth.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public actual constructor(public actual val year: Int, month: Int) : Comparable<
3030

3131
public actual val numberOfDays: Int get() = monthNumber.monthLength(isLeapYear(year))
3232

33-
// val days: LocalDateRange get() = firstDay..lastDay // no ranges yet
33+
public actual val days: LocalDateRange get() = firstDay..lastDay // no ranges yet
3434

3535
public actual constructor(year: Int, month: Month): this(year, month.number)
3636

core/jvm/src/YearMonthJvm.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public actual class YearMonth internal constructor(
2626
public actual val firstDay: LocalDate get() = LocalDate(value.atDay(1))
2727
public actual val lastDay: LocalDate get() = LocalDate(value.atEndOfMonth())
2828
public actual val numberOfDays: Int get() = value.lengthOfMonth()
29-
30-
// val days: LocalDateRange get() = firstDay..lastDay // no ranges yet
29+
public actual val days: LocalDateRange get() = firstDay..lastDay // no ranges yet
3130

3231
public actual constructor(year: Int, month: Int): this(try {
3332
jtYearMonth.of(year, month)

0 commit comments

Comments
 (0)