times

package
v1.0.54 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: MIT Imports: 11 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")
	ErrParseError = errors.New("parse error")
)

Functions

func Between added in v1.0.6

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

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

func Debounce added in v1.0.54

func Debounce(yield func(), wait time.Duration) (func(), func())

Debounce 生成一个防抖动函数,用于在一段时间内避免重复触发事件。 参数:

  • yield: 要防抖动的函数。
  • wait: 等待时间,用于确定在触发事件后是否执行函数。

返回值:

  • 第一个返回的函数用于触发防抖动事件。
  • 第二个返回的函数用于阻塞调用,直到防抖动函数执行完毕。
Example
package main

import (
	"fmt"
	"time"

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

func main() {
	play, wait := times.Debounce(func() { fmt.Println("play") }, time.Millisecond)
	defer wait()

	for range 10 {
		play()
	}

}
Output:
play

func Delay added in v1.0.54

func Delay(interval time.Duration) func()

Delay 返回一个闭包,该闭包在调用时确保每次执行之间至少间隔指定的 duration。 这对于需要定期执行任务或限制操作频率的场景非常有用。

Example
package main

import (
	"fmt"
	"time"

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

func main() {
	delay := times.Delay(time.Millisecond * 100)
	before := time.Now()

	for range 3 {
		delay()
	}

	fmt.Println(time.Since(before) >= time.Millisecond*300)

}
Output:
true

func DelayContext added in v1.0.54

func DelayContext(interval time.Duration) func(context.Context) error

DelayContext 返回一个函数,该函数用于在给定间隔时间后处理上下文。 这个函数的主要目的是在满足指定间隔时间之前延迟上下文的处理。

Example
package main

import (
	"context"
	"fmt"
	"time"

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

func main() {
	delay := times.DelayContext(time.Millisecond * 100)
	before := time.Now()

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	for num := range 3 {
		if delay(ctx) != nil {
			break
		}

		if num > 0 {
			cancel()
		}
	}

	fmt.Println(time.Since(before) >= time.Millisecond*200)

}
Output:
true

func Format added in v1.0.26

func Format(input time.Time) string
Example
package main

import (
	"fmt"

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

// nolint: gochecknoglobals
var _time, _ = times.Parse("2023-08-02 12:11:30")

func main() {
	fmt.Println(times.Format(_time))

}
Output:
2023-08-02 12:11:30

func FormatDate added in v1.0.26

func FormatDate(input time.Time) string
Example
package main

import (
	"fmt"

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

// nolint: gochecknoglobals
var _time, _ = times.Parse("2023-08-02 12:11:30")

func main() {
	fmt.Println(times.FormatDate(_time))

}
Output:
2023-08-02

func FormatTime added in v1.0.26

func FormatTime(input time.Time) string
Example
package main

import (
	"fmt"

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

// nolint: gochecknoglobals
var _time, _ = times.Parse("2023-08-02 12:11:30")

func main() {
	fmt.Println(times.FormatTime(_time))

}
Output:
12:11:30

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")

}

func InScope added in v1.0.6

func InScope(start, stop int) bool

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

func Parse added in v1.0.26

func Parse(str string) (time.Time, error)

func ParseNumber added in v1.0.26

func ParseNumber[T constraints.Integer | constraints.Float](num T) time.Time

func Sleep added in v1.0.6

func Sleep(stop int) time.Duration

Sleep 计算睡眠时间.

func Throttle added in v1.0.54

func Throttle(yield func(), interval time.Duration) (func(), func())

Throttle 创建一个节流控制器,用于控制函数的调用频率。

参数:

  • yield: 要节流的函数。
  • interval: 间隔时间。

返回值:

  • 第一个返回的函数用于触发节流事件。
  • 第二个返回的函数用于阻塞调用,直到节流函数执行完毕。
Example
package main

import (
	"fmt"
	"time"

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

func main() {
	play, wait := times.Throttle(func() { fmt.Println("play") }, time.Millisecond*100)
	defer wait()

	delay := times.Delay(time.Millisecond * 30)

	for range 10 {
		play()
		delay()
	}

}
Output:
play
play
play

func WithContextTimer added in v1.0.12

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

WithContextTimer 是一个函数,它使用一个定时器,并在指定时间后执行给定的yield函数, 同时允许通过上下文参数来控制是否取消这个执行。

参数:

ctx: 用于取消定时器操作的上下文。
duration: 定时器的持续时间,决定何时执行yield函数。
yield: 一个函数,当定时器到期时会被调用。

func WithTimer added in v1.0.12

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

WithTimer 创建一个协程,其中包含一个计时器和一个回调函数。 计时器将在指定的持续时间后触发,并调用回调函数。 返回一个停止函数,可以通过调用它来取消计时器和回调函数的执行。

Types

type Duration added in v1.0.29

type Duration int64

func (Duration) Short added in v1.0.29

func (d Duration) Short() string
Example
package main

import (
	"fmt"
	"time"

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

func main() {
	fmt.Println(times.Duration(time.Hour * 30).Short())
	fmt.Println(times.Duration(time.Hour * 3).Short())
	fmt.Println(times.Duration(time.Minute * 3).Short())
	fmt.Println(times.Duration(time.Millisecond * 3).Short())
	fmt.Println(times.Duration(time.Second * 3).Short())
	fmt.Println(times.Duration(time.Second*3 + time.Hour*3 + 3).Short())

}
Output:
1天6小时
3小时
3分钟
3毫秒
3秒钟
3小时3秒钟

func (Duration) String added in v1.0.29

func (d Duration) String() string
Example
package main

import (
	"fmt"
	"time"

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

func main() {
	fmt.Println(times.Duration(time.Microsecond * 3))
	fmt.Println(times.Duration(time.Hour*48 + 1))

}
Output:
3微秒
2天1纳秒

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
package main

import (
	"fmt"

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

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

	for i := range size {
		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
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
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 := range size {
		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
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)

func (*IDWorker) String added in v1.0.14

func (p *IDWorker) String() string

String 字符串ID.

type IntDay added in v1.0.37

type IntDay int32

nolint: recvcheck

func Now2IntDay added in v1.0.37

func Now2IntDay() IntDay
Example
package main

import (
	"fmt"

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

func main() {
	day := times.Now2IntDay()
	fmt.Println(day > 0)

}
Output:
true

func ParseIntDay added in v1.0.37

func ParseIntDay(str string) (IntDay, error)
Example
package main

import (
	"fmt"

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

func main() {
	day, err := times.ParseIntDay("20230918")

	fmt.Println(err)
	fmt.Println(day)
	fmt.Println(day.Year())
	fmt.Println(day.Month())
	fmt.Println(day.Day())

}
Output:
<nil>
20230918
2023
9
18

func Time2IntDay added in v1.0.37

func Time2IntDay(input time.Time) IntDay

func (IntDay) Day added in v1.0.37

func (p IntDay) Day() int

func (IntDay) Marshal added in v1.0.39

func (p IntDay) Marshal() []byte

func (IntDay) MarshalJSON added in v1.0.39

func (p IntDay) MarshalJSON() ([]byte, error)

func (IntDay) Month added in v1.0.37

func (p IntDay) Month() int

func (IntDay) String added in v1.0.37

func (p IntDay) String() string

func (*IntDay) Unmarshal added in v1.0.39

func (p *IntDay) Unmarshal(data []byte) error

func (*IntDay) UnmarshalJSON added in v1.0.39

func (p *IntDay) UnmarshalJSON(data []byte) error

func (IntDay) Year added in v1.0.37

func (p IntDay) Year() int

Jump to

Keyboard shortcuts

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