golang time.Now().Unix()在文档和实现之间存在差异吗?

huangapple go评论94阅读模式
英文:

golang time.Now().Unix() discrepancy between documentation and implementation?

问题

这是一个关于Go语言中时间包的问题。根据文档,time.Unix函数的签名是func Unix(sec int64, nsec int64) Time,意味着它返回一个Time对象。

但是下面的程序会中止,并显示错误信息:

>14: cannot use now.Unix() (type int64) as type time.Time in assignment`

func main() {
    var now time.Time
    now = time.Now()
    fmt.Println(now)

    var secs time.Time
    secs = now.Unix()
    fmt.Println(secs)
}

下面这个版本的程序会输出:

func main() {
    var now time.Time
    now = time.Now()
    fmt.Println(now)

    // var secs time.Time
    secs := now.Unix()
    fmt.Println(secs)
}

输出结果为:

2016-04-12 18:20:22.566965512 -0400 EDT
1460499622

这只是文档中的错误吗?

英文:

https://golang.org/pkg/time/#Unix

states that the signature of this function is

func Unix(sec int64, nsec int64) Time

meaning, it returns a Time object.

But the following program aborts with the error message:

>14: cannot use now.Unix() (type int64) as type time.Time in assignment`

func main() {
	var now time.Time
	now = time.Now()
	fmt.Println(now)

	var secs time.Time
	secs = now.Unix()
	fmt.Println(secs)
}

The following version of the program produces output

func main() {
	var now time.Time
	now = time.Now()
	fmt.Println(now)

	// var secs time.Time
	secs := now.Unix()
	fmt.Println(secs)
}

2016-04-12 18:20:22.566965512 -0400 EDT

1460499622

Is this just a matter of error in documentation?

答案1

得分: 5

文档是正确的,你正在查看错误版本的Unix。你正在使用的方法是这个:https://golang.org/pkg/time/#Time.Unix 它不接受任何参数,是在时间对象上调用的,并将Unix时间作为int64返回。你引用的版本接受两个参数,都是int64类型的,秒和纳秒?(不确定nseconds代表什么)它表示Unix格式的时间,并返回一个time.Time对象。

所以,为了扩展你的第二个示例:

func main() {
    var now time.Time
    now = time.Now()
    fmt.Println(now)

    // var secs time.Time
    secs := now.Unix()
    fmt.Println(secs)
    t := time.Unix(secs, 0) // 从秒值获取一个Unix时间的时间
    fmt.Println(t)
}

在上面的示例中,我添加了一行代码来打印secs的类型。你可以在这里查看它的运行结果:https://play.golang.org/p/KksPPbQ1Jy

英文:

The docs are correct, you're looking at the wrong version of Unix. The method you're using is this; https://golang.org/pkg/time/#Time.Unix it takes no arguments, is called on a time object and returns the unix time as an int64. The version you're referencing takes two arguments, both int64's, the seconds and nanoseconds? (not positive that's what the nseconds stands for) which represent a time in unix format and it returns a time.Time

So to extend your second example;

func main() {
    var now time.Time
    now = time.Now()
    fmt.Println(now)

    // var secs time.Time
    secs := now.Unix()
    fmt.Println(secs)
    t := time.Unix(secs, 0) // get a time from the seconds value which is a unix time
    fmt.Println(t)
}

Here's the above in play with one other line added to print the type of secs. https://play.golang.org/p/KksPPbQ1Jy

huangapple
  • 本文由 发表于 2016年4月13日 06:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/36585143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定