带有偏移量的时间

This commit is contained in:
kercylan 2023-04-23 17:13:58 +08:00
parent 165e99f08e
commit e1c216ffe2
2 changed files with 23 additions and 1 deletions

20
utils/time/time.go Normal file
View File

@ -0,0 +1,20 @@
package time
import "time"
// Time 带有偏移量的时间
type Time struct {
offset time.Duration
}
func (slf *Time) SetOffset(offset time.Duration) {
slf.offset = offset
}
func (slf *Time) Now() time.Time {
return time.Now().Add(slf.offset)
}
func (slf *Time) Since(t time.Time) time.Duration {
return slf.Now().Sub(t)
}

View File

@ -13,6 +13,7 @@ const (
IntervalHour
IntervalMinute
IntervalSecond
IntervalNow
)
var (
@ -22,6 +23,7 @@ var (
IntervalHour: "小时前",
IntervalMinute: "分钟前",
IntervalSecond: "秒钟前",
IntervalNow: "刚刚",
}
)
@ -40,7 +42,7 @@ func IntervalFormat(time1, time2 time.Time) string {
cur := time1.Unix()
ct := cur - time2.Unix()
if ct <= 0 {
return "刚刚"
return intervalFormat[IntervalNow]
}
var res string
for i := 0; i < len(byTime); i++ {