diff --git a/src/main/kotlin/me/moallemi/tools/daterange/date/DateExt.kt b/src/main/kotlin/me/moallemi/tools/daterange/date/DateExt.kt new file mode 100644 index 0000000..bf73416 --- /dev/null +++ b/src/main/kotlin/me/moallemi/tools/daterange/date/DateExt.kt @@ -0,0 +1,5 @@ +package me.moallemi.tools.daterange.date + +import java.util.Date + +operator fun Date.rangeTo(other: Date) = DateRange(this, other) diff --git a/src/main/kotlin/me/moallemi/tools/daterange/date/DateIterator.kt b/src/main/kotlin/me/moallemi/tools/daterange/date/DateIterator.kt new file mode 100644 index 0000000..0e6b483 --- /dev/null +++ b/src/main/kotlin/me/moallemi/tools/daterange/date/DateIterator.kt @@ -0,0 +1,27 @@ +package me.moallemi.tools.daterange.date + +import java.util.Calendar +import java.util.Date +import java.util.Locale + +class DateIterator( + startDate: Date, + private val endDate: Date, + private val stepDays: Int +) : Iterator { + + private val calendar = Calendar.getInstance(Locale.getDefault()) + private var currentDate = startDate + + override fun hasNext() = currentDate <= endDate + + override fun next(): Date { + val next = currentDate + + calendar.time = currentDate + calendar.add(Calendar.DATE, stepDays) + currentDate = calendar.time + + return next + } +} diff --git a/src/main/kotlin/me/moallemi/tools/daterange/date/DateRange.kt b/src/main/kotlin/me/moallemi/tools/daterange/date/DateRange.kt new file mode 100644 index 0000000..11646a2 --- /dev/null +++ b/src/main/kotlin/me/moallemi/tools/daterange/date/DateRange.kt @@ -0,0 +1,15 @@ +package me.moallemi.tools.daterange.date + +import java.util.Date + +class DateRange( + override val start: Date, + override val endInclusive: Date, + private val stepDays: Int = 1 +) : Iterable, ClosedRange { + + override fun iterator(): Iterator = + DateIterator(start, endInclusive, stepDays) + + infix fun step(days: Int) = DateRange(start, endInclusive, days) +} diff --git a/src/test/kotlin/me/moallemi/tools/daterange/date/DateRangeTest.kt b/src/test/kotlin/me/moallemi/tools/daterange/date/DateRangeTest.kt new file mode 100644 index 0000000..cc1ff06 --- /dev/null +++ b/src/test/kotlin/me/moallemi/tools/daterange/date/DateRangeTest.kt @@ -0,0 +1,53 @@ +package me.moallemi.tools.daterange.date + +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Locale +import junit.framework.TestCase.assertEquals +import org.junit.Test + +class DateRangeTest { + + private val calendar = Calendar.getInstance(Locale.getDefault()) + private val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) + + @Test + fun testRange() { + val expected = listOf( + "2020-01-01", + "2020-01-02", + "2020-01-03", + "2020-01-04", + "2020-01-05" + ) + calendar.set(2020, 0, 1) + val startDate = calendar.time + calendar.set(2020, 0, 5) + val endDate = calendar.time + + val actual = (startDate..endDate).map { + simpleDateFormat.format(it) + } + + assertEquals(expected, actual) + } + + @Test + fun testStep() { + val expected = listOf( + "2020-01-01", + "2020-01-03", + "2020-01-05" + ) + calendar.set(2020, 0, 1) + val startDate = calendar.time + calendar.set(2020, 0, 5) + val endDate = calendar.time + + val actual = (startDate..endDate step 2).map { + simpleDateFormat.format(it) + } + + assertEquals(expected, actual) + } +}