time

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 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 Point

type Point struct {
	Hour   int
	Minute int
	Second int
}

Point 表示一个时间点, 用于比较。支持 30 小时制 (实际上支持到 48 小时)

func NewPoint

func NewPoint[I constraints.Integer](hour, minute, second I) Point

NewPoint 创建一个时间点

func (Point) MarshalText

func (p Point) MarshalText() ([]byte, error)

func (Point) String

func (p Point) String() string

func (*Point) UnmarshalText

func (p *Point) UnmarshalText(text []byte) error

type Range

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

Range 表示一个时间范围, 用于判断时间是否在这个范围内

func DetermineRange

func DetermineRange(tm time.Time) *Range

DetermineRange 确定时间是否在指定范围内

func (*Range) Check

func (r *Range) Check() bool

Check 根据已经配置好的范围, 检查时间是否在范围内

func (*Range) GE

func (r *Range) GE(p Point) *Range

GE 设定 p <= tm

func (*Range) GT

func (r *Range) GT(p Point) *Range

GT 设定 p < tm

func (*Range) LE

func (r *Range) LE(p Point) *Range

LE 设定 tm <= p

func (*Range) LT

func (r *Range) LT(p Point) *Range

LT 设定 tm < p

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