Search code examples
timezonego-gorm

How can specify time location for CreatedAt when selecting results?


I have a table with CreatedAt fild as "timestamp with timezone"

when I select rows I have entities with CreatedAt with time zone in my location, and I want to have it as UTC.

    type app struct {
    ID        uuid.UUID
    CreatedAt time.Time
    }

    db, err := gorm.Open(
    postgres.Open(databaseURL),
    &gorm.Config{},
    )
    if err != nil {
    panic(err)
    }

    a := &app{}
    db.Table("applications").Take(a)
    fmt.Println(a.CreatedAt)

it prints "2023-02-01 11:26:29.554589 +0300 MSK" and I would like to have "2023-02-01 08:26:29.554589 +0000 UTC"

Is there some option for that (gorm-v2)?

gorm-v1 retrieves as UTC, by the way. And gorm-v2 - in current location


Solution

  • The solution is to set timezone to UTC: os.Setenv("TZ", "UTC")

    Gorm v2 then retrieves datatime in UTC.