Skip to content

parseInterval - Convert a string to a time interval

A very common way to write out a duration is to use a string form, for example 2d. This is good, but it does not make it easy to parse and calculate with this value. parseInterval will take a string and turn it into an interval, to make it possible to perform calculations.

Syntax

parseInterval(string: string): interval

Arguments

NameTypeRequiredDescription
stringstringtrueThe interval to parse

Expected format

There are some strict requirements for the format that a string must take, in order for it to be parsed. The string should be of the form NdNhNmNsNmsNusNns, where N is the amount of each time unit.

NOTE: A given string doesn't need to list every timeunit.

The following must be true in order for a string to be parsed by parseInterval:

  • A string must consist of time unit components. A time unit component is defined as some non-negative integer, followed by one of: 'd''h''m''s''ms''us''ns'
  • There must be at least one time unit component
  • The same time unit cannot appear more than once. For example, 1d2d is considered invalid.
  • Components must be decreasing in time unit order - from days to nanoseconds. For example, 1d1s is valid, but 1s1d is not.
  • The string can begin with - to signify a negative interval. This is the only place where - may appear.

NOTE: If a string does not match the expected format, then parseInterval will return null.

Parse an interval string and add to a timestamp

Consider the following document:

{
    "timestamp": 1728763337,
    "job_name": "BUILD_VIDEO",
    "completed_in": "35m10s"
}

If we wish to calculate when this job will finish, we must first parse the value of completed_in, before adding it to the timestamp:

create completed_time from addTime(timestamp, completed_in.parseInterval())

Try it yourself

Open up your log or trace explore screen and enter the following command to see this in action:

create completed_in from '2d3s'
| create finished_time from addTime($m.timestamp, parseInterval(completed_in))
| choose formatTimestamp(finished_time)