Set Golang Standard Timezone

Asked

Viewed 254 times

2

Is there any way to set a standard Timezone (Location) in Golang so you don’t have to be on time?

I am currently manually setting the Timezone every time I need to work with Time using it:

loc, _ := time.LoadLocation("America/Sao_Paulo")
tim := time.Now().In(loc)

4 answers

2


You can set to váriavél time.Local. According to the documentation:

var Local *Location = &localLoc

Local represents the system’s local time zone. On Unix systems, Local > consults the TZ Environment variable to find the time zone to use. No > TZ Means use the system default /etc/localtime. TZ="" Means use UTC. > TZ="foo" Means use file foo in the system Timezone directory.

Then you can use to change the Location.


For example:

time.Local = time.UTC

Thus in executing the time.Now() will be in UTC.


In the case of America/Sao_Paulo just do:

time.Local, _ = time.LoadLocation("America/Sao_Paulo")

This can be stated in main() and all subsequent calls will use that time zone.


If you take a test:

func main() {
    fmt.Println("Tempo padrão:", time.Now())

    time.Local = time.UTC
    fmt.Println("Tempo UTC:", time.Now())
    fmt.Println("Tempo UTC:", time.Now())

    time.Local, _ = time.LoadLocation("America/Sao_Paulo")
    fmt.Println("Tempo SP:", time.Now())
    fmt.Println("Tempo SP:", time.Now())
}

Upshot:

Tempo padrão: 2021-06-17 15:28:04.5675042 +0100 BST m=+0.092369601
Tempo UTC: 2021-06-17 14:28:04.5844768 +0000 UTC m=+0.109342201
Tempo UTC: 2021-06-17 14:28:04.5844768 +0000 UTC m=+0.109342201
Tempo SP: 2021-06-17 11:28:04.5844768 -0300 -03 m=+0.109342201
Tempo SP: 2021-06-17 11:28:04.5844768 -0300 -03 m=+0.109342201
  • Ball show! It worked perfectly! Can you tell me if there has always been this variable? Because years ago when I researched I found nothing.

  • It has always existed, at least since Go 1.0.1, there from ~2012. You can see the source code at https://go.googlesource.com/go/+/refs/tags/go1.0.1/src/pkg/time/zoneinfo.go#57.

  • I get it. Strange that when I researched I didn’t find this anywhere, and I researched a lot, very much, then as I didn’t find anything at the time I ended up creating a function that initializes a new team.time in the Timezone that I want. But this way ai is simpler and is native to GO. Thank you again.

-1

It is necessary to use the function time. Now(). In()

loc, _ := time.LoadLocation("America/Sao_Paulo")
// handle err
currentTime := time.Now().In(loc)

// outra forma de fazer o exemplo acima
loc, _ := time.LoadLocation("America/Sao_Paulo")
currentTime := time.Now()
currentTime.In(loc)

Example in Go Playground

  • This does not set a standard Timezone for all dates which is what I want. This will set a pattern for my current team variable.

-1

You can create a separate package by setting this configuration and importing it in the places you need, for example:

package tzinit

import (
    "os"
)

func init() {
    os.Setenv("TZ", "America/Sao_Paulo")
}

Importing configuration

main package

import _ "path_pacote/tzinit"

import (
    "fmt"
    "math"
    //mais algum outro pacote
)

I hope I’ve helped !

  • I had seen this answer on other topics and it just doesn’t work. I had already done some tests with it and it doesn’t work. If you want to test you can use the following code snippet: https://play.golang.org/p/-RnSNe2HVY2

-1

Straight arrow on the machine, then the team. Now() comes with the right Timezone.

Example of Dockerfile:

FROM alpine:latest
RUN apk add --no-cache tzdata
ENV TZ=America/Sao_Paulo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
  • 2

    It is a good alternative, I could see to set these ENV by GO, because not everyone uses Docker, but is a great idea setar ENV. By the way, you tested to see if it works?

  • Setting this Timezone on the server machine has the same result if you don’t use Docker.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.