Browse code

Moved HumanDuration() to the main package

Solomon Hykes authored on 2013/03/21 16:52:43
Showing 4 changed files
... ...
@@ -449,7 +449,7 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri
449 449
 		//					for idx, field := range []string{
450 450
 		//						/* NAME */ name,
451 451
 		//						/* ID */ img.Id,
452
-		//						/* CREATED */ future.HumanDuration(time.Now().Sub(time.Unix(img.Created, 0))) + " ago",
452
+		//						/* CREATED */ HumanDuration(time.Now().Sub(time.Unix(img.Created, 0))) + " ago",
453 453
 		//						/* PARENT */ img.Parent,
454 454
 		//					} {
455 455
 		//						if idx == 0 {
... ...
@@ -499,7 +499,7 @@ func (srv *Server) CmdPs(stdin io.ReadCloser, stdout io.Writer, args ...string)
499 499
 				/* ID */ container.Id,
500 500
 				/* IMAGE */ container.Image,
501 501
 				/* COMMAND */ command,
502
-				/* CREATED */ future.HumanDuration(time.Now().Sub(container.Created)) + " ago",
502
+				/* CREATED */ HumanDuration(time.Now().Sub(container.Created)) + " ago",
503 503
 				/* STATUS */ container.State.String(),
504 504
 				/* COMMENT */ "",
505 505
 			} {
... ...
@@ -23,29 +23,6 @@ func ComputeId(content io.Reader) (string, error) {
23 23
 	return fmt.Sprintf("%x", h.Sum(nil)[:8]), nil
24 24
 }
25 25
 
26
-func HumanDuration(d time.Duration) string {
27
-	if seconds := int(d.Seconds()); seconds < 1 {
28
-		return "Less than a second"
29
-	} else if seconds < 60 {
30
-		return fmt.Sprintf("%d seconds", seconds)
31
-	} else if minutes := int(d.Minutes()); minutes == 1 {
32
-		return "About a minute"
33
-	} else if minutes < 60 {
34
-		return fmt.Sprintf("%d minutes", minutes)
35
-	} else if hours := int(d.Hours()); hours == 1 {
36
-		return "About an hour"
37
-	} else if hours < 48 {
38
-		return fmt.Sprintf("%d hours", hours)
39
-	} else if hours < 24*7*2 {
40
-		return fmt.Sprintf("%d days", hours/24)
41
-	} else if hours < 24*30*3 {
42
-		return fmt.Sprintf("%d weeks", hours/24/7)
43
-	} else if hours < 24*365*2 {
44
-		return fmt.Sprintf("%d months", hours/24/30)
45
-	}
46
-	return fmt.Sprintf("%d years", d.Hours()/24/365)
47
-}
48
-
49 26
 func randomBytes() io.Reader {
50 27
 	return bytes.NewBuffer([]byte(fmt.Sprintf("%x", rand.Int())))
51 28
 }
... ...
@@ -2,7 +2,6 @@ package docker
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"github.com/dotcloud/docker/future"
6 5
 	"sync"
7 6
 	"time"
8 7
 )
... ...
@@ -20,7 +19,7 @@ type State struct {
20 20
 // String returns a human-readable description of the state
21 21
 func (s *State) String() string {
22 22
 	if s.Running {
23
-		return fmt.Sprintf("Up %s", future.HumanDuration(time.Now().Sub(s.StartedAt)))
23
+		return fmt.Sprintf("Up %s", HumanDuration(time.Now().Sub(s.StartedAt)))
24 24
 	}
25 25
 	return fmt.Sprintf("Exit %d", s.ExitCode)
26 26
 }
... ...
@@ -3,13 +3,40 @@ package docker
3 3
 import (
4 4
 	"bytes"
5 5
 	"container/list"
6
+	"fmt"
6 7
 	"io"
7 8
 	"os"
8 9
 	"os/exec"
9 10
 	"path/filepath"
10 11
 	"sync"
12
+	"time"
11 13
 )
12 14
 
15
+// HumanDuration returns a human-readable approximation of a duration
16
+// (eg. "About a minute", "4 hours ago", etc.)
17
+func HumanDuration(d time.Duration) string {
18
+	if seconds := int(d.Seconds()); seconds < 1 {
19
+		return "Less than a second"
20
+	} else if seconds < 60 {
21
+		return fmt.Sprintf("%d seconds", seconds)
22
+	} else if minutes := int(d.Minutes()); minutes == 1 {
23
+		return "About a minute"
24
+	} else if minutes < 60 {
25
+		return fmt.Sprintf("%d minutes", minutes)
26
+	} else if hours := int(d.Hours()); hours == 1 {
27
+		return "About an hour"
28
+	} else if hours < 48 {
29
+		return fmt.Sprintf("%d hours", hours)
30
+	} else if hours < 24*7*2 {
31
+		return fmt.Sprintf("%d days", hours/24)
32
+	} else if hours < 24*30*3 {
33
+		return fmt.Sprintf("%d weeks", hours/24/7)
34
+	} else if hours < 24*365*2 {
35
+		return fmt.Sprintf("%d months", hours/24/30)
36
+	}
37
+	return fmt.Sprintf("%d years", d.Hours()/24/365)
38
+}
39
+
13 40
 func Trunc(s string, maxlen int) string {
14 41
 	if len(s) <= maxlen {
15 42
 		return s