Skip to content

Commit

Permalink
feat: 新增全局偏移时间
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Jun 26, 2023
1 parent 2127978 commit 6c4f59f
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions utils/offset/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand All @@ -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)
}

0 comments on commit 6c4f59f

Please sign in to comment.