Parse input timestamps with standard RFC3339

Fix for #13175.

This change allows user-input timestamps (e.g. to `docker events
--since/--until`  or `docker logs --since` to be parsed using
standard RFC3339Nano layout in Go instead of the layout that parses
all timestamps into fixed-length strings (currently buggy).

User inputs need not to be complying to the internal format
(`RFC3339NanoFixed`) anyway.

Added test case for `events --since/--until` with all possible
timestamp input formats.

Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>
This commit is contained in:
Ahmet Alp Balkan 2015-05-13 16:27:00 +00:00
parent f72d5179d8
commit dfba096de9
2 changed files with 46 additions and 3 deletions

View file

@ -2,14 +2,21 @@ package timeutils
import (
"strconv"
"strings"
"time"
)
// GetTimestamp tries to parse given string as RFC3339 time
// or Unix timestamp, if successful returns a Unix timestamp
// as string otherwise returns value back.
// or Unix timestamp (with seconds precision), if successful
//returns a Unix timestamp as string otherwise returns value back.
func GetTimestamp(value string) string {
format := RFC3339NanoFixed
var format string
if strings.Contains(value, ".") {
format = time.RFC3339Nano
} else {
format = time.RFC3339
}
loc := time.FixedZone(time.Now().Zone())
if len(value) < len(format) {
format = format[:len(value)]