clock

package
v0.0.0-...-b628505 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TimeFormat         = "2006-01-02 15:04:05"
	DateFormat         = "2006-01-02"
	UnixTimeUnitOffset = uint64(time.Millisecond / time.Nanosecond)
)

Variables

This section is empty.

Functions

func FormatDate

func FormatDate(tsMillis uint64) string

FormatDate 将Unix时间戳(ms)格式化为日期字符串

func FormatTimeMillis

func FormatTimeMillis(tsMillis uint64) string

FormatTimeMillis 将Unix时间戳(ms)格式化为时间字符串

func GetTimeMillis

func GetTimeMillis() uint64

GetTimeMillis 返回当前的Unix时间戳(ms)

func GetTimeNano

func GetTimeNano() uint64

GetTimeNano 返回当前的Unix时间戳(ns)

func GetTimestamp

func GetTimestamp() uint64

GetTimestamp 获得当前时间戳,单位是ms

func Now

func Now() time.Time

Now 返回当前本地时间。

func SetClock

func SetClock(c Clock)

SetClock 设置 Clock

func SetTickerCreator

func SetTickerCreator(tc TickerCreator)

SetTickerCreator 设置 Ticker 对象.

func Sleep

func Sleep(d time.Duration)

func StartTicker

func StartTicker()

StartTicker 启动一个后台任务,该任务每毫秒缓存当前时间戳,在高并发情况下可能会提供更好的性能.

Types

type Clock

type Clock interface {
	Now() time.Time
	Sleep(d time.Duration)
	GetTimeMillis() uint64
	GetTimeNano() uint64
}

Clock 时钟接口

func CurrentClock

func CurrentClock() Clock

CurrentClock 返回 Clock 对象

type MockClock

type MockClock struct {
	// contains filtered or unexported fields
}

MockClock 测试使用的Clock对象

func NewMockClock

func NewMockClock() *MockClock

func (*MockClock) GetTimeMillis

func (t *MockClock) GetTimeMillis() uint64

func (*MockClock) GetTimeNano

func (t *MockClock) GetTimeNano() uint64

func (*MockClock) Now

func (t *MockClock) Now() time.Time

func (*MockClock) Sleep

func (t *MockClock) Sleep(d time.Duration)

type MockTicker

type MockTicker struct {
	// contains filtered or unexported fields
}

MockTicker 测试使用的 Ticker 对象 MockTicker 和 MockClock 一般搭配使用

func NewMockTicker

func NewMockTicker(d time.Duration) *MockTicker

func (*MockTicker) C

func (t *MockTicker) C() <-chan time.Time

func (*MockTicker) Stop

func (t *MockTicker) Stop()

type MockTickerCreator

type MockTickerCreator struct{}

MockTickerCreator 创建 MockTicker 用于测试 MockTickerCreator 和 MockClock 通常一起使用

func NewMockTickerCreator

func NewMockTickerCreator() *MockTickerCreator

func (*MockTickerCreator) NewTicker

func (tc *MockTickerCreator) NewTicker(d time.Duration) Ticker

type RealClock

type RealClock struct{}

RealClock 真实使用的Clock对象

func NewRealClock

func NewRealClock() *RealClock

func (*RealClock) GetTimeMillis

func (t *RealClock) GetTimeMillis() uint64

func (*RealClock) GetTimeNano

func (t *RealClock) GetTimeNano() uint64

func (*RealClock) Now

func (t *RealClock) Now() time.Time

func (*RealClock) Sleep

func (t *RealClock) Sleep(d time.Duration)

type RealTicker

type RealTicker struct {
	// contains filtered or unexported fields
}

RealTicker 真实使用的 Ticker 对象

func NewRealTicker

func NewRealTicker(d time.Duration) *RealTicker

func (*RealTicker) C

func (t *RealTicker) C() <-chan time.Time

func (*RealTicker) Stop

func (t *RealTicker) Stop()

type RealTickerCreator

type RealTickerCreator struct{}

RealTickerCreator 创建真实的 RealTicker 和 time.Ticker 对象.

func NewRealTickerCreator

func NewRealTickerCreator() *RealTickerCreator

func (*RealTickerCreator) NewTicker

func (tc *RealTickerCreator) NewTicker(d time.Duration) Ticker

type Ticker

type Ticker interface {
	C() <-chan time.Time
	Stop()
}

Ticker time.Ticker 对象封装

func NewTicker

func NewTicker(d time.Duration) Ticker

type TickerCreator

type TickerCreator interface {
	NewTicker(d time.Duration) Ticker
}

TickerCreator 实例化Ticker.

func CurrentTickerCreator

func CurrentTickerCreator() TickerCreator

CurrentTickerCreator 获取 Ticker 对象

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL