From dbf1fc5a4b13dcae206cefc6226dbd4d7fde81be Mon Sep 17 00:00:00 2001 From: Hugo Correia Date: Wed, 5 Dec 2018 10:28:24 +0000 Subject: [PATCH] Fix SetTimestamp in the correct location (#50) * Add Test SetUnix * SetTimestamp in the correct location --- carbon.go | 3 +-- carbon_test.go | 13 +++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/carbon.go b/carbon.go index 2d5f359..1e0b2bd 100644 --- a/carbon.go +++ b/carbon.go @@ -738,8 +738,7 @@ func (c *Carbon) SetWeekendDays(wds []time.Weekday) { // SetTimestamp sets the current time given a timestamp func (c *Carbon) SetTimestamp(sec int64) { - t := time.Unix(sec, 0) - c.Time = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), c.Location()) + c.Time = time.Unix(sec, 0).In(c.Location()) } // SetTimeZone sets the location from a string diff --git a/carbon_test.go b/carbon_test.go index 5a3deb2..afff004 100644 --- a/carbon_test.go +++ b/carbon_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestAddYearsPositive(t *testing.T) { @@ -2592,8 +2593,8 @@ func TestCreateFromTimestampUTC(t *testing.T) { func TestCreateFromTimestamp(t *testing.T) { c, _ := CreateFromTimestamp(1171502725, "Africa/Cairo") - expected, _ := Create(2007, time.February, 15, 1, 25, 25, 0, "Africa/Cairo") - assert.Equal(t, expected, c, "The date should be 07-02-15 01:25:25") + expected, _ := Create(2007, time.February, 15, 3, 25, 25, 0, "Africa/Cairo") + assert.Equal(t, expected, c, "The date should be 07-02-15 03:25:25") } func TestCreateFromTime(t *testing.T) { @@ -2745,3 +2746,11 @@ func TestIsLastMonthFalse(t *testing.T) { assert.Nil(t, err) assert.False(t, c.IsLastMonth()) } + +func TestSetUnix(t *testing.T) { + unix := time.Now().UTC().Unix() + utcTime, err := CreateFromTimestampUTC(unix) + + require.Nil(t, err) + assert.Equal(t, unix, utcTime.Unix()) +}