Force time.Now to always return UTC?

Is there a reasonable way to get time.Now() to always return the time in UTC regardless of the configured time zone of the host on which I’m running?

If that’s not possible the alternatives I can think of are:

  1. just remember to call UTC() when getting a time
  2. figure out what platform the dependent way is to change the time zone before initLocal runs – which seems trivial on linuxy environments (TZ environment variable) and looks to require changing the system TZ on Windows.

Anyway, neither of those is particularly nice so I was hoping I missed something?


Set time.Local to time.UTC.

Sigh. I’m a doofus…

I guess I’m so used to something like being a const / package private I didn’t think to just reassign after I imported it

Thanks much

1 Like

We are all learning! I’m glad it solved your problem

That said, why did you need to force the time zone to UTC? A time.Time should work the same everywhere regardless of the time zone so this should only be relevant when printing a time - at which point following the user preferences seem preferable?

For special things like HTTP header fields that must be in UTC, calling .UTC() at that point seems appropriate.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.