From e9ec23b511e29eb6343215537e548e08b840a096 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Tue, 22 Aug 2023 12:56:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC=E5=8F=B7?= =?UTF-8?q?=EF=BC=8C=E9=83=A8=E5=88=86=E5=B0=86=E8=A6=81=E5=BC=83=E7=94=A8?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=A2=9E=E5=8A=A0=20Deprecated=20=E6=A0=87?= =?UTF-8?q?=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- carbon.go | 20 ++++++++++++++++---- carbon_test.go | 8 ++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/carbon.go b/carbon.go index fbde370..142b4d1 100644 --- a/carbon.go +++ b/carbon.go @@ -14,7 +14,7 @@ import ( // Version current version // 当前版本号 -const Version = "2.2.3" +const Version = "2.2.4" // timezone constants // 时区常量 @@ -192,8 +192,18 @@ func NewCarbon() Carbon { return Carbon{weekStartsAt: time.Sunday, loc: time.Local, lang: NewLanguage()} } +// CreateFromStdTime creates a Carbon instance from standard time.Time. +// 从标准的 time.Time 创建 Carbon 实例 +func CreateFromStdTime(tt time.Time) Carbon { + c := NewCarbon() + c.time = tt + c.loc = tt.Location() + return c +} + // FromStdTime converts standard time.Time to Carbon. -// 将标准 time.Time 转换成 Carbon +// Deprecated: It will be removed in the future, CreateFromStdTime is recommended. +// 将标准 time.Time 转换成 Carbon,未来将移除,推荐使用 CreateFromStdTime func FromStdTime(tt time.Time) Carbon { c := NewCarbon() c.time = tt @@ -207,13 +217,15 @@ func (c Carbon) ToStdTime() time.Time { return c.time.In(c.loc) } -// Time2Carbon converts standard time.Time to Carbon, will be removed in the future, recommended use FromStdTime +// Time2Carbon converts standard time.Time to Carbon. +// Deprecated: It will be removed in the future, FromStdTime is recommended. // 将标准 time.Time 转换成 Carbon,未来将移除,推荐使用 FromStdTime func Time2Carbon(tt time.Time) Carbon { return FromStdTime(tt) } -// Carbon2Time converts Carbon to standard time.Time, will be removed in the future, recommended use ToStdTime +// 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 index 695826c..1a1de0f 100755 --- a/carbon_test.go +++ b/carbon_test.go @@ -7,6 +7,14 @@ import ( "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)