Skip to content

Commit b484deb

Browse files
committed
Rename zone parameter in Instant operations to timeZone
1 parent 48b0ea0 commit b484deb

File tree

4 files changed

+68
-67
lines changed

4 files changed

+68
-67
lines changed

core/commonMain/src/Instant.kt

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package kotlinx.datetime
88
import kotlin.time.Duration
99
import kotlin.time.ExperimentalTime
1010

11-
@OptIn(kotlin.time.ExperimentalTime::class)
11+
@OptIn(ExperimentalTime::class)
1212
public expect class Instant : Comparable<Instant> {
1313

1414
/** */
@@ -88,68 +88,70 @@ public fun String.toInstant(): Instant = Instant.parse(this)
8888
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
8989
* [LocalDateTime].
9090
*/
91-
public expect fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant
91+
public expect fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Instant
9292

9393
/**
9494
* @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
9595
*/
96-
public expect fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimePeriod
96+
public expect fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateTimePeriod
9797

9898
/**
9999
* The return value is clamped to [Long.MAX_VALUE] or [Long.MIN_VALUE] if [unit] is more granular than
100100
* [DateTimeUnit.DAY] and the result is too large.
101101
*
102102
* @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
103103
*/
104-
public expect fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long
104+
public expect fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long
105105

106106
/**
107107
* The return value is clamped to [Int.MAX_VALUE] or [Int.MIN_VALUE] if the result would otherwise cause an arithmetic
108108
* overflow.
109109
*
110110
* @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
111111
*/
112-
public fun Instant.daysUntil(other: Instant, zone: TimeZone): Int =
113-
until(other, DateTimeUnit.DAY, zone).clampToInt()
112+
public fun Instant.daysUntil(other: Instant, timeZone: TimeZone): Int =
113+
until(other, DateTimeUnit.DAY, timeZone).clampToInt()
114114

115115
/**
116116
* The return value is clamped to [Int.MAX_VALUE] or [Int.MIN_VALUE] if the result would otherwise cause an arithmetic
117117
* overflow.
118118
*
119119
* @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
120120
*/
121-
public fun Instant.monthsUntil(other: Instant, zone: TimeZone): Int =
122-
until(other, DateTimeUnit.MONTH, zone).clampToInt()
121+
public fun Instant.monthsUntil(other: Instant, timeZone: TimeZone): Int =
122+
until(other, DateTimeUnit.MONTH, timeZone).clampToInt()
123123

124124
/**
125125
* The return value is clamped to [Int.MAX_VALUE] or [Int.MIN_VALUE] if the result would otherwise cause an arithmetic
126126
* overflow.
127127
*
128128
* @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
129129
*/
130-
public fun Instant.yearsUntil(other: Instant, zone: TimeZone): Int =
131-
until(other, DateTimeUnit.YEAR, zone).clampToInt()
130+
public fun Instant.yearsUntil(other: Instant, timeZone: TimeZone): Int =
131+
until(other, DateTimeUnit.YEAR, timeZone).clampToInt()
132132

133-
public fun Instant.minus(other: Instant, zone: TimeZone): DateTimePeriod = other.periodUntil(this, zone)
133+
public fun Instant.minus(other: Instant, timeZone: TimeZone): DateTimePeriod =
134+
other.periodUntil(this, timeZone)
134135

135136

136137
/**
137138
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
138139
*/
139-
public expect fun Instant.plus(unit: DateTimeUnit, zone: TimeZone): Instant
140+
public expect fun Instant.plus(unit: DateTimeUnit, timeZone: TimeZone): Instant
140141

141142
/**
142143
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
143144
*/
144-
public expect fun Instant.plus(value: Int, unit: DateTimeUnit, zone: TimeZone): Instant
145+
public expect fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZone): Instant
145146

146147
/**
147148
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
148149
*/
149-
public expect fun Instant.plus(value: Long, unit: DateTimeUnit, zone: TimeZone): Instant
150+
public expect fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZone): Instant
150151

151152

