diff --git a/tag.bat b/tag.bat index b5ef061..e55f10a 100644 --- a/tag.bat +++ b/tag.bat @@ -1,4 +1,4 @@ -git tag v1.1.25 +git tag v1.1.26 git tag bank_card/v1.1.5 git tag geoip/v1.1.5 diff --git a/timeutil/format.go b/timeutil/format.go index 6ac458c..9a297f4 100644 --- a/timeutil/format.go +++ b/timeutil/format.go @@ -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 { diff --git a/timeutil/trans.go b/timeutil/trans.go index b6605fa..75bf307 100644 --- a/timeutil/trans.go +++ b/timeutil/trans.go @@ -172,6 +172,14 @@ func TimeToDateString(tm *time.Time) *string { return trans.String(tm.In(GetDefaultTimeLocation()).Format(DateLayout)) } +func TimeToDateStringWithLayout(tm *time.Time, layout string) *string { + if tm == nil { + return nil + } + + return trans.String(tm.Format(layout)) +} + // TimestamppbToTime timestamppb.Timestamp -> time.Time func TimestamppbToTime(timestamp *timestamppb.Timestamp) *time.Time { if timestamp != nil { diff --git a/timeutil/trans_test.go b/timeutil/trans_test.go index 33e4c4e..8a62032 100644 --- a/timeutil/trans_test.go +++ b/timeutil/trans_test.go @@ -149,6 +149,15 @@ func TestTimeToDateString(t *testing.T) { assert.Nil(t, result) } +func TestTimeToDateStringWithLayout(t *testing.T) { + now := time.Now() + + fmt.Println(*TimeToDateStringWithLayout(&now, DateLayout)) + fmt.Println(*TimeToDateStringWithLayout(&now, ISO8601)) + fmt.Println(*TimeToDateStringWithLayout(&now, ISO8601TZHour)) + fmt.Println(*TimeToDateStringWithLayout(&now, ISO8601NoTZ)) +} + func TestTimestamppbToTime(t *testing.T) { // 测试有效输入 timestamp := timestamppb.Now()