Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The timestamp will change after calling the carbon.CreateFromTimestampUTC() method #48

Closed
BigKuCha opened this issue Dec 3, 2018 · 4 comments
Labels

Comments

@BigKuCha
Copy link

BigKuCha commented Dec 3, 2018

unix := time.Now().UTC().Unix()
utcTime, _ := carbon.CreateFromTimestampUTC(unix)
if unix != utcTime.Unix() {
    fmt.Println(unix, utcTime.Unix())
}

Why are unix and utcTime.Unix() not equal?
I got 1543837131 1543865931

@dcb9
Copy link

dcb9 commented Dec 3, 2018

It seems that func time.Unix, source code, would give Time a default location from the OS so we should change it to a location which we needed before getting its datetime.

Maybe below is a correct implementation:

filename: carbon.go

// SetTimestamp sets the current time given a timestamp
func (c *Carbon) SetTimestamp(sec int64) {
-   t := time.Unix(sec, 0)
+   t := time.Unix(sec, 0).In(c.Location())
    c.Time = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), c.Location())
}

@BigKuCha
btw, Time.Unix() returns a Unix time

the number of seconds elapsed since January 1, 1970 UTC. The result does not depend on the location associated with t.

@hfcorreia
Copy link
Contributor

I've created a test case for this #50, but it seems to pass. Do you see anything wrong?

@dcb9
Copy link

dcb9 commented Dec 5, 2018

@hfcorreia the result is as same as before. it will pass if i set my local timezone to UTC+0, so i guess that your local timezone is UTC+0.

The function, carbon.CreateFromTimestampUTC() func, should not depend on the OS local timezone isn't it? It is necessary to test it on different timezone.

➜  carbon git:(master) ✗ date +"%Z %z" ; go test -run TestSetUnix ./ -v
UTC +0000
=== RUN   TestSetUnix
--- PASS: TestSetUnix (0.00s)
PASS
ok  	github.com/uniplaces/carbon	0.015s
➜  carbon git:(master) ✗ date +"%Z %z" ; go test -run TestSetUnix ./ -v
PST -0800
=== RUN   TestSetUnix
--- FAIL: TestSetUnix (0.00s)
        Error Trace:    carbon_test.go:2754
        Error:		Not equal: 1543970164 (expected)
        	        != 1543941364 (actual)

FAIL
FAIL	github.com/uniplaces/carbon	0.026s

after i did this no matter what my system timezone is, the test will never fail.

Maybe below is a correct implementation:

filename: carbon.go

// SetTimestamp sets the current time given a timestamp
func (c *Carbon) SetTimestamp(sec int64) {
-   t := time.Unix(sec, 0)
+   t := time.Unix(sec, 0).In(c.Location())
    c.Time = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), c.Location())
}

@hfcorreia
Copy link
Contributor

Should be fixed in the latest release! Thanks, @BigKuCha and @dcb9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants