Skip to content

time: LoadLocation returns undescriptive error messages if no time zone database is available #50248

@GreenLightning

Description

@GreenLightning

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

go version go1.17 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system are you using?

Windows

What did you do?

Attempt to load time zone information using time.LoadLocation() and test under various conditions where no time zone database is available.

This issue is made worse by the fact that the Go distribution includes a time zone database, but it is not embedded into programs being compiled by default. Therefore binaries of my program worked fine for me, but not for my users that did not have Go installed. The "works on my machine" syndrome combined with the very generic error message made identifying the root issue a lot more difficult than it should have been.

Therefore to reproduce this issue, you have to be on Windows (where no system-wide time zone database is available) and temporarily rename the folder $GOROOT/lib/time to something else.

[If you are only here because your calls to LoadLocation() return the same error, the solution to this problem is to import _ time/tzdata to embed a fallback copy of the database into the binary. See the time/tzdata package documentation for more details.]

package main

import (
	"fmt"
	"time"
)

func main() {
	_, err := time.LoadLocation("EST")
	fmt.Println("Error:", err)
}

What did you expect to see?

A clear error message that no time zone database is available.

What did you see instead?

Error: The system cannot find the path specified.

This error is very generic. Compare for example with os.Open(), which returns an error of the form open <filename>: The system cannot find the path specified.. Even better would be a specific error message for the case that none of the searched locations contained a time zone database.

Interestingly, if you rename the zip file inside $GOROOT/lib/time instead of the directory itself then you get a different nonsensical error Error: unknown time zone EST, because ENOENT is ignored here. I think separate cases are confused in this logic. In the case where the time zone database is a plain directory, ENOENT means that there is no file for this specific time zone, and ignoring the error is correct. However, ENOENT can also be returned from loadTzinfoFromZip if the zip file cannot be found, in which case I would expect the error to be returned further. The same problem can be found here regarding the ZONEINFO environment variable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.help wanted

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions