1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-30 07:18:58 +01:00

Add more parse time formats

This commit is contained in:
Oliver Steele 2017-07-08 10:21:35 -04:00
parent 12045b5f93
commit 77c5dc9027
2 changed files with 29 additions and 7 deletions

View File

@ -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)

View File

@ -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
}
}