Golang : Convert seconds to human readable time format example
Occasionally in the scientific world, we will encounter instruments that gave reading in seconds denomination and in this tutorial, we will learn how to convert the seconds into a human readable time format.
For example, converting 8888888888
seconds to 40
years 9
months 27
weeks 1
day 15
hours 48
minutes 8
seconds
The following code will first break the seconds down to the highest time unit (years) and take the remainder to break it down further until smallest time unit(seconds).
Here we go!
package main
import (
"fmt"
"math"
"strconv"
)
func plural(count int, singular string) (result string) {
if (count == 1) || (count == 0) {
result = strconv.Itoa(count) + " " + singular + " "
} else {
result = strconv.Itoa(count) + " " + singular + "s "
}
return
}
func secondsToHuman(input int) (result string) {
years := math.Floor(float64(input) / 60 / 60 / 24 / 7 / 30 / 12)
seconds := input % (60 * 60 * 24 * 7 * 30 * 12)
months := math.Floor(float64(seconds) / 60 / 60 / 24 / 7 / 30)
seconds = input % (60 * 60 * 24 * 7 * 30)
weeks := math.Floor(float64(seconds) / 60 / 60 / 24 / 7)
seconds = input % (60 * 60 * 24 * 7)
days := math.Floor(float64(seconds) / 60 / 60 / 24)
seconds = input % (60 * 60 * 24)
hours := math.Floor(float64(seconds) / 60 / 60)
seconds = input % (60 * 60)
minutes := math.Floor(float64(seconds) / 60)
seconds = input % 60
if years > 0 {
result = plural(int(years), "year") + plural(int(months), "month") + plural(int(weeks), "week") + plural(int(days), "day") + plural(int(hours), "hour") + plural(int(minutes), "minute") + plural(int(seconds), "second")
} else if months > 0 {
result = plural(int(months), "month") + plural(int(weeks), "week") + plural(int(days), "day") + plural(int(hours), "hour") + plural(int(minutes), "minute") + plural(int(seconds), "second")
} else if weeks > 0 {
result = plural(int(weeks), "week") + plural(int(days), "day") + plural(int(hours), "hour") + plural(int(minutes), "minute") + plural(int(seconds), "second")
} else if days > 0 {
result = plural(int(days), "day") + plural(int(hours), "hour") + plural(int(minutes), "minute") + plural(int(seconds), "second")
} else if hours > 0 {
result = plural(int(hours), "hour") + plural(int(minutes), "minute") + plural(int(seconds), "second")
} else if minutes > 0 {
result = plural(int(minutes), "minute") + plural(int(seconds), "second")
} else {
result = plural(int(seconds), "second")
}
return
}
func main() {
fmt.Println("3600 seconds : ", secondsToHuman(3600))
fmt.Println("9999 seconds : ", secondsToHuman(9999))
fmt.Println("8888888888 seconds : ", secondsToHuman(8888888888))
}
Output:
3600 seconds : 1 hour 0 minute 0 second
9999 seconds : 2 hours 46 minutes 39 seconds
8888888888 seconds : 40 years 9 months 27 weeks 1 day 15 hours 48 minutes 8 seconds
Happy coding!
Reference:
https://www.socketloop.com/tutorials/golang-convert-seconds-to-minutes-and-remainder-seconds
See also : Golang : Convert seconds to minutes and remainder seconds
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.5k Get Facebook friends working in same company
+4.8k Golang : Issue HTTP commands to server and port example
+33.9k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+10.7k Golang : Intercept and process UNIX signals example
+7.6k Golang : Get all countries phone codes
+33.3k Golang : Create x509 certificate, private and public keys
+7k Golang : Create zip/ePub file without compression(use Store algorithm)
+31.7k Golang : Convert []string to []byte examples
+6.4k Golang : Calculate pivot points for a cross
+11k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+20.6k Golang : Get password from console input without echo or masked
+22k Golang : How to read JPG(JPEG), GIF and PNG files ?