ctime

package
v0.0.0-...-c902e56 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2022 License: MIT Imports: 2 Imported by: 0

README

ctime

基本用途

  1. 解析时间字符串到时间类型,例如 1s, 500ms等,常用于解析配置文件

示例

见example_test.go的example

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Duration

type Duration time.Duration

用于将具体的时间字符串解析成duration类型 例如 1s, 500ms

func (Duration) Shrink

将当前时间与context超时时间比较,并缩短至最小值 若当前context不存在超时时间,则使用duration作为超时时间,并返回包含超时时间的新context

Example
d := Duration(time.Second * 5)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
d, ctx, cancel = d.Shrink(ctx)
defer cancel()
fmt.Printf("%v\n", time.Duration(d) == time.Second*5)

d = Duration(time.Second * 5)
d, ctx, cancel = d.Shrink(context.Background())
defer cancel()
_, ok := ctx.Deadline()
fmt.Printf("%v\n", ok)
Output:

false
true

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(text []byte) error

反序列化方法

Example
var d Duration
err := d.UnmarshalText([]byte("1s"))
if err != nil {
	return
}
fmt.Printf("%v\n", time.Duration(d) == time.Second)
Output:

true

Jump to

Keyboard shortcuts

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