Skip to content

Commit

Permalink
feat: timex add new TimeX.CustomHMS for custom the hour, minute, seco…
Browse files Browse the repository at this point in the history
…nd create time
  • Loading branch information
inhere committed Apr 28, 2022
1 parent 2c29d9c commit 8307e58
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
33 changes: 33 additions & 0 deletions arrutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,39 @@ func SliceToStrings(arr []interface{}) []string {
return ss
}

// AnyToString convert any array, slice to string
func AnyToString(arr interface{}) string {
if arr == nil {
return "[]"
}

rftVal := reflect.ValueOf(arr)
if rftVal.Type().Kind() == reflect.Ptr {
rftVal = rftVal.Elem()
}

if rftVal.Kind() != reflect.Slice && rftVal.Kind() != reflect.Array {
return ""
}

if rftVal.Len() == 0 {
return "[]"
}

var sb strings.Builder
sb.WriteByte('[')

for i := 0; i < rftVal.Len(); i++ {
if i > 0 {
sb.WriteByte(',')
}
sb.WriteString(strutil.MustString(rftVal.Index(i).Interface()))
}

sb.WriteByte(']')
return sb.String()
}

// SliceToString convert []interface{} to string
func SliceToString(arr ...interface{}) string { return ToString(arr) }

Expand Down
11 changes: 11 additions & 0 deletions arrutil/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ func TestStringsToString(t *testing.T) {
is.Equal("a,b", arrutil.StringsJoin(",", "a", "b"))
}

func TestAnyToString(t *testing.T) {
is := assert.New(t)
arr := [2]string{"a", "b"}

is.Equal("[]", arrutil.AnyToString(nil))
is.Equal("[]", arrutil.AnyToString([]string{}))
is.Equal("[a,b]", arrutil.AnyToString(arr))
is.Equal("[a,b]", arrutil.AnyToString([]string{"a", "b"}))
is.Equal("", arrutil.AnyToString("invalid"))
}

func TestSliceToString(t *testing.T) {
is := assert.New(t)

Expand Down
4 changes: 2 additions & 2 deletions timex/timex.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ func (t *TimeX) DayEnd() *TimeX {
return New(newTime)
}

// ChangeHMS change the hour, minute, second for create new time.
func (t *TimeX) ChangeHMS(hour, min, sec int) *TimeX {
// CustomHMS custom change the hour, minute, second for create new time.
func (t *TimeX) CustomHMS(hour, min, sec int) *TimeX {
y, m, d := t.Date()
newTime := time.Date(y, m, d, hour, min, sec, int(time.Second-time.Nanosecond), t.Location())

Expand Down
7 changes: 6 additions & 1 deletion timex/timex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestTimeX_SubUnix(t *testing.T) {
tx := timex.Now()

after1m := tx.AddMinutes(1)

assert.Equal(t, timex.OneMinSec, after1m.SubUnix(tx.Time))
}

Expand All @@ -49,3 +49,8 @@ func TestTimeX_AddDay(t *testing.T) {
assert.True(t, yd.IsBefore(tx.Time))
assert.Equal(t, tx.Unix()-yd.Unix(), int64(timex.OneDaySec))
}

func TestTimeX_CustomHMS(t *testing.T) {
tx := timex.Now()
assert.Equal(t, "12:23:34", tx.CustomHMS(12, 23, 34).TplFormat("H:I:S"))
}

0 comments on commit 8307e58

Please sign in to comment.