seconds

Returns a Duration equal to this Int number of seconds.

Since Kotlin

1.6

Samples

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
}

Returns a Duration equal to this Long number of seconds.

Since Kotlin

1.6

Samples

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
}

Returns a Duration equal to this Double number of seconds.

Depending on its magnitude, the value is rounded to an integer number of nanoseconds or milliseconds.

Since Kotlin

1.6

Throws

if this Double value is NaN.

Samples

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
}