tick

package module
v0.0.0-...-8dfad59 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: MIT Imports: 12 Imported by: 13

README

tick

跟时间相关

  1. 支持 JSON 序列化的 Duration: tick.Dur

  2. 解析 天/d/周/w/月/month 等标准库不支持的单位

  3. Round 规整,100秒以下最多3位数字

  4. tick.Sleep() 带 context.Context 的休眠

  5. tick.SleepRandom() 随机休眠

  6. tick.Tick() 滴答执行

  7. func ParseThinkTime(think string) (t *ThinkTime, err error) 思考时间

  8. tick.Time 支持 JSON 反序列化

  9. func SleepRandom(ctx context.Context, max time.Duration)

  10. func Sleep(ctx context.Context, d time.Duration)

  11. func Jitter(interval, jitter time.Duration) time.Duration 在 interval 上增加最大 jitter 随机抖动时间

  12. func ParseTime(tm string) (t time.Time, err error) 解析时间字符串

    1. 格式1(绝对时间): RFC3339 "2006-01-02T15:04:05Z07:00"
    2. 格式2(偏移间隔): -10d 10天前的此时
  13. func ParseTimeMilli(tm string) (unixMilli int64, err error)

  14. func Round(d time.Duration) time.Duration 格式化,100秒以下规整到最多3位数字

  15. func Parse(s string, allowUnits ...string) (time.Duration, []Fraction, error) 扩展解析 天/d/周/w/月/month 等标准库不支持的单位

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrUnknownTimeFormat = errors.New("unknown time format")

ErrUnknownTimeFormat defines the error type for unknown time format.

Functions

func FormatTime

func FormatTime(t time.Time, format string) string

FormatTime format time with Java style layout.

func Getenv

func Getenv(str string, defaultValue time.Duration) (time.Duration, error)

func Jitter

func Jitter(interval, jitter time.Duration) time.Duration

Jitter 在 interval 上增加最大 jitter 随机抖动时间

func ParseTime

func ParseTime(tm string) (t time.Time, err error)

ParseTime 解析时间字符串 格式1(绝对时间): RFC3339 "2006-01-02T15:04:05Z07:00" 格式2(偏移间隔): -10d 10天前的此时

func ParseTimeMilli

func ParseTimeMilli(tm string) (unixMilli int64, err error)

func Round

func Round(d time.Duration) time.Duration

Round rounds a duration with a precision of 3 digits if it is less than 100s. https://stackoverflow.com/a/68870075

Example
package main

import (
	"fmt"
	"math"
	"time"

	"github.com/bingoohuang/ngg/tick"
)

func main() {
	for i := 0; i < 15; i++ {
		d := time.Duration(3.455555 * math.Pow(10, float64(i)))
		fmt.Printf("%2d  %12v  %6s\n", i, d, tick.Round(d))

		//         original formatted

	}
}
Output:

 0           3ns     3ns
 1          34ns    34ns
 2         345ns   345ns
 3       3.455µs  3.46µs
 4      34.555µs  34.6µs
 5     345.555µs   346µs
 6    3.455555ms  3.46ms
 7    34.55555ms  34.6ms
 8    345.5555ms   346ms
 9     3.455555s   3.46s
10     34.55555s   34.6s
11    5m45.5555s   5m46s
12    57m35.555s  57m36s
13   9h35m55.55s  9h35m56s
14   95h59m15.5s  95h59m16s

func Sleep

func Sleep(ctx context.Context, d time.Duration) error

func SleepRandom

func SleepRandom(ctx context.Context, max time.Duration) error

SleepRandom will sleep for a random amount of time up to max. If the shutdown channel is closed, it will return before it has finished sleeping.

func Tick

func Tick(ctx context.Context, interval, jitter time.Duration, f func() error) error

func ToLayout

func ToLayout(s string) string

ToLayout converts Java style layout to golang.

Types

type Dur

type Dur struct {
	time.Duration
}

func MakeDur

func MakeDur(d time.Duration) Dur

func (Dur) MarshalJSON

func (d Dur) MarshalJSON() ([]byte, error)

func (*Dur) UnmarshalJSON

func (d *Dur) UnmarshalJSON(b []byte) (err error)

type ThinkTime

type ThinkTime struct {
	Min, Max time.Duration
}

func ParseThinkTime

func ParseThinkTime(think string) (t *ThinkTime, err error)

func (ThinkTime) Think

func (t ThinkTime) Think(thinkNow bool) (thinkTime time.Duration)

type Time

type Time time.Time

Time defines a time.Time that can be used in struct tag for JSON unmarshalling.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals bytes to JSONTime.

Jump to

Keyboard shortcuts

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