golang 时间missing Location in call to Date
golang使用"Asia/Shanghai"时区转换时间格式报:missing Location in call to Date
当然解决方法1是:time.FixedZone
//os.Setenv("ZONEINFO","D:\\ProgramFiles\\Go\\lib\\time\\zoneinfo") loc, err := time.LoadLocation("Asia/Shanghai") //设置时区 if err != nil { loc = time.FixedZone("CST-8", 8*3600) } fmt.Print(loc)
解决方法二是:os.Setenv("ZONEINFO","xxx") 值可以是那个zip文件,也可以是一个目录,比如把zip解压后的目录,我这是解压zoneinfo.zip,【我的问题是,公司强制安装一个软件,会加密电脑上的word ,txt,zip 等文件,导致读出来的zip 文件头有问题】
源码如下【1.15.6版本,省略了一些无关的的代码】
func LoadLocation(name string) (*Location, error) { ....... zoneinfoOnce.Do(func() { env, _ := syscall.Getenv("ZONEINFO") zoneinfo = &env }) var firstErr error if *zoneinfo != "" { if zoneData, err := loadTzinfoFromDirOrZip(*zoneinfo, name); err == nil { if z, err := LoadLocationFromTZData(name, zoneData); err == nil { return z, nil } firstErr = err } else if err != syscall.ENOENT { firstErr = err } } ...... return nil, firstErr } func loadTzinfoFromDirOrZip(dir, name string) ([]byte, error) { if len(dir) > 4 && dir[len(dir)-4:] == ".zip" { return loadTzinfoFromZip(dir, name) } if dir != "" { name = dir + "/" + name } return readFile(name) } // loadTzinfoFromZip returns the contents of the file with the given name // in the given uncompressed zip file. func loadTzinfoFromZip(zipfile, name string) ([]byte, error) { fd, err := open(zipfile) if err != nil { return nil, err } defer closefd(fd) .................................... buf := make([]byte, ztailsize) if err := preadn(fd, buf, -ztailsize); err != nil || get4(buf) != zecheader { return nil, errors.New("corrupt zip file " + zipfile) } ................................. return nil, syscall.ENOENT }
方法调用路线如下:
zip: LoadLocation->loadTzinfoFromDirOrZip->loadTzinfoFromZip->get4
路径 : LoadLocation->loadTzinfoFromDirOrZip->readFile
如果是zip,方法get4会验证zip文件头。我这里有公司软件加密,所以zip 情况下get4方法错误,所以改为路径
windows技术爱好者