gxtime

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: Apache-2.0 Imports: 10 Imported by: 24

Documentation

Overview

Package gxtime encapsulates some golang.time functions

Package gxtime encapsulates some golang.time functions

Package gxtime encapsulates some golang.time functions

Package gxtime encapsulates some golang.time functions

Package gxtime encapsulates some golang.time functions

Package gxtime encapsulates some golang.time functions ref: https://github.com/AlexStocks/go-practice/blob/master/time/siddontang_time_wheel.go

Index

Constants

View Source
const (
	TimerActionAdd   timerAction = 1
	TimerActionDel   timerAction = 2
	TimerActionReset timerAction = 3
)

Variables

View Source
var ErrTimeChannelClosed = errors.New("timer channel closed")

nolint

Functions

func After added in v1.9.10

func After(d time.Duration) <-chan time.Time

After waits for the duration to elapse and then sends the current time on the returned channel.

func Future

func Future(sec int, f func())

func GetEndTime added in v1.9.10

func GetEndTime(format string) time.Time

func InitDefaultTimerWheel added in v1.9.10

func InitDefaultTimerWheel()

InitDefaultTimerWheel initializes a default timer wheel

func Now added in v1.9.10

func Now() time.Time

Now returns the current time.

func Sleep added in v1.9.10

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

func Tick(d time.Duration) <-chan time.Time

Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. While Tick is useful for clients that have no need to shut down the Ticker, be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks". Unlike NewTicker, Tick will return nil if d <= 0.

func Time2Unix

func Time2Unix(t time.Time) int64

注意把time转换成unix的时候有精度损失,只返回了秒值,没有用到纳秒值

func Time2UnixNano

func Time2UnixNano(t time.Time) int64

func TimeDayDuration added in v1.9.10

func TimeDayDuration(day float64) time.Duration

func TimeHourDuration added in v1.9.10

func TimeHourDuration(hour float64) time.Duration

func TimeMicrosecondDuration

func TimeMicrosecondDuration(m float64) time.Duration

func TimeMillisecondDuration

func TimeMillisecondDuration(m float64) time.Duration

func TimeMinuteDuration

func TimeMinuteDuration(minute float64) time.Duration

func TimeNanosecondDuration

func TimeNanosecondDuration(n float64) time.Duration

func TimeSecondDuration

func TimeSecondDuration(sec float64) time.Duration

func Unix2Time

func Unix2Time(unix int64) time.Time

func UnixNano2Time

func UnixNano2Time(nano int64) time.Time

func UnixString2Time

func UnixString2Time(unix string) time.Time

func YMD

func YMD(year int, month int, day int, hour int, minute int, sec int) int

desc: convert year-month-day-hour-minute-second to int in second @month: 1 ~ 12 @hour: 0 ~ 23 @minute: 0 ~ 59

func YMDPrint

func YMDPrint(sec int, nsec int) string

func YMDUTC

func YMDUTC(year int, month int, day int, hour int, minute int, sec int) int

@YMD in UTC timezone

Types

type CountWatch

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

func (*CountWatch) Count

func (w *CountWatch) Count() int64

func (*CountWatch) Reset

func (w *CountWatch) Reset()

func (*CountWatch) Start

func (w *CountWatch) Start()

type Ticker added in v1.9.10

type Ticker struct {
	C  <-chan time.Time
	ID TimerID
	// contains filtered or unexported fields
}

Ticker is a wrapper of TimerWheel in golang Ticker style

func NewTicker added in v1.9.10

func NewTicker(d time.Duration) *Ticker

NewTicker returns a new Ticker

func TickFunc added in v1.9.10

func TickFunc(d time.Duration, f func()) *Ticker

TickFunc returns a Ticker

func (*Ticker) Reset added in v1.9.10

func (t *Ticker) Reset(d time.Duration)

Reset stops a ticker and resets its period to the specified duration. The next tick will arrive after the new period elapses.

func (*Ticker) Stop added in v1.9.10

func (t *Ticker) Stop()

Stop turns off a ticker. After Stop, no more ticks will be sent. Stop does not close the channel, to prevent a concurrent goroutine reading from the channel from seeing an erroneous "tick".

type Timer added in v1.9.10

type Timer struct {
	C  <-chan time.Time
	ID TimerID
	// contains filtered or unexported fields
}

Timer is a wrapper of TimeWheel to supply go timer funcs

func AfterFunc added in v1.9.10

func AfterFunc(d time.Duration, f func()) *Timer

