time

package module
v0.0.0-...-0d9f38a Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: MIT Imports: 9 Imported by: 3

Documentation

Index

Constants

View Source
const (
	// ErrTimerIsNotRunning indicates that timer is not running
	ErrTimerIsNotRunning = E("timer is not running")
	// ErrTimerIsAlreadyRunning indicates that timer is already running
	ErrTimerIsAlreadyRunning = E("timer is already running")
)

Variables

View Source
var (
	// Beijing 表示北京时间
	Beijing = time.FixedZone("Asia/Beijing", 8*60*60)
)

Functions

func Days

func Days[T constraints.Integer](days T) time.Duration

Days 用天数生成 time.Duration

func Hour

func Hour[T constraints.Integer](hours T) time.Duration

Hour 用小时数生成 time.Duration

func Milli

func Milli[T constraints.Integer](msecs T) time.Duration

Milli 用毫秒数生成 time.Duration

func Min

func Min[T constraints.Integer](mins T) time.Duration

Min 用分钟数生成 time.Duration

func Sec

func Sec[T constraints.Integer](secs T) time.Duration

Sec 用秒数生成 time.Duration

func Sleep

func Sleep[N constraints.Float | constraints.Integer](secs N)

Sleep 按秒数 sleep

func SleepToNextSecond

func SleepToNextSecond()

SleepToNextSecond sleep 到墙上时钟的下一秒

func SleepToNextSecondsN

func SleepToNextSecondsN(n int)

SleepToNextSecondsN sleep x N 版

func UnixFloat

func UnixFloat(t time.Time) float64

UnixFloat 返回个位数为秒、但包含了小数的 UTC 时间戳值

func UpTime

func UpTime() time.Duration

UpTime 返回程序启动了多久。单调时钟

func Wait

func Wait(f func() (done bool), interval ...time.Duration)

Wait 等待是否满足条件,如果不满足则自旋等待。interval 如果不传参的话,默认是 100ms。如果传参的话,最小则为 1ms

Types

type Age

type Age struct {
	Years  int
	Months int
	Days   int
}

Age 表示年龄

func CalculateAge

func CalculateAge(birthday time.Time, to ...time.Time) Age

CalculateAge 计算年龄。如果目标时间未到, 则统一返回 0

func (Age) DetailedDesc

func (a Age) DetailedDesc() string

DetailedDesc 详细描述

func (Age) IsZero

func (a Age) IsZero() bool

func (Age) String

func (a Age) String() string

type E

type E string

E is the internal error type

func (E) Error

func (e E) Error() string

Error implements errors interface

type PeriodicSleeper

type PeriodicSleeper interface {
	Sleep(d time.Duration)
	Reset()
}

PeriodicSleeper 用在以下场景: 每次 sleep 结束之后, 当指定下一次 sleep 时长 T 时, 不会 完全按照 T 来 sleep, 而是参照自身上一次 sleep 的预期时间 tₙ₋₁ + T 来 sleep。这与 Tick 类似, 不同的是每次 sleep 的时间需要重新指定。

需要注意的是: 如果 tₙ₋₁ + T 小于当前时间, 则会触发一次 Reset 动作之后再 + T

func NewPeriodicSleeper

func NewPeriodicSleeper() PeriodicSleeper

NewPeriodicSleeper 以当前时间新建一个 PeriodicSleeper

type Tick

type Tick interface {
	Run()
	Stop()
	SetCallback(fn TickCallback)
}

Tick 提供一个长期尽可能周期性的 tick

func NewTickBeta

func NewTickBeta(interval time.Duration, callback TickCallback) (Tick, error)

NewTickBeta 新建一个 tick, 目前暂时实现毫秒级, 再低了不支持。此外, 仅支持精确到毫秒, 更低的不支持

此外, 目前理论上再高并发时会有竞争问题, 建议不要频繁创建销毁

type TickCallback

type TickCallback func(param TickCallbackParam)

type TickCallbackParam

type TickCallbackParam struct {
}

type TimeoutCallback

type TimeoutCallback func()

TimeoutCallback 表示超时时的回调函数

type Timer

type Timer interface {
	// Run starts timer in the background.
	Run() error

	// Stop stops the timer if it is running.
	Stop() error

	// Running check whether timer is running.
	Running() bool

	// Remain returns remaining time in the timer
	Remain() time.Duration

	// Elapsed returns elapsed time in the timer
	Elapsed() time.Duration
}

Timer is re-packaged time.Timer object, but have different methods.

func NewTimer

func NewTimer(d time.Duration, cb TimeoutCallback) Timer

NewTimer returns a new timer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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