Skip to content

Commit 48b0ea0

Browse files
committed
Rename Instant.offsetAt(TZ) -> offsetIn, introduce TimeZone.offsetAt(Instant)
1 parent 338b2ea commit 48b0ea0

File tree

6 files changed

+12
-10
lines changed

6 files changed

+12
-10
lines changed

core/commonMain/src/TimeZone.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ public expect class ZoneOffset : TimeZone {
4040
val totalSeconds: Int
4141
}
4242

43+
public fun TimeZone.offsetAt(instant: Instant): ZoneOffset =
44+
instant.offsetIn(this) // TODO: make the other way around
4345

4446
/**
4547
* @throws DateTimeArithmeticException if this value is too large to fit in [LocalDateTime].
4648
*/
4749
public expect fun Instant.toLocalDateTime(timeZone: TimeZone): LocalDateTime
4850

4951
/** */
50-
public expect fun Instant.offsetAt(timeZone: TimeZone): ZoneOffset
52+
public expect fun Instant.offsetIn(timeZone: TimeZone): ZoneOffset
5153

5254
/** */
5355
public expect fun LocalDateTime.toInstant(timeZone: TimeZone): Instant

core/commonTest/src/InstantTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,20 @@ class InstantTest {
182182
val zone = TimeZone.of("Europe/Berlin")
183183
val instant1 = LocalDateTime(2019, 10, 27, 2, 59, 0, 0).toInstant(zone)
184184
val ldt1 = instant1.toLocalDateTime(zone)
185-
val offset1 = instant1.offsetAt(zone)
185+
val offset1 = instant1.offsetIn(zone)
186186
checkComponents(ldt1, 2019, 10, 27, 2, 59)
187187
assertEquals(instant1, ldt1.toInstant(offset1))
188188

189189
val instant2 = instant1 + 1.hours
190190
val ldt2 = instant2.toLocalDateTime(zone)
191-
val offset2 = instant2.offsetAt(zone)
191+
val offset2 = instant2.offsetIn(zone)
192192
assertEquals(ldt1, ldt2)
193193
assertEquals(instant2, ldt2.toInstant(offset2))
194194
assertNotEquals(offset1, offset2)
195195
assertEquals(offset1.totalSeconds.seconds, offset2.totalSeconds.seconds + 1.hours)
196196

197197
val instant3 = instant2 - 2.hours
198-
val offset3 = instant3.offsetAt(zone)
198+
val offset3 = instant3.offsetIn(zone)
199199
assertEquals(offset1, offset3)
200200
}
201201

core/commonTest/src/TimeZoneTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TimeZoneTest {
2222
fun system() {
2323
val tz = TimeZone.currentSystemDefault()
2424
println(tz)
25-
val offset = Clock.System.now().offsetAt(tz)
25+
val offset = Clock.System.now().offsetIn(tz)
2626
assertTrue(offset.totalSeconds in -18 * 60 * 60 .. 18 * 60 * 60)
2727
// assertTrue(tz.id.contains('/')) // does not work on build agents, whose timezone is "UTC"
2828
// TODO: decide how to assert system tz properties

core/jsMain/src/TimeZone.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ actual open class TimeZone internal constructor(internal val zoneId: ZoneId) {
1313

1414
// experimental member-extensions
1515
public actual fun Instant.toLocalDateTime(): LocalDateTime = toLocalDateTime(this@TimeZone)
16-
public actual val Instant.offset: ZoneOffset get() = offsetAt(this@TimeZone)
16+
public actual val Instant.offset: ZoneOffset get() = offsetIn(this@TimeZone)
1717
public actual fun LocalDateTime.toInstant(): Instant = toInstant(this@TimeZone)
1818

1919
override fun equals(other: Any?): Boolean =
@@ -52,7 +52,7 @@ public actual fun Instant.toLocalDateTime(timeZone: TimeZone): LocalDateTime = t
5252
throw e
5353
}
5454

55-
public actual fun Instant.offsetAt(timeZone: TimeZone): ZoneOffset =
55+
public actual fun Instant.offsetIn(timeZone: TimeZone): ZoneOffset =
5656
timeZone.zoneId.rules().offsetOfInstant(this.value).let(::ZoneOffset)
5757

5858
public actual fun LocalDateTime.toInstant(timeZone: TimeZone): Instant =

core/jvmMain/src/TimeZone.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ actual open class TimeZone internal constructor(internal val zoneId: ZoneId) {
1515

1616
// experimental member-extensions
1717
public actual fun Instant.toLocalDateTime(): LocalDateTime = toLocalDateTime(this@TimeZone)
18-
public actual val Instant.offset: ZoneOffset get() = offsetAt(this@TimeZone)
18+
public actual val Instant.offset: ZoneOffset get() = offsetIn(this@TimeZone)
1919
public actual fun LocalDateTime.toInstant(): Instant = toInstant(this@TimeZone)
2020

2121
override fun equals(other: Any?): Boolean =
@@ -54,7 +54,7 @@ public actual fun Instant.toLocalDateTime(timeZone: TimeZone): LocalDateTime = t
5454
throw DateTimeArithmeticException(e)
5555
}
5656

57-
public actual fun Instant.offsetAt(timeZone: TimeZone): ZoneOffset =
57+
public actual fun Instant.offsetIn(timeZone: TimeZone): ZoneOffset =
5858
timeZone.zoneId.rules.getOffset(this.value).let(::ZoneOffset)
5959

6060
public actual fun LocalDateTime.toInstant(timeZone: TimeZone): Instant =

core/nativeMain/src/TimeZone.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public actual fun Instant.toLocalDateTime(timeZone: TimeZone): LocalDateTime =
289289
public actual fun LocalDateTime.toInstant(timeZone: TimeZone): Instant =
290290
with(timeZone) { toInstant() }
291291

292-
public actual fun Instant.offsetAt(timeZone: TimeZone): ZoneOffset =
292+
public actual fun Instant.offsetIn(timeZone: TimeZone): ZoneOffset =
293293
with(timeZone) { offset }
294294

295295
public actual fun LocalDate.atStartOfDayIn(timeZone: TimeZone): Instant =

0 commit comments

Comments
 (0)