Fixes #13107. This change enables Go duration strings
computed relative to the client machine’s time to be used
as input parameters to `docker events --since/--until`
and `docker logs --since` arguments.
Added unit tests for pkg/timeutils.GetTimestamp as well.
Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>
... | ... |
@@ -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. |
... | ... |
@@ -1100,6 +1100,10 @@ and Docker images will report: |
1100 | 1100 |
|
1101 | 1101 |
untag, delete |
1102 | 1102 |
|
1103 |
+The `--since` and `--until` parameters can be Unix timestamps, RFC3339 |
|
1104 |
+dates or Go duration strings (e.g. `10m`, `1h30m`) computed relative to |
|
1105 |
+client machine’s time. |
|
1106 |
+ |
|
1103 | 1107 |
#### Filtering |
1104 | 1108 |
|
1105 | 1109 |
The filtering flag (`-f` or `--filter`) format is of "key=value". If you would like to use |
... | ... |
@@ -1162,6 +1166,15 @@ You'll need two shells for this example. |
1162 | 1162 |
2014-05-10T17:42:14.999999999Z07:00 7805c1d35632: (from redis:2.8) die |
1163 | 1163 |
2014-09-03T15:49:29.999999999Z07:00 7805c1d35632: (from redis:2.8) stop |
1164 | 1164 |
|
1165 |
+This example outputs all events that were generated in the last 3 minutes, |
|
1166 |
+relative to the current time on the client machine: |
|
1167 |
+ |
|
1168 |
+ $ docker events --since '3m' |
|
1169 |
+ 2015-05-12T11:51:30.999999999Z07:00 4386fb97867d: (from ubuntu-1:14.04) die |
|
1170 |
+ 2015-05-12T15:52:12.999999999Z07:00 4 4386fb97867d: (from ubuntu-1:14.04) stop |
|
1171 |
+ 2015-05-12T15:53:45.999999999Z07:00 7805c1d35632: (from redis:2.8) die |
|
1172 |
+ 2015-05-12T15:54:03.999999999Z07:00 7805c1d35632: (from redis:2.8) stop |
|
1173 |
+ |
|
1165 | 1174 |
**Filter events:** |
1166 | 1175 |
|
1167 | 1176 |
$ docker events --filter 'event=stop' |
... | ... |
@@ -1655,9 +1668,11 @@ timestamp, for example `2014-09-16T06:17:46.000000000Z`, to each |
1655 | 1655 |
log entry. To ensure that the timestamps for are aligned the |
1656 | 1656 |
nano-second part of the timestamp will be padded with zero when necessary. |
1657 | 1657 |
|
1658 |
-The `--since` option shows logs of a container generated only after |
|
1659 |
-the given date, specified as RFC 3339 or UNIX timestamp. The `--since` option |
|
1660 |
-can be combined with the `--follow` and `--tail` options. |
|
1658 |
+The `--since` option shows only the container logs generated after |
|
1659 |
+a given date. You can specify the date as an RFC 3339 date, a UNIX |
|
1660 |
+timestamp, or a Go duration string (e.g. `1m30s`, `3h`). Docker computes |
|
1661 |
+the date relative to the client machine’s time. You can combine |
|
1662 |
+the `--since` option with either or both of the `--follow` or `--tail` options. |
|
1661 | 1663 |
|
1662 | 1664 |
## pause |
1663 | 1665 |
|
... | ... |
@@ -28,9 +28,10 @@ func (s *DockerSuite) TestEventsTimestampFormats(c *check.C) { |
28 | 28 |
// List of available time formats to --since |
29 | 29 |
unixTs := func(t time.Time) string { return fmt.Sprintf("%v", t.Unix()) } |
30 | 30 |
rfc3339 := func(t time.Time) string { return t.Format(time.RFC3339) } |
31 |
+ duration := func(t time.Time) string { return time.Now().Sub(t).String() } |
|
31 | 32 |
|
32 | 33 |
// --since=$start must contain only the 'untag' event |
33 |
- for _, f := range []func(time.Time) string{unixTs, rfc3339} { |
|
34 |
+ for _, f := range []func(time.Time) string{unixTs, rfc3339, duration} { |
|
34 | 35 |
since, until := f(start), f(end) |
35 | 36 |
cmd := exec.Command(dockerBinary, "events", "--since="+since, "--until="+until) |
36 | 37 |
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 |
} |