douleijiang8111 2019-03-29 02:44
浏览 159
已采纳

在Linux和macOS上的Go中RFC3339的时间格式化结果不同

I ran the go code following.

package main

import (
    "fmt"
    "strconv"
    "time"
)

func main() {
    i, err := strconv.ParseInt("1405544146", 10, 64)
    if err != nil {
        panic(err)
    }
    tm := time.Unix(i, 0).Format(time.RFC3339)
    fmt.Println(tm)
    fmt.Println(time.RFC3339)

}

Then the result on Linux is

2014-07-16T20:55:46Z
2006-01-02T15:04:05Z07:00

and on macOS is

2014-07-17T05:55:46+09:00
2006-01-02T15:04:05Z07:00

It's the same time but formatted results are different. Do you know the reason?

  • 写回答

4条回答 默认 最新

  • douping1825 2019-03-29 03:50
    关注

    Don't jump to conclusions. Examine all the evidence. For instance, consider the local time zone.

    Package time

    import "time" 
    

    func Unix

    func Unix(sec int64, nsec int64) Time
    

    Unix returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC.


    For example,

    package main
    
    import (
        "fmt"
        "runtime"
        "strconv"
        "time"
    )
    
    func main() {
        i, err := strconv.ParseInt("1405544146", 10, 64)
        if err != nil {
            panic(err)
        }
        t := time.Unix(i, 0)
        fmt.Println(t)
        fmt.Println(t.Format(time.RFC3339))
        fmt.Println(time.RFC3339)
        fmt.Println(runtime.GOOS, runtime.GOARCH, runtime.Version())
    }
    

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

    Output (Playground):

    2014-07-16 20:55:46 +0000 UTC
    2014-07-16T20:55:46Z
    2006-01-02T15:04:05Z07:00
    nacl amd64p32 go1.12
    

    Output (Linux):

    2014-07-16 16:55:46 -0400 EDT
    2014-07-16T16:55:46-04:00
    2006-01-02T15:04:05Z07:00
    linux amd64 devel +5b68cb65d3 Thu Mar 28 23:49:52 2019 +0000
    

    Different time zones (UTC versus EDT) so different formatted dates and times.


    In your examples you have 2014-07-16T20:55:46Z and 2014-07-17T05:55:46+09:00, different time zones so different formatted dates and times.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)