summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-01-24 10:57:30 -0800
committerGitHub <noreply@github.com>2020-01-24 10:57:30 -0800
commit975854ad5c79346c72615030249c1f2bf8aecde0 (patch)
tree76fc4708e8b4ef7bbf6b0eb82028dcaed8fce13b /pkg
parent81e59a742b46d41848c8c213e155fbc9ecc4e5f8 (diff)
parent9a2e275abd407d1218d687cf674eb720bc2cf8ca (diff)
downloadpodman-975854ad5c79346c72615030249c1f2bf8aecde0.tar.gz
podman-975854ad5c79346c72615030249c1f2bf8aecde0.tar.bz2
podman-975854ad5c79346c72615030249c1f2bf8aecde0.zip
Merge pull request #4966 from jwhonce/wip/filters
[CI:DOCS] Refactor time parsing to be more liberal in accepted values
Diffstat (limited to 'pkg')
-rw-r--r--pkg/api/handlers/decoder.go51
1 files changed, 46 insertions, 5 deletions
diff --git a/pkg/api/handlers/decoder.go b/pkg/api/handlers/decoder.go
index d87409394..890d77ecc 100644
--- a/pkg/api/handlers/decoder.go
+++ b/pkg/api/handlers/decoder.go
@@ -11,6 +11,8 @@ import (
// NewAPIDecoder returns a configured schema.Decoder
func NewAPIDecoder() *schema.Decoder {
+ _ = ParseDateTime
+
d := schema.NewDecoder()
d.IgnoreUnknownKeys(true)
d.RegisterConverter(map[string][]string{}, convertUrlValuesString)
@@ -39,12 +41,51 @@ func convertUrlValuesString(query string) reflect.Value {
return reflect.ValueOf(f)
}
+// isZero() can be used to determine if parsing failed.
func convertTimeString(query string) reflect.Value {
- t, err := time.Parse(time.RFC3339, query)
- if err != nil {
- logrus.Infof("convertTimeString: Failed to Unmarshal %s: %s", query, err.Error())
+ var (
+ err error
+ t time.Time
+ )
+
+ for _, f := range []string{
+ time.UnixDate,
+ time.ANSIC,
+ time.RFC1123,
+ time.RFC1123Z,
+ time.RFC3339,
+ time.RFC3339Nano,
+ time.RFC822,
+ time.RFC822Z,
+ time.RFC850,
+ time.RubyDate,
+ time.Stamp,
+ time.StampMicro,
+ time.StampMilli,
+ time.StampNano,
+ } {
+ t, err = time.Parse(f, query)
+ if err == nil {
+ return reflect.ValueOf(t)
+ }
+
+ if _, isParseError := err.(*time.ParseError); isParseError {
+ // Try next format
+ continue
+ } else {
+ break
+ }
+ }
- return reflect.ValueOf(time.Time{})
+ // We've exhausted all formats, or something bad happened
+ if err != nil {
+ logrus.Infof("convertTimeString: Failed to parse %s: %s", query, err.Error())
}
- return reflect.ValueOf(t)
+ return reflect.ValueOf(time.Time{})
+}
+
+// ParseDateTime is a helper function to aid in parsing different Time/Date formats
+// isZero() can be used to determine if parsing failed.
+func ParseDateTime(query string) time.Time {
+ return convertTimeString(query).Interface().(time.Time)
}