Compare commits

...

6 Commits

Author SHA1 Message Date
Bobo
3f713e489b feat: time util 2025-05-29 00:15:37 +08:00
Bobo
ace6bd4237 feat: time util 2025-05-29 00:07:27 +08:00
Bobo
3ec9bbbd27 feat: time util 2025-05-28 23:58:08 +08:00
Bobo
faa5817857 feat: time util 2025-05-28 23:55:43 +08:00
Bobo
89a69a9d4d feat: entgo 2025-05-28 23:29:21 +08:00
Bobo
63789d090d feat: entgo 2025-05-28 23:08:18 +08:00
8 changed files with 86 additions and 19 deletions

View File

@@ -42,11 +42,15 @@ var TimestamppbToTimeConverter = copier.TypeConverter{
},
}
func TimeToString(tm *time.Time) *string {
return timeutil.TimeToString(tm, timeutil.ISO8601)
}
func NewTimeStringConverterPair() []copier.TypeConverter {
srcType := &time.Time{}
dstType := trans.Ptr("")
fromFn := timeutil.TimeToTimeString
fromFn := TimeToString
toFn := timeutil.StringTimeToTime
return NewGenericTypeConverterPair(srcType, dstType, fromFn, toFn)

View File

@@ -6,7 +6,7 @@ toolchain go1.24.3
require (
github.com/jinzhu/copier v0.4.0
github.com/tx7do/go-utils v1.1.24
github.com/tx7do/go-utils v1.1.27
)
require (

View File

@@ -11,7 +11,7 @@ require (
github.com/go-kratos/kratos/v2 v2.8.4
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.10.0
github.com/tx7do/go-utils v1.1.22
github.com/tx7do/go-utils v1.1.27
go.opentelemetry.io/otel v1.36.0
google.golang.org/protobuf v1.36.6
)
@@ -23,7 +23,7 @@ require (
github.com/bmatcuk/doublestar v1.3.4 // indirect
github.com/bufbuild/protocompile v0.14.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/inflect v0.21.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect

View File

@@ -23,6 +23,8 @@ github.com/go-kratos/kratos/v2 v2.8.4/go.mod h1:mq62W2101a5uYyRxe+7IdWubu7gZCGYq
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-openapi/inflect v0.21.2 h1:0gClGlGcxifcJR56zwvhaOulnNgnhc4qTAkob5ObnSM=

View File

@@ -1,12 +1,12 @@
git tag v1.1.24
git tag v1.1.27
git tag bank_card/v1.1.5
git tag geoip/v1.1.5
git tag translator/v1.1.2
git tag copierutil/v0.0.4
git tag copierutil/v0.0.5
git tag jwtutil/v0.0.2
git tag entgo/v1.1.28
git tag entgo/v1.1.30
git tag gorm/v1.1.6
git push origin --tags

View File

@@ -217,7 +217,7 @@ func timeMarshalJSON(t time.Time, layout string) ([]byte, error) {
}
func ParseSlice(layout string, strings []string) ([]time.Time, error) {
times := []time.Time{}
var times []time.Time
for _, raw := range strings {
t, err := time.Parse(layout, raw)
if err != nil {

View File

@@ -33,10 +33,7 @@ func UnixMilliToStringPtr(milli *int64) *string {
tm := time.UnixMilli(*milli)
// 设置默认时区
tm.In(GetDefaultTimeLocation())
str := tm.Format(TimeLayout)
str := tm.In(GetDefaultTimeLocation()).Format(TimeLayout)
return &str
}
@@ -133,10 +130,7 @@ func TimeToTimeString(tm *time.Time) *string {
return nil
}
// 设置默认时区
tm.In(GetDefaultTimeLocation())
return trans.String(tm.Format(TimeLayout))
return trans.String(tm.In(GetDefaultTimeLocation()).Format(TimeLayout))
}
// StringDateToTime 字符串 -> 时间
@@ -175,10 +169,34 @@ func TimeToDateString(tm *time.Time) *string {
return nil
}
// 设置默认时区
tm.In(GetDefaultTimeLocation())
return trans.String(tm.In(GetDefaultTimeLocation()).Format(DateLayout))
}
return trans.String(tm.Format(DateLayout))
func StringToTime(str *string, layout string) *time.Time {
if str == nil {
return nil
}
if len(*str) == 0 {
return nil
}
var err error
var theTime time.Time
theTime, err = time.ParseInLocation(layout, *str, GetDefaultTimeLocation())
if err == nil {
return &theTime
}
return nil
}
func TimeToString(tm *time.Time, layout string) *string {
if tm == nil {
return nil
}
return trans.String(tm.Format(layout))
}
// TimestamppbToTime timestamppb.Timestamp -> time.Time

View File

@@ -149,6 +149,49 @@ func TestTimeToDateString(t *testing.T) {
assert.Nil(t, result)
}
func TestTimeToString(t *testing.T) {
now := time.Now()
fmt.Println(*TimeToString(&now, DateLayout))
fmt.Println(*TimeToString(&now, ISO8601))
fmt.Println(*TimeToString(&now, ISO8601TZHour))
fmt.Println(*TimeToString(&now, ISO8601NoTZ))
layout := "2006-01-02"
expected := now.Format(layout)
result := TimeToString(&now, layout)
assert.NotNil(t, result)
assert.Equal(t, expected, *result)
// 测试空输入
result = TimeToString(nil, layout)
assert.Nil(t, result)
}
func TestStringToTime(t *testing.T) {
// 测试有效时间字符串输入
input := "2023-03-09 12:34:56"
layout := "2006-01-02 15:04:05"
expected := time.Date(2023, 3, 9, 12, 34, 56, 0, GetDefaultTimeLocation())
result := StringToTime(&input, layout)
assert.NotNil(t, result)
assert.Equal(t, expected, *result)
// 测试无效时间字符串输入
invalidInput := "invalid-date"
result = StringToTime(&invalidInput, layout)
assert.Nil(t, result)
// 测试空字符串输入
emptyInput := ""
result = StringToTime(&emptyInput, layout)
assert.Nil(t, result)
// 测试空指针输入
result = StringToTime(nil, layout)
assert.Nil(t, result)
}
func TestTimestamppbToTime(t *testing.T) {
// 测试有效输入
timestamp := timestamppb.Now()