Home > Net >  In GORM, how to configure autoCreateTime and autoUpdateTime in specific timezone?
In GORM, how to configure autoCreateTime and autoUpdateTime in specific timezone?

Time:08-26

The created_at and updated_at fields of my MariaDB database are previously filled with dates of my specific timezone (Europe/Paris).

I have now integrated GORM (v1.23.8), however, when using autoCreateTime and autoUpdateTime in my GORM model, the dates are always written in UTC. How can I configure GORM, so that the autoCreateTime and autoUpdateTime dates are written in a different timezone than UTC.

I have tried to add the Loc and ParseTime params to the MariaDB connection string, but that didn't fix the issue.

CodePudding user response:

GORM v1.23.8 specifies the autoCreateTime and autoUpdateTime field config to contain a UNIX timestamp, which is implicitly UTC timezone, therefor it seems impossible to change the timezone for autoCreateTime and autoUpdateTime.

The workaround would be to not specify your created_at and updated_at fields with autoCreateTime and autoUpdateTime, but as normal date fields and set the dates manually in your code.

CodePudding user response:

i am not sure it works. i give one example:

# connStr
[user]:[password]@tcp([host]:[port])/[database]?serverTimezone=Asia/Shanghai&parseTime=true&...
    
    gorm.Open(mysql.Open([connStr]))
  • Related