diff --git a/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt index 30167332049..e83184bcb77 100644 --- a/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt +++ b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/MainAPI.kt @@ -2560,6 +2560,9 @@ fun Episode.addDate(date: Instant?) { this.date = date?.toEpochMilliseconds() } +@Prerelease +fun Int.toYear(): LocalDate = LocalDate(this, 1, 1) + // Deprecate after next stable /* @Deprecated( message = "Use addDate with LocalDate, Instant, or String instead.", diff --git a/library/src/commonTest/kotlin/com/lagradost/cloudstream3/IntToYearTest.kt b/library/src/commonTest/kotlin/com/lagradost/cloudstream3/IntToYearTest.kt new file mode 100644 index 00000000000..e6f8fb9cfc8 --- /dev/null +++ b/library/src/commonTest/kotlin/com/lagradost/cloudstream3/IntToYearTest.kt @@ -0,0 +1,63 @@ +package com.lagradost.cloudstream3 + +import kotlinx.datetime.LocalDate +import kotlinx.datetime.number +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse + +class IntToYearTest { + + @Test + fun toYearReturnsFirstOfJanuary() { + val result = 2026.toYear() + assertEquals(LocalDate(2026, 1, 1), result) + } + + @Test + fun toYearSetsCorrectYear() { + val result = 1999.toYear() + assertEquals(1999, result.year) + } + + @Test + fun toYearSetsMonthToJanuary() { + val result = 2026.toYear() + assertEquals(1, result.month.number) + } + + @Test + fun toYearSetsDayToFirst() { + val result = 2026.toYear() + assertEquals(1, result.day) + } + + @Test + fun toYearHandlesLeapYear() { + val result = 2028.toYear() + assertEquals(LocalDate(2028, 1, 1), result) + } + + @Test + fun toYearHandlesEpochYear() { + val result = 1970.toYear() + assertEquals(LocalDate(1970, 1, 1), result) + } + + @Test + fun toYearHandlesFarPastYear() { + val result = 1.toYear() + assertEquals(LocalDate(1, 1, 1), result) + } + + @Test + fun toYearHandlesFarFutureYear() { + val result = 9999.toYear() + assertEquals(LocalDate(9999, 1, 1), result) + } + + @Test + fun toYearDifferentYearsProduceDifferentDates() { + assertFalse(2025.toYear() == 2026.toYear()) + } +}