1 Star 0 Fork 1

go-genie/sqlx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
pg_timestamptz.go 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
文兄 提交于 2025-01-10 14:03 +08:00 . feat. support for pgsql timestamptz
package datatypes
import (
"database/sql/driver"
"fmt"
"time"
)
var (
TimestampTzZero = PgTimestampTz(time.Time{})
TimestampTzUnixZero = PgTimestampTz(time.Unix(0, 0))
)
type PgTimestampTz time.Time
func (p *PgTimestampTz) UnmarshalText(text []byte) (err error) {
str := string(text)
if len(str) == 0 || str == "0" {
return nil
}
*p, err = ParseTimestampTzFromString(str)
return
}
func (p *PgTimestampTz) MarshalText() (text []byte, err error) {
return []byte(p.String()), nil
}
func (p *PgTimestampTz) String() string {
if p.IsZero() {
return ""
}
return time.Time(*p).In(CST).Format(time.RFC3339)
}
func (p *PgTimestampTz) Format(layout string) string {
return time.Time(*p).In(CST).Format(layout)
}
func (p *PgTimestampTz) Unix() int64 {
return time.Time(*p).Unix()
}
func (p *PgTimestampTz) IsZero() bool {
unix := p.Unix()
return unix == 0 || unix == TimestampZero.Unix()
}
func (p *PgTimestampTz) Value() (driver.Value, error) {
return p.String(), nil
}
func (p *PgTimestampTz) Scan(value any) error {
switch v := value.(type) {
case time.Time:
*p = PgTimestampTz(v)
case nil:
*p = TimestampTzZero
default:
return fmt.Errorf("cannot sql.Scan() strfmt.Timestamp from: %#v", v)
}
return nil
}
func (PgTimestampTz) DataType(driverName string) string {
return "timestamptz"
}
func ParseTimestampTzFromString(s string) (dt PgTimestampTz, err error) {
var t time.Time
t, err = time.Parse(time.RFC3339, s)
dt = PgTimestampTz(t)
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/go-genie/sqlx.git
git@gitee.com:go-genie/sqlx.git
go-genie
sqlx
sqlx
v1.1.3

搜索帮助