2

When I ues beego/orm to operate postgresql database,there is an error like this "missing Location in call to Time.In".

code example

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)
1
  • 4
    Don't ignore the error returned by time.LoadLocation() (in fact, don't ignore any errors). Also for UTC timezone use the time.UTC variable.
    – icza
    Commented Jan 25, 2018 at 9:24

4 Answers 4

11

For docker (build multi stage)

Install tzdata after build

RUN apk --no-cache add tzdata

List of time zones available, see: https://golang.org/src/time/zoneinfo_abbrs_windows.go

loc, _ := time.LoadLocation("America/Bogota")
now := time.Now().In(loc)
1
  • Life saviour spotted! Commented Jan 30, 2023 at 16:46
1

Problem

OS: Windows.

When I distribute the executable file to other computers, they encounter the following problem when running it.

time: missing Location in call to Time.In

However, there are no issues when debugging the code on my own computer.

Later, I discovered that the problem was caused by the time.LoadLocation function, which references runtime.GOROOT/lib/time/zoneinfo/zoneinfo.zipgorootZoneSource(runtime.GOROOT()) gorootZoneSource. Therefore, if the runtime.GOROOT on other computers is different from that of the developer or even nonexistent, it will cause problems.

Solution

Unzip %GOROOT%/lib/time/zoneinfo/zoneinfo.zip, select the desired time zone file, and use embed together with time.LoadLocationFromTZData to obtain the data for that region.

Example

package main

import (
    "embed"
    "time"
)

//go:embed zoneInfo
var zoneInfoFS embed.FS // get it from GOROOT/lib/time/zoneInfo.zip

func getLocation(name string) (loc *time.Location) {
    bs, err := zoneInfoFS.ReadFile("zoneInfo/" + name)
    if err != nil {
        panic(err)
    }
    loc, err = time.LoadLocationFromTZData(name, bs)
    if err != nil {
        panic(err)
    }
    return loc
}

func main() {
    locTW := getLocation("Asia/Taipei")
    locJPN := getLocation("Asia/Tokyo")
}

Directory Structure

-📜main.go
- 📂zoneInfo
  - 📂Asia
      - 📜Taipei
      - 📜Tokyo
  - ...
0

This should work:

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)
2
  • I have the same problem but this did not work for me. Any other solutions? Commented Nov 20, 2019 at 19:01
  • 2
    I had the same issue, but I was running my application in Docker, with an alpine base image. What solved it for me was to install the tzdata OS package for alpine in the Dockerfile. (this GitHub repo helped me: github.com/takecy/tz-sample)
    – bouwerp
    Commented Apr 17, 2020 at 6:46
0

Since Go 1.15 (released 2020-08-11) you can embed time zone info into your program. This allow not to rely on tzdata provided by OS.

import _ "time/tzdata"

See https://go.dev/doc/go1.15#time_tzdata

Not the answer you're looking for? Browse other questions tagged or ask your own question.