152-
public fun Instant.minus(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long = other.until(this, unit, zone)
153+
public fun Instant.minus(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long =
154+
other.until(this, unit, timeZone)
153155

154156
internal const val DISTANT_PAST_SECONDS = -3217862419201
155157
internal const val DISTANT_FUTURE_SECONDS = 3093527980800

core/jsMain/src/Instant.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import kotlinx.datetime.internal.JSJoda.Instant as jtInstant
1414
import kotlinx.datetime.internal.JSJoda.Duration as jtDuration
1515
import kotlinx.datetime.internal.JSJoda.Clock as jtClock
1616
import kotlinx.datetime.internal.JSJoda.ChronoUnit
17-
import kotlinx.datetime.internal.JSJoda.LocalTime
1817
import kotlin.math.nextTowards
1918
import kotlin.math.truncate
2019

@@ -97,8 +96,8 @@ public actual class Instant internal constructor(internal val value: jtInstant)
9796
}
9897

9998

100-
public actual fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant = try {
101-
val thisZdt = this.value.atZone(zone.zoneId)
99+
public actual fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Instant = try {
100+
val thisZdt = this.value.atZone(timeZone.zoneId)
102101
with(period) {
103102
thisZdt
104103
.run { if (years != 0 && months == 0) plusYears(years) else this }
@@ -143,16 +142,16 @@ private fun ZonedDateTime.plusNanosFix(nanoseconds: Long): ZonedDateTime {
143142
private fun Instant.atZone(zone: TimeZone): ZonedDateTime = value.atZone(zone.zoneId)
144143
private fun jtInstant.checkZone(zone: TimeZone): jtInstant = apply { atZone(zone.zoneId) }
145144

146-
public actual fun Instant.plus(unit: DateTimeUnit, zone: TimeZone): Instant =
147-
plus(1, unit, zone)
145+
public actual fun Instant.plus(unit: DateTimeUnit, timeZone: TimeZone): Instant =
146+
plus(1, unit, timeZone)
148147

149-
public actual fun Instant.plus(value: Long, unit: DateTimeUnit, zone: TimeZone): Instant =
148+
public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZone): Instant =
150149
try {
151-
val thisZdt = this.atZone(zone)
150+
val thisZdt = this.atZone(timeZone)
152151
when (unit) {
153152
is DateTimeUnit.TimeBased -> {
154153
multiplyAndDivide(value, unit.nanoseconds, NANOS_PER_ONE.toLong()).let {
155-
(d, r) -> this.plusFix(d.toDouble(), r.toInt()).checkZone(zone)
154+
(d, r) -> this.plusFix(d.toDouble(), r.toInt()).checkZone(timeZone)
156155
}
157156
}
158157
is DateTimeUnit.DateBased.DayBased ->
@@ -165,13 +164,13 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, zone: TimeZone):
165164
throw e
166165
}
167166

168-
public actual fun Instant.plus(value: Int, unit: DateTimeUnit, zone: TimeZone): Instant =
167+
public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZone): Instant =
169168
try {
170-
val thisZdt = this.atZone(zone)
169+
val thisZdt = this.atZone(timeZone)
171170
when (unit) {
172171
is DateTimeUnit.TimeBased -> {
173172
multiplyAndDivide(value.toLong(), unit.nanoseconds, NANOS_PER_ONE.toLong()).let {
174-
(d, r) -> this.plusFix(d.toDouble(), r.toInt()).checkZone(zone)
173+
(d, r) -> this.plusFix(d.toDouble(), r.toInt()).checkZone(timeZone)
175174
}
176175
}
177176
is DateTimeUnit.DateBased.DayBased ->
@@ -185,9 +184,9 @@ public actual fun Instant.plus(value: Int, unit: DateTimeUnit, zone: TimeZone):
185184
}
186185

187186
@OptIn(ExperimentalTime::class)
188-
public actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimePeriod = try {
189-
var thisZdt = this.value.atZone(zone.zoneId)
190-
val otherZdt = other.value.atZone(zone.zoneId)
187+
public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateTimePeriod = try {
188+
var thisZdt = this.value.atZone(timeZone.zoneId)
189+
val otherZdt = other.value.atZone(timeZone.zoneId)
191190

192191
val months = thisZdt.until(otherZdt, ChronoUnit.MONTHS).toDouble(); thisZdt = thisZdt.plusMonths(months)
193192
val days = thisZdt.until(otherZdt, ChronoUnit.DAYS).toDouble(); thisZdt = thisZdt.plusDays(days) as ZonedDateTime
@@ -200,9 +199,9 @@ public actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimeP
200199
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e) else throw e
201200
}
202201

203-
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long = try {
204-
val thisZdt = this.atZone(zone)
205-
val otherZdt = other.atZone(zone)
202+
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long = try {
203+
val thisZdt = this.atZone(timeZone)
204+
val otherZdt = other.atZone(timeZone)
206205
when(unit) {
207206
is DateTimeUnit.TimeBased -> {
208207
multiplyAddAndDivide(other.epochSeconds - epochSeconds,

core/jvmMain/src/Instant.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ private fun Instant.atZone(zone: TimeZone): java.time.ZonedDateTime = try {
8686
throw DateTimeArithmeticException(e)
8787
}
8888

89-
public actual fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant {
89+
public actual fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Instant {
9090
try {
91-
val thisZdt = atZone(zone)
91+
val thisZdt = atZone(timeZone)
9292
return with(period) {
9393
thisZdt
9494
.run { if (years != 0 && months == 0) plusYears(years.toLong()) else this }
@@ -104,19 +104,19 @@ public actual fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant
104104
}
105105
}
106106

107-
public actual fun Instant.plus(unit: DateTimeUnit, zone: TimeZone): Instant =
108-
plus(1L, unit, zone)
107+
public actual fun Instant.plus(unit: DateTimeUnit, timeZone: TimeZone): Instant =
108+
plus(1L, unit, timeZone)
109109

110-
public actual fun Instant.plus(value: Int, unit: DateTimeUnit, zone: TimeZone): Instant =
111-
plus(value.toLong(), unit, zone)
110+
public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZone): Instant =
111+
plus(value.toLong(), unit, timeZone)
112112

113-
public actual fun Instant.plus(value: Long, unit: DateTimeUnit, zone: TimeZone): Instant =
113+
public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZone): Instant =
114114
try {
115-
val thisZdt = atZone(zone)
115+
val thisZdt = atZone(timeZone)
116116
when (unit) {
117117
is DateTimeUnit.TimeBased -> {
118118
multiplyAndDivide(value, unit.nanoseconds, NANOS_PER_ONE.toLong()).let {
119-
(d, r) -> this.value.plusSeconds(d).plusNanos(r).also { it.atZone(zone.zoneId) }
119+
(d, r) -> this.value.plusSeconds(d).plusNanos(r).also { it.atZone(timeZone.zoneId) }
120120
}
121121
}
122122
is DateTimeUnit.DateBased.DayBased ->
@@ -131,9 +131,9 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, zone: TimeZone):
131131

132132

133133
@OptIn(ExperimentalTime::class)
134-
public actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimePeriod {
135-
var thisZdt = this.atZone(zone)
136-
val otherZdt = other.atZone(zone)
134+
public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateTimePeriod {
135+
var thisZdt = this.atZone(timeZone)
136+
val otherZdt = other.atZone(timeZone)
137137

138138
val months = thisZdt.until(otherZdt, ChronoUnit.MONTHS); thisZdt = thisZdt.plusMonths(months)
139139
val days = thisZdt.until(otherZdt, ChronoUnit.DAYS); thisZdt = thisZdt.plusDays(days)
@@ -144,9 +144,9 @@ public actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimeP
144144
}
145145
}
146146

147-
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long = try {
148-
val thisZdt = this.atZone(zone)
149-
val otherZdt = other.atZone(zone)
147+
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long = try {
148+
val thisZdt = this.atZone(timeZone)
149+
val otherZdt = other.atZone(timeZone)
150150
when(unit) {
151151
is DateTimeUnit.TimeBased -> {
152152
multiplyAddAndDivide(other.epochSeconds - epochSeconds,

core/nativeMain/src/Instant.kt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -273,39 +273,39 @@ private fun Instant.check(zone: TimeZone): Instant = [email protected] {
273273
toZonedLocalDateTimeFailing(zone)
274274
}
275275

276-
actual fun Instant.plus(period: DateTimePeriod, zone: TimeZone): Instant = try {
276+
actual fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Instant = try {
277277
with(period) {
278-
val withDate = toZonedLocalDateTimeFailing(zone)
278+
val withDate = toZonedLocalDateTimeFailing(timeZone)
279279
.run { if (years != 0 && months == 0) plus(years, DateTimeUnit.YEAR) else this }
280280
.run { if (months != 0) plus(safeAdd(safeMultiply(years, 12), months), DateTimeUnit.MONTH) else this }
281281
.run { if (days != 0) plus(days, DateTimeUnit.DAY) else this }
282282
withDate.toInstant()
283283
.run { if (hours != 0)
284-
plus(hours.toLong() * SECONDS_PER_HOUR, 0).check(zone) else this }
284+
plus(hours.toLong() * SECONDS_PER_HOUR, 0).check(timeZone) else this }
285285
.run { if (minutes != 0)
286-
plus(minutes.toLong() * SECONDS_PER_MINUTE, 0).check(zone) else this }
287-
.run { if (seconds != 0L) plus(seconds, 0).check(zone) else this }
288-
.run { if (nanoseconds != 0L) plus(0, nanoseconds).check(zone) else this }
289-
}.check(zone)
286+
plus(minutes.toLong() * SECONDS_PER_MINUTE, 0).check(timeZone) else this }
287+
.run { if (seconds != 0L) plus(seconds, 0).check(timeZone) else this }
288+
.run { if (nanoseconds != 0L) plus(0, nanoseconds).check(timeZone) else this }
289+
}.check(timeZone)
290290
} catch (e: ArithmeticException) {
291291
throw DateTimeArithmeticException("Arithmetic overflow when adding CalendarPeriod to an Instant", e)
292292
} catch (e: IllegalArgumentException) {
293293
throw DateTimeArithmeticException("Boundaries of Instant exceeded when adding CalendarPeriod", e)
294294
}
295295

296-
public actual fun Instant.plus(unit: DateTimeUnit, zone: TimeZone): Instant =
297-
plus(1L, unit, zone)
298-
public actual fun Instant.plus(value: Int, unit: DateTimeUnit, zone: TimeZone): Instant =
299-
plus(value.toLong(), unit, zone)
300-
public actual fun Instant.plus(value: Long, unit: DateTimeUnit, zone: TimeZone): Instant = try {
296+
public actual fun Instant.plus(unit: DateTimeUnit, timeZone: TimeZone): Instant =
297+
plus(1L, unit, timeZone)
298+
public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZone): Instant =
299+
plus(value.toLong(), unit, timeZone)
300+
public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZone): Instant = try {
301301
when (unit) {
302302
is DateTimeUnit.DateBased -> {
303303
if (value < Int.MIN_VALUE || value > Int.MAX_VALUE)
304304
throw ArithmeticException("Can't add a Long date-based value, as it would cause an overflow")
305-
toZonedLocalDateTimeFailing(zone).plus(value.toInt(), unit).toInstant()
305+
toZonedLocalDateTimeFailing(timeZone).plus(value.toInt(), unit).toInstant()
306306
}
307307
is DateTimeUnit.TimeBased -> multiplyAndDivide(value, unit.nanoseconds, NANOS_PER_ONE.toLong()).let { (seconds, nanoseconds) ->
308-
check(zone).plus(seconds, nanoseconds).check(zone)
308+
check(timeZone).plus(seconds, nanoseconds).check(timeZone)
309309
}
310310
}
311311
} catch (e: ArithmeticException) {
@@ -315,9 +315,9 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, zone: TimeZone):
315315
}
316316

317317
@OptIn(ExperimentalTime::class)
318-
actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimePeriod {
319-
var thisLdt = toZonedLocalDateTimeFailing(zone)
320-
val otherLdt = other.toZonedLocalDateTimeFailing(zone)
318+
actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateTimePeriod {
319+
var thisLdt = toZonedLocalDateTimeFailing(timeZone)
320+
val otherLdt = other.toZonedLocalDateTimeFailing(timeZone)
321321

322322
val months = thisLdt.until(otherLdt, DateTimeUnit.MONTH).toInt() // `until` on dates never fails
323323
thisLdt = thisLdt.plus(months, DateTimeUnit.MONTH) // won't throw: thisLdt + months <= otherLdt, which is known to be valid
@@ -330,13 +330,13 @@ actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimePeriod {
330330
}
331331
}
332332

333-
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long =
333+
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long =
334334
when (unit) {
335335
is DateTimeUnit.DateBased ->
336-
toZonedLocalDateTimeFailing(zone).dateTime.until(other.toZonedLocalDateTimeFailing(zone).dateTime, unit)
336+
toZonedLocalDateTimeFailing(timeZone).dateTime.until(other.toZonedLocalDateTimeFailing(timeZone).dateTime, unit)
337337
.toLong()
338338
is DateTimeUnit.TimeBased -> {
339-
check(zone); other.check(zone)
339+
check(timeZone); other.check(timeZone)
340340
try {
341341
multiplyAddAndDivide(other.epochSeconds - epochSeconds,
342342
NANOS_PER_ONE.toLong(),

0 commit comments

Comments
 (0)