Duration
| Description: | Represent a unit of a passage of time |
| Annotation: | duration |
| Literal Syntax: | A numeric (integer or decimal) literal followed immediately by a duration unit (listed below). E.g., 10sec, 987us, -34.65day |
| Casts: | into duration |
| See also: | Types of Data - Durations |
Additional Language Notes
Durations are internally stored as a number of nanoseconds. When displayed, a duration is broken down into all of its units:
3.14day # => 3day 3hr 21min 36sec -34.65day # => -4wk 6day 15hr 36minThis chart shows all duration units currently supported:
Duration Length nsnanosecond us,µsorμsmicrosecond msmillisecond secsecond minminute hrhour dayday wkweek The microsecond unit has three spellings:
us,µswith U+00B5 MICRO SIGN, andμswith U+03BC GREEK SMALL LETTER MU. The two look alike but are different characters. Nushell displays microseconds with the micro sign,µs.Rules for duration literals:
Units are case-sensitive.
1Secand1HRare not durations. At the start of a pipeline Nushell looks for a command with that name, and anywhere else the word is a string.The number can have
_separators, a leading+, an exponent, or a leading dot:1_000ms,+5sec,1e3secand.5secare all durations. A leading dot can't follow a sign, though.-.5secis a string, so write-0.5sec.A fraction of a nanosecond is dropped, so
1.5nsis1ns.A literal has one number and one unit.
1hr30minis not a duration. Add the parts instead:1hr + 30min.A duration is a signed 64-bit count of nanoseconds, which covers about 292 years in either direction. A literal outside that range is a parse error, and arithmetic that leaves it is a
nu::shell::operator_overflowerror:9223372036854775808ns # => Error: nu::parser::literal_too_large # => # => × Invalid literal # => ╭─[repl_entry #1:1:1] # => 1 │ 9223372036854775808ns # => · ──────────┬────────── # => · ╰── duration too large # => ╰────
Datetime values can be combined with durations in calculations:
2024-08-12T11:49:27-04:00 + 1day | format date "%+" # => 2024-08-13T11:49:27-04:00 2024-08-12T11:50:30-04:00 - 2019-05-10T09:59:12-07:00 # => 274wk 2day 22hr 51min 18secFor example,
(date now) + 1dayis this time tomorrow.Months, years, centuries and millenniums are not precise as to the exact number of nanoseconds and thus are not valid duration literals. Users are free to define their own constants for specific months or years.
Converting to a duration
into duration accepts these input types:
| Input | Result |
|---|---|
int | That many nanoseconds, or that many of the unit that --unit names |
float | The same as an int. A fraction of a nanosecond is dropped, so 1.5 gives 1ns. |
string | A number, one or more duration literals separated by spaces, or a clock time (see below) |
record | A duration built from fields named after units (see below) |
duration | The value unchanged |
--unit (-u) names the unit of a number: ns, us, µs, ms, sec, min, hr, day or wk. The number can be an int, a float, or a string that holds only a number. Months and years aren't units, and any other name is a nu::shell::invalid_unit error. With a duration or record as input, --unit is an incompatible_parameters error.
90 | into duration --unit sec
# => 1min 30sec
1.5 | into duration --unit hr
# => 1hr 30min
"90" | into duration
# => 90nsA string that isn't a number is read in one of two ways:
- If it contains a colon, the whole string has to be a clock time,
h:mm:sswith an optional fraction of up to nine digits after the seconds. The hours can have any number of digits, while the minutes and seconds take two each and must be below 60. A clock time can't be negative."100:00:00"is4day 4hr. - Otherwise every word, separated by whitespace, has to be a duration literal, and the words are added together. The rules for literals apply: units are case-sensitive, a fraction or an exponent is allowed (
"1.5hr"), and a minus sign subtracts that part ("1hr -30min"is30min). A space between number and unit, or two parts written together as in"1hr30min", is an error.
"1hr 30min" | into duration
# => 1hr 30min
"01:02:03.5" | into duration
# => 1hr 2min 3sec 500ms
"1hr30min" | into duration
# => Error: nu::shell::invalid_unit
# =>
# => × Invalid unit
# => ╭─[repl_entry #1:1:1]
# => 1 │ "1hr30min" | into duration
# => · ────┬───
# => · ╰── encountered here
# => ╰────
# => help: Supported units are: ns, us, µs, ms, sec, min, hr, day, wkAn empty string, or one with only whitespace, fails with "empty duration string". A clock time that doesn't fit the pattern, such as "1:30", fails with "invalid clock-style duration".
A record can have the fields week, day, hour, minute, second, millisecond, microsecond and nanosecond, each holding a non-negative int, plus a sign field that holds "+" or "-". Missing fields count as 0, so {} | into duration is 0sec. Any other field name, a float, or a negative number is an error. This is the record that into record makes from a duration, so the two commands undo each other:
{hour: 1, minute: 30, sign: "-"} | into duration
# => -1hr 30min
(-1hr - 30min) | into record | into duration
# => -1hr 30minTo convert some fields of a record or columns of a table, name them as cell paths after the command. A table without cell paths is read as a table of such records, one duration per row. A list of strings isn't accepted, so convert each item with each:
[[task time]; [build "1:30:00"] [test "0:02:15"]] | into duration time
# => ╭───┬───────┬────────────╮
# => │ # │ task │ time │
# => ├───┼───────┼────────────┤
# => │ 0 │ build │ 1hr 30min │
# => │ 1 │ test │ 2min 15sec │
# => ╰───┴───────┴────────────╯
["1hr" "2min"] | each { into duration }
# => ╭───┬──────╮
# => │ 0 │ 1hr │
# => │ 1 │ 2min │
# => ╰───┴──────╯A result outside the duration range, about 292 years either way, fails with "duration too large".
Formatting durations
format duration turns a duration into a string in a single unit, instead of the mix of units that Nushell displays:
90sec | format duration min
# => 1.50 min
1day | format duration min
# => 1440 minThe unit can be any duration unit, or month (30 days), yr (365 days) or dec (10 years of 365 days). Case doesn't matter, so MIN works too. us is shown as µs. Any other unit is an error:
1hr | format duration hours
# => Error: nu::shell::invalid_unit
# =>
# => × Invalid unit
# => ╭─[repl_entry #1:1:23]
# => 1 │ 1hr | format duration hours
# => · ──┬──
# => · ╰── encountered here
# => ╰────
# => help: Supported units are: ns, us, µs, ms, sec, min, hr, day, wk, month, yr,
# => decA whole number in that unit is shown without decimals. Otherwise the number of decimal places comes from $env.config.float_precision (default 2), as set in config.nu or by an earlier command line.
The input can be a duration, a list of durations, or a table with the columns to format named as cell paths after the unit. A record isn't accepted, and neither is a table without cell paths.
[[name time]; [build 95sec] [test 2min]] | format duration min time
# => ╭───┬───────┬──────────╮
# => │ # │ name │ time │
# => ├───┼───────┼──────────┤
# => │ 0 │ build │ 1.58 min │
# => │ 1 │ test │ 2 min │
# => ╰───┴───────┴──────────╯The result is a string, so it no longer sorts or adds up as a duration. Keep the duration for calculations and format it last.
Common commands that can be used with duration
sleeptimeitinto duration,format durationmathsubcommands such asmath sum,math maxandmath stddev, described in Working with Numberssys host(theuptimecolumn)
Operators that can be used with duration
==,!=,<,<=,>,>=+,-*,/,//andmodwith a number, which return a duration/,//andmodwith another duration, which return afloat, anintand a duration respectively1hr * 2 # => 2hr 1wk / 1day # => 7.0 1wk // 1day # => 7 10sec mod 3sec # => 1sec+with a datetime, in either order, and-from a datetime. See Datetime.
A number can also go on the left of *, so 2 * 1hr is 2hr and 1.5 * 1hr is 1hr 30min. It can't go on the left of /: 2 / 1hr is an error.
A duration and a plain number don't mix in +, -, <, <=, > or >=, because a bare number has no unit. 1hr + 1 and 1hr > 60 are parse errors. Write the unit: 1hr + 1sec. == and != don't fail. Values of different types are never equal, so 1hr == 60 is false.