Skip to content

Commit

Permalink
chore: update readme and add some timex tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 13, 2022
1 parent 83ba265 commit 81d5904
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 11 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ can be replaced with:

#### Examples

Examples for use `errorx` package, more please see [errorx/README](errorx/README.md)
Examples for use `errorx` package, more please see [./errorx/README](errorx/README.md)

```go
err := errorx.New("the error message")
Expand Down Expand Up @@ -782,17 +782,19 @@ func MockOsEnv(mp map[string]string, fn func())
func Now() TimeX
func Local() TimeX
func LocalByName(tzName string) TimeX
func SetLocalByName(tzName string) error
// source at timex/util.go
func NowUnix() int64
func Format(t time.Time) string
func FormatBy(t time.Time, layout string) string
func NowAddDay(day int) time.Time
func NowAddHour(hour int) time.Time
func NowAddMinutes(minutes int) time.Time
func NowAddSeconds(seconds int) time.Time
func AddDay(t time.Time, day int) time.Time
func AddHour(t time.Time, hour int) time.Time
func AddMinutes(t time.Time, minutes int) time.Time
func AddSeconds(t time.Time, seconds int) time.Time
func SetLocalByName(tzName string) error
```

## Code Check & Testing
Expand Down Expand Up @@ -821,8 +823,7 @@ go test ./...
## License

[MIT](LICENSE)
e{}, fns template.FuncMap, isFile ...bool) string
func RenderText(input string, data interface{}, fns template.FuncMap, isFile ...bool) string
interface{}, fns template.FuncMap, isFile ...bool) string
```

### System
Expand Down
6 changes: 4 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func doSomething() error {

#### 使用示例

Examples for use `errorx` package, more please see [errorx/README](errorx/README.md)
Examples for use `errorx` package, more please see [./errorx/README](errorx/README.md)

```go
err := errorx.New("the error message")
Expand Down Expand Up @@ -782,17 +782,19 @@ func MockOsEnv(mp map[string]string, fn func())
func Now() TimeX
func Local() TimeX
func LocalByName(tzName string) TimeX
func SetLocalByName(tzName string) error
// source at timex/util.go
func NowUnix() int64
func Format(t time.Time) string
func FormatBy(t time.Time, layout string) string
func NowAddDay(day int) time.Time
func NowAddHour(hour int) time.Time
func NowAddMinutes(minutes int) time.Time
func NowAddSeconds(seconds int) time.Time
func AddDay(t time.Time, day int) time.Time
func AddHour(t time.Time, hour int) time.Time
func AddMinutes(t time.Time, minutes int) time.Time
func AddSeconds(t time.Time, seconds int) time.Time
func SetLocalByName(tzName string) error
```

## Code Check & Testing
Expand Down
4 changes: 2 additions & 2 deletions internal/template/part-errorx.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ can be replaced with:

#### Examples

Examples for use `errorx` package, more please see [errorx/README](errorx/README.md)
Examples for use `errorx` package, more please see [./errorx/README](errorx/README.md)

```go
err := returnXErrL2("the error message")
err := errorx.New("the error message")

fmt.Println(err)
// fmt.Printf("%v\n", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/template/part-errorx.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func doSomething() error {

#### 使用示例

Examples for use `errorx` package, more please see [errorx/README](errorx/README.md)
Examples for use `errorx` package, more please see [./errorx/README](errorx/README.md)

```go
err := returnXErrL2("the error message")
err := errorx.New("the error message")

fmt.Println(err)
// fmt.Printf("%v\n", err)
Expand Down
3 changes: 2 additions & 1 deletion timex/timex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
OneMinSec = 60
OneHourSec = 3600
OneDaySec = 86400
OneWeekSec = 7 * 86400

OneMin = time.Minute
OneHour = time.Hour
Expand Down Expand Up @@ -62,7 +63,7 @@ func SetLocalByName(tzName string) error {
return nil
}

// Datetime format time use DefaultLayout
// Datetime use DefaultLayout format time to date
func (t *TimeX) Datetime() string {
if t.DateLayout == "" {
t.DateLayout = DefaultLayout
Expand Down
13 changes: 13 additions & 0 deletions timex/timex_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
package timex_test

import (
"testing"

"github.com/gookit/goutil/timex"
"github.com/stretchr/testify/assert"
)

func TestTimeX_basic(t *testing.T) {
tx := timex.Now()
assert.NotEmpty(t, tx.String())
assert.NotEmpty(t, tx.Datetime())
}
10 changes: 10 additions & 0 deletions timex/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ func FormatBy(t time.Time, layout string) string {
return t.Format(layout)
}

// FormatUnix time seconds use default layout
func FormatUnix(sec int64) string {
return time.Unix(sec, 0).Format(DefaultLayout)
}

// FormatUnixBy format time seconds use given layout
func FormatUnixBy(sec int64, layout string) string {
return time.Unix(sec, 0).Format(layout)
}

// NowAddDay add some day time from now
func NowAddDay(day int) time.Time {
return time.Now().AddDate(0, 0, day)
Expand Down
15 changes: 15 additions & 0 deletions timex/util_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
package timex_test

import (
"testing"
"time"

"github.com/gookit/goutil/timex"
"github.com/stretchr/testify/assert"
)

func TestBasic(t *testing.T) {
sec := timex.NowUnix()

assert.NotEmpty(t, timex.FormatUnix(sec))
assert.NotEmpty(t, timex.FormatUnixBy(sec, time.RFC3339))
}

0 comments on commit 81d5904

Please sign in to comment.