Skip to content

Commit

Permalink
No longer make Month and DayOfWeek type aliases on the JVM
Browse files Browse the repository at this point in the history
Fixes #96
  • Loading branch information
dkhalanskyjb committed Apr 3, 2024
1 parent 9d59199 commit 2e1c4f6
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 58 deletions.
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]
4 changes: 2 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 Down
7 changes: 2 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
38 changes: 31 additions & 7 deletions core/jvm/test/ConvertersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package kotlinx.datetime.test

import kotlinx.datetime.*
import org.junit.experimental.theories.suppliers.TestedOn
import kotlin.random.Random
import kotlin.test.*
import java.time.Instant as JTInstant
Expand All @@ -26,7 +27,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 @@ -37,10 +38,11 @@ class ConvertersTest {
}
}

@OptIn(ExperimentalStdlibApi::class)
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 +63,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 +87,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 +99,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 +191,24 @@ class ConvertersTest {
test("+08")
test("-103030")
}

@Test
fun month() {
fun test(month: Month) {
val jtMonth = month.toJavaMonth()
assertEquals(month, jtMonth.toKotlinMonth())
}
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())
}
DayOfWeek.entries.forEach(::test)
assertEquals(DayOfWeek.MONDAY, java.time.DayOfWeek.MONDAY.toKotlinDayOfWeek())
}
}
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

0 comments on commit 2e1c4f6

Please sign in to comment.