Skip to main content
Use the totimespan function to convert various data types to a timespan value representing a duration. This is helpful when you need to normalize duration values from different sources into timespan format for time-based calculations, comparisons, or aggregations. You typically use totimespan when working with duration strings, numeric values representing time intervals, or other types that need to be converted to timespan format for duration calculations.

For users of other query languages

If you come from other query languages, this section explains how to adjust your existing queries to achieve the same results in APL.
In Splunk, you use time functions or duration calculations with numeric values. In APL, totimespan provides a direct way to convert values to timespan format for duration operations.
... | eval duration = duration_field
In standard SQL, you use INTERVAL types or duration functions to work with time spans. In APL, totimespan provides a simpler way to convert values to timespan format.
SELECT INTERVAL '1' DAY AS duration FROM logs;

Usage

Syntax

totimespan(value)

Parameters

NameTypeDescription
valuedynamicThe value to convert to timespan.

Returns

If conversion is successful, the result is a timespan value. If conversion isn’t successful, the result is null.

Conversion behavior

The totimespan function converts values based on their type:
  • Integer/Float: Interpreted as nanoseconds. For example, 1000000000 represents one second.
  • String: Parsed as a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h", or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".

Use case examples

Convert numeric duration values to timespan format for duration-based analysis and filtering.Query
['sample-http-logs']
| extend duration_span = totimespan(['req_duration_ms'] * 1000000)
| extend is_slow = duration_span > totimespan('1ms')
| where is_slow == true
| project _time, ['uri'], ['req_duration_ms'], duration_span, is_slow
Run in PlaygroundOutput
_timeurireq_duration_msduration_spanis_slow
Jun 24, 09:28:10/api/users150000:00:01.5000000true
This example converts millisecond durations to timespan format and compares them to a threshold, enabling precise duration-based filtering and analysis.
  • todatetime: Converts input to datetime. Use todatetime for absolute time points, and totimespan for duration values.