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 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