Skip to content

Commit

Permalink
Fix SetTimestamp in the correct location (#50)
Browse files Browse the repository at this point in the history
* Add Test SetUnix

* SetTimestamp in the correct location
  • Loading branch information
Hugo Correia authored Dec 5, 2018
1 parent 5c2074d commit dbf1fc5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
3 changes: 1 addition & 2 deletions carbon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions carbon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAddYearsPositive(t *testing.T) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
}

0 comments on commit dbf1fc5

Please sign in to comment.