Back to Influxdb

duration() function

content/flux/v0/stdlib/universe/duration.md

latest2.7 KB
Original Source
<!------------------------------------------------------------------------------ IMPORTANT: This page was generated from comments in the Flux source code. Any edits made directly to this page will be overwritten the next time the documentation is generated. To make updates to this documentation, update the function comments above the function definition in the Flux source code: https://github.com/influxdata/flux/blob/master/stdlib/universe/universe.flux#L3210-L3210 Contributing to Flux: https://github.com/influxdata/flux#contributing Fluxdoc syntax: https://github.com/influxdata/flux/blob/master/docs/fluxdoc.md ------------------------------------------------------------------------------->

duration() converts a value to a duration type.

duration() treats integers and unsigned integers as nanoseconds. For a string to be converted to a duration type, the string must use duration literal representation.

Function type signature
js
(v: A) => duration

{{% caption %}} For more information, see Function type signatures. {{% /caption %}}

Parameters

v

({{< req >}}) Value to convert.

Examples

Convert a string to a duration

js
duration(v: "1h20m")// Returns 1h20m


Convert numeric types to durations

js
duration(v: 4800000000000)

// Returns 1h20m
duration(v: uint(v: 9600000000000))// Returns 2h40m


Convert values in a column to durations

Flux does not support duration column types. To store durations in a column, convert duration types to strings.

js
data
    |> map(fn: (r) => ({r with _value: string(v: duration(v: r._value))}))

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_timetag_value
2022-01-01T05:00:00Zt1-27000000
2022-01-01T09:00:10Zt112000000
2022-01-01T11:00:20Zt178000000
2022-01-01T16:00:30Zt117000000
2022-01-01T19:00:40Zt115000000
2022-01-01T20:00:50Zt1-42000000

Output data

_time_valuetag
2022-01-01T05:00:00Z-27mst1
2022-01-01T09:00:10Z12mst1
2022-01-01T11:00:20Z78mst1
2022-01-01T16:00:30Z17mst1
2022-01-01T19:00:40Z15mst1
2022-01-01T20:00:50Z-42mst1

{{% /expand %}} {{< /expand-wrapper >}}