https://groups.google.com/g/golang-nuts/c/uqENEJYLvfc

lege…@gmail.com

unread,Feb 10, 2022, 7:36:10 AM to golang-nuts

I noticed a phenomenon while maintaining my golang application, the local timezone of the application always keep the value when it starts, the local timezone will not change even though I change the system timezone. It looks like the golang time package has been caching the current timezone.
Is there any way to change the local timezone in the golang application as the system’s timezone changes?

Matthew Walster's profile photo

Matthew Walster

unread,Feb 10, 2022, 7:56:55 AM to lege…@gmail.com, golang-nuts On Wed, 9 Feb 2022 at 23:36, E Z <lege…@gmail.com> wrote:

I noticed a phenomenon while maintaining my golang application, the local timezone of the application always keep the value when it starts, the local timezone will not change even though I change the system timezone. It looks like the golang time package has been caching the current timezone.
Is there any way to change the local timezone in the golang application as the system’s timezone changes
See https://cs.opensource.google/go/go/+/refs/tags/go1.17.6:src/time/zoneinfo.go and https://cs.opensource.google/go/go/+/refs/tags/go1.17.6:src/time/zoneinfo_unix.go — there is a sync.Once to set it. I would have thought changing the timezone underneath a running program would not be a good idea due to it often not being an atomic swap.
M

Ian Lance Taylor's profile photo

Ian Lance Taylor

unread,Feb 10, 2022, 9:16:28 AMto lege…@gmail.com, golang-nutsOn Wed, Feb 9, 2022 at 3:37 PM E Z <lege…@gmail.com> wrote:
>
> I noticed a phenomenon while maintaining my golang application, the local timezone of the application always keep the value when it starts, the local timezone will not change even though I change the system timezone. It looks like the golang time package has been caching the current timezone.
>
> Is there any way to change the local timezone in the golang application as the system’s timezone changes?

htttps://go.dev/issue/28020

Ian

Ian Lance Taylor's profile photo

Ian Lance Taylor

unread,8:11 AM (4 minutes ago) to carl…@gmail.com, golang-nutsOn Tue, Mar 1, 2022 at 4:05 PM Carl <carl…@gmail.com> wrote:
>
> I’m interested in the reason for the current behaviour.
> I know there are ways to work around it and am aware of the consequences of a change to the stdlib now.
> I’m also aware of what will happen if the timezone changes on a standard system.
> I assume all platforms that Go supports support the ability to change the timezone without rebooting the system and they already handle it.
>
> If that’s the case, why does Go ignore the case that the time zone can change when a program is running?
>
> Please don’t misunderstand me – I love Go and really respect the design decisions that went into the language.
> This is why I’m interested in the reason for the current behaviour – to learn.
>
> I would really appreciate a reply from someone who can provide the actual reason the time package was implemented this way.

I don’t think there is any mystery about it. It’s the simplest way to
implement the time package. Additional mechanism to detect that the
timezone has changed and use the new timezone is increased complexity.

That’s not to say that we shouldn’t change it (as I said above, that
is https://go.dev/issue/28020). But it’s actually not all that
obvious how to change it. We certainly don’t want to check the local
timezone every time that a program asks for the current time; that
would be a measurable performance cost for a case that approximately
never happens.

Ian

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.