From 4c466d3aa18da7f2993e350647acd7f90f07e2d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Fri, 13 May 2022 13:53:35 +0800 Subject: [PATCH] update time --- LICENSE | 21 --------------------- go.mod | 1 + go.sum | 2 ++ time.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ version.go | 2 +- 5 files changed, 52 insertions(+), 22 deletions(-) delete mode 100644 LICENSE create mode 100644 time.go diff --git a/LICENSE b/LICENSE deleted file mode 100644 index a0d0e5b..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2018 茂名聚合科技有限公司 - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/go.mod b/go.mod index bf73844..5493795 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module go.dtapp.net/golog go 1.18 require ( + go.dtapp.net/gotime v1.0.2 gorm.io/datatypes v1.0.6 gorm.io/gorm v1.23.5 ) diff --git a/go.sum b/go.sum index 7399793..e3b3142 100644 --- a/go.sum +++ b/go.sum @@ -122,6 +122,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= +go.dtapp.net/gotime v1.0.2 h1:CFIJHQXC/4t9bsJhk2cLhjHd6rpdPcJXr8BcHKHDuQo= +go.dtapp.net/gotime v1.0.2/go.mod h1:Gq7eNLr2iMLP18UNWONRq4V3Uhf/ADp4bIrS+Tc6ktY= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= diff --git a/time.go b/time.go new file mode 100644 index 0000000..dc9631c --- /dev/null +++ b/time.go @@ -0,0 +1,48 @@ +package golog + +import ( + "database/sql/driver" + "errors" + "fmt" + "go.dtapp.net/gotime" + "gorm.io/gorm" + "gorm.io/gorm/schema" + "time" +) + +type TimeString time.Time + +// GormDataType gorm通用数据类型 +func (t TimeString) GormDataType() string { + return "string" +} + +func (t TimeString) GormDBDataType(db *gorm.DB, field *schema.Field) string { + // 使用 field.Tag、field.TagSettings 获取字段的 tag + // 查看 https://github.com/go-gorm/gorm/blob/master/schema/field.go 获取全部的选项 + + // 根据不同的数据库驱动返回不同的数据类型 + switch db.Dialector.Name() { + case "mysql", "sqlite": + return "string" + case "postgres": + return "string" + } + return "" +} + +// Scan 实现 sql.Scanner 接口,Scan 将 value 扫描至 Time +func (t *TimeString) Scan(value interface{}) error { + str, ok := value.(string) + if !ok { + return errors.New(fmt.Sprint("无法解析:", value)) + } + t1 := gotime.SetCurrentParse(str).Time + *t = TimeString(t1) + return nil +} + +// Value 实现 driver.Valuer 接口,Value 返回 string value +func (t TimeString) Value() (driver.Value, error) { + return gotime.SetCurrent(time.Time(t)).Format(), nil +} diff --git a/version.go b/version.go index 74c0a35..b2dfcd6 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package golog -const Version = "1.0.1" +const Version = "1.0.2"