feat: 新增全局偏移时间
This commit is contained in:
parent
2127978093
commit
6c4f59f1a0
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue