Skip to content
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

No longer make Month and DayOfWeek type aliases on the JVM #378

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion core/common/src/DayOfWeek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package kotlinx.datetime
/**
* The enumeration class representing the days of the week.
*/
public expect enum class DayOfWeek {
public enum class DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
Expand Down
2 changes: 1 addition & 1 deletion core/common/src/Month.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package kotlinx.datetime
/**
* The enumeration class representing the 12 months of the year.
*/
public expect enum class Month {
public enum class Month {
/** January, month #01, with 31 days. */
JANUARY,

Expand Down
17 changes: 16 additions & 1 deletion core/common/test/DayOfWeekTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,19 @@ class DayOfWeekTest {
assertFailsWith<IllegalArgumentException> { DayOfWeek(8) }
assertFailsWith<IllegalArgumentException> { DayOfWeek(Int.MIN_VALUE) }
}
}

@Test
fun testExhaustiveWhenDayOfWeek() {
for (dayOfWeek in DayOfWeek.entries) {
when (dayOfWeek) {
DayOfWeek.MONDAY -> assertEquals(1, dayOfWeek.isoDayNumber)
DayOfWeek.TUESDAY -> assertEquals(2, dayOfWeek.isoDayNumber)
DayOfWeek.WEDNESDAY -> assertEquals(3, dayOfWeek.isoDayNumber)
DayOfWeek.THURSDAY -> assertEquals(4, dayOfWeek.isoDayNumber)
DayOfWeek.FRIDAY -> assertEquals(5, dayOfWeek.isoDayNumber)
DayOfWeek.SATURDAY -> assertEquals(6, dayOfWeek.isoDayNumber)
DayOfWeek.SUNDAY -> assertEquals(7, dayOfWeek.isoDayNumber)
}
}
}
}
31 changes: 31 additions & 0 deletions core/common/test/MonthTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019-2024 JetBrains s.r.o. and contributors.
* 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.test.*

class MonthTest {
@Test
fun testExhaustiveWhen() {
for (month in Month.entries) {
when (month) {
Month.JANUARY -> assertEquals(1, month.number)
Month.FEBRUARY -> assertEquals(2, month.number)
Month.MARCH -> assertEquals(3, month.number)
Month.APRIL -> assertEquals(4, month.number)
Month.MAY -> assertEquals(5, month.number)
Month.JUNE -> assertEquals(6, month.number)
Month.JULY -> assertEquals(7, month.number)
Month.AUGUST -> assertEquals(8, month.number)
Month.SEPTEMBER -> assertEquals(9, month.number)
Month.OCTOBER -> assertEquals(10, month.number)
Month.NOVEMBER -> assertEquals(11, month.number)
Month.DECEMBER -> assertEquals(12, month.number)
}
}
}
}
12 changes: 1 addition & 11 deletions core/commonJs/src/DayOfWeek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,4 @@ package kotlinx.datetime

import kotlinx.datetime.internal.JSJoda.DayOfWeek as jsDayOfWeek

public actual enum class DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}

internal fun jsDayOfWeek.toDayOfWeek(): DayOfWeek = DayOfWeek(this.value())
internal fun jsDayOfWeek.toDayOfWeek(): DayOfWeek = DayOfWeek(this.value())
17 changes: 1 addition & 16 deletions core/commonJs/src/Month.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,4 @@ package kotlinx.datetime

import kotlinx.datetime.internal.JSJoda.Month as jsMonth

public actual enum class Month {
JANUARY,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER;
}

internal fun jsMonth.toMonth(): Month = Month(this.value())
internal fun jsMonth.toMonth(): Month = Month(this.value())
19 changes: 19 additions & 0 deletions core/jvm/src/Converters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,22 @@ public fun UtcOffset.toJavaZoneOffset(): java.time.ZoneOffset = this.zoneOffset
*/
public fun java.time.ZoneOffset.toKotlinUtcOffset(): UtcOffset = UtcOffset(this)

/**
* Converts this [kotlinx.datetime.Month][Month] value to a [java.time.Month][java.time.Month] value.
*/
public fun Month.toJavaMonth(): java.time.Month = java.time.Month.of(number)

/**
* Converts this [java.time.Month][java.time.Month] value to a [kotlinx.datetime.Month][Month] value.
*/
public fun java.time.Month.toKotlinMonth(): Month = Month.entries[this.value - 1]

/**
* Converts this [kotlinx.datetime.DayOfWeek][DayOfWeek] value to a [java.time.DayOfWeek][java.time.DayOfWeek] value.
*/
public fun DayOfWeek.toJavaDayOfWeek(): java.time.DayOfWeek = java.time.DayOfWeek.of(isoDayNumber)

