From 77c5dc902725ebcc5a2f37b65e03e75b39aed3dc Mon Sep 17 00:00:00 2001 From: Oliver Steele Date: Sat, 8 Jul 2017 10:21:35 -0400 Subject: [PATCH] Add more parse time formats --- evaluator/convert_test.go | 4 +--- evaluator/time.go | 32 ++++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/evaluator/convert_test.go b/evaluator/convert_test.go index 219b34b..f416e05 100644 --- a/evaluator/convert_test.go +++ b/evaluator/convert_test.go @@ -45,8 +45,8 @@ func TestConvert(t *testing.T) { for i, test := range convertTests { t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) { typ := reflect.TypeOf(test.proto) - value, err := Convert(test.value, typ) name := fmt.Sprintf("Convert %#v -> %v", test.value, typ) + value, err := Convert(test.value, typ) require.NoErrorf(t, err, name) require.Equalf(t, test.expected, value, name) }) @@ -72,8 +72,6 @@ func TestConvert_map_synonym(t *testing.T) { } func TestConvert_map_to_array(t *testing.T) { - // this test needs to sort the output keys before comparing - // {map[int]string{1: "a", 2: "b"}, []string{}, []string{"b", "a"}}, typ := reflect.TypeOf([]string{}) v, err := Convert(map[int]string{1: "b", 2: "a"}, typ) require.NoError(t, err) diff --git a/evaluator/time.go b/evaluator/time.go index ac2fd1f..4e48ccc 100644 --- a/evaluator/time.go +++ b/evaluator/time.go @@ -1,6 +1,7 @@ package evaluator import ( + "fmt" "reflect" "time" @@ -14,12 +15,36 @@ var dateFormats = []string{ } var dateLayouts = []string{ + // from the Go library + time.ANSIC, // "Mon Jan _2 15:04:05 2006" + time.UnixDate, // "Mon Jan _2 15:04:05 MST 2006" + time.RubyDate, // "Mon Jan 02 15:04:05 -0700 2006" + time.RFC822, // "02 Jan 06 15:04 MST" + time.RFC822Z, // "02 Jan 06 15:04 -0700" // RFC822 with numeric zone + time.RFC850, // "Monday, 02-Jan-06 15:04:05 MST" + time.RFC1123, // "Mon, 02 Jan 2006 15:04:05 MST" + time.RFC1123Z, // "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone + time.RFC3339, // "2006-01-02T15:04:05Z07:00" + + // ISO 8601 + "2016-01-02", + "2016-01-02T15:04:05-07:00", // this is also XML Schema + "2016-01-02T15:04:05Z", + "20160102T150405Z", + + // from Ruby's Time.parse docs + "Mon, 02 Jan 2006 15:04:05 -0700", // "RFC822" -- but not really + + // From Jekyll docs + "02 Jan 2006", // Jekyll short string + "02 January 2006", // Jekyll long string + + // observed in the wild; plus some variants "2006-01-02 15:04:05 -07:00", "2006-01-02 15:04:05 -0700", "2006-01-02 15:04:05 -7", "2006-01-02 15:04:05", "2006-01-02 15:04", - "2006-01-02", "January 2, 2006", "January 2 2006", "Jan 2, 2006", @@ -32,17 +57,16 @@ func ParseTime(s string) (time.Time, error) { return time.Now(), nil } for _, layout := range dateLayouts { - // fmt.Printf("%s\n%s\n\n", time.Now().Format(layout), s) t, err := time.Parse(layout, s) if err == nil { + fmt.Println("found", layout) return t, nil } } for _, format := range dateFormats { - // xx, _ := datefmt.Strftime(format, time.Now()) - // fmt.Printf("%s\n%s\n\n", xx, s) t, err := datefmt.Strptime(format, s) if err == nil { + fmt.Println("found", format) return t, nil } }