1 Star 0 Fork 0

lyc7751/我的公用工具

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
times.go 2.12 KB
一键复制 编辑 原始数据 按行查看 历史
lee 提交于 2024-05-23 22:57 +08:00 . 转换为string
package utils
import (
"database/sql/driver"
"fmt"
"time"
)
// Times 时间处理类
type times struct {
MyTime
}
// 默认处理时间格式
const (
timeFormart = "2006-01-02 15:04:05"
)
// CurrTime 当前时间处理
func Curr() *times {
return &times{}
}
// MyTime 自定义time的json格式
type MyTime time.Time
// Time 获取当前时间
func (t *MyTime) Time() MyTime {
return MyTime(time.Now())
}
// Unix获取时间戳
func (t MyTime) Unix() int64 {
tt := time.Time(t)
return tt.Unix()
}
// String 获取时间字符串
func (t *MyTime) String() string {
return time.Now().Format(timeFormart)
}
//
// ToString
// @Description: 获取时间字符串
// @return string
//
func (t *MyTime) ToString() string {
tt := time.Time(*t)
return tt.Format(timeFormart)
}
// Gt 大于判断
func (t MyTime) Gt(t1 MyTime) bool {
return t.GetTime().After(t1.GetTime())
}
// GtCurrTime 是否大于当前时间
func (t MyTime) GtCurrTime() bool {
return t.GetTime().After(time.Now())
}
// Lt 小于判断
func (t MyTime) Lt(t1 MyTime) bool {
return t.GetTime().Before(t1.GetTime())
}
func (t MyTime) GetTime() time.Time {
return time.Time(t)
}
// AddMonth 加上几个月
func (t MyTime) AddMonth(month uint) MyTime {
m := int(month)
return MyTime(t.GetTime().AddDate(0, m, 0))
}
//
// AddDay
// @Description: 增加几天后的日期
// @return MyTime
//
func (t MyTime) AddDay(day int) MyTime {
return MyTime(t.GetTime().AddDate(0, 0, day))
}
// MarshalJSON 自定义time的json格式
func (t MyTime) MarshalJSON() ([]byte, error) {
tt := time.Time(t)
tstr := tt.Format(timeFormart)
return []byte(fmt.Sprintf("\"%v\"", tstr)), nil
}
// UnmarshalJSON 自定义time的json格式
func (t *MyTime) UnmarshalJSON(data []byte) (err error) {
now, err := time.ParseInLocation(`"`+timeFormart+`"`, string(data), time.Local)
*t = MyTime(now)
return
}
func (t MyTime) Value() (driver.Value, error) {
tt := time.Time(t)
return tt.Format(timeFormart), nil
}
func (t *MyTime) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*t = MyTime(value)
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lyc7751/utils.git
git@gitee.com:lyc7751/utils.git
lyc7751
utils
我的公用工具
v0.0.8

搜索帮助