enhance(types):喂日期时间类型增加有字符串解析的功能。

This commit is contained in:
徐涛 2023-06-10 06:27:31 +08:00
parent 005f9020a1
commit ab2bcb3cf6

View File

@ -22,6 +22,12 @@ func Now() DateTime {
}
}
func NewEmptyDateTime() DateTime {
return DateTime{
Time: time.Time{}.In(loc),
}
}
func Timestamp() int64 {
startline := time.Date(2022, 2, 22, 22, 22, 22, 0, loc).Unix()
return Now().Unix() - startline
@ -33,6 +39,19 @@ func FromTime(t time.Time) DateTime {
}
}
func ParseDateTime(t string) (DateTime, error) {
if len(t) == 0 {
return NewEmptyDateTime(), fmt.Errorf("不能解析空白的日期时间。")
}
d, err := time.ParseInLocation("2006-01-02 13:04:05", t, loc)
if err != nil {
return NewEmptyDateTime(), fmt.Errorf("无法解析给定的日期, %w", err)
}
return DateTime{
Time: d,
}, nil
}
var _ driver.Valuer = (*DateTime)(nil)
func (dt DateTime) Value() (driver.Value, error) {