util

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2022 License: Apache-2.0 Imports: 12 Imported by: 5

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 CreateDirIfNotExists

func CreateDirIfNotExists(dirname string) error

func CurrentTimeMillis

func CurrentTimeMillis() uint64

Returns the current Unix timestamp in milliseconds.

func CurrentTimeMillsWithTicker

func CurrentTimeMillsWithTicker() uint64

func CurrentTimeNano

func CurrentTimeNano() uint64

Returns the current Unix timestamp in nanoseconds.

func FileExists

func FileExists(name string) (b bool, err error)

func FilePosition

func FilePosition(file *os.File) (int64, error)

func Float64Equals

func Float64Equals(x, y float64) bool

func FormatDate

func FormatDate(tsMillis uint64) string

FormatDate formats Unix timestamp (ms) to date string

func FormatTimeMillis

func FormatTimeMillis(tsMillis uint64) string

FormatTimeMillis formats Unix timestamp (ms) to time string.

func IsBlank

func IsBlank(s string) bool

IsBlank checks whether the given string is blank.

func NewUuid

func NewUuid() string

func Now

func Now() time.Time

Now returns the current local time.

func RunWithRecover

func RunWithRecover(f func())

func SetClock

func SetClock(c Clock)

SetClock sets the clock used by util package. In general, no need to set it. It is usually used for testing.

func SetTickerCreator

func SetTickerCreator(tc TickerCreator)

SetClock sets the ticker creator used by util package. In general, no need to set it. It is usually used for testing.

func Sleep

func Sleep(d time.Duration)

Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately.

func StartTimeTicker

func StartTimeTicker()

StartTimeTicker starts a background task that caches current timestamp per millisecond, which may provide better performance in high-concurrency scenarios.

Types

type AtomicBool

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

func (*AtomicBool) CompareAndSet

func (b *AtomicBool) CompareAndSet(old, new bool) bool

func (*AtomicBool) Get

func (b *AtomicBool) Get() bool

func (*AtomicBool) Set

func (b *AtomicBool) Set(value bool)

type Clock

type Clock interface {
	Now() time.Time
	Sleep(d time.Duration)
	CurrentTimeMillis() uint64
	CurrentTimeNano() uint64
}

func CurrentClock

func CurrentClock() Clock

CurrentClock returns the current clock used by util package.

type MockClock

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

MockClock is used for testing.

func NewMockClock

func NewMockClock() *MockClock

func (*MockClock) CurrentTimeMillis

func (t *MockClock) CurrentTimeMillis() uint64

func (*MockClock) CurrentTimeNano

func (t *MockClock) CurrentTimeNano() 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 is usually used for testing. MockTicker and MockClock are usually used together.

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 is used create MockTicker which is usually used for testing. MockTickerCreator and MockClock are usually used together.

func NewMockTickerCreator

func NewMockTickerCreator() *MockTickerCreator

func (*MockTickerCreator) NewTicker

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

type RealClock

type RealClock struct{}

RealClock wraps some APIs of time package.

func NewRealClock

func NewRealClock() *RealClock

func (*RealClock) CurrentTimeMillis

func (t *RealClock) CurrentTimeMillis() uint64

func (*RealClock) CurrentTimeNano

func (t *RealClock) CurrentTimeNano() 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 wraps time.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 is used to creates RealTicker which wraps time.Ticker.

func NewRealTickerCreator

func NewRealTickerCreator() *RealTickerCreator

func (*RealTickerCreator) NewTicker

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

type SliceHeader

type SliceHeader struct {
	Data unsafe.Pointer
	Len  int
	Cap  int
}

SliceHeader is a safe version of SliceHeader used within this project.

type StringHeader

type StringHeader struct {
	Data unsafe.Pointer
	Len  int
}

StringHeader is a safe version of StringHeader used within this project.

type Ticker

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

Ticker interface encapsulates operations like time.Ticker.

func NewTicker

func NewTicker(d time.Duration) Ticker

type TickerCreator

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

TickerCreator is used to create Ticker.

func CurrentTickerCreator

func CurrentTickerCreator() TickerCreator

CurrentTickerCreator returns the current ticker creator used by util package.

Jump to

Keyboard shortcuts

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