Skip to content

Commit ca05466

Browse files
committed
CODEREVIEW AMMEND
1 parent 074bcc5 commit ca05466

16 files changed

+76
-92
lines changed

core/build.gradle.kts

+7-7
Original file line numberDiff line numberDiff line change
@@ -202,41 +202,41 @@ kotlin {
202202
}
203203
}
204204

205-
val jsAndWasmSharedMain by creating {
205+
val commonJsMain by creating {
206206
dependsOn(commonMain.get())
207207
dependencies {
208208
api("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
209209
implementation(npm("@js-joda/core", "3.2.0"))
210210
}
211211
}
212212

213-
val jsAndWasmSharedTest by creating {
213+
val commonJsTest by creating {
214214
dependsOn(commonTest.get())
215215
dependencies {
216216
implementation(npm("@js-joda/timezone", "2.3.0"))
217217
}
218218
}
219219

220220
val jsMain by getting {
221-
dependsOn(jsAndWasmSharedMain)
221+
dependsOn(commonJsMain)
222222
dependencies {
223223
api("org.jetbrains.kotlin:kotlin-stdlib-js")
224224
}
225225
}
226226

227227
val jsTest by getting {
228-
dependsOn(jsAndWasmSharedTest)
228+
dependsOn(commonJsTest)
229229
}
230230

231231
val wasmJsMain by getting {
232-
dependsOn(jsAndWasmSharedMain)
232+
dependsOn(commonJsMain)
233233
dependencies {
234234
api("org.jetbrains.kotlin:kotlin-stdlib-wasm-js")
235235
}
236236
}
237237

238238
val wasmJsTest by getting {
239-
dependsOn(jsAndWasmSharedTest)
239+
dependsOn(commonJsTest)
240240
}
241241

242242
val nativeMain by getting {
@@ -418,7 +418,7 @@ tasks.withType<AbstractDokkaLeafTask>().configureEach {
418418

419419
// Disable intermediate sourceSet compilation because we do not need js-wasmJs artifact
420420
tasks.configureEach {
421-
if (name == "compileJsAndWasmSharedMainKotlinMetadata") {
421+
if (name == "compileCommonJsMainKotlinMetadata") {
422422
enabled = false
423423
}
424424
}

core/common/src/serializers/DateTimeUnitSerializers.kt

+15-23
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ import kotlin.reflect.KClass
2121
*/
2222
public object TimeBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.TimeBased> {
2323

24-
override val descriptor: SerialDescriptor =
25-
buildClassSerialDescriptor("TimeBased") {
26-
element<Long>("nanoseconds")
27-
}
28-
24+
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("TimeBased") {
25+
element<Long>("nanoseconds")
26+
}
2927

3028
override fun serialize(encoder: Encoder, value: DateTimeUnit.TimeBased) {
3129
encoder.encodeStructure(descriptor) {
@@ -67,10 +65,9 @@ public object TimeBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.TimeBase
6765
*/
6866
public object DayBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.DayBased> {
6967

70-
override val descriptor: SerialDescriptor =
71-
buildClassSerialDescriptor("DayBased") {
72-
element<Int>("days")
73-
}
68+
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("DayBased") {
69+
element<Int>("days")
70+
}
7471

7572
override fun serialize(encoder: Encoder, value: DateTimeUnit.DayBased) {
7673
encoder.encodeStructure(descriptor) {
@@ -112,10 +109,9 @@ public object DayBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.DayBased>
112109
*/
113110
public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBased> {
114111

115-
override val descriptor: SerialDescriptor =
116-
buildClassSerialDescriptor("MonthBased") {
117-
element<Int>("months")
118-
}
112+
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("MonthBased") {
113+
element<Int>("months")
114+
}
119115

120116
override fun serialize(encoder: Encoder, value: DateTimeUnit.MonthBased) {
121117
encoder.encodeStructure(descriptor) {
@@ -160,12 +156,10 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBa
160156
public object DateBasedDateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit.DateBased>() {
161157

162158
private val impl by lazy(LazyThreadSafetyMode.PUBLICATION) {
163-
SealedClassSerializer(
164-
"kotlinx.datetime.DateTimeUnit.DateBased",
159+
SealedClassSerializer("kotlinx.datetime.DateTimeUnit.DateBased",
165160
DateTimeUnit.DateBased::class,
166161
arrayOf(DateTimeUnit.DayBased::class, DateTimeUnit.MonthBased::class),
167-
arrayOf(DayBasedDateTimeUnitSerializer, MonthBasedDateTimeUnitSerializer)
168-
)
162+
arrayOf(DayBasedDateTimeUnitSerializer, MonthBasedDateTimeUnitSerializer))
169163
}
170164

171165
@InternalSerializationApi
@@ -197,13 +191,11 @@ public object DateBasedDateTimeUnitSerializer: AbstractPolymorphicSerializer<Dat
197191
@OptIn(InternalSerializationApi::class)
198192
public object DateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit>() {
199193

200-
private val impl by lazy(LazyThreadSafetyMode.PUBLICATION) {
201-
SealedClassSerializer(
202-
"kotlinx.datetime.DateTimeUnit",
194+
private val impl by lazy(LazyThreadSafetyMode.PUBLICATION) {
195+
SealedClassSerializer("kotlinx.datetime.DateTimeUnit",
203196
DateTimeUnit::class,
204197
arrayOf(DateTimeUnit.DayBased::class, DateTimeUnit.MonthBased::class, DateTimeUnit.TimeBased::class),
205-
arrayOf(DayBasedDateTimeUnitSerializer, MonthBasedDateTimeUnitSerializer, TimeBasedDateTimeUnitSerializer)
206-
)
198+
arrayOf(DayBasedDateTimeUnitSerializer, MonthBasedDateTimeUnitSerializer, TimeBasedDateTimeUnitSerializer))
207199
}
208200

209201
@InternalSerializationApi
@@ -221,4 +213,4 @@ public object DateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit
221213
override val descriptor: SerialDescriptor
222214
get() = impl.descriptor
223215

224-
}
216+
}

core/jsAndWasmShared/src/Instant.kt renamed to core/commonJs/src/Instant.kt

+22-27
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import kotlinx.datetime.internal.JodaTimeInstant as jtInstant
99
import kotlinx.datetime.internal.JodaTimeOffsetDateTime as jtOffsetDateTime
1010
import kotlinx.datetime.internal.JodaTimeDuration as jtDuration
1111
import kotlinx.datetime.internal.JodaTimeClock as jtClock
12+
import kotlinx.datetime.internal.JodaTimeChronoUnit as jtChronoUnit
1213
import kotlinx.datetime.internal.safeAdd
1314
import kotlinx.datetime.internal.*
1415
import kotlinx.datetime.serializers.InstantIso8601Serializer
@@ -30,9 +31,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
3031

3132
public actual operator fun plus(duration: Duration): Instant = duration.toComponents { seconds, nanoseconds ->
3233
return try {
33-
// jsTry {
34-
Instant(plusFix(seconds.toDouble(), nanoseconds))
35-
// }
34+
Instant(plusFix(seconds.toDouble(), nanoseconds))
3635
} catch (e: Throwable) {
3736
if (!e.isJodaDateTimeException()) throw e
3837
if (duration.isPositive()) MAX else MIN
@@ -41,8 +40,8 @@ public actual class Instant internal constructor(internal val value: jtInstant)
4140

4241
internal fun plusFix(seconds: Double, nanos: Int): jtInstant {
4342
val newSeconds = value.epochSecond() + seconds
44-
val newNanos = (value.nano() + nanos).toInt()
45-
return jtInstant.ofEpochSecond(newSeconds, newNanos)
43+
val newNanos = value.nano() + nanos
44+
return jtInstant.ofEpochSecond(newSeconds, newNanos.toInt())
4645
}
4746

4847
public actual operator fun minus(duration: Duration): Instant = plus(-duration)
@@ -55,7 +54,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
5554
public actual override operator fun compareTo(other: Instant): Int = this.value.compareTo(other.value)
5655

5756
override fun equals(other: Any?): Boolean =
58-
(this === other) || (other is Instant && (this.value === other.value || this.value.equals(other.value)))
57+
(this === other) || (other is Instant && this.value == other.value)
5958

6059
override fun hashCode(): Int = value.hashCode()
6160

@@ -92,12 +91,12 @@ public actual class Instant internal constructor(internal val value: jtInstant)
9291
}
9392

9493
public actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long): Instant = try {
95-
/* Performing normalization here because otherwise this fails:
94+
/* Performing normalization here because otherwise this fails:
9695
assertEquals((Long.MAX_VALUE % 1_000_000_000).toInt(),
9796
Instant.fromEpochSeconds(0, Long.MAX_VALUE).nanosecondsOfSecond) */
98-
val secs = safeAdd(epochSeconds, nanosecondAdjustment.floorDiv(NANOS_PER_ONE.toLong()))
99-
val nos = nanosecondAdjustment.mod(NANOS_PER_ONE.toLong()).toInt()
100-
Instant(jtInstant.ofEpochSecond(secs.toDouble(), nos))
97+
val secs = safeAdd(epochSeconds, nanosecondAdjustment.floorDiv(NANOS_PER_ONE.toLong()))
98+
val nos = nanosecondAdjustment.mod(NANOS_PER_ONE.toLong()).toInt()
99+
Instant(jtInstant.ofEpochSecond(secs.toDouble(), nos))
101100
} catch (e: Throwable) {
102101
if (!e.isJodaDateTimeException() && e !is ArithmeticException) throw e
103102
if (epochSeconds > 0) MAX else MIN
@@ -123,12 +122,12 @@ public actual fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Inst
123122
val thisZdt = this.value.atZone(timeZone.zoneId)
124123
with(period) {
125124
thisZdt
126-
.run { if (totalMonths != 0) plusMonths(totalMonths) else this }
127-
.run { if (days != 0) plusDays(days) else this }
128-
.run { if (hours != 0) plusHours(hours) else this }
129-
.run { if (minutes != 0) plusMinutes(minutes) else this }
130-
.run { if (seconds != 0) plusSeconds(seconds) else this }
131-
.run { if (nanoseconds != 0) plusNanos(nanoseconds.toDouble()) else this }
125+
.run { if (totalMonths != 0) plusMonths(totalMonths) else this }
126+
.run { if (days != 0) plusDays(days) else this }
127+
.run { if (hours != 0) plusHours(hours) else this }
128+
.run { if (minutes != 0) plusMinutes(minutes) else this }
129+
.run { if (seconds != 0) plusSeconds(seconds) else this }
130+
.run { if (nanoseconds != 0) plusNanos(nanoseconds.toDouble()) else this }
132131
}.toInstant().let(::Instant)
133132
} catch (e: Throwable) {
134133
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
@@ -149,10 +148,8 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
149148
is DateTimeUnit.TimeBased -> {
150149
plus(value, unit).value.checkZone(timeZone)
151150
}
152-
153151
is DateTimeUnit.DayBased ->
154152
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
155-
156153
is DateTimeUnit.MonthBased ->
157154
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
158155
}.let(::Instant)
@@ -167,10 +164,8 @@ public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZon
167164
when (unit) {
168165
is DateTimeUnit.TimeBased ->
169166
plus(value.toLong(), unit).value.checkZone(timeZone)
170-
171167
is DateTimeUnit.DayBased ->
172168
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
173-
174169
is DateTimeUnit.MonthBased ->
175170
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
176171
}.let(::Instant)
@@ -201,9 +196,9 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
201196
var thisZdt = this.value.atZone(timeZone.zoneId)
202197
val otherZdt = other.value.atZone(timeZone.zoneId)
203198

204-
val months = thisZdt.until(otherZdt, JodaTimeChronoUnit.MONTHS); thisZdt = thisZdt.plusMonths(months)
205-
val days = thisZdt.until(otherZdt, JodaTimeChronoUnit.DAYS); thisZdt = thisZdt.plusDays(days)
206-
val nanoseconds = thisZdt.until(otherZdt, JodaTimeChronoUnit.NANOS)
199+
val months = thisZdt.until(otherZdt, jtChronoUnit.MONTHS); thisZdt = thisZdt.plusMonths(months)
200+
val days = thisZdt.until(otherZdt, jtChronoUnit.DAYS); thisZdt = thisZdt.plusDays(days)
201+
val nanoseconds = thisZdt.until(otherZdt, jtChronoUnit.NANOS)
207202

208203
buildDateTimePeriod(months.toInt(), days.toInt(), nanoseconds.toLong())
209204
} catch (e: Throwable) {
@@ -213,10 +208,10 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
213208
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long = try {
214209
val thisZdt = this.atZone(timeZone)
215210
val otherZdt = other.atZone(timeZone)
216-
when (unit) {
211+
when(unit) {
217212
is DateTimeUnit.TimeBased -> until(other, unit)
218-
is DateTimeUnit.DayBased -> (thisZdt.until(otherZdt, JodaTimeChronoUnit.DAYS) / unit.days).toLong()
219-
is DateTimeUnit.MonthBased -> (thisZdt.until(otherZdt, JodaTimeChronoUnit.MONTHS) / unit.months).toLong()
213+
is DateTimeUnit.DayBased -> (thisZdt.until(otherZdt, jtChronoUnit.DAYS) / unit.days).toLong()
214+
is DateTimeUnit.MonthBased -> (thisZdt.until(otherZdt, jtChronoUnit.MONTHS) / unit.months).toLong()
220215
}
221216
} catch (e: ArithmeticException) {
222217
if (this < other) Long.MAX_VALUE else Long.MIN_VALUE
@@ -225,4 +220,4 @@ public actual fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: Ti
225220
}
226221

227222
internal actual fun Instant.toStringWithOffset(offset: UtcOffset): String =
228-
jtOffsetDateTime.ofInstant(this.value, offset.zoneOffset).toString()
223+
jtOffsetDateTime.ofInstant(this.value, offset.zoneOffset).toString()

core/jsAndWasmShared/src/LocalDate.kt renamed to core/commonJs/src/LocalDate.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
package kotlinx.datetime
77

8-
import kotlinx.datetime.internal.JodaTimeChronoUnit
98
import kotlinx.datetime.serializers.LocalDateIso8601Serializer
109
import kotlinx.serialization.Serializable
1110
import kotlinx.datetime.internal.JodaTimeLocalDate as jtLocalDate
11+
import kotlinx.datetime.internal.JodaTimeChronoUnit as jtChronoUnit
1212

1313
@Serializable(with = LocalDateIso8601Serializer::class)
1414
public actual class LocalDate internal constructor(internal val value: jtLocalDate) : Comparable<LocalDate> {
@@ -49,7 +49,7 @@ public actual class LocalDate internal constructor(internal val value: jtLocalDa
4949
public actual val dayOfYear: Int get() = value.dayOfYear()
5050

5151
override fun equals(other: Any?): Boolean =
52-
(this === other) || (other is LocalDate && (this.value === other.value || this.value.equals(other.value)))
52+
(this === other) || (other is LocalDate && this.value == other.value)
5353

5454
override fun hashCode(): Int = value.hashCode()
5555

@@ -81,8 +81,8 @@ private fun LocalDate.plusNumber(value: Number, unit: DateTimeUnit.DateBased): L
8181
public actual operator fun LocalDate.plus(period: DatePeriod): LocalDate = try {
8282
with(period) {
8383
return@with value
84-
.run { if (totalMonths != 0) plusMonths(totalMonths) else this }
85-
.run { if (days != 0) plusDays(days) else this }
84+
.run { if (totalMonths != 0) plusMonths(totalMonths) else this }
85+
.run { if (days != 0) plusDays(days) else this }
8686

8787
}.let(::LocalDate)
8888
} catch (e: Throwable) {
@@ -95,8 +95,8 @@ public actual operator fun LocalDate.plus(period: DatePeriod): LocalDate = try {
9595
public actual fun LocalDate.periodUntil(other: LocalDate): DatePeriod {
9696
var startD = this.value
9797
val endD = other.value
98-
val months = startD.until(endD, JodaTimeChronoUnit.MONTHS).toInt(); startD = startD.plusMonths(months)
99-
val days = startD.until(endD, JodaTimeChronoUnit.DAYS).toInt()
98+
val months = startD.until(endD, jtChronoUnit.MONTHS).toInt(); startD = startD.plusMonths(months)
99+
val days = startD.until(endD, jtChronoUnit.DAYS).toInt()
100100

101101
return DatePeriod(totalMonths = months, days)
102102
}
@@ -107,10 +107,10 @@ public actual fun LocalDate.until(other: LocalDate, unit: DateTimeUnit.DateBased
107107
}
108108

109109
public actual fun LocalDate.daysUntil(other: LocalDate): Int =
110-
this.value.until(other.value, JodaTimeChronoUnit.DAYS).toInt()
110+
this.value.until(other.value, jtChronoUnit.DAYS).toInt()
111111

112112
public actual fun LocalDate.monthsUntil(other: LocalDate): Int =
113-
this.value.until(other.value, JodaTimeChronoUnit.MONTHS).toInt()
113+
this.value.until(other.value, jtChronoUnit.MONTHS).toInt()
114114

115115
public actual fun LocalDate.yearsUntil(other: LocalDate): Int =
116-
this.value.until(other.value, JodaTimeChronoUnit.YEARS).toInt()
116+
this.value.until(other.value, jtChronoUnit.YEARS).toInt()

core/jsAndWasmShared/src/LocalDateTime.kt renamed to core/commonJs/src/LocalDateTime.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public actual class LocalDateTime internal constructor(internal val value: jtLoc
4242
public actual val time: LocalTime get() = LocalTime(value.toLocalTime())
4343

4444
override fun equals(other: Any?): Boolean =
45-
(this === other) || (other is LocalDateTime && this.value == other.value)
45+
(this === other) || (other is LocalDateTime && this.value == other.value)
4646

4747
override fun hashCode(): Int = value.hashCode()
4848

@@ -62,5 +62,4 @@ public actual class LocalDateTime internal constructor(internal val value: jtLoc
6262
internal actual val MAX: LocalDateTime = LocalDateTime(jtLocalDateTime.MAX)
6363
}
6464

65-
}
66-
65+
}

core/jsAndWasmShared/src/LocalTime.kt renamed to core/commonJs/src/LocalTime.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@ public actual class LocalTime internal constructor(internal val value: jtLocalTi
7070
internal actual val MIN: LocalTime = LocalTime(jtLocalTime.MIN)
7171
internal actual val MAX: LocalTime = LocalTime(jtLocalTime.MAX)
7272
}
73-
}
73+
}
File renamed without changes.

0 commit comments

Comments
 (0)