From 1e670b2e261dc2b68ca7f5dc36c7acae83ec702b Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Wed, 6 Sep 2023 11:30:34 +0800 Subject: [PATCH] Move deprecated methods to `deprecated.go` and `deprecated_test.go` --- carbon.go | 29 +------------------------- carbon_test.go | 51 ---------------------------------------------- creator_test.go | 9 ++++++++ deprecated.go | 26 +++++++++++++++++++++++ deprecated_test.go | 30 +++++++++++++++++++++++++++ traveler_test.go | 15 ++++++++++++++ 6 files changed, 81 insertions(+), 79 deletions(-) delete mode 100755 carbon_test.go create mode 100644 deprecated.go create mode 100755 deprecated_test.go diff --git a/carbon.go b/carbon.go index b949eaca..b92afe73 100644 --- a/carbon.go +++ b/carbon.go @@ -14,7 +14,7 @@ import ( // Version current version // 当前版本号 -const Version = "2.2.5" +const Version = "2.2.6" // timezone constants // 时区常量 @@ -195,30 +195,3 @@ func NewCarbon() Carbon { defer c.lang.rw.Unlock() return c } - -// FromStdTime converts standard time.Time to Carbon. -// Deprecated: It will be removed in the future, CreateFromStdTime is recommended. -// 将标准 time.Time 转换成 Carbon,未来将移除,推荐使用 CreateFromStdTime -func FromStdTime(tt time.Time) Carbon { - return CreateFromStdTime(tt) -} - -// ToStdTime converts Carbon to standard time.Time. -// 将 Carbon 转换成标准 time.Time -func (c Carbon) ToStdTime() time.Time { - return c.time.In(c.loc) -} - -// Time2Carbon converts standard time.Time to Carbon. -// Deprecated: It will be removed in the future, CreateFromStdTime is recommended. -// 将标准 time.Time 转换成 Carbon,未来将移除,推荐使用 CreateFromStdTime -func Time2Carbon(tt time.Time) Carbon { - return CreateFromStdTime(tt) -} - -// Carbon2Time converts Carbon to standard time.Time. -// Deprecated: It will be removed in the future, ToStdTime is recommended. -// 将 Carbon 转换成标准 time.Time,未来将移除,推荐使用 ToStdTime -func (c Carbon) Carbon2Time() time.Time { - return c.ToStdTime() -} diff --git a/carbon_test.go b/carbon_test.go deleted file mode 100755 index 1a1de0f8..00000000 --- a/carbon_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package carbon - -import ( - "testing" - "time" - - "github.com/stretchr/testify/assert" -) - -func TestCarbon_CreateFromStdTime(t *testing.T) { - loc, _ := time.LoadLocation("Asia/Shanghai") - tt := time.Now().In(loc) - expected := tt.Format(DateTimeLayout) - actual := CreateFromStdTime(tt).ToDateTimeString() - assert.Equal(t, expected, actual) -} - -func TestCarbon_FromStdTime(t *testing.T) { - loc, _ := time.LoadLocation("Asia/Shanghai") - tt := time.Now().In(loc) - expected := tt.Format(DateTimeLayout) - actual := FromStdTime(tt).ToDateTimeString() - assert.Equal(t, expected, actual) -} - -func TestCarbon_ToStdTime(t *testing.T) { - expected := time.Now().Format(DateTimeLayout) - actual := Now().ToStdTime().Format(DateTimeLayout) - assert.Equal(t, expected, actual) -} - -func TestCarbon_Time2Carbon(t *testing.T) { - loc, _ := time.LoadLocation("Asia/Shanghai") - tt := time.Now().In(loc) - expected := tt.Format(DateTimeLayout) - actual := Time2Carbon(tt).ToDateTimeString() - assert.Equal(t, expected, actual) -} - -func TestCarbon_Carbon2Time(t *testing.T) { - expected := time.Now().Format(DateTimeLayout) - actual := Now().Carbon2Time().Format(DateTimeLayout) - assert.Equal(t, expected, actual) -} - -func TestError_Carbon(t *testing.T) { - timezone := "xxx" - assert.NotNil(t, Now(timezone).Error, "It should catch an exception in Now()") - assert.NotNil(t, Tomorrow(timezone).Error, "It should catch an exception in Tomorrow()") - assert.NotNil(t, Yesterday(timezone).Error, "It should catch an exception in Yesterday()") -} diff --git a/creator_test.go b/creator_test.go index e94f1e05..ac66ceab 100755 --- a/creator_test.go +++ b/creator_test.go @@ -3,10 +3,19 @@ package carbon import ( "strconv" "testing" + "time" "github.com/stretchr/testify/assert" ) +func TestCarbon_CreateFromStdTime(t *testing.T) { + loc, _ := time.LoadLocation("Asia/Shanghai") + tt := time.Now().In(loc) + expected := tt.Format(DateTimeLayout) + actual := CreateFromStdTime(tt).ToDateTimeString() + assert.Equal(t, expected, actual) +} + func TestCarbon_CreateFromTimestamp(t *testing.T) { assert := assert.New(t) diff --git a/deprecated.go b/deprecated.go new file mode 100644 index 00000000..fdeee0bc --- /dev/null +++ b/deprecated.go @@ -0,0 +1,26 @@ +// The methods in this file will be removed in the future + +package carbon + +import "time" + +// FromStdTime converts standard time.Time to Carbon. +// Deprecated: It will be removed in the future, CreateFromStdTime is recommended. +// 将标准 time.Time 转换成 Carbon,未来将移除,推荐使用 CreateFromStdTime +func FromStdTime(tt time.Time) Carbon { + return CreateFromStdTime(tt) +} + +// Time2Carbon converts standard time.Time to Carbon. +// Deprecated: It will be removed in the future, CreateFromStdTime is recommended. +// 将标准 time.Time 转换成 Carbon,未来将移除,推荐使用 CreateFromStdTime +func Time2Carbon(tt time.Time) Carbon { + return CreateFromStdTime(tt) +} + +// Carbon2Time converts Carbon to standard time.Time. +// Deprecated: It will be removed in the future, ToStdTime is recommended. +// 将 Carbon 转换成标准 time.Time,未来将移除,推荐使用 ToStdTime +func (c Carbon) Carbon2Time() time.Time { + return c.ToStdTime() +} diff --git a/deprecated_test.go b/deprecated_test.go new file mode 100755 index 00000000..d5d2daa0 --- /dev/null +++ b/deprecated_test.go @@ -0,0 +1,30 @@ +package carbon + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestCarbon_FromStdTime(t *testing.T) { + loc, _ := time.LoadLocation("Asia/Shanghai") + tt := time.Now().In(loc) + expected := tt.Format(DateTimeLayout) + actual := FromStdTime(tt).ToDateTimeString() + assert.Equal(t, expected, actual) +} + +func TestCarbon_Time2Carbon(t *testing.T) { + loc, _ := time.LoadLocation("Asia/Shanghai") + tt := time.Now().In(loc) + expected := tt.Format(DateTimeLayout) + actual := Time2Carbon(tt).ToDateTimeString() + assert.Equal(t, expected, actual) +} + +func TestCarbon_Carbon2Time(t *testing.T) { + expected := time.Now().Format(DateTimeLayout) + actual := Now().Carbon2Time().Format(DateTimeLayout) + assert.Equal(t, expected, actual) +} diff --git a/traveler_test.go b/traveler_test.go index c93678f3..f2c1039b 100755 --- a/traveler_test.go +++ b/traveler_test.go @@ -85,6 +85,11 @@ func TestCarbon_AddDuration(t *testing.T) { assert.Nil(c.Error) assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index)) } + + duration := time.Duration(3)*time.Hour + time.Duration(30)*time.Minute + c := Parse("2020-08-05 13:14:15").AddDuration(duration.String()) + assert.Nil(c.Error) + assert.Equal("2020-08-05 16:44:15", c.ToDateTimeString()) } func TestCarbon_SubDuration(t *testing.T) { @@ -116,6 +121,11 @@ func TestCarbon_SubDuration(t *testing.T) { assert.Nil(c.Error) assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index)) } + + duration := time.Duration(3)*time.Hour + time.Duration(30)*time.Minute + c := Parse("2020-08-05 13:14:15").SubDuration(duration.String()) + assert.Nil(c.Error) + assert.Equal("2020-08-05 09:44:15", c.ToDateTimeString()) } func TestCarbon_AddCenturies(t *testing.T) { @@ -2022,4 +2032,9 @@ func TestError_Traveler(t *testing.T) { duration := "10x" c := Parse("2020-08-05").AddDuration(duration) assert.NotNil(t, c.Error, "It should catch an exception in AddDuration()") + + timezone := "xxx" + assert.NotNil(t, Now(timezone).Error, "It should catch an exception in Now()") + assert.NotNil(t, Tomorrow(timezone).Error, "It should catch an exception in Tomorrow()") + assert.NotNil(t, Yesterday(timezone).Error, "It should catch an exception in Yesterday()") }