@@ -10,6 +10,8 @@ import kotlin.math.roundToInt
10
10
import kotlin.test.*
11
11
import kotlin.time.Duration.Companion.milliseconds
12
12
import kotlin.time.Duration.Companion.seconds
13
+ import kotlinx.datetime.test.JSJoda.Instant as jtInstant
14
+ import kotlinx.datetime.test.JSJoda.ZoneId as jtZoneId
13
15
14
16
class JsJodaTimezoneTest {
15
17
@Test
@@ -25,33 +27,35 @@ class JsJodaTimezoneTest {
25
27
fun iterateOverAllTimezones () {
26
28
for (id in TimeZone .availableZoneIds) {
27
29
val rules = rulesForId(id) ? : throw AssertionError (" No rules for $id " )
28
- val jodaZone = kotlinx.datetime.test. JSJoda . ZoneId .of(id)
29
- assertNull(rules.recurringZoneRules)
30
+ val jodaZone = jtZoneId .of(id)
31
+ assertNull(rules.recurringZoneRules) // js-joda doesn't expose recurring rules
30
32
fun checkAtInstant (instant : Instant ) {
31
- val jodaInstant = kotlinx.datetime.test.JSJoda .Instant .ofEpochMilli(instant.toEpochMilliseconds().toDouble())
32
- val zdt = jodaInstant.atZone(jodaZone)
33
33
val offset = rules.infoAtInstant(instant)
34
34
val ourLdt = instant.toLocalDateTime(offset)
35
- val theirLdt = LocalDateTime (
36
- zdt.year(),
37
- zdt.monthValue(),
38
- zdt.dayOfMonth(),
39
- zdt.hour(),
40
- zdt.minute(),
41
- zdt.second(),
42
- zdt.nano().roundToInt()
43
- )
44
- if ((ourLdt.toInstant(TimeZone .UTC ) - theirLdt.toInstant(TimeZone .UTC )).absoluteValue > 1 .seconds) {
45
- // It seems that sometimes, js-joda interprets its data incorrectly by at most one second,
46
- // and we don't want to replicate that.
47
- // Example: America/Noronha at 1914-01-01T02:09:39.998Z:
48
- // - Computed 1913-12-31T23:59:59.998 with offset -02:09:40
49
- // - 1914-01-01T00:00:00.998-02:09:39[America/Noronha] is js-joda's interpretation
50
- // The raw data representing the offset is `29.E`, which is `2 * 60 + 9 + (ord 'E' - 29) / 60`,
51
- // and `ord 'E'` is 69, so the offset is -2:09:40.
52
- // Thus, we allow a difference of 1 second.
53
- throw AssertionError (" Failed for $id at $instant : computed $ourLdt with offset $offset , but $zdt is correct" )
35
+ val zdt = jtInstant.ofEpochMilli(instant.toEpochMilliseconds().toDouble()).atZone(jodaZone)
36
+ val theirLdt = with (zdt) {
37
+ LocalDateTime (
38
+ year(),
39
+ monthValue(),
40
+ dayOfMonth(),
41
+ hour(),
42
+ minute(),
43
+ second(),
44
+ nano().roundToInt()
45
+ )
54
46
}
47
+ // It seems that sometimes, js-joda interprets its data incorrectly by at most one second,
48
+ // and we don't want to replicate that.
49
+ // Example: America/Noronha at 1914-01-01T02:09:39.998Z:
50
+ // - Computed 1913-12-31T23:59:59.998 with offset -02:09:40
51
+ // - 1914-01-01T00:00:00.998-02:09:39[America/Noronha] is js-joda's interpretation
52
+ // The raw data representing the offset is `29.E`, which is `2 * 60 + 9 + (ord 'E' - 29) / 60`,
53
+ // and `ord 'E'` is 69, so the offset is -2:09:40.
54
+ // Thus, we allow a difference of 1 second.
55
+ assertTrue(
56
+ (ourLdt.toInstant(TimeZone .UTC ) - theirLdt.toInstant(TimeZone .UTC )).absoluteValue <= 1 .seconds,
57
+ " Failed for $id at $instant : computed $ourLdt with offset $offset , but $zdt is correct"
58
+ )
55
59
}
56
60
fun checkTransition (instant : Instant ) {
57
61
checkAtInstant(instant - 2 .milliseconds)
0 commit comments