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?

Thanks
Mike



On Thursday, December 6, 2018 at 3:38:36 PM UTC+10, Matt Harden wrote:
>
> Just wrapping a write with a mutex is not enough; you have to wrap all the 
> reads as well. In this case that's not possible because there are reads of 
> time.Local all over the place. Anyway, as has been said, time.Local should 
> never be written to in user code.
>
> On Fri, Nov 30, 2018 at 7:46 AM Agniva De Sarker <agniva.q...@gmail.com 
> <javascript:>> wrote:
>
>> Can you show us the code ? It would help 
>>
>> Are you trying to set a variable of type time.Location ? Or are you 
>> trying to set time.Local to something else ?
>>
>> Like Ian said, if you want to change the location of a specific time 
>> value, use the In method.
>>
>> > But there are lots of place that time.Location used by time package of 
>> golang. For example: time.Now(), time.locabs()
>>
>> > I can’t change them.
>> I don't understand. Use time.Now() uses the time.Local variable. But all 
>> you need to do is wrap the setting of time.Local value with a mutex. Why is 
>> it not possible to do that ?
>>
>>
>>
>>
>> On Friday, 30 November 2018 11:31:42 UTC+5:30, Ian Lance Taylor wrote:
>>>
>>> On Thu, Nov 29, 2018 at 9:47 PM <zhangzh...@gmail.com> wrote: 
>>> > 
>>> > My go version is 1.11. 
>>> > 
>>> > When I set time.Location, then … 
>>> > 
>>> > ================== 
>>> > WARNING: DATA RACE 
>>> > Write at 0x000002e275d0 by main goroutine: 
>>> >   vcs.taiyouxi.net/platform/planx/timeutil.SetTimeLocal() 
>>> >       /Users/zhangzhen/serverthreekingdom/src/
>>> vcs.taiyouxi.net/platform/planx/timeutil/time_util.go:51 +0xb4 
>>> >   vcs.taiyouxi.net/platform/planx/timeutil.init.0() 
>>> >       /Users/zhangzhen/serverthreekingdom/src/
>>> vcs.taiyouxi.net/platform/planx/timeutil/time_util.go:42 +0x43 
>>> >   vcs.taiyouxi.net/platform/planx/timeutil.init() 
>>> >       <autogenerated>:1 +0xd0 
>>> >   vcs.taiyouxi.net/jws2/common/time.init() 
>>> >       <autogenerated>:1 +0xa6 
>>> >   vcs.taiyouxi.net/jws2/gamex/account/account.init() 
>>> >       <autogenerated>:1 +0xa6 
>>> >   vcs.taiyouxi.net/jws2/gamex/logics.init() 
>>> >       <autogenerated>:1 +0xa6 
>>> >   vcs.taiyouxi.net/jws2/gamex/cmds/gamemode.init() 
>>> >       <autogenerated>:1 +0xa6 
>>> >   main.init() 
>>> >       <autogenerated>:1 +0xa6 
>>> > 
>>> > Previous read at 0x000002e275d0 by goroutine 8: 
>>> >   time.Now() 
>>> >       /Users/zhangzhen/.gvm/gos/go1.11/src/time/time.go:1060 +0xcf 
>>> >   time.sendTime() 
>>> >       /Users/zhangzhen/.gvm/gos/go1.11/src/time/sleep.go:141 +0x44 
>>> > 
>>> > Goroutine 8 (running) created at: 
>>> >   runtime.(*timersBucket).addtimerLocked() 
>>> >       /Users/zhangzhen/.gvm/gos/go1.11/src/runtime/time.go:170 +0x113 
>>> >   
>>> vcs.taiyouxi.net/vendor/github.com/siddontang/go/timingwheel.NewTimingWheel()
>>>  
>>> >       /Users/zhangzhen/serverthreekingdom/src/
>>> vcs.taiyouxi.net/vendor/github.com/siddontang/go/timingwheel/timingwheel.go:39
>>>  
>>> +0x2a0 
>>> >   vcs.taiyouxi.net/platform/planx/util.init() 
>>> >       /Users/zhangzhen/serverthreekingdom/src/
>>> vcs.taiyouxi.net/platform/planx/util/timer_helper.go:10 +0xf3 
>>> >   vcs.taiyouxi.net/platform/planx/metrics.init() 
>>> >       <autogenerated>:1 +0xbf 
>>> >   vcs.taiyouxi.net/jws2/gamex/cmds/gamemode.init() 
>>> >       <autogenerated>:1 +0x9c 
>>> >   main.init() 
>>> >       <autogenerated>:1 +0xa6 
>>> > ================== 
>>> > 
>>> > The feature of golang is goroutine, and it is a normal situation that 
>>> get time in different goroutine. 
>>> > But only can set time.Location in one goroutine. So the Data Race is 
>>> inevitable. 
>>> > 
>>> > 
>>> > And there is the replay from agnivade 3 
>>> > 
>>> > “It is not a bug. Please synchronize access to time.Location using 
>>> synchronization primitives in the sync andsync/atomic packages.” 
>>> > 
>>> > 
>>> > But there are lots of place that time.Location used by time package of 
>>> golang. For example: time.Now(), time.locabs() 
>>> > I can’t change them. 
>>> > 
>>> > 
>>> > So, What should I do ? Please help me. 
>>>
>>> You said time.Location.  Do you mean time.Local?  It's true that you 
>>> should never change time.Local.  Why  do you want to?  If you want to 
>>> change the location of a specific time.Time value, call the In method. 
>>>
>>> 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 golan...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
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/59d1e05b-63d4-4d9a-adeb-00126c3298dco%40googlegroups.com.

Reply via email to