xhit / go-str2duration

Convert string to duration in golang
BSD 3-Clause "New" or "Revised" License
101 stars 7 forks source link

Proposal: Add String function that prints out days and weeks #2

Closed lukasmalkmus closed 1 year ago

lukasmalkmus commented 1 year ago

This is the current situation:

t, _ := str2duration.ParseDuration("2w")
fmt.Println(t.String()) // 672h0m0s

It would be nice to have str2duration.String(t time.Duration) string:

t, _ := str2duration.ParseDuration("2w")
fmt.Println(str2duration.String(t)) // 2w

Or to align with Go's output format (but that looks rather ugly):

t, _ := str2duration.ParseDuration("2w")
fmt.Println(str2duration.String(t)) // 2w0d0h0m0s
xhit commented 1 year ago

Sounds good!

lukasmalkmus commented 1 year ago

If you want to I can give a naive PR a try?

xhit commented 1 year ago

Hi Lukas, I have an implementation for this, currently is the long format 2w0d0h0m0s but I is possible return the short form 2w, I need to go home to make a PR. Thanks for the interest to contribute, I hope this will be released in this week.

xhit commented 1 year ago

Added in v2.1.0, thanks for the proposal Lukas 🚀

lukasmalkmus commented 1 year ago

Damn, that was fast! Nice one and thank you!