Thanks Ian, this does not work as I mentioned because there is no way to
guarantee that my init function will run before any of my libraries
creating a time object (which many do as they start various background
tasks).

Doing what you say is fragile because even if it appears to work, one day I
might link a library and it will mysteriously break randomly (this happened
to us previously).

Is this a bug in the time package? Or simply a bug or design shortfall in
the json package? One way I thought to fix this issue is to simply fork the
json encoder and handle time.Time structs specifically, but this obviously
does not scale to eg yaml encoders etc.

Thanks
Mike

On Fri, Jul 24, 2020, 07:17 Ian Lance Taylor <i...@golang.org> wrote:

> On Thu, Jul 23, 2020 at 9:47 AM <scude...@gmail.com> wrote:
> >
> > A bit late to the party here but I would like to explore your rational
> for stating that time.Local should not be modified by user code. The code
> seems to suggest this is expected:
> >
> > https://golang.org/src/time/zoneinfo.go
> > ```
> >
> > // localLoc is separate so that initLocal can initialize
> >     76  // it even if a client has changed Local.
> >
> > ```
> >
> > I want to change Local because I want all JSON encoding to be normalized
> to UTC and not local time - there does not seem to be a way to do this
> (please let me know if I am missing something). It is possible to set the
> TZ env var but not from within the program itself - it has to be done via a
> hacky shell script before I launch the program. This is because it is
> impossible to guarantee that my code will run before any library might
> access any time functions causing initLocal to fire. After this changing
> the TZ env var makes no difference.
> >
> > I know I can set the Location in every time.Time object I create but
> this is impossible for time.Time objects coming from other libraries. I
> essentially need to write my own visitor to try to identify such foreign
> time.Time objects and fix their Location before they can be json serialized.
> >
> > I tried to update time.Local using atomic.SetPointer so there really is
> no race but the race detector still complains - if there a way to turn it
> off?
>
> There is no way to change time.Local.  It is accessed without any
> protection, because the time package assumes that it will not change.
>
> If your program doesn't fetch any timezones at initialization time,
> you could use
>
> func init() {
>     os.Setenv("TZ", "")
> }
>
> Ian
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAJ%2BtYkZ%2BSASiOnm33Y9ygtfBJ0orxJquTPDx2eGvywPDzM8Gcg%40mail.gmail.com.

Reply via email to