feat: times 包支持重置全局时间偏移量和获取当前全局时间偏移量

This commit is contained in:
kercylan98 2023-11-10 12:35:19 +08:00
parent f03dd4ac4f
commit 707fc6c5de
1 changed files with 17 additions and 0 deletions

View File

@ -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
}