From 707fc6c5de283af320c46e6d1dc978ad38d86299 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Fri, 10 Nov 2023 12:35:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20times=20=E5=8C=85=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=87=8D=E7=BD=AE=E5=85=A8=E5=B1=80=E6=97=B6=E9=97=B4=E5=81=8F?= =?UTF-8?q?=E7=A7=BB=E9=87=8F=E5=92=8C=E8=8E=B7=E5=8F=96=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E6=97=B6=E9=97=B4=E5=81=8F=E7=A7=BB=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/times/offset.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/utils/times/offset.go b/utils/times/offset.go index 4d09c05..a211b42 100644 --- a/utils/times/offset.go +++ b/utils/times/offset.go @@ -7,6 +7,7 @@ import ( var sourceLocation = time.Local var offsetLock sync.Mutex +var currentOffsetDuration time.Duration // SetGlobalTimeOffset 设置全局时间偏移量 func SetGlobalTimeOffset(offset time.Duration) { @@ -17,6 +18,7 @@ func SetGlobalTimeOffset(offset time.Duration) { newOffset := currentOffset + int(offset.Seconds()) location := time.FixedZone("OFFSET", newOffset) time.Local = location + currentOffsetDuration = offset } // NowByNotOffset 获取未偏移的当前时间 @@ -29,3 +31,18 @@ func NowByNotOffset() time.Time { time.Local = offset return now } + +// GetGlobalTimeOffset 获取全局时间偏移量 +func GetGlobalTimeOffset() time.Duration { + offsetLock.Lock() + defer offsetLock.Unlock() + return currentOffsetDuration +} + +// ResetGlobalTimeOffset 重置全局时间偏移量 +func ResetGlobalTimeOffset() { + offsetLock.Lock() + defer offsetLock.Unlock() + time.Local = sourceLocation + currentOffsetDuration = 0 +}