Golang : Convert seconds to human readable time format example



Tags : golang convert-seconds-to-human time plural

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



Tags : golang convert-seconds-to-human time plural

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