Skip to content

Commit

Permalink
Add previous and next YearMonth properties
Browse files Browse the repository at this point in the history
  • Loading branch information
kizitonwose committed Jul 18, 2024
1 parent 7a93a9b commit 4f1afeb
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions compose-multiplatform/library/api/android/library.api
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ public final class com/kizitonwose/calendar/core/YearMonthKt {
public static final fun atDay (Lcom/kizitonwose/calendar/core/YearMonth;I)Lkotlinx/datetime/LocalDate;
public static final fun atEndOfMonth (Lcom/kizitonwose/calendar/core/YearMonth;)Lkotlinx/datetime/LocalDate;
public static final fun atStartOfMonth (Lcom/kizitonwose/calendar/core/YearMonth;)Lkotlinx/datetime/LocalDate;
public static final fun getNext (Lcom/kizitonwose/calendar/core/YearMonth;)Lcom/kizitonwose/calendar/core/YearMonth;
public static final fun getPrevious (Lcom/kizitonwose/calendar/core/YearMonth;)Lcom/kizitonwose/calendar/core/YearMonth;
public static final fun lengthOfMonth (Lcom/kizitonwose/calendar/core/YearMonth;)I
public static final fun minus (Lcom/kizitonwose/calendar/core/YearMonth;ILkotlinx/datetime/DateTimeUnit$MonthBased;)Lcom/kizitonwose/calendar/core/YearMonth;
public static final fun monthsUntil (Lcom/kizitonwose/calendar/core/YearMonth;Lcom/kizitonwose/calendar/core/YearMonth;)I
Expand Down
2 changes: 2 additions & 0 deletions compose-multiplatform/library/api/desktop/library.api
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ public final class com/kizitonwose/calendar/core/YearMonthKt {
public static final fun atDay (Lcom/kizitonwose/calendar/core/YearMonth;I)Lkotlinx/datetime/LocalDate;
public static final fun atEndOfMonth (Lcom/kizitonwose/calendar/core/YearMonth;)Lkotlinx/datetime/LocalDate;
public static final fun atStartOfMonth (Lcom/kizitonwose/calendar/core/YearMonth;)Lkotlinx/datetime/LocalDate;
public static final fun getNext (Lcom/kizitonwose/calendar/core/YearMonth;)Lcom/kizitonwose/calendar/core/YearMonth;
public static final fun getPrevious (Lcom/kizitonwose/calendar/core/YearMonth;)Lcom/kizitonwose/calendar/core/YearMonth;
public static final fun lengthOfMonth (Lcom/kizitonwose/calendar/core/YearMonth;)I
public static final fun minus (Lcom/kizitonwose/calendar/core/YearMonth;ILkotlinx/datetime/DateTimeUnit$MonthBased;)Lcom/kizitonwose/calendar/core/YearMonth;
public static final fun monthsUntil (Lcom/kizitonwose/calendar/core/YearMonth;Lcom/kizitonwose/calendar/core/YearMonth;)I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,25 @@ public fun YearMonth.plus(value: Int, unit: DateTimeUnit.MonthBased): YearMonth
*/
public fun YearMonth.minus(value: Int, unit: DateTimeUnit.MonthBased): YearMonth =
atStartOfMonth().minus(value, unit).yearMonth

/**
* Returns a [YearMonth] that results from adding the 1 month this year-month.
*
* @throws DateTimeArithmeticException if the result exceeds the boundaries
* of [YearMonth] which is essentially the [LocalDate] boundaries.
*
* @see YearMonth.plus
*/
public val YearMonth.next: YearMonth
get() = this.plus(1, DateTimeUnit.MONTH)

/**
* Returns a [YearMonth] that results from subtracting the 1 month this year-month.
*
* @throws DateTimeArithmeticException if the result exceeds the boundaries
* of [YearMonth] which is essentially the [LocalDate] boundaries.
*
* @see YearMonth.minus
*/
public val YearMonth.previous: YearMonth
get() = this.minus(1, DateTimeUnit.MONTH)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import com.kizitonwose.calendar.core.atEndOfMonth
import com.kizitonwose.calendar.core.atStartOfMonth
import com.kizitonwose.calendar.core.next
import com.kizitonwose.calendar.core.previous
import com.kizitonwose.calendar.core.yearMonth
import kotlinx.datetime.LocalDate
import kotlinx.datetime.daysUntil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import com.kizitonwose.calendar.core.CalendarDay
import com.kizitonwose.calendar.core.DayPosition
import com.kizitonwose.calendar.core.YearMonth
import com.kizitonwose.calendar.core.daysOfWeek
import com.kizitonwose.calendar.core.next
import com.kizitonwose.calendar.core.previous
import kotlinx.coroutines.launch
import kotlinx.datetime.DayOfWeek
import org.jetbrains.compose.ui.tooling.preview.Preview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import com.kizitonwose.calendar.core.DayPosition
import com.kizitonwose.calendar.core.OutDateStyle
import com.kizitonwose.calendar.core.YearMonth
import com.kizitonwose.calendar.core.daysOfWeek
import com.kizitonwose.calendar.core.next
import com.kizitonwose.calendar.core.previous
import kotlinx.coroutines.launch
import kotlinx.datetime.DayOfWeek
import org.jetbrains.compose.ui.tooling.preview.Preview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ import com.kizitonwose.calendar.core.YearMonth
import com.kizitonwose.calendar.core.atEndOfMonth
import com.kizitonwose.calendar.core.atStartOfMonth
import com.kizitonwose.calendar.core.daysOfWeek
import com.kizitonwose.calendar.core.next
import com.kizitonwose.calendar.core.now
import com.kizitonwose.calendar.core.previous
import com.kizitonwose.calendar.core.yearMonth
import kotlinx.coroutines.launch
import kotlinx.datetime.DayOfWeek
Expand Down
4 changes: 0 additions & 4 deletions compose-multiplatform/sample/src/commonMain/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,3 @@ internal fun LocalDate.plusDays(value: Int): LocalDate = plus(value, DateTimeUni
internal fun LocalDate.minusDays(value: Int): LocalDate = minus(value, DateTimeUnit.DAY)
internal fun YearMonth.plusMonths(value: Int): YearMonth = plus(value, DateTimeUnit.MONTH)
internal fun YearMonth.minusMonths(value: Int): YearMonth = minus(value, DateTimeUnit.MONTH)
internal val YearMonth.next: YearMonth
get() = this.minusMonths(1)
internal val YearMonth.previous: YearMonth
get() = this.minusMonths(1)

0 comments on commit 4f1afeb

Please sign in to comment.