gxtime

package
v1.14.1 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

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

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

func GetEndTime(format string) time.Time

func InitDefaultTimerWheel

func InitDefaultTimerWheel()

InitDefaultTimerWheel initializes a default timer wheel

func Now

func Now() time.Time

Now returns the current time.

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 Tick

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

func TimeDayDuration(day float64) time.Duration

func TimeHourDuration

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

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

func NewTicker(d time.Duration) *Ticker

NewTicker returns a new Ticker

func TickFunc

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

TickFunc returns a Ticker

func (*Ticker) Reset

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

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

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

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

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

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

func (t *Timer) Stop()

Stop prevents the Timer from firing.

type TimerFunc

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

type TimerID = uint64

TimerID is the id of a timer node

type TimerType

type TimerType int32

TimerType defines a timer task type.

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

type TimerWheel

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

TimerWheel is a timer based on multiple wheels

func GetDefaultTimerWheel

func GetDefaultTimerWheel() *TimerWheel

func NewTimerWheel

func NewTimerWheel() *TimerWheel

NewTimerWheel returns a @TimerWheel object.

func (*TimerWheel) AddTimer

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

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

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

func (w *TimerWheel) Close()

Close stops the timer wheel and wait for all grs.

func (*TimerWheel) NewTicker

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

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

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

Now returns the current time

func (*TimerWheel) Sleep

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

func (w *TimerWheel) Stop()

Stop stops the ticker

func (*TimerWheel) Tick

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

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

TickFunc returns a Ticker

func (*TimerWheel) TimerNumber

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