GORM Playground Link

https://github.com/go-gorm/playground/pull/416

Description

Database: cockroachdb Timezone: Europe/Rome or any other

When a record is created, the created_at field contains current date and time of this timezone, but after selecting this record using First() function by ID, created_at contains same date and time but with different timezone, in my case +0000 UTC. Expected 2021-12-14 14:40:04.856988 +0000 UTC or better 2021-12-14 15:40:04.856988 +0100 UTC.

Gorm Timezone issue using cockroachdb & postgres

Comment From: eddbbt

I encountered the same problem, we need a fix asap pls. thank u

Comment From: bliberi

I have the same problem, timezone is not consistent between insert/update and select

Comment From: nikzanda

Update: time zones don't work with postgres either. @jinzhu any news on this issue? Thanks.

Comment From: jinzhu

seems the tests pass? https://github.com/go-gorm/playground/runs/4592902633?check_suite_focus=true

Comment From: nikzanda

I suppose tests pass probably because github's time zone is Europe/London, so there are not problems because offset is always zero. Instead, with any other time zone where offset is not zero, for example my timezone, Europe/Rome, the problem persists. In the screencast taken from my pc, I show different situations: the first with time zone Europe/London (here all tests pass correctly); and the others where time zones have offset bigger than zero and here the tests failed.

https://user-images.githubusercontent.com/71180446/146936425-af1f85e6-88f8-4ec8-a951-08ce57acca01.mp4

Comment From: bliberi

@jinzhu any update on the issue?

Comment From: bliberi

@jinzhu hello any update or workaround on the issue? I have the same problem and I don't know how to fix this.

Comment From: jinzhu

The returned timezone is from db server, it doesn't remember the timezone you used when saving, and you can change the connection's timezone with DSN, more details refer: https://github.com/go-sql-driver/mysql#loc

Comment From: eddbbt

@jinzhu that's about cockroachdb (POSTGRES) NOT mysql. What's the correlation?

Comment From: jinzhu

you can set timezone for postgres driver like:

import (
  "gorm.io/driver/postgres"
  "gorm.io/gorm"
)

dsn := "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

Refer: https://gorm.io/docs/connecting_to_the_database.html#PostgreSQL