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
+7.6k Golang : Trim everything onward after a word
+11.2k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+11.2k Android Studio : Create custom icons for your application example
+19.9k Golang : Count number of digits from given integer value
+30.2k Golang : Generate random string
+6.3k PHP : Shuffle to display different content or advertisement
+29.6k Golang : How to get HTTP request header information?
+7.6k Golang : Ways to recover memory during run time.
+26.5k Golang : Force your program to run with root permissions
+17.6k Golang : Qt image viewer example
+6.7k Golang : Pat multiplexer routing example
+6.1k Apt-get to install and uninstall Golang