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

如何正确配置上海时区 #32

Closed
thetrueself opened this issue Aug 2, 2022 · 2 comments
Closed

如何正确配置上海时区 #32

thetrueself opened this issue Aug 2, 2022 · 2 comments

Comments

@thetrueself
Copy link

  1. 时区设置成Asia/Shanghai,提示以下错误:
    数据库连接错误:invalid DSN: did you forget to escape a param value? panic: 数据库连接错误:invalid DSN: did you forget to escape a param value?
  2. 时区设置成Asia Shanghai,提示以下错误:
    数据库连接错误:unknown time zone Asia Shanghai panic: 数据库连接错误:unknown time zone Asia Shanghai
  3. 时区设置成+8:00,提示以下错误:
    数据库连接错误:unknown time zone 8:00 panic: 数据库连接错误:unknown time zone 8:00
@zhaoyangkun
Copy link
Owner

很抱歉,该部分代码存在问题,我会在下一个版本中修复。

出现问题的原因为:gorm 设置 mysql 时区参数 loc 的值时需进行 UrlEncode

解决办法为:对时区参数 loc 的值进行 Url 编码,需要手动修改项目路径下的 config/db/db.go 中的相关代码:

import "net/url"

// 获取数据库连接
func getDataSource() string {
	dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true&loc=%s",
		setting.Config.Database.UserName,
		setting.Config.Database.Password,
		setting.Config.Database.Host,
		setting.Config.Database.Port,
		setting.Config.Database.Database,
		url.QueryEscape(setting.Config.Database.TimeZone), // 对时区进行 Url 编码
	)
	return dataSource
}

@zhaoyangkun
Copy link
Owner

修改完代码之后,在 develop.yaml 配置文件中将时区 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

2 participants