@@ -164,17 +164,15 @@ public actual class Instant internal constructor(internal val epochSeconds: Long
164
164
}
165
165
166
166
actual companion object {
167
- actual fun now (): Instant {
168
- return memScoped {
169
- val timespecBuf = alloc< timespec> ()
170
- val error = clock_gettime(CLOCK_REALTIME , timespecBuf.ptr)
171
- assertEquals(0 , error)
172
- // according to https://en.cppreference.com/w/c/chrono/timespec,
173
- // tv_nsec in [0; 10^9), so no need to call [ofEpochSecond].
174
- val seconds = timespecBuf.tv_sec.toLong() // conversion is needed on some platforms
175
- val nanosec = timespecBuf.tv_nsec.toInt()
176
- Instant (seconds, nanosec)
177
- }
167
+ actual fun now (): Instant = memScoped {
168
+ val timespecBuf = alloc< timespec> ()
169
+ val error = clock_gettime(CLOCK_REALTIME , timespecBuf.ptr)
170
+ assertEquals(0 , error)
171
+ // according to https://en.cppreference.com/w/c/chrono/timespec,
172
+ // tv_nsec in [0; 10^9), so no need to call [ofEpochSecond].
173
+ val seconds = timespecBuf.tv_sec.toLong() // conversion is needed on some platforms
174
+ val nanosec = timespecBuf.tv_nsec.toInt()
175
+ Instant (seconds, nanosec)
178
176
}
179
177
180
178
// org.threeten.bp.Instant#ofEpochMilli
@@ -202,24 +200,24 @@ actual fun Instant.plus(period: CalendarPeriod, zone: TimeZone): Instant {
202
200
minutes.toLong() * SECONDS_PER_MINUTE ,
203
201
hours.toLong() * SECONDS_PER_HOUR ))
204
202
}
205
- val localDateTime = toLocalDateTime (zone)
203
+ val localDateTime = toZonedLocalDateTime (zone)
206
204
return with (period) {
207
205
localDateTime
208
206
.run { if (years != 0 && months == 0 ) plusYears(years.toLong()) else this }
209
207
.run { if (months != 0 ) plusMonths(years * 12L + months.toLong()) else this }
210
208
.run { if (days != 0 ) plusDays(days.toLong()) else this }
211
- }.toInstant(zone ).plus(seconds, period.nanoseconds)
209
+ }.toInstant().plus(seconds, period.nanoseconds)
212
210
}
213
211
214
212
actual fun Instant.plus (value : Int , unit : CalendarUnit , zone : TimeZone ): Instant =
215
213
plus(value.toLong(), unit, zone)
216
214
217
215
actual fun Instant.plus (value : Long , unit : CalendarUnit , zone : TimeZone ): Instant =
218
216
when (unit) {
219
- CalendarUnit .YEAR -> toLocalDateTime (zone).plusYears(value).toInstant(zone )
220
- CalendarUnit .MONTH -> toLocalDateTime (zone).plusMonths(value).toInstant(zone )
221
- CalendarUnit .WEEK -> toLocalDateTime (zone).plusDays(value * 7 ).toInstant(zone )
222
- CalendarUnit .DAY -> toLocalDateTime (zone).plusDays(value).toInstant(zone )
217
+ CalendarUnit .YEAR -> toZonedLocalDateTime (zone).plusYears(value).toInstant()
218
+ CalendarUnit .MONTH -> toZonedLocalDateTime (zone).plusMonths(value).toInstant()
219
+ CalendarUnit .WEEK -> toZonedLocalDateTime (zone).plusDays(value * 7 ).toInstant()
220
+ CalendarUnit .DAY -> toZonedLocalDateTime (zone).plusDays(value).toInstant()
223
221
/* From org.threeten.bp.ZonedDateTime#plusHours: the time is added to the raw LocalDateTime,
224
222
then org.threeten.bp.ZonedDateTime#create is called on the absolute instant
225
223
(gotten from org.threeten.bp.chrono.ChronoLocalDateTime#toEpochSecond). This, in turn,
@@ -244,8 +242,8 @@ actual fun Instant.plus(value: Long, unit: CalendarUnit, zone: TimeZone): Instan
244
242
245
243
@OptIn(ExperimentalTime ::class )
246
244
actual fun Instant.periodUntil (other : Instant , zone : TimeZone ): CalendarPeriod {
247
- var thisLdt = with (zone) { toZonedLocalDateTime() }
248
- val otherLdt = with (zone) { other.toZonedLocalDateTime() }
245
+ var thisLdt = toZonedLocalDateTime (zone)
246
+ val otherLdt = other.toZonedLocalDateTime(zone)
249
247
250
248
val months = thisLdt.until(otherLdt, CalendarUnit .MONTH ); thisLdt = thisLdt.plusMonths(months)
251
249
val days = thisLdt.until(otherLdt, CalendarUnit .DAY ); thisLdt = thisLdt.plusDays(days)
@@ -257,7 +255,7 @@ actual fun Instant.periodUntil(other: Instant, zone: TimeZone): CalendarPeriod {
257
255
}
258
256
259
257
actual fun Instant.until (other : Instant , unit : CalendarUnit , zone : TimeZone ): Long =
260
- with (zone) { toZonedLocalDateTime() .until(other.toZonedLocalDateTime(), unit) }
258
+ toZonedLocalDateTime (zone).until(other.toZonedLocalDateTime(zone ), unit)
261
259
262
260
actual fun Instant.daysUntil (other : Instant , zone : TimeZone ): Int = until(other, CalendarUnit .DAY , zone).toInt()
263
261
actual fun Instant.monthsUntil (other : Instant , zone : TimeZone ): Int = until(other, CalendarUnit .MONTH , zone).toInt()
0 commit comments