Spec-Zone.ru › Kotlin 2

seconds

kotlin-stdlib/kotlin.time/Duration/Companion/seconds

Версия Kotlin: 1.6

val Int.seconds: Duration

Возвращает Duration, равный этому количеству секунд типа Int.

Примеры

import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.microseconds
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds

fun main() { 
   //sampleStart 
   println(61.seconds) // 1m 1s
println(0.5.seconds) // 500ms

// Large values can be converted to an infinite duration
println(9_000_000_000_000_000.seconds) // Infinity

// Fractional part is rounded to the nearest integer nanosecond value
println(2.000_000_000_4.seconds) // 2s
println(2.000_000_000_9.seconds) // 2.000000001s
// Or to the nearest integer millisecond value
println(9_000_000_000_000.001_5.seconds) // 104166666d 16h 0m 0.002s

// Double.NaN.seconds // will fail with IllegalArgumentException 
   //sampleEnd
}

val Long.seconds: Duration

Возвращает Duration, равный этому количеству секунд типа Long.

Примеры

import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.microseconds
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds

fun main() { 
   //sampleStart 
   println(61.seconds) // 1m 1s
println(0.5.seconds) // 500ms

// Large values can be converted to an infinite duration
println(9_000_000_000_000_000.seconds) // Infinity

// Fractional part is rounded to the nearest integer nanosecond value
println(2.000_000_000_4.seconds) // 2s
println(2.000_000_000_9.seconds) // 2.000000001s
// Or to the nearest integer millisecond value
println(9_000_000_000_000.001_5.seconds) // 104166666d 16h 0m 0.002s

// Double.NaN.seconds // will fail with IllegalArgumentException 
   //sampleEnd
}

val Double.seconds: Duration

Возвращает Duration, равный этому количеству секунд типа Double.

В зависимости от величины значение округляется до целого числа наносекунд или миллисекунд.

Выбрасывает исключение

IllegalArgumentException

если это значение типа Double является NaN.

Примеры

import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.microseconds
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds

fun main() { 
   //sampleStart 
   println(61.seconds) // 1m 1s
println(0.5.seconds) // 500ms

// Large values can be converted to an infinite duration
println(9_000_000_000_000_000.seconds) // Infinity

// Fractional part is rounded to the nearest integer nanosecond value
println(2.000_000_000_4.seconds) // 2s
println(2.000_000_000_9.seconds) // 2.000000001s
// Or to the nearest integer millisecond value
println(9_000_000_000_000.001_5.seconds) // 104166666d 16h 0m 0.002s

// Double.NaN.seconds // will fail with IllegalArgumentException 
   //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/-duration/-companion/seconds.html

Spec-Zone.ru

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