1 Star 1 Fork 0

bigbase / pg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
time.go 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
package pg
import (
"bytes"
"database/sql"
"encoding/json"
"time"
"github.com/go-pg/pg/types"
)
var jsonNull = []byte("null")
// NullTime is a time.Time wrapper that marshals zero time as JSON null and
// PostgreSQL NULL.
type NullTime struct {
time.Time
}
var _ json.Marshaler = (*NullTime)(nil)
var _ json.Unmarshaler = (*NullTime)(nil)
var _ sql.Scanner = (*NullTime)(nil)
var _ types.ValueAppender = (*NullTime)(nil)
func (tm NullTime) MarshalJSON() ([]byte, error) {
if tm.IsZero() {
return jsonNull, nil
}
return tm.Time.MarshalJSON()
}
func (tm *NullTime) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, jsonNull) {
tm.Time = time.Time{}
return nil
}
return tm.Time.UnmarshalJSON(b)
}
func (tm NullTime) AppendValue(b []byte, quote int) ([]byte, error) {
if tm.IsZero() {
return types.AppendNull(b, quote), nil
}
return types.AppendTime(b, tm.Time, quote), nil
}
func (tm *NullTime) Scan(b interface{}) error {
if b == nil {
tm.Time = time.Time{}
return nil
}
newtm, err := types.ParseTime(b.([]byte))
if err != nil {
return err
}
tm.Time = newtm
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/bigbase/pg.git
git@gitee.com:bigbase/pg.git
bigbase
pg
pg
v6.6.16

搜索帮助

344bd9b3 5694891 D2dac590 5694891