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)
fangtao
  • 31
  • 1
  • 4
  • 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 Jan 25 '18 at 09:24

3 Answers3

9

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)
erajuan
  • 2,224
  • 20
  • 31
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
  - ...
Carson
  • 6,105
  • 2
  • 37
  • 45
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)
Ilayaraja
  • 2,388
  • 1
  • 9
  • 9
  • I have the same problem but this did not work for me. Any other solutions? – Ahmed Shahid Nov 20 '19 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: https://github.com/takecy/tz-sample) – bouwerp Apr 17 '20 at 06:46