1 Star 0 Fork 66

chewel/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
database.go 1.74 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2021-02-24 09:48 . 精简代码
package carbon
import (
"database/sql/driver"
"fmt"
"time"
)
type ToDateTimeString struct {
Carbon
}
type ToDateString struct {
Carbon
}
type ToTimeString struct {
Carbon
}
type ToTimestamp struct {
Carbon
}
type ToTimestampWithSecond struct {
Carbon
}
type ToTimestampWithMillisecond struct {
Carbon
}
type ToTimestampWithMicrosecond struct {
Carbon
}
type ToTimestampWithNanosecond struct {
Carbon
}
func (c *Carbon) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*c = Carbon{Time: value}
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}
func (c Carbon) Value() (driver.Value, error) {
var tt time.Time
if c.Time.UnixNano() == tt.UnixNano() {
return nil, nil
}
return c.Time, nil
}
func (c ToDateTimeString) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, c.ToDateTimeString())), nil
}
func (c ToDateString) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, c.ToDateString())), nil
}
func (c ToTimeString) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, c.ToTimeString())), nil
}
func (c ToTimestamp) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, c.ToTimestamp())), nil
}
func (c ToTimestampWithSecond) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithSecond())), nil
}
func (c ToTimestampWithMillisecond) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMillisecond())), nil
}
func (c ToTimestampWithMicrosecond) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMicrosecond())), nil
}
func (c ToTimestampWithNanosecond) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithNanosecond())), nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/chewel/carbon.git
git@gitee.com:chewel/carbon.git
chewel
carbon
carbon
master

搜索帮助