Blog Infos
Author
Published
Topics
Author
Published
Topics

Kotlin 1.9.0 relased on August 23, 2023.

In addition to the new features, experimental features from earlier versions are now stable.

In today’s article, we will explore the stable Time API

Duration is stable since version 1.6.0

Major functions/interfaces/classes
  • markNow()— Marks a point in time on the given time source.
val valueTimeMark1 = TimeSource.Monotonic.markNow()
  • measureTime()— It returns the duration of the elapsed time interval after executing the given function block.
  • measureTimedValue() — It returns the duration as well as the result of the function execution
  • TimeSource — It’s an interface which provides the following methods

markNow(), measureTime(), measureTimedValue()

  • TimeMark — It’s an interface which provides the following methods
  • hasNotPassedNow()— Returns true , If the given time mark has not passed yet.
  • hasPassedNow() — Returns true if the time mark has passed based on the time source from which it was taken.
Sample code

By default measureTime() as well as measureTimedValue() both use a Monotonictime source.

If you want to override this behavior you can by implementing AbstractLongTimeSource .

Please check this example to see how we can use SystemClock.elapsedRealtimeNanos() in the case of Android

measureTime()
fun calculateProcessingTime() {
    val duration = measureTime {
        for (i in 0..<100) {
            println(i)
        }
    }
    println(duration)
}
measureTimedValue()
  • Learn more about destructuring declarations here
val sumOfTwoNums = { num1: Int, num2: Int ->
    num1 + num2
}

fun getResultAndTime() {
    // destructuring
    val (value, duration1) = TimeSource.Monotonic.measureTimedValue {
        sumOfTwoNums(5, 7)
    }

    println("Result value: $value")

    println("Time took: $duration1")
}
Mark and measure differences in time
Mark a specific moment
  • You can read more about Monotonic time here
val timeSource = TimeSource.Monotonic
val mark1 = TimeSource.Monotonic.markNow()
Measure differences in time ⏰
val timeSource = TimeSource.Monotonic

val valueTimeMark1 = TimeSource.Monotonic.markNow()
    
Thread.sleep(5000) // 5 seconds
    
val valueTimeMarkAfter5Seconds = timeSource.markNow()
    
println(valueTimeMarkAfter5Seconds > valueTimeMark1)  // true

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Kobweb:Creating websites in Kotlin leveraging Compose HTML

Kobweb is a Kotlin web framework that aims to make web development enjoyable by building on top of Compose HTML and drawing inspiration from Jetpack Compose.
Watch Video

Kobweb:Creating websites in Kotlin leveraging Compose HTML

David Herman
Ex-Googler, author of Kobweb

Kobweb:Creating websites in Kotlin leveraging Compose HTML

David Herman
Ex-Googler, author o ...

Kobweb:Creating websites in Kotlin leveraging Compose HTML

David Herman
Ex-Googler, author of Kob ...

Jobs

Getting duration components
48.days.toComponents { days, hours, minutes, seconds, nanoseconds ->
    // process each unit here as per your requirements 
}
Checking if the time mark has passed or not
hasPassedNow()
val valueTimeMark1 = TimeSource.Monotonic.markNow()

val valueTimeMark2 = valueTimeMark1 + 2.seconds

println(alueTimeMark2.hasPassedNow()) // true || false
hasNotPassedNow()
val valueTimeMark1 = TimeSource.Monotonic.markNow()

val valueTimeMark2 = valueTimeMark1 + 2.seconds

println(valueTimeMark2.hasNotPassedNow()) // true || false
Kotlin 1.9.0 relased on August 23, 2023. In addition to the new features, experimental features from earlier versions are now stable.

 

 

This article was previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
Hi, today I come to you with a quick tip on how to update…
READ MORE
blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Drag and Drop reordering in Recyclerview can be achieved with ItemTouchHelper (checkout implementation reference).…
READ MORE
Menu