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
+12.1k Golang : Convert a rune to unicode style string \u
+6.3k Golang : Process non-XML/JSON formatted ASCII text file example
+7.9k Golang : Regular Expression find string example
+6.2k Golang : Measure execution time for a function
+4.4k Javascript : How to show different content with noscript?
+7.6k Golang : Detect sample rate, channels or latency with PortAudio
+6.8k Golang : Humanize and Titleize functions
+8.1k Findstr command the Grep equivalent for Windows
+48.3k Golang : How to convert JSON string to map and slice
+10.7k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+13.5k Golang : error parsing regexp: invalid or unsupported Perl syntax
+20k Golang : Count JSON objects and convert to slice/array