Skip to content

Commit 023b7ac

Browse files
committed
Test TimeZone.currentSystemDefault() = ZoneId.system() on JS without tzdb
1 parent 019c71b commit 023b7ac

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
@file:JsModule("@js-joda/core")
6+
@file:JsNonModule
7+
package kotlinx.datetime.test.JSJoda
8+
9+
import kotlinx.datetime.internal.InteropInterface
10+
import kotlinx.datetime.internal.JsModule
11+
import kotlinx.datetime.internal.JsNonModule
12+
13+
external class ZonedDateTime : InteropInterface {
14+
fun year(): Int
15+
fun monthValue(): Int
16+
fun dayOfMonth(): Int
17+
fun hour(): Int
18+
fun minute(): Int
19+
fun second(): Int
20+
fun nano(): Double
21+
}
22+
23+
external class Instant : InteropInterface {
24+
fun atZone(zone: ZoneId): ZonedDateTime
25+
companion object {
26+
fun ofEpochMilli(epochMilli: Double): Instant
27+
}
28+
}
29+
30+
external class ZoneId : InteropInterface {
31+
companion object {
32+
fun systemDefault(): ZoneId
33+
}
34+
}

js-without-timezones/common/test/TimezonesWithoutDatabaseTest.kt

+24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package kotlinx.datetime.test
77

88
import kotlinx.datetime.*
99
import kotlin.test.*
10+
import kotlinx.datetime.test.JSJoda.ZoneId as jtZoneId
11+
import kotlinx.datetime.test.JSJoda.Instant as jtInstant
1012

1113
class TimezonesWithoutDatabaseTest {
1214
@Test
@@ -25,6 +27,28 @@ class TimezonesWithoutDatabaseTest {
2527
assertEquals(today.atTime(0, 0).toInstant(tz), today.atStartOfDayIn(tz))
2628
}
2729

30+
@Test
31+
fun systemSameAsJoda() {
32+
val tz = TimeZone.currentSystemDefault()
33+
val jodaTz = jtZoneId.systemDefault()
34+
assertEquals(jodaTz.toString(), tz.id)
35+
val range = Instant.DISTANT_PAST.toEpochMilliseconds()..Instant.DISTANT_FUTURE.toEpochMilliseconds()
36+
repeat(1000) {
37+
val epochMilli = range.random()
38+
val instant = Instant.fromEpochMilliseconds(epochMilli)
39+
val jodaInstant = jtInstant.ofEpochMilli(epochMilli.toDouble())
40+
val localDateTime = instant.toLocalDateTime(tz)
41+
val jodaLocalDateTime = jodaInstant.atZone(jodaTz)
42+
assertEquals(jodaLocalDateTime.year(), localDateTime.year)
43+
assertEquals(jodaLocalDateTime.monthValue(), localDateTime.monthNumber)
44+
assertEquals(jodaLocalDateTime.dayOfMonth(), localDateTime.dayOfMonth)
45+
assertEquals(jodaLocalDateTime.hour(), localDateTime.hour)
46+
assertEquals(jodaLocalDateTime.minute(), localDateTime.minute)
47+
assertEquals(jodaLocalDateTime.second(), localDateTime.second)
48+
assertEquals(jodaLocalDateTime.nano(), localDateTime.nanosecond.toDouble())
49+
}
50+
}
51+
2852
@Test
2953
fun utc() {
3054
val utc: FixedOffsetTimeZone = TimeZone.UTC

0 commit comments

Comments
 (0)