diff --git a/utils/time/time.go b/utils/time/time.go new file mode 100644 index 0000000..ab1671f --- /dev/null +++ b/utils/time/time.go @@ -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) +} diff --git a/utils/times/format.go b/utils/times/format.go index ed3c29c..7f549c7 100644 --- a/utils/times/format.go +++ b/utils/times/format.go @@ -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++ {