util

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2021 License: Apache-2.0 Imports: 12 Imported by: 57

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 added in v0.4.0

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 added in v0.6.0

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 added in v0.3.0

func NewUuid() string

func Now added in v1.0.3

func Now() time.Time

Now returns the current local time.

func RunWithRecover

func RunWithRecover(f func())

func SetClock added in v1.0.3

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 added in v1.0.3

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 added in v1.0.3

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 added in v0.4.0

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 added in v1.0.3

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

func CurrentClock added in v1.0.3

func CurrentClock() Clock

CurrentClock returns the current clock used by util package.

type MockClock added in v1.0.3

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

MockClock is used for testing.

func NewMockClock added in v1.0.3

func NewMockClock() *MockClock

func (*MockClock) CurrentTimeMillis added in v1.0.3

func (t *MockClock) CurrentTimeMillis() uint64

func (*MockClock) CurrentTimeNano added in v1.0.3

func (t *MockClock) CurrentTimeNano() uint64

func (*MockClock) Now added in v1.0.3

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

func (*MockClock) Sleep added in v1.0.3

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

type MockTicker added in v1.0.3

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

MockTicker is usually used for testing. MockTicker and MockClock are usually used together.

func NewMockTicker added in v1.0.3

func NewMockTicker(d time.Duration) *MockTicker

func (*MockTicker) C added in v1.0.3

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

func (*MockTicker) Stop added in v1.0.3

func (t *MockTicker) Stop()

type MockTickerCreator added in v1.0.3

type MockTickerCreator struct{}

MockTickerCreator is used create MockTicker which is usually used for testing. MockTickerCreator and MockClock are usually used together.

func NewMockTickerCreator added in v1.0.3

func NewMockTickerCreator() *MockTickerCreator

func (*MockTickerCreator) NewTicker added in v1.0.3

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

type RealClock added in v1.0.3

type RealClock struct{}

RealClock wraps some APIs of time package.

func NewRealClock added in v1.0.3

func NewRealClock() *RealClock

func (*RealClock) CurrentTimeMillis added in v1.0.3

func (t *RealClock) CurrentTimeMillis() uint64

func (*RealClock) CurrentTimeNano added in v1.0.3

func (t *RealClock) CurrentTimeNano() uint64

func (*RealClock) Now added in v1.0.3

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

func (*RealClock) Sleep added in v1.0.3

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

type RealTicker added in v1.0.3

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

RealTicker wraps time.Ticker.

func NewRealTicker added in v1.0.3

func NewRealTicker(d time.Duration) *RealTicker

func (*RealTicker) C added in v1.0.3

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

func (*RealTicker) Stop added in v1.0.3

func (t *RealTicker) Stop()

type RealTickerCreator added in v1.0.3

type RealTickerCreator struct{}

RealTickerCreator is used to creates RealTicker which wraps time.Ticker.

func NewRealTickerCreator added in v1.0.3

func NewRealTickerCreator() *RealTickerCreator

func (*RealTickerCreator) NewTicker added in v1.0.3

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 added in v1.0.3

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

Ticker interface encapsulates operations like time.Ticker.

func NewTicker added in v1.0.3

func NewTicker(d time.Duration) Ticker

type TickerCreator added in v1.0.3

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

TickerCreator is used to create Ticker.

func CurrentTickerCreator added in v1.0.3

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