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
+39.9k Golang : UDP client server read write example
+8.1k Golang : Metaprogramming example of wrapping a function
+8.9k Golang : Inject/embed Javascript before sending out to browser example
+8.8k Golang : Sort lines of text example
+10k Golang : Identifying Golang HTTP client request
+8.1k Swift : Convert (cast) Character to Integer?
+12.3k Golang : Search and extract certain XML data example
+5.2k Golang : Pad file extension automagically
+7k Nginx : How to block user agent ?
+10k Golang : How to tokenize source code with text/scanner package?
+14.3k Golang : How to check if your program is running in a terminal
+7.5k Android Studio : AlertDialog to get user attention example