Skip to content

Commit

Permalink
feat: add more time util func on ./timex
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 8, 2022
1 parent 5d71cba commit c09cdb9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 21 deletions.
28 changes: 15 additions & 13 deletions strutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ func ToTime(s string, layouts ...string) (t time.Time, err error) {
var layout string
if len(layouts) > 0 { // custom layout
layout = layouts[0]
if layout == "" {
err = ErrInvalidParam
return
}
} else { // auto match layout.
switch len(s) {
case 8:
Expand All @@ -287,22 +291,20 @@ func ToTime(s string, layouts ...string) (t time.Time, err error) {
layout = "2006-01-02 15:04:05"
case 20: // time.RFC3339
layout = "2006-01-02T15:04:05Z07:00"
default:
err = ErrInvalidParam
return
}
}

if layout == "" {
err = ErrInvalidParam
return
}

// has 'T' eg: "2006-01-02T15:04:05"
if strings.ContainsRune(s, 'T') {
layout = strings.Replace(layout, " ", "T", -1)
}
// has 'T' eg: "2006-01-02T15:04:05"
if strings.ContainsRune(s, 'T') {
layout = strings.Replace(layout, " ", "T", -1)
}

// eg: "2006/01/02 15:04:05"
if strings.ContainsRune(s, '/') {
layout = strings.Replace(layout, "-", "/", -1)
// eg: "2006/01/02 15:04:05"
if strings.ContainsRune(s, '/') {
layout = strings.Replace(layout, "-", "/", -1)
}
}

t, err = time.Parse(layout, s)
Expand Down
55 changes: 47 additions & 8 deletions timex/timex.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,34 @@ type TimeX struct {
Layout string
}

// Now time
/*************************************************************
* Create timex instance
*************************************************************/

// Now time instance
func Now() *TimeX {
return &TimeX{
Time: time.Now(),
Layout: DefaultLayout,
}
}

// New form given time
// New instance form given time
func New(t time.Time) *TimeX {
return &TimeX{
Time: t,
Layout: DefaultLayout,
}
}

// FromTime new instance form given time.Time
func FromTime(t time.Time) *TimeX {
return &TimeX{
Time: t,
Layout: DefaultLayout,
}
}

// Local time for now
func Local() *TimeX {
return New(time.Now().In(time.Local))
Expand All @@ -61,6 +73,14 @@ func FromUnix(sec int64) *TimeX {
return New(time.Unix(sec, 0))
}

// FromDate create from datetime string.
func FromDate(s string, template ...string) (*TimeX, error) {
if len(template) > 0 && template[0] != "" {
return FromString(s, ToLayout(template[0]))
}
return FromString(s)
}

// FromString create from datetime string.
// see strutil.ToTime()
func FromString(s string, layouts ...string) (*TimeX, error) {
Expand Down Expand Up @@ -93,6 +113,15 @@ func SetLocalByName(tzName string) error {
return nil
}

/*************************************************************
* timex usage
*************************************************************/

// T returns the t.Time
func (t TimeX) T() time.Time {
return t.Time
}

// Format returns a textual representation of the time value formatted according to the layout defined by the argument.
//
// see time.Time.Format()
Expand Down Expand Up @@ -164,17 +193,17 @@ func (t *TimeX) AddSeconds(seconds int) *TimeX {
}
}

// SubUnix calc diff seconds for t - u
func (t TimeX) SubUnix(u time.Time) int {
return int(t.Sub(u) / time.Second)
}

// Diff calc diff duration for t - u.
// alias of time.Time.Sub()
func (t TimeX) Diff(u time.Time) time.Duration {
return t.Sub(u)
}

// SubUnix calc diff seconds for t - u
func (t TimeX) SubUnix(u time.Time) int {
return int(t.Sub(u) / time.Second)
}

// DiffSec calc diff seconds for t - u
func (t TimeX) DiffSec(u time.Time) int {
return int(t.Sub(u) / time.Second)
Expand Down Expand Up @@ -217,19 +246,29 @@ 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())

return New(newTime)
return FromTime(newTime)
}

// IsBefore the given time
func (t *TimeX) IsBefore(u time.Time) bool {
return t.Before(u)
}

// IsBeforeUnix the given unix timestamp
func (t *TimeX) IsBeforeUnix(ux int64) bool {
return t.Before(time.Unix(ux, 0))
}

// IsAfter the given time
func (t *TimeX) IsAfter(u time.Time) bool {
return t.After(u)
}

// IsAfterUnix the given unix timestamp
func (t *TimeX) IsAfterUnix(ux int64) bool {
return t.After(time.Unix(ux, 0))
}

// Timestamp value. alias t.Unix()
func (t TimeX) Timestamp() int64 {
return t.Unix()
Expand Down
10 changes: 10 additions & 0 deletions timex/timex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import (
"github.com/stretchr/testify/assert"
)

func TestFromDate(t *testing.T) {
tx, err := timex.FromDate("2022-04-20 19:40:34")
assert.NoError(t, err)
assert.Equal(t, "2022-04-20 19:40:34", tx.Datetime())

tx, err = timex.FromDate("2022-04-20 19:40:34", "Y-M-D H:I:S")
assert.NoError(t, err)
assert.Equal(t, "2022-04-20 19:40:34", tx.Datetime())
}

func TestTimeX_basic(t *testing.T) {
tx := timex.Now()
assert.NotEmpty(t, tx.String())
Expand Down

0 comments on commit c09cdb9

Please sign in to comment.