• 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技术爱好者
  • 相关阅读:
    关于html5的一些知识。
    常见的http状态码总结。
    踩坑记录-安装node-sass运行报错TypeError: this.getResolve is not a function at Object.loader
    踩坑记录-!!vue-style-loader!css-loader错误
    koa-passport做登录注册验证
    nuxt项目里使用vuex状态树
    node(koa、nuxt等项目)中使用import报错问题
    koa+nodemailer实现邮箱验证注册功能
    踩坑记录-nuxt引入vuex报错store/index.js should export a method that returns a Vuex instance.
    常用shell命令积累
  • 原文地址:https://www.cnblogs.com/majiang/p/15576618.html
Copyright © 2020-2023  润新知