Your Question
I am using Postgres and when I declare a struct with a field of type time.Time, the location is set to "" even that I set the timezone in the connection. In the gorm configuration I changed the nowFunc to set the timezone, but this only works for the created at and updated at fields, not for other time.Time fields. As an example:
type TestStruct struct {
ID int
CreatedAt time.Time
UpdatedAt time.Time
StartTime. time.Time
}
And I use the following gorm config:
gorm.Open(postgres.New(postgres.Config{
Conn: gc.Db,
}), &gorm.Config{
NowFunc: func() time.Time {
return time.Now().UTC().Truncate(time.Second)
},
})
The issue with this is that, if I create the TestStruct with an UTC location, and then I try to do assert.Equal with the one I retrieve form the database, the only difference is the loc between expected.StartTime (loc is nil) and actual.StartTime (loc is "")
The document you expected this should be explained
I only found this post in Stackoverflow related to my question (https://stackoverflow.com/questions/71019353/how-to-set-default-timezone-on-gorm-time-time-data-type)
Expected answer
I think all the time.Time fields should have the timezone set to declared timezone in the connection, or maybe I am missing something?
Comment From: saeidee
Have you tried changing your application's default timezone(You need to put this code before using any time package functions)? if yes could you share your DSN?
os.Setenv("TZ", "Europe/Istanbul)
Comment From: manuelarte
yes, I tried and still get loc = ""
Comment From: lox
I get this issue too. @saeidee why was this closed?