Parcourir la source

添加LocalTime数据库解析函数,防止空时间异常

wuww il y a 6 ans
Parent
commit
0e813a45a7
1 fichiers modifiés avec 20 ajouts et 0 suppressions
  1. 20 0
      models/LocalTime.go

+ 20 - 0
models/LocalTime.go

@@ -66,3 +66,23 @@ func (t LocalTime) MarshalJSON() ([]byte, error) {
 
 	return b, nil
 }
+
+
+func (t LocalTime) Value() time.Time {
+	return time.Time(t)
+}
+
+func (t LocalTime) String() string {
+	return t.Value().String()
+}
+
+// 数据库解析到对象时调用
+func (t *LocalTime) FromDB(data []byte) error {
+	return t.UnmarshalJSON(data)
+}
+// 对象解析到数据库时调用
+func (t LocalTime) ToDB() ([]byte, error) {
+	b := make([]byte, 0, len(timeFormat)+2)
+	b = append(b, []byte(time.Time(t).Format(timeFormat))...)
+	return b, nil
+}