github.com/damirazo/docker@v1.9.0/pkg/units/duration.go (about)

     1  // Package units provides helper function to parse and print size and time units
     2  // in human-readable format.
     3  package units
     4  
     5  import (
     6  	"fmt"
     7  	"time"
     8  )
     9  
    10  // HumanDuration returns a human-readable approximation of a duration
    11  // (eg. "About a minute", "4 hours ago", etc.).
    12  func HumanDuration(d time.Duration) string {
    13  	if seconds := int(d.Seconds()); seconds < 1 {
    14  		return "Less than a second"
    15  	} else if seconds < 60 {
    16  		return fmt.Sprintf("%d seconds", seconds)
    17  	} else if minutes := int(d.Minutes()); minutes == 1 {
    18  		return "About a minute"
    19  	} else if minutes < 60 {
    20  		return fmt.Sprintf("%d minutes", minutes)
    21  	} else if hours := int(d.Hours()); hours == 1 {
    22  		return "About an hour"
    23  	} else if hours < 48 {
    24  		return fmt.Sprintf("%d hours", hours)
    25  	} else if hours < 24*7*2 {
    26  		return fmt.Sprintf("%d days", hours/24)
    27  	} else if hours < 24*30*3 {
    28  		return fmt.Sprintf("%d weeks", hours/24/7)
    29  	} else if hours < 24*365*2 {
    30  		return fmt.Sprintf("%d months", hours/24/30)
    31  	}
    32  	return fmt.Sprintf("%d years", int(d.Hours())/24/365)
    33  }