enhance(types):日期时间类型增加从字符串解析成为日期时间指针类型的函数。

This commit is contained in:
徐涛 2023-06-14 21:22:10 +08:00
parent d97db0cf50
commit 77587b8157
2 changed files with 31 additions and 0 deletions

View File

@ -57,6 +57,21 @@ func ParseDate(t string) (Date, error) {
return NewEmptyDate(), fmt.Errorf("无法解析给定的日期,格式不正确。")
}
func ParseDatep(t string) (*Date, error) {
if len(t) == 0 {
return nil, fmt.Errorf("不能解析空白的日期时间。")
}
for _, layout := range dateLayouts {
d, err := time.ParseInLocation(layout, t, loc)
if err == nil {
return &Date{
Time: d,
}, nil
}
}
return nil, fmt.Errorf("无法解析给定的日期,格式不正确。")
}
func ParseDateWithDefault(t string, defaultDate Date) Date {
if len(t) == 0 {
return defaultDate

View File

@ -67,6 +67,22 @@ func ParseDateTime(t string) (DateTime, error) {
return NewEmptyDateTime(), fmt.Errorf("无法解析给定的日期时间,格式不正确。")
}
func ParseDateTimep(t string) (*DateTime, error) {
if len(t) == 0 {
return nil, 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{
Time: d,
}, nil
}
}
return nil, fmt.Errorf("无法解析给定的日期时间,格式不正确。")
}
var _ driver.Valuer = (*DateTime)(nil)
func (dt DateTime) Value() (driver.Value, error) {