Skip to content

Commit

Permalink
fix 修改Str2Timestruct/Str2Timestamp/IsDate2time参数
Browse files Browse the repository at this point in the history
  • Loading branch information
kakuilan committed Mar 6, 2024
1 parent e31e829 commit f8d165f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
1 change: 1 addition & 0 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ var strTime4 = "2020-03-10 23:04:35"
var strTime5 = "2020-03-08 23:04:35"
var strTime6 = "2020-06-25 23:59:59"
var strTime7 = "1990-01-02 03:14:59"
var strTime8 = "2024-03-06 11:30:18"
var intTime1 = 1562811851
var myDate1, _ = time.ParseInLocation("2006-01-02 15:04:05", strTime4, time.Local)
var myDate2, _ = time.ParseInLocation("2006-01-02 15:04:05", strTime5, time.Local)
Expand Down
15 changes: 15 additions & 0 deletions kgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ type (
LkkDebug struct {
}

TimeOption struct {
IsUTC bool //是否UTC
Layout string //时间格式
Zone string //时区名称
Location *time.Location //本地化
}

// LkkFileCover 枚举类型,文件是否覆盖
LkkFileCover int8
// LkkFileType 枚举类型,文件类型
Expand Down Expand Up @@ -418,4 +425,12 @@ var (
"qwertyuiop",
"zxcvbnm",
}

//默认时间选项
DefaultTimeOption = TimeOption{
IsUTC: false,
Layout: "2006-01-02 15:04:05",
Zone: "",
Location: kuptime.Location(),
}
)
49 changes: 31 additions & 18 deletions time.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kgo

import (
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -63,29 +62,42 @@ func (kt *LkkTime) MicroTime() int64 {
return time.Now().UnixNano() / int64(time.Microsecond)
}

// Str2Timestruct 将字符串转换为时间结构.
// Str2Timestruct 将字符串转换为时间结构,默认为本地时区.
// str 为要转换的字符串;
// format 为该字符串的格式,默认为"2006-01-02 15:04:05" .
func (kt *LkkTime) Str2Timestruct(str string, format ...string) (time.Time, error) {
var f string
if len(format) > 0 {
f = strings.Trim(format[0], " ")
// option 为时间选项TimeOption.
func (kt *LkkTime) Str2Timestruct(str string, option ...TimeOption) (res time.Time, err error) {
var opt TimeOption
if len(option) > 0 {
opt = option[0]
} else {
f = "2006-01-02 15:04:05"
opt = DefaultTimeOption
}
if opt.Layout == "" {
opt.Layout = DefaultTimeOption.Layout
}

if len(str) != len(f) {
return time.Now(), errors.New("[Str2Timestruct]`format error")
if opt.IsUTC {
res, err = time.Parse(opt.Layout, str)
} else if opt.Location != nil {
res, err = time.ParseInLocation(opt.Layout, str, opt.Location)
} else if opt.Zone != "" {
var loc *time.Location
loc, err = time.LoadLocation(opt.Zone)
if err == nil {
res, err = time.ParseInLocation(opt.Layout, str, loc)
}
} else {
res, err = time.ParseInLocation(opt.Layout, str, kuptime.Location())
}

return time.ParseInLocation(f, str, kuptime.Location())
return
}

// Str2Timestamp 将字符串转换为时间戳,秒.
// Str2Timestamp 将字符串转换为时间戳(默认为本地时区),秒.
// str 为要转换的字符串;
// format 为该字符串的格式,默认为"2006-01-02 15:04:05" .
func (kt *LkkTime) Str2Timestamp(str string, format ...string) (int64, error) {
tim, err := kt.Str2Timestruct(str, format...)
// option 为时间选项TimeOption.
func (kt *LkkTime) Str2Timestamp(str string, option ...TimeOption) (int64, error) {
tim, err := kt.Str2Timestruct(str, option...)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -325,6 +337,7 @@ func (kt *LkkTime) DaysBetween(fromDate, toDate time.Time) int {
}

// IsDate2time 检查字符串是否日期格式,并转换为时间戳.注意,时间戳可能为负数(小于1970年时).
// option 为时间选项TimeOption.
// 匹配如:
//
// 0000
Expand All @@ -340,7 +353,7 @@ func (kt *LkkTime) DaysBetween(fromDate, toDate time.Time) int {
// 0000/00/00 00:00:00
//
// 等日期格式.
func (kt *LkkTime) IsDate2time(str string) (bool, int64) {
func (kt *LkkTime) IsDate2time(str string, option ...TimeOption) (bool, int64) {
if str == "" {
return false, 0
} else if strings.ContainsRune(str, '/') {
Expand All @@ -358,12 +371,12 @@ func (kt *LkkTime) IsDate2time(str string) (bool, int64) {
str = str + reference[leng:19]
}

tim, err := KTime.Str2Timestamp(str)
ts, err := KTime.Str2Timestamp(str, option...)
if err != nil {
return false, 0
}

return true, tim
return true, ts
}

// FormatDuration 格式化时长为字符串(如*h*m*s);其中t为时长,默认为秒;colon为是否使用冒号.
Expand Down
21 changes: 18 additions & 3 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ func TestTime_Str2Timestruct(t *testing.T) {
assert.Equal(t, res.Year(), 2019)
assert.Equal(t, int(res.Month()), 7)
assert.Equal(t, res.Day(), 11)

//UTC时间
res, err = KTime.Str2Timestruct(strTime8, TimeOption{IsUTC: true})
assert.Nil(t, err)
assert.Equal(t, res.Unix(), int64(1709724618))
//CST
res, err = KTime.Str2Timestruct(strTime8, TimeOption{Zone: "Asia/Shanghai"})
assert.Nil(t, err)
assert.Equal(t, res.Unix(), int64(1709695818))
//本地时区
res, err = KTime.Str2Timestruct(strTime8, TimeOption{Location: kuptime.Location()})
assert.Nil(t, err)
//错误的zone
res, err = KTime.Str2Timestruct(strTime8, TimeOption{Zone: strHello})
assert.NotNil(t, err)
}

func BenchmarkTime_Str2Timestruct(b *testing.B) {
Expand All @@ -73,19 +88,19 @@ func TestTime_Str2Timestamp(t *testing.T) {
assert.Nil(t, err)
assert.Greater(t, res, int64(1))

res, err = KTime.Str2Timestamp(strTime3, "01/02/2006 15:04:05")
res, err = KTime.Str2Timestamp(strTime3, TimeOption{Layout: "01/02/2006 15:04:05"})
assert.Nil(t, err)
assert.Greater(t, res, int64(1))

//时间格式错误
res, err = KTime.Str2Timestamp(strTime2, "2006-01-02")
res, err = KTime.Str2Timestamp(strTime2, TimeOption{Layout: "2006-01-02"})
assert.NotNil(t, err)
}

func BenchmarkTime_Str2Timestamp(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = KTime.Str2Timestamp(strTime3, "01/02/2006 15:04:05")
_, _ = KTime.Str2Timestamp(strTime3, TimeOption{Layout: "01/02/2006 15:04:05"})
}
}

Expand Down

0 comments on commit f8d165f

Please sign in to comment.