golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.97k stars 17.67k forks source link

time: Date returns a Time object with incorrect offset for years 1900-1905 #30099

Closed jinfengnarvar closed 5 years ago

jinfengnarvar commented 5 years ago

What version of Go are you using (go version)?

$ go version
go version go1.11.2 darwin/amd64

Does this issue reproduce with the latest release?

Don't know. I did repro this issue on https://play.golang.org/.

What operating system and processor architecture are you using (go env)?

$ go env
GOARCH="amd64"
GOBIN="/Users/jinfeng/dev/narvar/go/bin"
GOCACHE="/Users/jinfeng/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/jinfeng/dev/narvar/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/opt/go/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/x2/6yhw9qq129gffvpx90dv5gyw0000gp/T/go-build291093703=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Here is the repro code:

package main

import (
    "fmt"
    "time"
)

func main() {
    for i := 1900; i <= 1910; i++ {
        //tz := "Asia/Calcutta"
        tz := "America/New_York"
        loc, err := time.LoadLocation(tz)
        if err != nil {
            fmt.Println(i, "/", err)
            return
        }
        t := time.Date(i, time.January, 1, 0, 0, 0, 0, loc)
        tstr := t.Format(time.RFC3339)
        tzName, tzOffset := t.Zone()
        fmt.Println(tstr, "/", tzName, "/", tzOffset)
    }
}

What did you expect to see?

1900-01-01T00:00:00-05:00 / EST / -18000
1901-01-01T00:00:00-05:00 / EST / -18000
1902-01-01T00:00:00-05:00 / EST / -18000
1903-01-01T00:00:00-05:00 / EST / -18000
1904-01-01T00:00:00-05:00 / EST / -18000
1905-01-01T00:00:00-05:00 / EST / -18000
1906-01-01T00:00:00-05:00 / EST / -18000
1907-01-01T00:00:00-05:00 / EST / -18000
1908-01-01T00:00:00-05:00 / EST / -18000
1909-01-01T00:00:00-05:00 / EST / -18000
1910-01-01T00:00:00-05:00 / EST / -18000

What did you see instead?

1900-01-01T00:00:00-04:56 / LMT / -17762
1901-01-01T00:00:00-04:56 / LMT / -17762
1902-01-01T00:00:00-05:00 / EST / -18000
1903-01-01T00:00:00-05:00 / EST / -18000
1904-01-01T00:00:00-05:00 / EST / -18000
1905-01-01T00:00:00-05:00 / EST / -18000
1906-01-01T00:00:00-05:00 / EST / -18000
1907-01-01T00:00:00-05:00 / EST / -18000
1908-01-01T00:00:00-05:00 / EST / -18000
1909-01-01T00:00:00-05:00 / EST / -18000
1910-01-01T00:00:00-05:00 / EST / -18000

Similar output for "Asia/Calcutta" time zone, except the deviation lasts through 1905:

1900-01-01T00:00:00+05:53 / HMT / 21200
1901-01-01T00:00:00+05:53 / HMT / 21200
1902-01-01T00:00:00+05:21 / MMT / 19270
1903-01-01T00:00:00+05:21 / MMT / 19270
1904-01-01T00:00:00+05:21 / MMT / 19270
1905-01-01T00:00:00+05:21 / MMT / 19270
1906-01-01T00:08:50+05:30 / IST / 19800
1907-01-01T00:00:00+05:30 / IST / 19800
1908-01-01T00:00:00+05:30 / IST / 19800
1909-01-01T00:00:00+05:30 / IST / 19800
1910-01-01T00:00:00+05:30 / IST / 19800
agnivade commented 5 years ago

/cc @ALTree @ianlancetaylor

gopherbot commented 5 years ago

Change https://golang.org/cl/161202 mentions this issue: time: read 64-bit data if available

ianlancetaylor commented 5 years ago

This is happening because we were only reading the 32-bit version of the tzdata info, which can't represent times before December 13, 1901.

jinfengnarvar commented 5 years ago

@ianlancetaylor What about "Asia/Calcutta", of which the anomaly seems to continue beyond 1901.

ianlancetaylor commented 5 years ago

It seems to me that with CL 161202 the times printed for "Asia/Calcutta" match the information in the tzdata database.

# Reed S, Low F. The Indian Year Book 1936-37. Bennett, Coleman, pp 27-8.
# https://archive.org/details/in.ernet.dli.2015.282212
# This lists +052110 as Madras local time used in railways, and says that on
# 1906-01-01 railways and telegraphs in India switched to +0530.  Some
# municipalities retained their former time, and the time in Calcutta
# continued to depend on whether you were at the railway station or at
# government offices.  Government time was at +055320 (according to Shanks) or
# at +0554 (according to the Indian Year Book).  Railway time is more
# appropriate for our purposes, as it was better documented, it is what we do
# elsewhere (e.g., Europe/London before 1880), and after 1906 it was
# consistent in the region now identified by Asia/Kolkata.  So, use railway
# time for 1870-1941.  Shanks is our only (and dubious) source for the
# 1941-1945 data.

# Zone  NAME        GMTOFF  RULES   FORMAT  [UNTIL]
Zone    Asia/Kolkata    5:53:28 -   LMT 1854 Jun 28 # Kolkata
            5:53:20 -   HMT 1870        # Howrah Mean Time?
            5:21:10 -   MMT 1906 Jan  1 # Madras local time
            5:30    -   IST 1941 Oct
            5:30    1:00    +0630   1942 May 15
            5:30    -   IST 1942 Sep
            5:30    1:00    +0630   1945 Oct 15
            5:30    -   IST
jinfengnarvar commented 5 years ago

Thanks @ianlancetaylor for investigation! And thanks @bradfitz for including the issue for next release!

In the meantime, I have my own wrapper around time.Date and limit the year to 1970 (an arbitrarily picked year that is just old enough :)

jinfengnarvar commented 4 years ago

@ianlancetaylor which version of go has this 64-bit reading fix been ported in? the label says go1.13, but I'm wondering if 1.12.4 has it or not, seems like my repro test works correctly now under 1.12.4.

ianlancetaylor commented 4 years ago

@jinfengnarvar The patch that fixed the problem is not on the 1.12 branch. I have no explanation. Perhaps the tzdata information on your system changed in some way?