zed icon indicating copy to clipboard operation
zed copied to clipboard

Function to format duration values

Open chrismo opened this issue 9 months ago • 2 comments

❯ super --version                 
Version: v1.18.0-350-g733dc02a1

❯ super -c "bucket(59s, 1m)"                                                     
0s

❯ super -c "bucket(59m, 1h)"
0s

I think bucket should retain the label of the second argument if possible. When it outputs 0 seconds for 59m in a 1h bucket, that's misleading IMO. I think it should say 0h for zero hours, implying it's anything less than 1 hour, not less than 1 second.

chrismo avatar May 20 '25 13:05 chrismo

@chrismo and I had a detailed offline discussion on this one, but I'll summarize here as I morph this issue into something we can circle back to in the future.

The effect shown above is not unique to the bucket function, but instead follows from the nature of the duration data type and how the tools present values in a single, unique form.

As regards the data type, we refer to the Data Model doc which describes a duration as:

signed 64-bit integer as nanoseconds

i.e., input values like 0s, 0m, and 0h are all equivalent ways of expressing what's ultimately stored as "zero nanoseconds". Then when such a value is read back, it's shown in a unique form, i.e., the tools have one way of expressing "zero nanoseconds", which in this case is 0s for all three.

$ super -version
Version: 9fcbd222b

$ super -S -c "
values {
  zero_seconds: 0s,
  zero_minutes: 0m,
  zero_hours: 0h
}"
{
    zero_seconds: 0s,
    zero_minutes: 0s,
    zero_hours: 0s
}

The tools don't make any attempt to "remember" the units in which a duration was originally expressed in a query (e.g., the m in the call to bucket) and express results with those same units, instead preferring to have one way to express whatever values make up a query result.

For when a user wants to output a duration value in a non-default way, the way we expect we'd go about it is similar to what currently exists with the strftime function for time values, i.e., a function that could use formatting rules to turn a duration into a string, which could potentially be cast back into a duration value again if desired (e.g., it could morph the duration value of 0s from bucket above into the string value 0m).

I'm going to change the title of this issue to reflect the intent to create such a function in the future.

philrz avatar May 22 '25 18:05 philrz

Sounds good, thx again Phil! :)

chrismo avatar May 23 '25 01:05 chrismo