docs-v2/content/flux/v0.x/stdlib/universe/duration.md

2.8 KiB

title description aliases menu weight flux/v0.x/tags related introduced
duration() function The `duration()` function converts a single value to a duration.
/influxdb/v2.0/reference/flux/functions/built-in/transformations/type-conversions/duration/
/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/type-conversions/duration/
/influxdb/cloud/reference/flux/stdlib/built-in/transformations/type-conversions/duration/
flux_0_x_ref
name parent
duration universe
102
type-conversions
/flux/v0.x/data-types/basic/duration/
0.7.0

The duration() function converts a single value to a duration.

Output data type: Duration

duration(v: "1m")

Parameters

v

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

{{% note %}} duration() assumes numeric input values are nanoseconds. String input values must use duration literal representation. {{% /note %}}

Examples

Convert an integer to a duration

duration(v: 120000000)
// Returns 120ms

Convert a string to a duration

duration(v: "12h30m")
// Returns 12h30m

Convert values in a column to durations

The following example uses generate.from() to generate sample data and show how to iterate over values in a stream of tables and convert them to duration values.

{{% note %}} Flux does not support duration column types. This example converts an integer to a duration and stores the value as a string. {{% /note %}}

import "generate"

data = generate.from(
    count: 5,
    fn: (n) => (n + 1) * 3600000000000,
    start: 2021-01-01T00:00:00Z,
    stop: 2021-01-01T05:00:00Z,
)

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

{{% expand "View input and output" %}} {{< flex >}} {{% flex-content %}}

Input data
_time _value
2021-01-01T00:00:00Z 3600000000000
2021-01-01T01:00:00Z 7200000000000
2021-01-01T02:00:00Z 10800000000000
2021-01-01T03:00:00Z 14400000000000
2021-01-01T04:00:00Z 18000000000000

{{% /flex-content %}} {{% flex-content %}}

Output data
_time _value
2021-01-01T00:00:00Z 1h
2021-01-01T01:00:00Z 2h
2021-01-01T02:00:00Z 3h
2021-01-01T03:00:00Z 4h
2021-01-01T04:00:00Z 5h

{{% /flex-content %}} {{< /flex >}} {{% /expand %}}