golang/go

time: allows signs for year/tz in format string

dvyukov opened this issue · 4 comments

The following program prints 1994-01-01 00:00:00 -0059 -0059:

package main

import (
    "fmt"
    "time"
)

func main() {
    ret, err := time.Parse("06Z070000", "-6+-1+1-1")
    if err != nil {
        panic(err)
    }
    fmt.Printf("%v\n", ret)
}

Most of the RFCs does not allow that.

I investigated this but found it difficult to decide what to do about it. Half the timezones on the planet are negative. Some timezones are off from their neighbors by 30 minutes, though none are -59 minutes, but that's not a bug, just an unused feature. So for TZ there's nothing to do. For year, it would be possible to return an error when a negative year is parsed. But what error is this saving users of the stdlib from? I find it to be a borderline case, and I'm leaning towards "works as intended".

For some format strings time package claims to parse dates according to some RFCs. I am sure there are cases where RFCs do not permit signs, but time package does.

Also for timezone -160 I propose to not create Location with offset=0 at all (just leave it as nil). This preserves Time object unchanged after serialization/deserialization.

On tip, the above program now panics https://go.dev/play/p/57DDuAejKx5?v=gotip
with

panic: parsing time "-6+-1+1-1" as "06Z070000": cannot parse "+-1+1-1" as "Z070000"

goroutine 1 [running]:
main.main()
	/tmp/sandbox524911219/prog.go:13 +0xb2

Many changes probably had an impact but issue seems solved.

Thanks. Closing.