From 6c4f59f1a0baf54e4bcd7ac13d3ecad06d9e3792 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Mon, 26 Jun 2023 19:08:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=81=8F=E7=A7=BB=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/offset/time.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/utils/offset/time.go b/utils/offset/time.go index 0967005..9fed9e2 100644 --- a/utils/offset/time.go +++ b/utils/offset/time.go @@ -2,6 +2,13 @@ package offset import "time" +var global *Time + +func init() { + global = NewTime(0) +} + +// NewTime 新建一个包含偏移的时间 func NewTime(offset time.Duration) *Time { return &Time{offset: offset} } @@ -11,14 +18,32 @@ type Time struct { offset time.Duration } +// SetOffset 设置时间偏移 func (slf *Time) SetOffset(offset time.Duration) { slf.offset = offset } +// Now 获取当前时间偏移后的时间 func (slf *Time) Now() time.Time { return time.Now().Add(slf.offset) } +// Since 获取当前时间偏移后的时间自从 t 以来经过的时间 func (slf *Time) Since(t time.Time) time.Duration { return slf.Now().Sub(t) } + +// SetGlobal 设置全局偏移时间 +func SetGlobal(offset time.Duration) { + global.SetOffset(offset) +} + +// Now 获取当前时间偏移后的时间 +func Now() time.Time { + return global.Now() +} + +// Since 获取当前时间偏移后的时间自从 t 以来经过的时间 +func Since(t time.Time) time.Duration { + return global.Since(t) +}