feat: time util

This commit is contained in:
Bobo
2025-05-28 23:55:43 +08:00
parent 89a69a9d4d
commit faa5817857
4 changed files with 19 additions and 2 deletions

View File

@@ -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

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

@@ -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 {

View File

@@ -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()