feat: time util
This commit is contained in:
2
tag.bat
2
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 bank_card/v1.1.5
|
||||||
git tag geoip/v1.1.5
|
git tag geoip/v1.1.5
|
||||||
|
|||||||
@@ -217,7 +217,7 @@ func timeMarshalJSON(t time.Time, layout string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ParseSlice(layout string, strings []string) ([]time.Time, error) {
|
func ParseSlice(layout string, strings []string) ([]time.Time, error) {
|
||||||
times := []time.Time{}
|
var times []time.Time
|
||||||
for _, raw := range strings {
|
for _, raw := range strings {
|
||||||
t, err := time.Parse(layout, raw)
|
t, err := time.Parse(layout, raw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -172,6 +172,14 @@ func TimeToDateString(tm *time.Time) *string {
|
|||||||
return trans.String(tm.In(GetDefaultTimeLocation()).Format(DateLayout))
|
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
|
// TimestamppbToTime timestamppb.Timestamp -> time.Time
|
||||||
func TimestamppbToTime(timestamp *timestamppb.Timestamp) *time.Time {
|
func TimestamppbToTime(timestamp *timestamppb.Timestamp) *time.Time {
|
||||||
if timestamp != nil {
|
if timestamp != nil {
|
||||||
|
|||||||
@@ -149,6 +149,15 @@ func TestTimeToDateString(t *testing.T) {
|
|||||||
assert.Nil(t, result)
|
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) {
|
func TestTimestamppbToTime(t *testing.T) {
|
||||||
// 测试有效输入
|
// 测试有效输入
|
||||||
timestamp := timestamppb.Now()
|
timestamp := timestamppb.Now()
|
||||||
|
|||||||
Reference in New Issue
Block a user