enhance(types):修正日期时间解析尝试模板。

This commit is contained in:
徐涛 2023-06-10 11:37:52 +08:00
parent 686890b5d8
commit 5d938da53e
2 changed files with 4 additions and 2 deletions

View File

@ -11,7 +11,7 @@ import (
)
var dateLayouts = []string{
"2006-01-02", "2006-1-2", "2006/01/02", "6-1-2", "6-01-02", "01/02/06", "1/2/6", "2006年01月02日", "06年1月2日",
"2006-01-02", "2006-1-2", "2006/01/02", "06-1-2", "6-01-02", "01/02/06", "1/2/06", "2006年01月02日", "06年1月2日",
}
type Date struct {

View File

@ -13,7 +13,8 @@ import (
var (
loc *time.Location = time.FixedZone("+0800", 8*60*60)
datetimeLayouts = []string{
"2006-01-02 15:04:05", "2006-1-2 15:04:05", "2006/01/02 15:04:05", "6-1-2 15:04:05", "6-01-02 15:04:05", "01/02/06 15:04:05", "1/2/6 15:04:05", "2006年01月02日 15:04:05", "06年1月2日 15:04:05",
"2006-01-02 15:04:05", "2006-1-2 15:04:05", "2006/01/02 15:04:05", "06-1-2 15:04:05", "06-01-02 15:04:05", "01/02/06 15:04:05", "1/2/06 15:04:05", "2006年01月02日 15:04:05", "06年1月2日 15:04:05",
"2006-01-02 15:04", "2006-1-2 15:04", "2006/01/02 15:04", "06-1-2 15:04", "06-01-02 15:04", "01/02/06 15:04", "1/2/06 15:04", "2006年01月02日 15:04", "06年1月2日 15:04",
}
)
@ -49,6 +50,7 @@ func ParseDateTime(t string) (DateTime, error) {
return NewEmptyDateTime(), fmt.Errorf("不能解析空白的日期时间。")
}
for _, layout := range datetimeLayouts {
fmt.Printf("Parse: %s, Try layout: %s\n", t, layout)
d, err := time.ParseInLocation(layout, t, loc)
if err == nil {
return DateTime{