From e1c216ffe2d0e00ef92f203ca8a04993fba2f907 Mon Sep 17 00:00:00 2001 From: kercylan <61743331+kercylan98@users.noreply.github.com> Date: Sun, 23 Apr 2023 17:13:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B8=A6=E6=9C=89=E5=81=8F=E7=A7=BB=E9=87=8F?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/time/time.go | 20 ++++++++++++++++++++ utils/times/format.go | 4 +++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 utils/time/time.go 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++ {