Spec-Zone.ru › Kotlin 2

nanosecondsOfSecond

kotlin-stdlib/kotlin.time/Instant/nanosecondsOfSecond

Начиная с Kotlin: 2.3

val nanosecondsOfSecond: Int

Количество наносекунд, на которое этот момент времени позже epochSeconds, отсчитываемых от эпохального момента.

Значение всегда неотрицательно и находится в диапазоне 0..999_999_999.

См. также

fromEpochSeconds

Примеры

import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds

fun main() { 
   //sampleStart 
   val currentInstant = Clock.System.now()
// The number of whole seconds that have passed since the Unix epoch.
println(currentInstant.epochSeconds)
// The number of nanoseconds that passed since the start of the second.
println(currentInstant.nanosecondsOfSecond) 
   //sampleEnd
}
import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds

fun main() { 
   //sampleStart 
   // When nanosecondAdjustment is within the range `0..999_999_999`,
// epochSeconds and nanosecondsOfSecond hold the exact values provided.
val instant1 = Instant.fromEpochSeconds(999_999, nanosecondAdjustment = 123_456_789)
println(instant1.epochSeconds) // 999999
println(instant1.nanosecondsOfSecond) // 123456789

// When nanosecondAdjustment exceeds `999_999_999`, the excess contributes to whole seconds,
// increasing epochSeconds. The remainder forms the new value of nanosecondsOfSecond.
val instant2 = Instant.fromEpochSeconds(1_000_000, nanosecondAdjustment = 100_123_456_789)
println(instant2.epochSeconds) // 1000100
println(instant2.nanosecondsOfSecond) // 123456789

// When nanosecondAdjustment is negative, epochSeconds is decreased
// to offset the negative nanoseconds and ensure nanosecondsOfSecond remains non-negative.
val instant3 = Instant.fromEpochSeconds(1_000_000, nanosecondAdjustment = -100_876_543_211)
println(instant3.epochSeconds) // 999899
println(instant3.nanosecondsOfSecond) // 123456789 
   //sampleEnd
}

© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-instant/nanoseconds-of-second.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API