Documentation ¶
Overview ¶
Package times 时间相关.
Index ¶
- Variables
- func Between(ctx context.Context, start, stop int, yield func(context.Context))
- func Format(input time.Time) string
- func FormatDate(input time.Time) string
- func FormatTime(input time.Time) string
- func Hour(yield func()) func() bool
- func InScope(start, stop int) bool
- func Parse(str string) (time.Time, error)
- func ParseNumber[T constraints.Integer | constraints.Float](num T) time.Time
- func Sleep(stop int) time.Duration
- func WithContextTimer(ctx context.Context, duration time.Duration, yield func())
- func WithTimer(duration time.Duration, yield func()) func()
- type Duration
- type IDWorker
- type IntDay
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrLast = errors.New("last id after now") ErrParseError = errors.New("parse error") )
Functions ¶
func Format ¶ added in v1.0.26
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
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
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") }
Output:
func ParseNumber ¶ added in v1.0.26
func ParseNumber[T constraints.Integer | constraints.Float](num T) time.Time
func WithContextTimer ¶ added in v1.0.12
Types ¶
type Duration ¶ added in v1.0.29
type Duration int64
func (Duration) Short ¶ added in v1.0.29
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秒钟
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 := 0; i < size; i++ { ids[worker.ID()] = i } fmt.Println(len(ids)) }
Output: 10000
func NewIDWorkerByKey ¶ added in v1.0.11
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
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 := 0; i < size; i++ { ids[worker.ID()] = i } fmt.Println(len(ids)) }
Output: 10000
func (*IDWorker) IDAndError ¶ added in v1.0.11
IDAndError 生成ID和错误信息.
func (*IDWorker) IDs ¶ added in v1.0.11
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
type IntDay ¶ added in v1.0.37
type IntDay int32
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
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 (IntDay) MarshalJSON ¶ added in v1.0.39
func (*IntDay) UnmarshalJSON ¶ added in v1.0.39
Click to show internal directories.
Click to hide internal directories.