This commit is contained in:
unknown
2026-02-04 20:27:13 +08:00
commit 3b042d1dad
9410 changed files with 1488147 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package fasttime
import (
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
var sharedFastTime = NewFastTime()
func init() {
if !teaconst.IsMain {
return
}
var ticker = time.NewTicker(200 * time.Millisecond)
goman.New(func() {
for range ticker.C {
sharedFastTime = NewFastTime()
}
})
}
func Now() *FastTime {
return sharedFastTime
}
type FastTime struct {
rawTime time.Time
unixTime int64
unixTimeMilli int64
unixTimeMilliString string
ymd string
round5Hi string
hour int
}
func NewFastTime() *FastTime {
var rawTime = time.Now()
return &FastTime{
rawTime: rawTime,
unixTime: rawTime.Unix(),
unixTimeMilli: rawTime.UnixMilli(),
unixTimeMilliString: types.String(rawTime.UnixMilli()),
ymd: timeutil.Format("Ymd", rawTime),
round5Hi: timeutil.FormatTime("Hi", rawTime.Unix()/300*300),
hour: rawTime.Hour(),
}
}
// Unix 最快获取时间戳的方式,通常用在不需要特别精确时间戳的场景
func (this *FastTime) Unix() int64 {
return this.unixTime
}
// UnixFloor 取整
func (this *FastTime) UnixFloor(seconds int) int64 {
return this.unixTime / int64(seconds) * int64(seconds)
}
// UnixCell 取整并加1
func (this *FastTime) UnixCell(seconds int) int64 {
return this.unixTime/int64(seconds)*int64(seconds) + int64(seconds)
}
// UnixNextMinute 获取下一分钟开始的时间戳
func (this *FastTime) UnixNextMinute() int64 {
return this.UnixCell(60)
}
// UnixMilli 获取时间戳,精确到毫秒
func (this *FastTime) UnixMilli() int64 {
return this.unixTimeMilli
}
func (this *FastTime) UnixMilliString() (int64, string) {
return this.unixTimeMilli, this.unixTimeMilliString
}
func (this *FastTime) Ymd() string {
return this.ymd
}
func (this *FastTime) Round5Hi() string {
return this.round5Hi
}
func (this *FastTime) Format(layout string) string {
return timeutil.Format(layout, this.rawTime)
}
func (this *FastTime) Hour() int {
return this.hour
}

View File

@@ -0,0 +1,62 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package fasttime_test
import (
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
timeutil "github.com/iwind/TeaGo/utils/time"
"testing"
"time"
)
func TestFastTime_Unix(t *testing.T) {
for i := 0; i < 5; i++ {
var now = fasttime.Now()
t.Log(now.Unix(), now.UnixMilli(), "real:", time.Now().Unix())
time.Sleep(1 * time.Second)
}
}
func TestFastTime_UnixMilli(t *testing.T) {
t.Log(fasttime.Now().UnixMilliString())
}
func TestFastTime_UnixFloor(t *testing.T) {
var now = fasttime.Now()
var timestamp = time.Now().Unix()
t.Log("floor 60:", timestamp, now.UnixFloor(60), timeutil.FormatTime("Y-m-d H:i:s", now.UnixFloor(60)))
t.Log("ceil 60:", timestamp, now.UnixCell(60), timeutil.FormatTime("Y-m-d H:i:s", now.UnixCell(60)))
t.Log("floor 300:", timestamp, now.UnixFloor(300), timeutil.FormatTime("Y-m-d H:i:s", now.UnixFloor(300)))
t.Log("next minute:", now.UnixNextMinute(), timeutil.FormatTime("Y-m-d H:i:s", now.UnixNextMinute()))
t.Log("day:", now.Ymd())
t.Log("round 5 minute:", now.Round5Hi())
}
func TestFastTime_Format(t *testing.T) {
var now = fasttime.Now()
t.Log(now.Format("Y-m-d H:i:s"))
}
func TestFastTime_Hour(t *testing.T) {
var now = fasttime.Now()
t.Log(now.Hour())
}
func BenchmarkNewFastTime(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var now = fasttime.Now()
_ = now.Ymd()
}
})
}
func BenchmarkNewFastTime_Raw(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var now = time.Now()
_ = timeutil.Format("Ymd", now)
}
})
}