donglun4682 2018-03-28 16:01
浏览 156

如何在Go中表示RFC-3339`-00:00`偏移量?

RFC-3339 Section 4.3 (https://tools.ietf.org/html/rfc3339#section-4.3) defines the -00:00 offset as the following, which is different than Z or +00:00.

4.3. Unknown Local Offset Convention

   If the time in UTC is known, but the offset to local time is unknown,
   this can be represented with an offset of "-00:00".  This differs
   semantically from an offset of "Z" or "+00:00", which imply that UTC
   is the preferred reference point for the specified time.  RFC2822
   [IMAIL-UPDATE] describes a similar convention for email.

However, I'm not sure how to represent this in Go. When I parse a time with -00:00 and format it, I get a Z offset. For example:

  • Input: 2018-01-01T00:00:00-00:00
  • Output: 2018-01-01T00:00:00Z

Here's some example code (https://play.golang.org/p/CVmNnhaSiiT):

package main

import (
    "fmt"
    "time"
)

func main() {
    t := "2018-01-01T00:00:00-00:00"
    fmt.Println("Input  " + t)
    p, err := time.Parse(time.RFC3339, t)
    if err != nil {
        fmt.Println(err)
    } else {
        t2 := p.Format(time.RFC3339)

        fmt.Println("Output " + t2)
    }
}
  • 写回答

2条回答 默认 最新

  • duanrang9348 2018-03-28 17:21
    关注

    Package time

    import "time"
    

    RFC3339, RFC822, RFC822Z, RFC1123, and RFC1123Z are useful for formatting; when used with time.Parse they do not accept all the time formats permitted by the RFCs.


    Go does not accept all the time formats permitted by the RFCs.

    The Go time.Time type uses integers which, unlike floating-point, have no concept of plus and minus zero. The results for parsing offsets of -00:00 and +00:00 are identical.

    For example,

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        var err error
        var minus, plus time.Time
        t := "2018-01-01T00:00:00-00:00"
        minus, err = time.Parse(time.RFC3339, t)
        if err != nil {
            fmt.Println(err)
        }
        t = "2018-01-01T00:00:00+00:00"
        plus, err = time.Parse(time.RFC3339, t)
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println(minus, plus, minus.Equal(plus), minus == plus)
    }
    

    Playground: https://play.golang.org/p/Urf8VlKYoMH

    Output:

    2018-01-01 00:00:00 +0000 UTC 2018-01-01 00:00:00 +0000 UTC true true
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!