Browse code

Merge pull request #13165 from ahmetalpbalkan/durations

Allow duration strings as --since/--until

Antonio Murdaca authored on 2015/06/04 05:14:42
Showing 8 changed files
... ...
@@ -2,6 +2,7 @@ package client
2 2
 
3 3
 import (
4 4
 	"net/url"
5
+	"time"
5 6
 
6 7
 	"github.com/docker/docker/opts"
7 8
 	flag "github.com/docker/docker/pkg/mflag"
... ...
@@ -36,11 +37,12 @@ func (cli *DockerCli) CmdEvents(args ...string) error {
36 36
 			return err
37 37
 		}
38 38
 	}
39
+	ref := time.Now()
39 40
 	if *since != "" {
40
-		v.Set("since", timeutils.GetTimestamp(*since))
41
+		v.Set("since", timeutils.GetTimestamp(*since, ref))
41 42
 	}
42 43
 	if *until != "" {
43
-		v.Set("until", timeutils.GetTimestamp(*until))
44
+		v.Set("until", timeutils.GetTimestamp(*until, ref))
44 45
 	}
45 46
 	if len(eventFilterArgs) > 0 {
46 47
 		filterJSON, err := filters.ToParam(eventFilterArgs)
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"encoding/json"
5 5
 	"fmt"
6 6
 	"net/url"
7
+	"time"
7 8
 
8 9
 	"github.com/docker/docker/api/types"
9 10
 	flag "github.com/docker/docker/pkg/mflag"
... ...
@@ -46,7 +47,7 @@ func (cli *DockerCli) CmdLogs(args ...string) error {
46 46
 	v.Set("stderr", "1")
47 47
 
48 48
 	if *since != "" {
49
-		v.Set("since", timeutils.GetTimestamp(*since))
49
+		v.Set("since", timeutils.GetTimestamp(*since, time.Now()))
50 50
 	}
51 51
 
52 52
 	if *times {
... ...
@@ -37,6 +37,10 @@ and Docker images will report:
37 37
 **--until**=""
38 38
    Stream events until this timestamp
39 39
 
40
+You can specify `--since` and `--until` parameters as an RFC 3339 date,
41
+a UNIX timestamp, or a Go duration string (e.g. `1m30s`, `3h`). Docker computes
42
+the date relative to the client machine’s time.
43
+
40 44
 # EXAMPLES
41 45
 
42 46
 ## Listening for Docker events
... ...
@@ -63,6 +67,15 @@ Again the output container IDs have been shortened for the purposes of this docu
63 63
     2015-01-28T20:25:45.000000000-08:00 c21f6c22ba27: (from whenry/testimage:latest) die
64 64
     2015-01-28T20:25:46.000000000-08:00 c21f6c22ba27: (from whenry/testimage:latest) stop
65 65
 
66
+The following example outputs all events that were generated in the last 3 minutes,
67
+relative to the current time on the client machine:
68
+
69
+    # docker events --since '3m'
70
+    2015-05-12T11:51:30.999999999Z07:00 4386fb97867d: (from ubuntu-1:14.04) die
71
+    2015-05-12T15:52:12.999999999Z07:00 4 4386fb97867d: (from ubuntu-1:14.04) stop
72
+    2015-05-12T15:53:45.999999999Z07:00  7805c1d35632: (from redis:2.8) die
73
+    2015-05-12T15:54:03.999999999Z07:00  7805c1d35632: (from redis:2.8) stop
74
+
66 75
 # HISTORY
67 76
 April 2014, Originally compiled by William Henry (whenry at redhat dot com)
68 77
 based on docker.com source material and internal work.
... ...
@@ -41,6 +41,12 @@ then continue streaming new output from the container’s stdout and stderr.
41 41
 **--tail**="all"
42 42
    Output the specified number of lines at the end of logs (defaults to all logs)
43 43
 
44
+The `--since` option shows only the container logs generated after
45
+a given date. You can specify the date as an RFC 3339 date, a UNIX
46
+timestamp, or a Go duration string (e.g. `1m30s`, `3h`). Docker computes
47
+the date relative to the client machine’s time. You can combine
48
+the `--since` option with either or both of the `--follow` or `--tail` options.
49
+
44 50
 # HISTORY
45 51
 April 2014, Originally compiled by William Henry (whenry at redhat dot com)
46 52
 based on docker.com source material and internal work.
... ...
@@ -1123,6 +1123,10 @@ and Docker images will report:
1123 1123
 
1124 1124
     untag, delete
1125 1125
 
1126
+The `--since` and `--until` parameters can be Unix timestamps, RFC3339
1127
+dates or Go duration strings (e.g. `10m`, `1h30m`) computed relative to
1128
+client machine’s time.
1129
+
1126 1130
 #### Filtering
1127 1131
 
1128 1132
 The filtering flag (`-f` or `--filter`) format is of "key=value". If you would
... ...
@@ -1186,6 +1190,15 @@ You'll need two shells for this example.
1186 1186
     2014-05-10T17:42:14.999999999Z07:00 7805c1d35632: (from redis:2.8) die
1187 1187
     2014-09-03T15:49:29.999999999Z07:00 7805c1d35632: (from redis:2.8) stop
1188 1188
 
1189
+This example outputs all events that were generated in the last 3 minutes,
1190
+relative to the current time on the client machine:
1191
+
1192
+    $ docker events --since '3m'
1193
+    2015-05-12T11:51:30.999999999Z07:00 4386fb97867d: (from ubuntu-1:14.04) die
1194
+    2015-05-12T15:52:12.999999999Z07:00 4 4386fb97867d: (from ubuntu-1:14.04) stop
1195
+    2015-05-12T15:53:45.999999999Z07:00  7805c1d35632: (from redis:2.8) die
1196
+    2015-05-12T15:54:03.999999999Z07:00  7805c1d35632: (from redis:2.8) stop
1197
+
1189 1198
 **Filter events:**
1190 1199
 
1191 1200
     $ docker events --filter 'event=stop'
... ...
@@ -1679,9 +1692,11 @@ timestamp, for example `2014-09-16T06:17:46.000000000Z`, to each
1679 1679
 log entry. To ensure that the timestamps for are aligned the
1680 1680
 nano-second part of the timestamp will be padded with zero when necessary.
1681 1681
 
1682
-The `--since` option shows logs of a container generated only after
1683
-the given date, specified as RFC 3339 or UNIX timestamp. The `--since` option
1684
-can be combined with the `--follow` and `--tail` options.
1682
+The `--since` option shows only the container logs generated after
1683
+a given date. You can specify the date as an RFC 3339 date, a UNIX
1684
+timestamp, or a Go duration string (e.g. `1m30s`, `3h`). Docker computes
1685
+the date relative to the client machine’s time. You can combine
1686
+the `--since` option with either or both of the `--follow` or `--tail` options.
1685 1687
 
1686 1688
 ## pause
1687 1689
 
... ...
@@ -29,9 +29,10 @@ func (s *DockerSuite) TestEventsTimestampFormats(c *check.C) {
29 29
 	// List of available time formats to --since
30 30
 	unixTs := func(t time.Time) string { return fmt.Sprintf("%v", t.Unix()) }
31 31
 	rfc3339 := func(t time.Time) string { return t.Format(time.RFC3339) }
32
+	duration := func(t time.Time) string { return time.Now().Sub(t).String() }
32 33
 
33 34
 	// --since=$start must contain only the 'untag' event
34
-	for _, f := range []func(time.Time) string{unixTs, rfc3339} {
35
+	for _, f := range []func(time.Time) string{unixTs, rfc3339, duration} {
35 36
 		since, until := f(start), f(end)
36 37
 		cmd := exec.Command(dockerBinary, "events", "--since="+since, "--until="+until)
37 38
 		out, _, err := runCommandWithOutput(cmd)
... ...
@@ -6,10 +6,17 @@ import (
6 6
 	"time"
7 7
 )
8 8
 
9
-// GetTimestamp tries to parse given string as RFC3339 time
10
-// or Unix timestamp (with seconds precision), if successful
11
-//returns a Unix timestamp as string otherwise returns value back.
12
-func GetTimestamp(value string) string {
9
+// GetTimestamp tries to parse given string as golang duration,
10
+// then RFC3339 time and finally as a Unix timestamp. If
11
+// any of these were successful, it returns a Unix timestamp
12
+// as string otherwise returns the given value back.
13
+// In case of duration input, the returned timestamp is computed
14
+// as the given reference time minus the amount of the duration.
15
+func GetTimestamp(value string, reference time.Time) string {
16
+	if d, err := time.ParseDuration(value); value != "0" && err == nil {
17
+		return strconv.FormatInt(reference.Add(-d).Unix(), 10)
18
+	}
19
+
13 20
 	var format string
14 21
 	if strings.Contains(value, ".") {
15 22
 		format = time.RFC3339Nano
... ...
@@ -1,10 +1,13 @@
1 1
 package timeutils
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"testing"
6
+	"time"
5 7
 )
6 8
 
7 9
 func TestGetTimestamp(t *testing.T) {
10
+	now := time.Now()
8 11
 	cases := []struct{ in, expected string }{
9 12
 		{"0", "-62167305600"}, // 0 gets parsed year 0
10 13
 
... ...
@@ -23,12 +26,17 @@ func TestGetTimestamp(t *testing.T) {
23 23
 		// unix timestamps returned as is
24 24
 		{"1136073600", "1136073600"},
25 25
 
26
+		// Durations
27
+		{"1m", fmt.Sprintf("%d", now.Add(-1*time.Minute).Unix())},
28
+		{"1.5h", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix())},
29
+		{"1h30m", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix())},
30
+
26 31
 		// String fallback
27 32
 		{"invalid", "invalid"},
28 33
 	}
29 34
 
30 35
 	for _, c := range cases {
31
-		o := GetTimestamp(c.in)
36
+		o := GetTimestamp(c.in, now)
32 37
 		if o != c.expected {
33 38
 			t.Fatalf("wrong value for '%s'. expected:'%s' got:'%s'", c.in, c.expected, o)
34 39
 		}