AfterFunc waits for the duration to elapse and then calls f in its own goroutine. It returns a Timer that can be used to cancel the call using its Stop method.

func NewTimer added in v1.9.10

func NewTimer(d time.Duration) *Timer

NewTimer creates a new Timer that will send the current time on its channel after at least duration d.

func (*Timer) Reset added in v1.9.10

func (t *Timer) Reset(d time.Duration)

Reset changes the timer to expire after duration d. It returns true if the timer had been active, false if the timer had expired or been stopped.

func (*Timer) Stop added in v1.9.10

func (t *Timer) Stop()

Stop prevents the Timer from firing.

type TimerFunc added in v1.9.10

type TimerFunc func(ID TimerID, expire time.Time, arg interface{}) error

TimerFunc defines the time func. if the return error is not nil, the related timer will be closed.

type TimerID added in v1.9.10

type TimerID = uint64

TimerID is the id of a timer node

type TimerType added in v1.9.10

type TimerType int32

TimerType defines a timer task type.

const (
	TimerOnce TimerType = 0x1 << 0
	TimerLoop TimerType = 0x1 << 1
)

type TimerWheel added in v1.9.10

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

TimerWheel is a timer based on multiple wheels

func GetDefaultTimerWheel added in v1.9.10

func GetDefaultTimerWheel() *TimerWheel

func NewTimerWheel added in v1.9.10

func NewTimerWheel() *TimerWheel

NewTimerWheel returns a @TimerWheel object.

func (*TimerWheel) AddTimer added in v1.9.10

func (w *TimerWheel) AddTimer(f TimerFunc, typ TimerType, period time.Duration, arg interface{}) (*Timer, error)

AddTimer adds a timer asynchronously and returns a timer struct obj. It returns error if it failed.

Attention that @f may block the timer gr. So u should create a gr to exec ur function asynchronously if it may take a long time.

args:

@f: timer function.
@typ: timer type
@period: timer loop interval. its unit is nanosecond.
@arg: timer argument which is used by @f.

func (*TimerWheel) After added in v1.9.10

func (w *TimerWheel) After(d time.Duration) <-chan time.Time

After waits for the duration to elapse and then sends the current time on the returned channel.

func (*TimerWheel) AfterFunc added in v1.9.10

func (w *TimerWheel) AfterFunc(d time.Duration, f func()) *Timer

AfterFunc waits for the duration to elapse and then calls f in its own goroutine. It returns a Timer that can be used to cancel the call using its Stop method.

func (*TimerWheel) Close added in v1.9.10

func (w *TimerWheel) Close()

Close stops the timer wheel and wait for all grs.

func (*TimerWheel) NewTicker added in v1.9.10

func (w *TimerWheel) NewTicker(d time.Duration) *Ticker

NewTicker returns a new Ticker containing a channel that will send the time on the channel after each tick. The period of the ticks is specified by the duration argument. The ticker will adjust the time interval or drop ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources.

func (*TimerWheel) NewTimer added in v1.9.10

func (w *TimerWheel) NewTimer(d time.Duration) *Timer

NewTimer creates a new Timer that will send the current time on its channel after at least duration d.

func (*TimerWheel) Now added in v1.9.10

func (w *TimerWheel) Now() time.Time

Now returns the current time

func (*TimerWheel) Sleep added in v1.9.10

func (w *TimerWheel) 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 (*TimerWheel) Stop added in v1.9.10

func (w *TimerWheel) Stop()

Stop stops the ticker

func (*TimerWheel) Tick added in v1.9.10

func (w *TimerWheel) Tick(d time.Duration) <-chan time.Time

Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. While Tick is useful for clients that have no need to shut down the Ticker, be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks". Unlike NewTicker, Tick will return nil if d <= 0.

func (*TimerWheel) TickFunc added in v1.9.10

func (w *TimerWheel) TickFunc(d time.Duration, f func()) *Ticker

TickFunc returns a Ticker

func (*TimerWheel) TimerNumber added in v1.9.10

func (w *TimerWheel) TimerNumber() int

TimerNumber returns the timer obj number in wheel

type Wheel

type Wheel struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewWheel

func NewWheel(span time.Duration, buckets int) *Wheel

func (*Wheel) After

func (w *Wheel) After(timeout time.Duration) <-chan struct{}

func (*Wheel) Now

func (w *Wheel) Now() time.Time

func (*Wheel) Stop

func (w *Wheel) Stop()

Jump to

Keyboard shortcuts

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