enhance(types):日期时间类型增加从字符串解析成为日期时间指针类型的函数。
This commit is contained in:
parent
d97db0cf50
commit
77587b8157
|
@ -57,6 +57,21 @@ func ParseDate(t string) (Date, error) {
|
||||||
return NewEmptyDate(), fmt.Errorf("无法解析给定的日期,格式不正确。")
|
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 {
|
func ParseDateWithDefault(t string, defaultDate Date) Date {
|
||||||
if len(t) == 0 {
|
if len(t) == 0 {
|
||||||
return defaultDate
|
return defaultDate
|
||||||
|
|
|
@ -67,6 +67,22 @@ func ParseDateTime(t string) (DateTime, error) {
|
||||||
return NewEmptyDateTime(), fmt.Errorf("无法解析给定的日期时间,格式不正确。")
|
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)
|
var _ driver.Valuer = (*DateTime)(nil)
|
||||||
|
|
||||||
func (dt DateTime) Value() (driver.Value, error) {
|
func (dt DateTime) Value() (driver.Value, error) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user