from Epoch Seconds
Начиная с Kotlin: 2.3
fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long = 0): Instant
fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Int): Instant
Возвращает Instant, который находится на epochSeconds секунд позже момента эпохи 1970-01-01T00:00:00Z и на nanosecondAdjustment наносекунд позже.
nanosecondAdjustment указывает, на сколько наносекунд заданный момент времени позже момента, определяемого только значением epochSeconds. Для удобства и гибкости функция fromEpochSeconds принимает значения nanosecondAdjustment за пределами диапазона Instant.nanosecondsOfSecond. Отрицательное значение nanosecondAdjustment означает, что заданный момент времени будет раньше момента, определяемого только значением epochSeconds, а значения больше секунды будут учитываться при вычислении итогового количества секунд.
Например, если nanosecondAdjustment равно -1, результирующий Instant будет на 1 наносекунду раньше, чем если бы nanosecondAdjustment было равно 0, а если оно равно 1_000_000_000, результирующий Instant будет на 1 секунду позже.
Возвращаемое значение ограничивается границами Instant, если результат выходит за их пределы. В любом случае гарантируется возможность представления моментов времени между DISTANT_PAST и DISTANT_FUTURE.
fromEpochMilliseconds — аналогичная функция для входных данных с точностью до миллисекунд.
См. также
Примеры
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
// Creating an Instant representing the Unix epoch.
val unixEpochInstant = Instant.fromEpochSeconds(0)
// Creating an Instant from a specific number of milliseconds and nanoseconds since the Unix epoch.
val instant = Instant.fromEpochSeconds(999_999_999, nanosecondAdjustment = 999_999_999)
// The string representation of the constructed instants.
println(unixEpochInstant) // 1970-01-01T00:00:00Z
println(instant) // 2001-09-09T01:46:39.999999999Z
// Creating an Instant with negative nanosecondAdjustment
val earlierInstant = Instant.fromEpochSeconds(999_999_999, nanosecondAdjustment = -999_999_999)
println(earlierInstant) // 2001-09-09T01:46:38.000000001Z
println("earlierInstant < instant is ${earlierInstant < instant}") // true
// Creating an Instant with nanosecondAdjustment > 1s
val laterInstant = Instant.fromEpochSeconds(999_999_999, nanosecondAdjustment = 1_999_999_999)
println(laterInstant) // 2001-09-09T01:46:40.999999999Z
println("laterInstant > instant is ${laterInstant > instant}") // true
//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/-companion/from-epoch-seconds.html