Skip to content

Commit

Permalink
Now()、Yestoday()、Tomorrow()方法新增可选参数timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Jul 23, 2021
1 parent 6c6dc44 commit d0b5468
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 29 deletions.
55 changes: 41 additions & 14 deletions carbon.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// @Title carbon
// @Description A simple, semantic and developer-friendly golang package for datetime
// @Page github.com/golang-module/carbon
// @Version v1.4.3
// @Version v1.4.4
// @Author gouguoyin
// @Blog www.gouguoyin.cn
// @Email [email protected]

package carbon

import "time"
import (
"time"
)

// 时区常量
const (
Expand Down Expand Up @@ -126,6 +128,7 @@ const (
RFC2822Format = time.RFC1123Z
RFC3339Format = time.RFC3339
KitchenFormat = time.Kitchen
Iso8601Format = "2006-01-02T15:04:05-07:00"
CookieFormat = "Monday, 02-Jan-2006 15:04:05 MST"
RFC1036Format = "Mon, 02 Jan 06 15:04:05 -0700"
RFC7231Format = "Mon, 02 Jan 2006 15:04:05 GMT"
Expand Down Expand Up @@ -164,18 +167,34 @@ func (c Carbon) Carbon2Time() time.Time {
}

// Now 当前
func (c Carbon) Now() Carbon {
func (c Carbon) Now(timezone ...string) Carbon {
if len(timezone) == 1 {
loc, err := getLocationByTimezone(timezone[0])
c.Loc = loc
c.Error = err
}
if c.Error != nil {
return c
}
c.Time = time.Now().In(c.Loc)
return c
}

// Now 当前(默认时区)
func Now() Carbon {
return NewCarbon().Now()
// Now 当前
func Now(timezone ...string) Carbon {
return NewCarbon().Now(timezone...)
}

// Tomorrow 明天
func (c Carbon) Tomorrow() Carbon {
func (c Carbon) Tomorrow(timezone ...string) Carbon {
if len(timezone) == 1 {
loc, err := getLocationByTimezone(timezone[0])
c.Loc = loc
c.Error = err
}
if c.Error != nil {
return c
}
if c.IsZero() {
c.Time = time.Now().In(c.Loc).AddDate(0, 0, 1)
} else {
Expand All @@ -184,13 +203,21 @@ func (c Carbon) Tomorrow() Carbon {
return c
}

// Tomorrow 明天(默认时区)
func Tomorrow() Carbon {
return NewCarbon().Tomorrow()
// Tomorrow 明天
func Tomorrow(timezone ...string) Carbon {
return NewCarbon().Tomorrow(timezone...)
}

// Yesterday 昨天
func (c Carbon) Yesterday() Carbon {
func (c Carbon) Yesterday(timezone ...string) Carbon {
if len(timezone) == 1 {
loc, err := getLocationByTimezone(timezone[0])
c.Loc = loc
c.Error = err
}
if c.Error != nil {
return c
}
if c.IsZero() {
c.Time = time.Now().In(c.Loc).AddDate(0, 0, -1)
} else {
Expand All @@ -199,7 +226,7 @@ func (c Carbon) Yesterday() Carbon {
return c
}

// Yesterday 昨天(默认时区)
func Yesterday() Carbon {
return NewCarbon().Yesterday()
// Yesterday 昨天
func Yesterday(timezone ...string) Carbon {
return NewCarbon().Yesterday(timezone...)
}
62 changes: 47 additions & 15 deletions carbon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,81 @@ import (
func TestCarbon_Now(t *testing.T) {
assert := assert.New(t)

actual := time.Now().Format(DateFormat)
expected := Now().ToDateString()
assert.Equal(expected, actual)
actual1 := Now().ToDateString()
expected1 := time.Now().Format(DateFormat)
assert.Equal(expected1, actual1)

actual2 := Now(Local).ToDateString()
expected2 := time.Now().In(time.Local).Format(DateFormat)
assert.Equal(expected2, actual2)
}

func TestError_Now(t *testing.T) {
timezone := "xxx"
c := Now(timezone)
assert.Equal(t, invalidTimezoneError(timezone), c.Error, "Should catch an exception in Now()")
}

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

c1 := Yesterday()
actual := time.Now().AddDate(0, 0, -1).Format(DateFormat)
expected1 := time.Now().AddDate(0, 0, -1).Format(DateFormat)
assert.Nil(c1.Error)
assert.Equal(c1.ToDateString(), actual)
assert.Equal(expected1, c1.ToDateString())

c2 := Parse("2020-08-05").Yesterday()
c2 := Yesterday(Local)
expected2 := time.Now().In(time.Local).AddDate(0, 0, -1).Format(DateFormat)
assert.Nil(c2.Error)
assert.Equal(c2.ToDateString(), "2020-08-04", "It should be equal to 2020-08-04")
assert.Equal(expected2, c2.ToDateString())

c3 := Parse("2020-08-05").Yesterday()
assert.Nil(c3.Error)
assert.Equal("2020-08-04", c3.ToDateString(), "It should be equal to 2020-08-04")
}

func TestError_Yesterday(t *testing.T) {
timezone := "xxx"
c := Yesterday(timezone)
assert.Equal(t, invalidTimezoneError(timezone), c.Error, "Should catch an exception in Yesterday()")
}

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

c1 := Tomorrow()
actual := time.Now().AddDate(0, 0, 1).Format(DateFormat)
expected1 := time.Now().AddDate(0, 0, 1).Format(DateFormat)
assert.Nil(c1.Error)
assert.Equal(c1.ToDateString(), actual)
assert.Equal(expected1, c1.ToDateString())

c2 := Parse("2020-08-05").Tomorrow()
c2 := Tomorrow(Local)
expected2 := time.Now().In(time.Local).AddDate(0, 0, 1).Format(DateFormat)
assert.Nil(c2.Error)
assert.Equal(c2.ToDateString(), "2020-08-06", "It should be equal to 2020-08-06")
assert.Equal(expected2, c2.ToDateString())

c3 := Parse("2020-08-05").Tomorrow()
assert.Nil(c3.Error)
assert.Equal("2020-08-06", c3.ToDateString(), "It should be equal to 2020-08-06")
}

func TestError_Tomorrow(t *testing.T) {
timezone := "xxx"
c := Tomorrow(timezone)
assert.Equal(t, invalidTimezoneError(timezone), c.Error, "Should catch an exception in Tomorrow()")
}

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

actual := time.Now().Format(DateTimeFormat)
expected := Time2Carbon(time.Now()).ToDateTimeString()
expected := time.Now().Format(DateTimeFormat)
actual := Time2Carbon(time.Now()).ToDateTimeString()
assert.Equal(expected, actual)
}

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

actual := time.Now().Format(DateTimeFormat)
expected := Now().Carbon2Time().Format(DateTimeFormat)
expected := time.Now().Format(DateTimeFormat)
actual := Now().Carbon2Time().Format(DateTimeFormat)
assert.Equal(expected, actual)
}

0 comments on commit d0b5468

Please sign in to comment.