Documentation ¶
Index ¶
- Variables
- func FormatTime(t time.Time, format string) string
- func Getenv(str string, defaultValue time.Duration) (time.Duration, error)
- func Jitter(interval, jitter time.Duration) time.Duration
- func ParseTime(tm string) (t time.Time, err error)
- func ParseTimeMilli(tm string) (unixMilli int64, err error)
- func Round(d time.Duration) time.Duration
- func Sleep(ctx context.Context, d time.Duration) error
- func SleepRandom(ctx context.Context, max time.Duration) error
- func Tick(ctx context.Context, interval, jitter time.Duration, f func() error) error
- func ToLayout(s string) string
- type Dur
- type ThinkTime
- type Time
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 ¶
FormatTime format time with Java style layout.
func ParseTime ¶
ParseTime 解析时间字符串 格式1(绝对时间): RFC3339 "2006-01-02T15:04:05Z07:00" 格式2(偏移间隔): -10d 10天前的此时
func ParseTimeMilli ¶
func Round ¶
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 SleepRandom ¶
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.
Types ¶
type ThinkTime ¶
func ParseThinkTime ¶
Source Files ¶
Click to show internal directories.
Click to hide internal directories.