Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeZone is not work #199

Open
oomxp598 opened this issue Jun 25, 2023 · 6 comments
Open

TimeZone is not work #199

oomxp598 opened this issue Jun 25, 2023 · 6 comments
Assignees

Comments

@oomxp598
Copy link

Description

I use 'TimeZone=Asia/Shanghai' to create DSN.
image

when v1.3.1 it‘s ok.
but when I update to v1.5.0 or v1.5.2 it's not work.
image

image
The log is ok
image
But the database saved time is less 8 hours.
image

@mrcuix
Copy link

mrcuix commented Jul 1, 2023

@oomxp598
与gorm 无关,现在你有两个选择:

  1. 设置 navcat (客户端)的链接会话时区为 Asia/Shanghai
  2. 设置 postgres 服务启动环境的时区,如果是docker 可以设置环境变量 TZ=Asia/Shanghai,如果是裸机安装 设置其配置文件后重启

@oomxp598
Copy link
Author

oomxp598 commented Jul 3, 2023

@mrcuix 感谢回复,应该是我没有描述清楚,是这样的,我有两个项目,连的同一个数据库(centos7裸机部署),数据库的时区为Asia/Shanghai,两个项目连接数据库的配置完全一样,唯一不同的是,引用的依赖包gorm.io/driver/postgres版本不一样(其中一个项目创建的稍晚,引用的依赖包版本较新),引用旧版本v1.3.1的项目是正常的,引用新版本v1.5.0的项目出现了时区问题,当我将版本回退至v1.3.1时,两个项目的时区都正常了

@moolitayer
Copy link

moolitayer commented Jul 13, 2023

Hi @jinzhu,
I think I'm seeing the same issue, It worked fine with:

	gorm.io/driver/postgres v1.2.3
	gorm.io/gorm v1.22.5

And no longer worked with

	gorm.io/driver/postgres v1.3.10
	gorm.io/gorm v1.23.7

The value I'm getting from

err = processor.dbc.Raw("show TIMEZONE").Scan(&result).Error

Is the correct value I set (UTC), however when I save UTC values and load them again they become time.Local instead of time.UTC

@SbstnErhrdt
Copy link

SbstnErhrdt commented Jul 25, 2023

@moolitayer same problem from me.

But if you use .UTC() again it was correct for me.

I used the following DSN:

dsn := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s TimeZone=UTC", host, port, user, dbName, pw)

@onee-io
Copy link

onee-io commented Jan 28, 2024

@mrcuix 感谢回复,应该是我没有描述清楚,是这样的,我有两个项目,连的同一个数据库(centos7裸机部署),数据库的时区为Asia/Shanghai,两个项目连接数据库的配置完全一样,唯一不同的是,引用的依赖包gorm.io/driver/postgres版本不一样(其中一个项目创建的稍晚,引用的依赖包版本较新),引用旧版本v1.3.1的项目是正常的,引用新版本v1.5.0的项目出现了时区问题,当我将版本回退至v1.3.1时,两个项目的时区都正常了

@oomxp598 我尝试了几个版本,发现从1.4.6开始,pgx的版本从v4升到了v5,之后设置时区就都没用了,1.4.5及以下的版本都没问题

@onee-io
Copy link

onee-io commented Jan 28, 2024

看了下它初始化连接的方式,如下图:
image

如果在配置中指定了 DriverName,就可以用驱动本身的方式去初始化连接,所以可以用以下方式去解决这个问题:

db, err := gorm.Open(postgres.New(postgres.Config{
  DriverName: "pgx/v5",
  DSN:        "postgres://user:password@127.0.0.1:5432/dbname?Timezone=Asia/Shanghai",
}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants