github.com/michael-k/docker@v1.7.0-rc2/pkg/timeutils/utils.go (about) 1 package timeutils 2 3 import ( 4 "strconv" 5 "strings" 6 "time" 7 ) 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 { 13 var format string 14 if strings.Contains(value, ".") { 15 format = time.RFC3339Nano 16 } else { 17 format = time.RFC3339 18 } 19 20 loc := time.FixedZone(time.Now().Zone()) 21 if len(value) < len(format) { 22 format = format[:len(value)] 23 } 24 t, err := time.ParseInLocation(format, value, loc) 25 if err != nil { 26 return value 27 } 28 return strconv.FormatInt(t.Unix(), 10) 29 }