Skip to content

Commit 52dac5a

Browse files
committed
No longer make Month and DayOfWeek type aliases on the JVM
Fixes #96
1 parent 02e4e4d commit 52dac5a

File tree

12 files changed

+115
-57
lines changed

12 files changed

+115
-57
lines changed

core/common/src/DayOfWeek.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package kotlinx.datetime
88
/**
99
* The enumeration class representing the days of the week.
1010
*/
11-
public expect enum class DayOfWeek {
11+
public enum class DayOfWeek {
1212
MONDAY,
1313
TUESDAY,
1414
WEDNESDAY,

core/common/src/Month.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package kotlinx.datetime
88
/**
99
* The enumeration class representing the 12 months of the year.
1010
*/
11-
public expect enum class Month {
11+
public enum class Month {
1212
/** January, month #01, with 31 days. */
1313
JANUARY,
1414

core/common/test/DayOfWeekTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2019-2024 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.test
7+
8+
import kotlinx.datetime.*
9+
import kotlin.test.*
10+
11+
class DayOfWeekTest {
12+
@Test
13+
fun testExhaustiveWhenDayOfWeek() {
14+
for (dayOfWeek in DayOfWeek.entries) {
15+
when (dayOfWeek) {
16+
DayOfWeek.MONDAY -> assertEquals(1, dayOfWeek.isoDayNumber)
17+
DayOfWeek.TUESDAY -> assertEquals(2, dayOfWeek.isoDayNumber)
18+
DayOfWeek.WEDNESDAY -> assertEquals(3, dayOfWeek.isoDayNumber)
19+
DayOfWeek.THURSDAY -> assertEquals(4, dayOfWeek.isoDayNumber)
20+
DayOfWeek.FRIDAY -> assertEquals(5, dayOfWeek.isoDayNumber)
21+
DayOfWeek.SATURDAY -> assertEquals(6, dayOfWeek.isoDayNumber)
22+
DayOfWeek.SUNDAY -> assertEquals(7, dayOfWeek.isoDayNumber)
23+
}
24+
}
25+
}
26+
}

core/common/test/MonthTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2019-2024 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.test
7+
8+
import kotlinx.datetime.*
9+
import kotlin.test.*
10+
11+
class MonthTest {
12+
@Test
13+
fun testExhaustiveWhen() {
14+
for (month in Month.entries) {
15+
when (month) {
16+
Month.JANUARY -> assertEquals(1, month.number)
17+
Month.FEBRUARY -> assertEquals(2, month.number)
18+
Month.MARCH -> assertEquals(3, month.number)
19+
Month.APRIL -> assertEquals(4, month.number)
20+
Month.MAY -> assertEquals(5, month.number)
21+
Month.JUNE -> assertEquals(6, month.number)
22+
Month.JULY -> assertEquals(7, month.number)
23+
Month.AUGUST -> assertEquals(8, month.number)
24+
Month.SEPTEMBER -> assertEquals(9, month.number)
25+
Month.OCTOBER -> assertEquals(10, month.number)
26+
Month.NOVEMBER -> assertEquals(11, month.number)
27+
Month.DECEMBER -> assertEquals(12, month.number)
28+
}
29+
}
30+
}
31+
}

core/commonJs/src/DayOfWeek.kt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,4 @@ package kotlinx.datetime
77

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

10-
public actual enum class DayOfWeek {
11-
MONDAY,
12-
TUESDAY,
13-
WEDNESDAY,
14-
THURSDAY,
15-
FRIDAY,
16-
SATURDAY,
17-
SUNDAY;
18-
}
19-
20-
internal fun jsDayOfWeek.toDayOfWeek(): DayOfWeek = DayOfWeek(this.value())
10+
internal fun jsDayOfWeek.toDayOfWeek(): DayOfWeek = DayOfWeek(this.value())

core/commonJs/src/Month.kt

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,4 @@ package kotlinx.datetime
77

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

10-
public actual enum class Month {
11-
JANUARY,
12-
FEBRUARY,
13-
MARCH,
14-
APRIL,
15-
MAY,
16-
JUNE,
17-
JULY,
18-
AUGUST,
19-
SEPTEMBER,
20-
OCTOBER,
21-
NOVEMBER,
22-
DECEMBER;
23-
}
24-
25-
internal fun jsMonth.toMonth(): Month = Month(this.value())
10+
internal fun jsMonth.toMonth(): Month = Month(this.value())

core/jvm/src/Converters.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,22 @@ public fun UtcOffset.toJavaZoneOffset(): java.time.ZoneOffset = this.zoneOffset
9393
*/
9494
public fun java.time.ZoneOffset.toKotlinUtcOffset(): UtcOffset = UtcOffset(this)
9595

96+
/**
97+
* Converts this [kotlinx.datetime.Month][Month] value to a [java.time.Month][java.time.Month] value.
98+
*/
99+
public fun Month.toJavaMonth(): java.time.Month = java.time.Month.of(number)
100+
101+
/**
102+
* Converts this [java.time.Month][java.time.Month] value to a [kotlinx.datetime.Month][Month] value.
103+
*/
104+
public fun java.time.Month.toKotlinMonth(): Month = Month.entries[this.value - 1]
105+
106+
/**
107+
* Converts this [kotlinx.datetime.DayOfWeek][DayOfWeek] value to a [java.time.DayOfWeek][java.time.DayOfWeek] value.
108+
*/
109+
public fun DayOfWeek.toJavaDayOfWeek(): java.time.DayOfWeek = java.time.DayOfWeek.of(isoDayNumber)
110+
111+
/**
112+
* Converts this [java.time.DayOfWeek][java.time.DayOfWeek] value to a [kotlinx.datetime.DayOfWeek][DayOfWeek] value.
113+
*/
114+
public fun java.time.DayOfWeek.toKotlinDayOfWeek(): DayOfWeek = DayOfWeek.entries[this.value - 1]

core/jvm/src/LocalDate.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public actual class LocalDate internal constructor(internal val value: jtLocalDa
6161

6262
public actual val year: Int get() = value.year
6363
public actual val monthNumber: Int get() = value.monthValue
64-
public actual val month: Month get() = value.month
64+
public actual val month: Month get() = value.month.toKotlinMonth()
6565
public actual val dayOfMonth: Int get() = value.dayOfMonth
66-
public actual val dayOfWeek: DayOfWeek get() = value.dayOfWeek
66+
public actual val dayOfWeek: DayOfWeek get() = value.dayOfWeek.toKotlinDayOfWeek()
6767
public actual val dayOfYear: Int get() = value.dayOfYear
6868

6969
override fun equals(other: Any?): Boolean =

core/jvm/src/LocalDateTime.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ import java.time.DateTimeException
1212
import java.time.format.DateTimeParseException
1313
import java.time.LocalDateTime as jtLocalDateTime
1414

15-
public actual typealias Month = java.time.Month
16-
public actual typealias DayOfWeek = java.time.DayOfWeek
17-
1815
@Serializable(with = LocalDateTimeIso8601Serializer::class)
1916
public actual class LocalDateTime internal constructor(internal val value: jtLocalDateTime) : Comparable<LocalDateTime> {
2017

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

3431
public actual val year: Int get() = value.year
3532
public actual val monthNumber: Int get() = value.monthValue
36-
public actual val month: Month get() = value.month
33+
public actual val month: Month get() = value.month.toKotlinMonth()
3734
public actual val dayOfMonth: Int get() = value.dayOfMonth
38-
public actual val dayOfWeek: DayOfWeek get() = value.dayOfWeek
35+
public actual val dayOfWeek: DayOfWeek get() = value.dayOfWeek.toKotlinDayOfWeek()
3936
public actual val dayOfYear: Int get() = value.dayOfYear
4037

4138
public actual val hour: Int get() = value.hour

core/jvm/test/ConvertersTest.kt

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package kotlinx.datetime.test
66

77
import kotlinx.datetime.*
8+
import org.junit.experimental.theories.suppliers.TestedOn
89
import kotlin.random.Random
910
import kotlin.test.*
1011
import java.time.Instant as JTInstant
@@ -26,7 +27,7 @@ class ConvertersTest {
2627
assertEquals(ktInstant, jtInstant.toKotlinInstant())
2728
assertEquals(jtInstant, ktInstant.toJavaInstant())
2829

29-
assertEquals(ktInstant, jtInstant.toString().toInstant())
30+
assertEquals(ktInstant, jtInstant.toString().let(Instant::parse))
3031
assertEquals(jtInstant, ktInstant.toString().let(JTInstant::parse))
3132
}
3233

@@ -37,10 +38,11 @@ class ConvertersTest {
3738
}
3839
}
3940

41+
@OptIn(ExperimentalStdlibApi::class)
4042
private fun randomDate(): LocalDate {
4143
val year = Random.nextInt(-20000, 20000)
4244
val month = Month.entries.random()
43-
val day = (1..java.time.YearMonth.of(year, month).lengthOfMonth()).random()
45+
val day = (1..java.time.YearMonth.of(year, month.toJavaMonth()).lengthOfMonth()).random()
4446
return LocalDate(year, month.number, day)
4547
}
4648

@@ -61,12 +63,14 @@ class ConvertersTest {
6163
@Test
6264
fun localDateTime() {
6365
fun test(ktDateTime: LocalDateTime) {
64-
val jtDateTime = with(ktDateTime) { JTLocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanosecond) }
66+
val jtDateTime = with(ktDateTime) {
67+
JTLocalDateTime.of(year, month.toJavaMonth(), dayOfMonth, hour, minute, second, nanosecond)
68+
}
6569

6670
assertEquals(ktDateTime, jtDateTime.toKotlinLocalDateTime())
6771
assertEquals(jtDateTime, ktDateTime.toJavaLocalDateTime())
6872

69-
assertEquals(ktDateTime, jtDateTime.toString().toLocalDateTime())
73+
assertEquals(ktDateTime, jtDateTime.toString().let(LocalDateTime::parse))
7074
assertEquals(jtDateTime, ktDateTime.toString().let(JTLocalDateTime::parse))
7175
}
7276

@@ -83,7 +87,7 @@ class ConvertersTest {
8387
assertEquals(ktTime, jtTime.toKotlinLocalTime())
8488
assertEquals(jtTime, ktTime.toJavaLocalTime())
8589

86-
assertEquals(ktTime, jtTime.toString().toLocalTime())
90+
assertEquals(ktTime, jtTime.toString().let(LocalTime::parse))
8791
assertEquals(jtTime, ktTime.toString().let(JTLocalTime::parse))
8892
}
8993

@@ -95,12 +99,12 @@ class ConvertersTest {
9599
@Test
96100
fun localDate() {
97101
fun test(ktDate: LocalDate) {
98-
val jtDate = with(ktDate) { JTLocalDate.of(year, month, dayOfMonth) }
102+
val jtDate = with(ktDate) { JTLocalDate.of(year, month.toJavaMonth(), dayOfMonth) }
99103

100104
assertEquals(ktDate, jtDate.toKotlinLocalDate())
101105
assertEquals(jtDate, ktDate.toJavaLocalDate())
102106

103-
assertEquals(ktDate, jtDate.toString().toLocalDate())
107+
assertEquals(ktDate, jtDate.toString().let(LocalDate::parse))
104108
assertEquals(jtDate, ktDate.toString().let(JTLocalDate::parse))
105109
}
106110

@@ -187,4 +191,24 @@ class ConvertersTest {
187191
test("+08")
188192
test("-103030")
189193
}
194+
195+
@Test
196+
fun month() {
197+
fun test(month: Month) {
198+
val jtMonth = month.toJavaMonth()
199+
assertEquals(month, jtMonth.toKotlinMonth())
200+
}
201+
Month.entries.forEach(::test)
202+
assertEquals(Month.JANUARY, java.time.Month.JANUARY.toKotlinMonth())
203+
}
204+
205+
@Test
206+
fun dayOfWeek() {
207+
fun test(dayOfWeek: DayOfWeek) {
208+
val jtDayOfWeek = dayOfWeek.toJavaDayOfWeek()
209+
assertEquals(dayOfWeek, jtDayOfWeek.toKotlinDayOfWeek())
210+
}
211+
DayOfWeek.entries.forEach(::test)
212+
assertEquals(DayOfWeek.MONDAY, java.time.DayOfWeek.MONDAY.toKotlinDayOfWeek())
213+
}
190214
}

core/native/src/Instant.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ import kotlin.time.*
1616
import kotlin.time.Duration.Companion.nanoseconds
1717
import kotlin.time.Duration.Companion.seconds
1818

19-
public actual enum class DayOfWeek {
20-
MONDAY,
21-
TUESDAY,
22-
WEDNESDAY,
23-
THURSDAY,
24-
FRIDAY,
25-
SATURDAY,
26-
SUNDAY;
27-
}
28-
2919
/**
3020
* The minimum supported epoch second.
3121
*/

core/native/src/Month.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55

66
package kotlinx.datetime
77

8-
public actual enum class Month {
9-
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
10-
}
11-
128
// From threetenbp
139
internal fun Month.firstDayOfYear(leapYear: Boolean): Int {
1410
val leap = if (leapYear) 1 else 0

0 commit comments

Comments
 (0)