Moved HumanDuration() to the main package

This commit is contained in:
Solomon Hykes 2013-03-21 00:52:43 -07:00
parent b8547f31e4
commit 299d0b2720
4 changed files with 30 additions and 27 deletions

View File

@ -449,7 +449,7 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri
// for idx, field := range []string{
// /* NAME */ name,
// /* ID */ img.Id,
// /* CREATED */ future.HumanDuration(time.Now().Sub(time.Unix(img.Created, 0))) + " ago",
// /* CREATED */ HumanDuration(time.Now().Sub(time.Unix(img.Created, 0))) + " ago",
// /* PARENT */ img.Parent,
// } {
// if idx == 0 {
@ -499,7 +499,7 @@ func (srv *Server) CmdPs(stdin io.ReadCloser, stdout io.Writer, args ...string)
/* ID */ container.Id,
/* IMAGE */ container.Image,
/* COMMAND */ command,
/* CREATED */ future.HumanDuration(time.Now().Sub(container.Created)) + " ago",
/* CREATED */ HumanDuration(time.Now().Sub(container.Created)) + " ago",
/* STATUS */ container.State.String(),
/* COMMENT */ "",
} {

View File

@ -23,29 +23,6 @@ func ComputeId(content io.Reader) (string, error) {
return fmt.Sprintf("%x", h.Sum(nil)[:8]), nil
}
func HumanDuration(d time.Duration) string {
if seconds := int(d.Seconds()); seconds < 1 {
return "Less than a second"
} else if seconds < 60 {
return fmt.Sprintf("%d seconds", seconds)
} else if minutes := int(d.Minutes()); minutes == 1 {
return "About a minute"
} else if minutes < 60 {
return fmt.Sprintf("%d minutes", minutes)
} else if hours := int(d.Hours()); hours == 1 {
return "About an hour"
} else if hours < 48 {
return fmt.Sprintf("%d hours", hours)
} else if hours < 24*7*2 {
return fmt.Sprintf("%d days", hours/24)
} else if hours < 24*30*3 {
return fmt.Sprintf("%d weeks", hours/24/7)
} else if hours < 24*365*2 {
return fmt.Sprintf("%d months", hours/24/30)
}
return fmt.Sprintf("%d years", d.Hours()/24/365)
}
func randomBytes() io.Reader {
return bytes.NewBuffer([]byte(fmt.Sprintf("%x", rand.Int())))
}

View File

@ -2,7 +2,6 @@ package docker
import (
"fmt"
"github.com/dotcloud/docker/future"
"sync"
"time"
)
@ -20,7 +19,7 @@ type State struct {
// String returns a human-readable description of the state
func (s *State) String() string {
if s.Running {
return fmt.Sprintf("Up %s", future.HumanDuration(time.Now().Sub(s.StartedAt)))
return fmt.Sprintf("Up %s", HumanDuration(time.Now().Sub(s.StartedAt)))
}
return fmt.Sprintf("Exit %d", s.ExitCode)
}

View File

@ -3,13 +3,40 @@ package docker
import (
"bytes"
"container/list"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
)
// HumanDuration returns a human-readable approximation of a duration
// (eg. "About a minute", "4 hours ago", etc.)
func HumanDuration(d time.Duration) string {
if seconds := int(d.Seconds()); seconds < 1 {
return "Less than a second"
} else if seconds < 60 {
return fmt.Sprintf("%d seconds", seconds)
} else if minutes := int(d.Minutes()); minutes == 1 {
return "About a minute"
} else if minutes < 60 {
return fmt.Sprintf("%d minutes", minutes)
} else if hours := int(d.Hours()); hours == 1 {
return "About an hour"
} else if hours < 48 {
return fmt.Sprintf("%d hours", hours)
} else if hours < 24*7*2 {
return fmt.Sprintf("%d days", hours/24)
} else if hours < 24*30*3 {
return fmt.Sprintf("%d weeks", hours/24/7)
} else if hours < 24*365*2 {
return fmt.Sprintf("%d months", hours/24/30)
}
return fmt.Sprintf("%d years", d.Hours()/24/365)
}
func Trunc(s string, maxlen int) string {
if len(s) <= maxlen {
return s