Skip to content

Commit

Permalink
Move deprecated methods to deprecated.go and deprecated_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Sep 6, 2023
1 parent a036115 commit 1e670b2
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 79 deletions.
29 changes: 1 addition & 28 deletions carbon.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// Version current version
// 当前版本号
const Version = "2.2.5"
const Version = "2.2.6"

// timezone constants
// 时区常量
Expand Down Expand Up @@ -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()
}
51 changes: 0 additions & 51 deletions carbon_test.go

This file was deleted.

9 changes: 9 additions & 0 deletions creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 26 additions & 0 deletions deprecated.go
Original file line number Diff line number Diff line change
@@ -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()
}
30 changes: 30 additions & 0 deletions deprecated_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions traveler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()")
}

0 comments on commit 1e670b2

Please # to comment.