time.LoadLocation works regularly but throws an error on my docker instance! How do I fix it?
Asked Answered
T

2

8

time.LoadLocation works regularly but throws an error on my docker instance! How do I fix it?

I ran

t, err := time.LoadLocation("America/New_York")

and it returns an error even though it works just fine on my computer and on play.golang.org (https://play.golang.org/p/4VHlaku26T3)

However, when I run it on my docker instance, I get an error returned unknown time zone America/New_York

Why doesn't it detect my requested time zone?

Tuckerbag answered 6/2, 2020 at 20:45 Comment(0)
T
13

Linux Alpine does not have timezone information natively built in. You need to update your Dockerfile to get that information.

and add the command apk --no-cache add tzdata to the RUN line

e.g., for me I have a line that looks like the following

RUN apk update && apk add bash && apk --no-cache add tzdata

This fixed the issue for me.

Tuckerbag answered 6/2, 2020 at 20:45 Comment(2)
The documentation contains some additional info "The time zone database needed by LoadLocation may not be present on all systems, especially non-Unix systems. LoadLocation looks in the directory or uncompressed zip file named by the ZONEINFO environment variable, if any, then looks in known installation locations on Unix systems, and finally looks in $GOROOT/lib/time/zoneinfo.zip." (useful if building an image FROM scratch)Estrous
Thanks @Brits! They definitely do point it out. In my case even with that documentation, I was surprised to find that it was not in a Linux distro.Tuckerbag
B
13

A newer fix for this (since Go 1.15) that has the advantage of working on any platform is to add an import to your go code

import _ "time/tzdata"

https://go.dev/doc/go1.15#time/tzdata

Breault answered 18/7, 2022 at 6:9 Comment(2)
Nice find! Glad they have this option now!Tuckerbag
Awesome, this worked for me too! Thanks for the save.Conlan

© 2022 - 2024 — McMap. All rights reserved.