/**
* Converts this [java.time.DayOfWeek][java.time.DayOfWeek] value to a [kotlinx.datetime.DayOfWeek][DayOfWeek] value.
*/
public fun java.time.DayOfWeek.toKotlinDayOfWeek(): DayOfWeek = DayOfWeek.entries[this.value - 1]
14 changes: 12 additions & 2 deletions core/jvm/src/LocalDate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public actual class LocalDate internal constructor(internal val value: jtLocalDa

public actual val year: Int get() = value.year
public actual val monthNumber: Int get() = value.monthValue
public actual val month: Month get() = value.month
public actual val month: Month get() = value.month.toKotlinMonth()
public actual val dayOfMonth: Int get() = value.dayOfMonth
public actual val dayOfWeek: DayOfWeek get() = value.dayOfWeek
public actual val dayOfWeek: DayOfWeek get() = value.dayOfWeek.toKotlinDayOfWeek()
public actual val dayOfYear: Int get() = value.dayOfYear

override fun equals(other: Any?): Boolean =
Expand All @@ -78,6 +78,13 @@ public actual class LocalDate internal constructor(internal val value: jtLocalDa
public actual fun toEpochDays(): Int = value.toEpochDay().clampToInt()
}

@Deprecated(
"Use kotlinx.datetime.Month",
ReplaceWith("LocalDate(year, month.toKotlinMonth(), dayOfMonth)")
)
public fun LocalDate(year: Int, month: java.time.Month, dayOfMonth: Int): LocalDate =
LocalDate(year, month.toKotlinMonth(), dayOfMonth)

@Deprecated("Use the plus overload with an explicit number of units", ReplaceWith("this.plus(1, unit)"))
public actual fun LocalDate.plus(unit: DateTimeUnit.DateBased): LocalDate =
plus(1L, unit)
Expand Down Expand Up @@ -149,3 +156,6 @@ public actual fun LocalDate.monthsUntil(other: LocalDate): Int =

public actual fun LocalDate.yearsUntil(other: LocalDate): Int =
this.value.until(other.value, ChronoUnit.YEARS).clampToInt()

@Deprecated("Use kotlinx.datetime.Month", ReplaceWith("toKotlinMonth().number"))
public val java.time.Month.number: Int get() = toKotlinMonth().number
28 changes: 23 additions & 5 deletions core/jvm/src/LocalDateTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import java.time.DateTimeException
import java.time.format.DateTimeParseException
import java.time.LocalDateTime as jtLocalDateTime

public actual typealias Month = java.time.Month
public actual typealias DayOfWeek = java.time.DayOfWeek

@Serializable(with = LocalDateTimeIso8601Serializer::class)
public actual class LocalDateTime internal constructor(internal val value: jtLocalDateTime) : Comparable<LocalDateTime> {

Expand All @@ -33,9 +30,9 @@ public actual class LocalDateTime internal constructor(internal val value: jtLoc

public actual val year: Int get() = value.year
public actual val monthNumber: Int get() = value.monthValue
public actual val month: Month get() = value.month
public actual val month: Month get() = value.month.toKotlinMonth()
public actual val dayOfMonth: Int get() = value.dayOfMonth
public actual val dayOfWeek: DayOfWeek get() = value.dayOfWeek
public actual val dayOfWeek: DayOfWeek get() = value.dayOfWeek.toKotlinDayOfWeek()
public actual val dayOfYear: Int get() = value.dayOfYear

public actual val hour: Int get() = value.hour
Expand Down Expand Up @@ -85,3 +82,24 @@ public actual class LocalDateTime internal constructor(internal val value: jtLoc

}

@Deprecated(
"Use kotlinx.datetime.Month",
ReplaceWith("LocalDateTime(year, month.toKotlinMonth(), dayOfMonth, hour, minute, second, nanosecond)")
)
public fun LocalDateTime(
year: Int,
month: java.time.Month,
dayOfMonth: Int,
hour: Int,
minute: Int,
second: Int = 0,
nanosecond: Int = 0
): LocalDateTime = LocalDateTime(
year,
month.toKotlinMonth(),
dayOfMonth,
hour,
minute,
second,
nanosecond
)
7 changes: 7 additions & 0 deletions core/jvm/src/LocalTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,10 @@ public actual class LocalTime internal constructor(internal val value: jtLocalTi

}
}

@Deprecated(
"Use kotlinx.datetime.Month",
ReplaceWith("atDate(year, month.toKotlinMonth(), dayOfMonth)")
)
public fun LocalTime.atDate(year: Int, month: java.time.Month, dayOfMonth: Int = 0): LocalDateTime =
atDate(year, month.toKotlinMonth(), dayOfMonth)
38 changes: 31 additions & 7 deletions core/jvm/test/ConvertersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ConvertersTest {
assertEquals(ktInstant, jtInstant.toKotlinInstant())
assertEquals(jtInstant, ktInstant.toJavaInstant())

assertEquals(ktInstant, jtInstant.toString().toInstant())
assertEquals(ktInstant, jtInstant.toString().let(Instant::parse))
assertEquals(jtInstant, ktInstant.toString().let(JTInstant::parse))
}

Expand All @@ -40,7 +40,7 @@ class ConvertersTest {
private fun randomDate(): LocalDate {
val year = Random.nextInt(-20000, 20000)
val month = Month.entries.random()
val day = (1..java.time.YearMonth.of(year, month).lengthOfMonth()).random()
val day = (1..java.time.YearMonth.of(year, month.toJavaMonth()).lengthOfMonth()).random()
return LocalDate(year, month.number, day)
}

Expand All @@ -61,12 +61,14 @@ class ConvertersTest {
@Test
fun localDateTime() {
fun test(ktDateTime: LocalDateTime) {
val jtDateTime = with(ktDateTime) { JTLocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanosecond) }
val jtDateTime = with(ktDateTime) {
JTLocalDateTime.of(year, month.toJavaMonth(), dayOfMonth, hour, minute, second, nanosecond)
}

assertEquals(ktDateTime, jtDateTime.toKotlinLocalDateTime())
assertEquals(jtDateTime, ktDateTime.toJavaLocalDateTime())

assertEquals(ktDateTime, jtDateTime.toString().toLocalDateTime())
assertEquals(ktDateTime, jtDateTime.toString().let(LocalDateTime::parse))
assertEquals(jtDateTime, ktDateTime.toString().let(JTLocalDateTime::parse))
}

Expand All @@ -83,7 +85,7 @@ class ConvertersTest {
assertEquals(ktTime, jtTime.toKotlinLocalTime())
assertEquals(jtTime, ktTime.toJavaLocalTime())

assertEquals(ktTime, jtTime.toString().toLocalTime())
assertEquals(ktTime, jtTime.toString().let(LocalTime::parse))
assertEquals(jtTime, ktTime.toString().let(JTLocalTime::parse))
}

Expand All @@ -95,12 +97,12 @@ class ConvertersTest {
@Test
fun localDate() {
fun test(ktDate: LocalDate) {
val jtDate = with(ktDate) { JTLocalDate.of(year, month, dayOfMonth) }
val jtDate = with(ktDate) { JTLocalDate.of(year, month.toJavaMonth(), dayOfMonth) }

assertEquals(ktDate, jtDate.toKotlinLocalDate())
assertEquals(jtDate, ktDate.toJavaLocalDate())

assertEquals(ktDate, jtDate.toString().toLocalDate())
assertEquals(ktDate, jtDate.toString().let(LocalDate::parse))
assertEquals(jtDate, ktDate.toString().let(JTLocalDate::parse))
}

Expand Down Expand Up @@ -187,4 +189,26 @@ class ConvertersTest {
test("+08")
test("-103030")
}

@Test
fun month() {
fun test(month: Month) {
val jtMonth = month.toJavaMonth()
assertEquals(month, jtMonth.toKotlinMonth())
ilya-g marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(month.name, jtMonth.name)
}
Month.entries.forEach(::test)
assertEquals(Month.JANUARY, java.time.Month.JANUARY.toKotlinMonth())
}

@Test
fun dayOfWeek() {
fun test(dayOfWeek: DayOfWeek) {
val jtDayOfWeek = dayOfWeek.toJavaDayOfWeek()
assertEquals(dayOfWeek, jtDayOfWeek.toKotlinDayOfWeek())
assertEquals(dayOfWeek.name, jtDayOfWeek.name)
}
DayOfWeek.entries.forEach(::test)
assertEquals(DayOfWeek.MONDAY, java.time.DayOfWeek.MONDAY.toKotlinDayOfWeek())
}
}
33 changes: 33 additions & 0 deletions core/jvm/test/DeprecationReplacements.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019-2024 JetBrains s.r.o. and contributors.
* 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.*

/**
* To test the deprecation replacements, remove the `Suppress` annotation and try automatically replacing the deprecated
* API usages with the new ones.
*/
@Suppress("DEPRECATION")
class DeprecationReplacements {
fun localTimeAtDate() {
LocalTime(18, 43, 15, 100500000)
.atDate(2023, java.time.Month.JANUARY, 20)
}

fun monthNumber() {
java.time.Month.JANUARY.number
}

fun localDateConstruction() {
LocalDate(2023, java.time.Month.JANUARY, 20)
}

fun localDateTimeConstruction() {
LocalDateTime(2023, java.time.Month.JANUARY, 20, 18, 43, 15, 100500000)
LocalDateTime(2023, java.time.Month.JANUARY, 20, 18, 43, 15)
LocalDateTime(2023, java.time.Month.JANUARY, 20, 18, 43)
}
}
10 changes: 0 additions & 10 deletions core/native/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ import kotlin.time.*
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds

public actual enum class DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}

/**
* The minimum supported epoch second.
*/
Expand Down
4 changes: 0 additions & 4 deletions core/native/src/Month.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

package kotlinx.datetime

public actual enum class Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
}

// From threetenbp
internal fun Month.firstDayOfYear(leapYear: Boolean): Int {
val leap = if (leapYear) 1 else 0
Expand Down