zoukankan      html  css  js  c++  java
  • 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技术爱好者
  • 相关阅读:
    sql server 2000系统表sysproperties在SQL 2008中无效的问题
    查询字段的默认值
    OBJECT_ID()的使用方法
    查询指定表的数据类型和长度
    SQL SERVER 比较两个数据库中表和字段的差异
    [再寄小读者之数学篇](2014-06-26 Logarithmical Sobolev inequality using BMO space)
    [再寄小读者之数学篇](2014-06-26 Besov space estimates)
    [再寄小读者之数学篇](2014-06-23 Bernstein's inequality)
    [再寄小读者之数学篇](2014-06-26 绝对值不等式)
    [再寄小读者之数学篇](2014-06-23 Gronwall-type inequality)
  • 原文地址:https://www.cnblogs.com/majiang/p/15576618.html
Copyright © 2011-2022 走看看