This commit is contained in:
oneao committed 2026-02-06 22:21:26 +08:00
1 parent a030d1c92e
commit b8c8a04c85
9 files changed
+117 -68

No files matched your search

@@ -0,0 +1,30 @@
package timex
import (
"fmt"
"time"
)
func ParseTimeFlexible(value string) (time.Time, error) {
if value == "" {
return time.Time{}, fmt.Errorf("empty time string")
}
layouts := []string{
"2006-01-02 15:04:05",
"2006-01-02 15:04",
"2006-01-02",
"2006-01",
time.RFC3339,
}
var err error
for _, layout := range layouts {
var t time.Time
t, err = time.Parse(layout, value)
if err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("unsupported time format: %s", value)
}