Allow duration strings as --since/--until

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>
This commit is contained in:
Ahmet Alp Balkan 2015-05-12 19:59:34 +00:00
parent 527a1c4f7a
commit 5f7c584bb3
2 changed files with 20 additions and 5 deletions

View file

@ -6,10 +6,17 @@ import (
"time"
)
// GetTimestamp tries to parse given string as RFC3339 time
// or Unix timestamp (with seconds precision), if successful
//returns a Unix timestamp as string otherwise returns value back.
func GetTimestamp(value string) string {
// GetTimestamp tries to parse given string as golang duration,
// then RFC3339 time and finally as a Unix timestamp. If
// any of these were successful, it returns a Unix timestamp
// as string otherwise returns the given value back.
// In case of duration input, the returned timestamp is computed
// as the given reference time minus the amount of the duration.
func GetTimestamp(value string, reference time.Time) string {
if d, err := time.ParseDuration(value); value != "0" && err == nil {
return strconv.FormatInt(reference.Add(-d).Unix(), 10)
}
var format string
if strings.Contains(value, ".") {
format = time.RFC3339Nano