mirror of
https://gitee.com/dromara/carbon.git
synced 2024-11-30 03:07:36 +08:00
支持gorm
This commit is contained in:
parent
7538604683
commit
1cc4acd64d
12
gorm_model.go
Normal file
12
gorm_model.go
Normal file
@ -0,0 +1,12 @@
|
||||
package carbon
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
Id int64 `gorm:"primary_key;comment:'主键ID'" json:"id"`
|
||||
CreatedAt ToDateTimeString `gorm:"comment:'创建时间';type:timestamp not null;default:current_timestamp;" json:"created_at"`
|
||||
UpdatedAt ToDateTimeString `gorm:"comment:'更新时间';type:timestamp on update current_timestamp;omitempty;default:current_timestamp;" json:"updated_at"`
|
||||
DeletedAt *time.Time `gorm:"comment:'删除时间'" json:"deleted_at" sql:"index"`
|
||||
}
|
32
gorm_to_date.go
Normal file
32
gorm_to_date.go
Normal file
@ -0,0 +1,32 @@
|
||||
package carbon
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ToDateString struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t ToDateString) MarshalJSON() ([]byte, error) {
|
||||
formatted := fmt.Sprintf("\"%s\"", t.Format("2006-01-02"))
|
||||
return []byte(formatted), nil
|
||||
}
|
||||
|
||||
func (t ToDateString) Value() (driver.Value, error) {
|
||||
var zeroTime time.Time
|
||||
if t.Time.UnixNano() == zeroTime.UnixNano() {
|
||||
return nil, nil
|
||||
}
|
||||
return t.Time, nil
|
||||
}
|
||||
func (t *ToDateString) Scan(v interface{}) error {
|
||||
value, ok := v.(time.Time)
|
||||
if ok {
|
||||
*t = ToDateString{Time: value}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("can not convert %v to timestamp", v)
|
||||
}
|
32
gorm_to_datetime.go
Normal file
32
gorm_to_datetime.go
Normal file
@ -0,0 +1,32 @@
|
||||
package carbon
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ToDateTimeString struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t ToDateTimeString) MarshalJSON() ([]byte, error) {
|
||||
formatted := fmt.Sprintf("\"%s\"", t.Format("2006-01-02 15:04:05"))
|
||||
return []byte(formatted), nil
|
||||
|
||||
}
|
||||
func (t ToDateTimeString) Value() (driver.Value, error) {
|
||||
var zeroTime time.Time
|
||||
if t.Time.UnixNano() == zeroTime.UnixNano() {
|
||||
return nil, nil
|
||||
}
|
||||
return t.Time, nil
|
||||
}
|
||||
func (t *ToDateTimeString) Scan(v interface{}) error {
|
||||
value, ok := v.(time.Time)
|
||||
if ok {
|
||||
*t = ToDateTimeString{Time: value}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("can not convert %v to timestamp", v)
|
||||
}
|
31
gorm_to_time.go
Normal file
31
gorm_to_time.go
Normal file
@ -0,0 +1,31 @@
|
||||
package carbon
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ToTimeString struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t ToTimeString) MarshalJSON() ([]byte, error) {
|
||||
formatted := fmt.Sprintf("\"%s\"", t.Format("15-04-05"))
|
||||
return []byte(formatted), nil
|
||||
}
|
||||
func (t ToTimeString) Value() (driver.Value, error) {
|
||||
var zeroTime time.Time
|
||||
if t.Time.UnixNano() == zeroTime.UnixNano() {
|
||||
return nil, nil
|
||||
}
|
||||
return t.Time, nil
|
||||
}
|
||||
func (t *ToTimeString) Scan(v interface{}) error {
|
||||
value, ok := v.(time.Time)
|
||||
if ok {
|
||||
*t = ToTimeString{Time: value}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("can not convert %v to timestamp", v)
|
||||
}
|
33
gorm_to_timestamp.go
Normal file
33
gorm_to_timestamp.go
Normal file
@ -0,0 +1,33 @@
|
||||
package carbon
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ToTimestamp struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t ToTimestamp) MarshalJSON() ([]byte, error) {
|
||||
formatted := fmt.Sprintf("\"%d\"", t.Unix())
|
||||
return []byte(formatted), nil
|
||||
}
|
||||
|
||||
func (t ToTimestamp) Value() (driver.Value, error) {
|
||||
var zeroTime time.Time
|
||||
if t.Time.UnixNano() == zeroTime.UnixNano() {
|
||||
return nil, nil
|
||||
}
|
||||
return t.Time, nil
|
||||
}
|
||||
|
||||
func (t *ToTimestamp) Scan(v interface{}) error {
|
||||
value, ok := v.(time.Time)
|
||||
if ok {
|
||||
*t = ToTimestamp{Time: value}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("can not convert %v to timestamp", v)
|
||||
}
|
Loading…
Reference in New Issue
Block a user