2 Star 0 Fork 0

djienet / kratos

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
time.go 1.35 KB
一键复制 编辑 原始数据 按行查看 历史
chenli 提交于 2020-11-02 15:31 . init
package time
import (
"context"
"database/sql/driver"
"strconv"
xtime "time"
)
// Time be used to MySql timestamp converting.
type Time int64
// Scan scan time.
func (jt *Time) Scan(src interface{}) (err error) {
switch sc := src.(type) {
case xtime.Time:
*jt = Time(sc.Unix())
case string:
var i int64
i, err = strconv.ParseInt(sc, 10, 64)
*jt = Time(i)
}
return
}
// Value get time value.
func (jt Time) Value() (driver.Value, error) {
return xtime.Unix(int64(jt), 0), nil
}
// Time get time.
func (jt Time) Time() xtime.Time {
return xtime.Unix(int64(jt), 0)
}
// Duration be used toml unmarshal string time, like 1s, 500ms.
type Duration xtime.Duration
// UnmarshalText unmarshal text to duration.
func (d *Duration) UnmarshalText(text []byte) error {
tmp, err := xtime.ParseDuration(string(text))
if err == nil {
*d = Duration(tmp)
}
return err
}
// Shrink will decrease the duration by comparing with context's timeout duration
// and return new timeout\context\CancelFunc.
func (d Duration) Shrink(c context.Context) (Duration, context.Context, context.CancelFunc) {
if deadline, ok := c.Deadline(); ok {
if ctimeout := xtime.Until(deadline); ctimeout < xtime.Duration(d) {
// deliver small timeout
return Duration(ctimeout), c, func() {}
}
}
ctx, cancel := context.WithTimeout(c, xtime.Duration(d))
return d, ctx, cancel
}
1
https://gitee.com/djienet/kratos.git
git@gitee.com:djienet/kratos.git
djienet
kratos
kratos
v1.1.7

搜索帮助