Time Measurement
The Kotlin standard library gives you the tools to calculate and measure time in different units. Accurate time measurement is important for activities like:
Managing threads or processes
Collecting statistics
Detecting timeouts
Debugging
By default, time is measured using a monotonic time source, but other time sources can be configured.
Calculate duration
To represent an amount of time, the standard library has the Duration class. A Duration can be expressed in the following units from the DurationUnit enum class:
NANOSECONDSMICROSECONDSMILLISECONDSSECONDSMINUTESHOURSDAYS
A Duration can be positive, negative, zero, positive infinity, or negative infinity.
Create duration
To create a Duration, use the extension properties available for Int, Long, and Double types: nanoseconds, microseconds, milliseconds, seconds, minutes, hours, and days.
You can also perform basic arithmetic with Duration objects:
Get string representation
It can be useful to have a string representation of a Duration so that you can print, serialize, transfer, or store it.
To get a string representation, use the .toString() function. By default, the time is reported using each unit that is present. For example: 1h 0m 45.677s or -(6d 5h 5m 28.284s)
To configure the output, use the .toString() function with your desired DurationUnit and number of decimal places as function parameters:
Convert duration
To convert your Duration into a different DurationUnit, use the following properties:
inWholeNanosecondsinWholeMicrosecondsinWholeMillisecondsinWholeSecondsinWholeMinutesinWholeHoursinWholeDays
Alternatively, you can use your desired DurationUnit as a function parameter in the following extension functions:
.toInt().toDouble().toLong()
Compare duration
To check if Duration objects are equal, use the equality operator (==):
To compare Duration objects, use the comparison operators (<, >):
Break duration into components
To break down a Duration into its time components and perform a further action, use the overload of the toComponents() function.
Add your desired action as a function or lambda expression as a function parameter.
In this example, the lambda expression has hours and minutes as function parameters with underscores (_) for the unused seconds and nanoseconds parameters.
The expression returns a concatenated string using string templates to get the desired output format of hours and minutes.
Measure time
To track the passage of time, the standard library provides tools so that you can easily:
Measure the time taken to execute some code with your desired time unit.
Mark a moment in time.
Compare and subtract two moments in time.
Check how much time has passed since a specific moment in time.
Check whether the current time has passed a specific moment in time.
Measure code execution time
To measure the time taken to execute a block of code, use the measureTime inline function:
To measure the time taken to execute a block of code and return the value of the block of code, use inline function measureTimedValue.
By default, both functions use a monotonic time source.
Mark moments in time
To mark a specific moment in time, use the TimeSource interface and the markNow() function to create a TimeMark:
Measure differences in time
To measure differences between TimeMark objects from the same time source, use the subtraction operator (-).
To compare TimeMark objects from the same time source, use the comparison operators (<, >).
To check if a deadline has passed or a timeout has been reached, use the hasPassedNow() and hasNotPassedNow() extension functions: