donglu3184 2019-08-03 16:00
浏览 14
已采纳

如何使用自定义时区自动填充类型为time.Time的结构域?

I'm using GORM to retrieve data from a Postgresql database. Inside the postgresql database I'm storing times as the default UTC. When I load them through gorm/golang I would like to automatically convert them to 'Europe/London' location.

Currently, all times are returned as my local timezone (CEST). I'm struggling to find a way to manually override this?

Here is the relevant code:

type Booking struct {
    gorm.Model
    Service   Service
    ServiceID uint `json:serviceId`
    Start     time.Time
    Finish    time.Time
}

func getBookings() http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        bookings := &[]Booking{}

        GetDB().Find(bookings)

        render.JSON(w, r, bookings)
    }
}

I've been looking around and I can't seem to find any information from gorm or golang docs. The two closest things to mentions of this problem that I've found is:

https://github.com/jinzhu/gorm/wiki/How-To-Do-Time

Setting timezone globally in golang

I thought a work around could be to manually change the query results with a loop, but I'm not sure if this is the most efficient solution? - code below:

func getBookings() http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        bookings := &[]Booking{}
        timeZoneBookings := *&[]Booking{}

        GetDB().Find(bookings)

        for _, booking := range *bookings {

            booking.Start = parseToUkTime(booking.Start)
            booking.Finish = parseToUkTime(booking.Finish)

            timeZoneBookings = append(timeZoneBookings, booking)

        }

        render.JSON(w, r, timeZoneBookings)
    }
}



func parseToUkTime(timeToParse time.Time) time.Time {
    loc, _ := time.LoadLocation("Europe/London")

    t := timeToParse.In(loc)

    return t

}

Here is an image of the DB entry:

enter image description here

I assumed it would be easy to either state inside the type I would like the location to be set to Europe/London so the struct would automatically be populated this way, however this doesn't seem to be the case? It's the first time I have worked with timezones, so everything is quite confusing.

  • 写回答

1条回答 默认 最新

  • dounan4479 2019-08-03 17:00
    关注

    Loop through the slice and update the time values in place. Lookup the location once outside of the handler.

    func getBookings() http.HandlerFunc {
        loc, _ := time.LoadLocation("Europe/London")
        return func(w http.ResponseWriter, r *http.Request) {
            var bookings []Booking
            GetDB().Find(&bookings)
            for i := range bookings {
                booking[i].Start = bookings[i].Start.In(loc)
                booking[i].Finish = bookings[i].Finish.In(loc)
            }
            render.JSON(w, r, bookings)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新
  • ¥50 深度强化学习解决能源调度问题