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
Like many functions in DataPrime, parseInterval
supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.
Arguments
Name | Type | Required | Description |
---|---|---|---|
string | string | true | The 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, but1s1d
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:
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
:
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)