Golang insertion time. Now the database is shifted to UTC

I am trying to insert the time.Now () field into the database, but what happens is this time moves forward in UTC. I understand that the idea is always to then convert to local time when presented to the user. The problem is that I have inherited this system, and currently it’s hard to root in order to easily change it.

Any tips? I saw that you can set loc to a DSN, but it doesn’t explain what to change its or its actual effect, so I would appreciate the information.

Edit: some information, this is MySQL DB using go-mysql 1.1 and go to 1.6.3. Data is inserted into the DATETIME field.

The data is incorrect in the insert. GORM Debug shows once, and the MySQL query log shows that it is pushed forward.

I found that the problem is that the go-mysql driver automatically changes the time to UTC, and this can be changed using the loc parameter in the DSN. This also changes the return time.

To fix this, add loc = Local to your DSN.

+4
source share
2 answers

So I use to always keep local time. Works great even if your VPS is in another country with a time zone:

db, err = gorm.Open("mysql", "root:@tcp(localhost:3306)/mydatabase?charset=utf8&parseTime=True&loc=America%2FSao_Paulo")
+1
source

because time in golang parse will use UTC

-3
source

Source: https://habr.com/ru/post/1648419/


All Articles