代码拉取完成,页面将自动刷新
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 ×{}
}
// 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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。