connect icon indicating copy to clipboard operation
connect copied to clipboard

Format timestamp into a duration string

Open mpartipilo opened this issue 2 years ago • 7 comments

I'm looking for a method to parse timestamps into a duration string. In short, basically the opposite of parse_duration.

root.delay_for = this.delay_for_ns.ts_duration()

# In: {"delay_for_ns":50000}
# Out:  {"delay_for":"50us"}

mpartipilo avatar Oct 23 '23 15:10 mpartipilo

Hello. For this method, I was thinking of taking in the output parameters as a variable in this format: root.delay_for = this.delay_for_ns.ts_duration(format: "us")

This way we can provide the output in whichever format is preferred by the user. What do you think? @mihaitodor

ZiyanK avatar Dec 11 '23 07:12 ZiyanK

That would differ from the builtin Golang Duration.String() method so I'm not sure what people in general would expect to be able to provide as input to that format parameter, which could open a big can of worms...

Note that for simple cases such as us, it's probably easier to just do the arithmetic in bloblang.

mihaitodor avatar Dec 11 '23 14:12 mihaitodor

I never thought that formatting would be an issue.

I was thinking of just reflecting what Go does without many moving parts, just for pretty printing.

Any more complex scenario could be handled like @mihaitodor suggests.

mpartipilo avatar Dec 12 '23 14:12 mpartipilo

But how would you know which format the user would want the output in? Unless we are just going to do it for us format? @mihaitodor

ZiyanK avatar Dec 15 '23 18:12 ZiyanK

That's why I suggested to do what Go does:

String returns a string representing the duration in the form "3d1h3m". Leading zero units are omitted. As a special case, durations less than one second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure that the leading digit is non-zero. Duration more than a day or more than a week lose granularity and are truncated to resp. days-hours-minutes and weeks-days-hours. The zero duration formats as 0s.

Not sure if that's what most users expect / need, so it might be worth thinking of other options. Maybe it's ISO-8601 is another option, since we already import https://pkg.go.dev/github.com/rickb777/date/period. I'm trying to avoid having to come up with some new standard.

mihaitodor avatar Dec 15 '23 23:12 mihaitodor

I guess the Golang's Duration.String() makes most sense then. You're right. Can I work on this implementation?

ZiyanK avatar Dec 16 '23 06:12 ZiyanK

@ZiyanK sure! Please do.

mihaitodor avatar Dec 17 '23 02:12 mihaitodor