Skip to content

Commit

Permalink
👔 up: timex - add new func NowDate() for quick format time use date t…
Browse files Browse the repository at this point in the history
…emplate
  • Loading branch information
inhere committed Jul 28, 2023
1 parent 868b402 commit ce9d51e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions timex/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

// some common datetime templates
const (
DefaultTemplate = "Y-m-d H:i:s"
TemplateWithMs3 = "Y-m-d H:i:s.v" // end with ".000"
TemplateWithMs6 = "Y-m-d H:i:s.u" // end with ".000000"
DefaultTemplate = "Y-m-d H:I:S" // equals layout: "2006-01-02 15:04:05"
TemplateWithMs3 = "Y-m-d H:I:S.v" // end with ".000"
TemplateWithMs6 = "Y-m-d H:I:S.u" // end with ".000000"
)

// char to Go date layout
// eg: "Y-m-d H:i:s" => "2006-01-02 15:04:05",
// eg: "Y-m-d H:I:S" => "2006-01-02 15:04:05",
//
// # More see time.stdLongMonth
//
Expand Down
5 changes: 5 additions & 0 deletions timex/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
// NowUnix is short of time.Now().Unix()
func NowUnix() int64 { return time.Now().Unix() }

// NowDate quick get current date string. if template is empty, will use DefaultTemplate.
func NowDate(template ...string) string {
return FormatByTpl(time.Now(), basefn.FirstOr(template, DefaultTemplate))
}

// Format convert time to string use default layout
func Format(t time.Time) string { return t.Format(DefaultLayout) }

Expand Down
1 change: 1 addition & 0 deletions timex/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestNowAddDay(t *testing.T) {

func TestDateFormat(t *testing.T) {
now := time.Now()
assert.Eq(t, now.Format("2006-01-02 15:04:05"), timex.NowDate())

tests := []struct{ layout, template string }{
{"20060102 15:04:05", "Ymd H:I:S"},
Expand Down

0 comments on commit ce9d51e

Please sign in to comment.