Put time.Time before time.Duration

This commit is contained in:
TeoV
2017-12-12 13:50:16 +02:00
committed by Dan Christian Bogos
parent 501cbbc73a
commit 21707a5119
2 changed files with 10 additions and 4 deletions

View File

@@ -71,14 +71,14 @@ func StringToInterface(s string) interface{} {
if f, err := strconv.ParseFloat(s, 64); err == nil {
return f
}
// time.Duration
if d, err := time.ParseDuration(s); err == nil {
return d
}
// time.Time
if t, err := ParseTimeDetectLayout(s, "Local"); err == nil {
return t
}
// time.Duration
if d, err := time.ParseDuration(s); err == nil {
return d
}
// string
return s
}

View File

@@ -215,6 +215,12 @@ func TestStringToInterface(t *testing.T) {
if res := StringToInterface("45s"); res != time.Duration(45*time.Second) {
t.Error("not parsing time.Duration")
}
res := StringToInterface("+24h")
resTime := res.(time.Time)
now := time.Now()
if resTime.Hour() != now.Hour() && resTime.Minute() != now.Minute() {
t.Error("not parsing time.Time")
}
}
func TestCastFieldIfToString(t *testing.T) {