times

package
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: MIT Imports: 6 Imported by: 3

Documentation

Overview

Package times 时间相关.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrLast = errors.New("last id after now")

Functions

func Between added in v1.0.6

func Between(ctx context.Context, start, stop int, yield func(context.Context))

Between 时间范围内执行,超出时间终止.

func Hour

func Hour(yield func()) func() bool

Hour 整点运行,返回取消方法.

Example
package main

import (
	"time"

	"github.com/xuender/kit/logs"
	"github.com/xuender/kit/times"
)

func main() {
	logs.I.Println("start")

	cancel := times.Hour(func() {
		logs.I.Println("run")
	})

	go func() {
		time.Sleep(time.Second)
		cancel()
		logs.I.Println("stop")
	}()

	logs.I.Println("sleep")
	time.Sleep(2 * time.Second)
	logs.I.Println("end")

}
Output:

func InScope added in v1.0.6

func InScope(start, stop int) bool

InScope 返回是否在时间范围内. nolint: cyclop

func Sleep added in v1.0.6

func Sleep(stop int) time.Duration

Sleep 计算睡眠时间.

func WithContextTimer added in v1.0.12

func WithContextTimer(ctx context.Context, duration time.Duration, yield func())

func WithTimer added in v1.0.12

func WithTimer(duration time.Duration, yield func()) func()

Types

type IDWorker added in v1.0.11

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

func NewIDWorker added in v1.0.11

func NewIDWorker() *IDWorker

NewIDWorker 创建非分布式的 IDWorker.

Example

ExampleNewIDWorker is an example function.

package main

import (
	"fmt"

	"github.com/xuender/kit/times"
)

func main() {
	worker := times.NewIDWorker()
	size := 10000
	ids := make(map[int64]int, size)

	for i := 0; i < size; i++ {
		ids[worker.ID()] = i
	}

	fmt.Println(len(ids))

}
Output:

10000

func NewIDWorkerByKey added in v1.0.11

func NewIDWorkerByKey(key string) *IDWorker

NewIDWorkerByKey 根据字符串创建 IDWorker.

Example

ExampleNewIDWorkerByKey is an example function.

package main

import (
	"fmt"

	"github.com/xuender/kit/times"
)

func main() {
	worker1 := times.NewIDWorkerByKey("a")
	worker2 := times.NewIDWorkerByKey("b")

	fmt.Println(worker1.ID() != worker2.ID())

}
Output:

true

func NewIDWorkerByMachine added in v1.0.11

func NewIDWorkerByMachine(machine, machineLength int64) *IDWorker

NewIDWorkerByMachine 根据机器信息创建 IDWorker.

Example

ExampleNewIDWorkerByMachine is an example function.

package main

import (
	"fmt"

	"github.com/xuender/kit/times"
)

func main() {
	worker := times.NewIDWorkerByMachine(1, 19)
	size := 10000
	ids := make(map[int64]int, size)

	for i := 0; i < size; i++ {
		ids[worker.ID()] = i
	}

	fmt.Println(len(ids))

}
Output:

10000

func (*IDWorker) ID added in v1.0.11

func (p *IDWorker) ID() int64

ID 生成ID.

func (*IDWorker) IDAndError added in v1.0.11

func (p *IDWorker) IDAndError() (int64, error)

IDAndError 生成ID和错误信息.

func (*IDWorker) IDs added in v1.0.11

func (p *IDWorker) IDs(num int) []int64
Example

ExampleIDWorker_IDs is an example function.

package main

import (
	"fmt"

	"github.com/xuender/kit/times"
)

func main() {
	worker := times.NewIDWorker()
	size := 10000
	ids := make(map[int64]int, size)

	for i, uid := range worker.IDs(size) {
		ids[uid] = i
	}

	fmt.Println(len(ids))

}
Output:

10000

func (*IDWorker) IDsAndError added in v1.0.11

func (p *IDWorker) IDsAndError(num int) ([]int64, error)

Jump to

Keyboard shortcuts

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