Skip to content

Instantly share code, notes, and snippets.

@misterunix
Created April 26, 2024 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misterunix/556f8f98fa8a6113729ab88d0c3692f6 to your computer and use it in GitHub Desktop.
Save misterunix/556f8f98fa8a6113729ab88d0c3692f6 to your computer and use it in GitHub Desktop.
I run some very long tasks and I like to know how long they ran.
// Convert seconds to days, hours, minutes and seconds
func Seconds2Weeks(s float64) string {
var result string
si := int(s)
weeks := si / (7 * 24 * 3600) // get weeks from seconds
si = si % (7 * 24 * 3600)
day := si / (24 * 3600) // get days from seconds
si = si % (24 * 3600)
hour := si / 3600 // get hours from remaining seconds
si %= 3600
minutes := si / 60 // get minutes from remaining seconds
si %= 60
result = fmt.Sprintf("%dw:%dd:%02dh:%02dm:%02ds", weeks, day, hour, minutes, si)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment