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

time: time.Sub returns different results depending on Location (on both Windows and Linux) #3620

Closed
gopherbot opened this issue May 14, 2012 · 2 comments

Comments

@gopherbot
Copy link

by pkorotkov:

What steps will reproduce the problem?
This code does not work in Go Playground as-is, so just put it right there:

// DatesDiff returns the number of days and seconds resp. between two given dates
func DatesDiff(t1, t2 time.Time) (int, int64) {
    v := int64(t2.Sub(t1))
    return int(v / 864e11), v / 1e9
}

func main() {
    loc, err := time.LoadLocation("Europe/Berlin")
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    t1 := time.Date(1957, time.September, 10, 0, 0, 0, 0, loc)
    t2 := time.Date(2013, time.May, 14, 0, 0, 0, 0, loc)
    fmt.Println(DatesDiff(t1, t2))
    t3 := time.Date(1957, time.September, 10, 0, 0, 0, 0, time.UTC)
    t4 := time.Date(2013, time.May, 14, 0, 0, 0, 0, time.UTC)
    fmt.Println(DatesDiff(t3, t4))
}

What is the expected output?

20335 1756944000

20335 1756944000

What do you see instead?
20334 1756940400

20335 1756944000

Which compiler are you using (5g, 6g, 8g, gccgo)?
8g

Which operating system are you using?
Windows 7 Prof x86
Ubuntu Linux 12.04 LTS x86

Which version are you using?  (run 'go version')
go version go1.0.1

Please provide any additional information below.
@remyoudompheng
Copy link
Contributor

Comment 1:

Daylight saving time was not observed in Germany in 1957:
http://en.wikipedia.org/wiki/Daylight_saving_time_in_Germany
In your first example, you are computing the difference between "09 Sep 1957 23:00 UTC"
and "14 May 2013 22:00 UTC", which is one hour less than your second example. You should
take that into account if you need to compute *date* differences. Package time cares
about time.

@ianlancetaylor
Copy link
Contributor

Comment 2:

Sounds like all is well here, or at least what is not well is not Go's fault.

Status changed to WorkingAsIntended.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants