missing Location in call to Time.In
Asked Answered
D

4

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)
Diaphragm answered 25/1, 2018 at 9:20 Comment(1)
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.Fritzie
R
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)
Renaissance answered 2/6, 2020 at 19:32 Comment(1)
Life saviour spotted!Goshen
E
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
  - ...
Eyebright answered 22/3, 2023 at 6:48 Comment(0)
D
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)
Detestation answered 25/1, 2018 at 14:7 Comment(2)
I have the same problem but this did not work for me. Any other solutions?Storage
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)Evania
C
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

Cedilla answered 25/3 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.