github.com/lmars/docker@v1.6.0-rc2/pkg/units/duration.go (about)

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