【问题标题】:missing Location in call to Time.In调用 Time.In 时缺少位置
【发布时间】:2018-01-25 09:20:35
【问题描述】:

当我使用beego/orm操作postgresql数据库时,出现类似“missing Location in call to Time.In”这样的错误。

代码示例

type dataTest struct {
    Id      int         `pk:"auto"`
    Data    time.Time   `orm:"auto_now;type(timestamp);null"`
}

local, _ := time.LoadLocation("UTC")
test_time, err := time.ParseInLocation("2006-01-02 15:04:05", "1111-01-25 14:27:07", local)
orm.DefaultTimeLoc = time.UTC

o,err := orm.NewOrmWithDB("postgres","default",db)
temp := new(dataTest)
temp.Id = 1
temp.Data = test_time
o.Update(temp)

【问题讨论】:

  • 不要忽略time.LoadLocation()返回的错误(其实不要忽略任何错误)。同样对于 UTC 时区,使用 time.UTC 变量。

标签: go beego


【解决方案1】:

对于 docker(构建多阶段)

在构建后安装 tzdata

RUN apk --no-cache add tzdata

可用时区列表,请参阅:https://golang.org/src/time/zoneinfo_abbrs_windows.go

loc, _ := time.LoadLocation("America/Bogota")
now := time.Now().In(loc)

【讨论】:

    【解决方案2】:

    这应该可行:

    type dataTest struct {
        Id      int         `pk:"auto"`
        Data    time.Time   `orm:"auto_now;type(timestamp);null"`
    }
    
    test_time, err := time.ParseInLocation("2006-01-02 15:04:05", "1111-01-25 14:27:07", time.UTC)
    orm.DefaultTimeLoc = time.UTC
    
    o,err := orm.NewOrmWithDB("postgres","default",db)
    temp := new(dataTest)
    temp.Id = 1
    temp.Data = test_time
    o.Update(temp)
    

    【讨论】:

    • 我有同样的问题,但这对我不起作用。还有其他解决方案吗?
    • 我遇到了同样的问题,但我在 Docker 中运行我的应用程序,并使用 alpine 基础映像。为我解决的问题是在 Dockerfile 中为 alpine 安装 tzdata OS 包。 (这个 GitHub 存储库帮助了我:github.com/takecy/tz-sample
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2021-04-29
    • 2015-03-16
    • 2021-12-05
    • 2021-07-17
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多