From ab2bcb3cf63c590bdc2d88e9bfb5490c86dc6724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sat, 10 Jun 2023 06:27:31 +0800 Subject: [PATCH] =?UTF-8?q?enhance(types):=E5=96=82=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E7=B1=BB=E5=9E=8B=E5=A2=9E=E5=8A=A0=E6=9C=89?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E8=A7=A3=E6=9E=90=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/datetime.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/types/datetime.go b/types/datetime.go index 74bbd5a..ea5a41e 100644 --- a/types/datetime.go +++ b/types/datetime.go @@ -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) {