dqn8235 2016-04-14 06:03
浏览 58
已采纳

为什么与==相比,具有相同日期和时间的2个时间结构返回false?

I have a time.Time created using time.Date(). I then calculate the number of nanoseconds between 1970/1/1 00:00:00.000000000 and that time.

I then take the nanoseconds and turn them back into a time.Time using time.Unix().

However, if I compare the reconstituted time with the original using ==, it returns false. If I subtract these 2 times, the resulting duration is 0. If I compare these 2 times using time.Equal(), it returns true.

If I create another time using time.Date() with the same values as the first time, using == to compare this new time and the original time results in a true.

This is the code that demonstrates this (Golang Playground):

package main

import (
    "fmt"
    "time"
)

func main() {
    t1 := time.Date(2016, 4, 14, 1, 30, 30, 222000000, time.UTC)


    base := time.Date(1970, 1, 1, 0, 0, 0, 0, t1.Location())
    nsFrom1970 :=t1.Sub(base).Nanoseconds() // Calculate the number of nanoseconds from 1970/1/1 to t1

    t2 := time.Unix(0, nsFrom1970)

    fmt.Println(t1)
    fmt.Println(t2)
    fmt.Println(t1.Sub(t2)) // 0
    fmt.Println(t1 == t2) //false
    fmt.Println(t1.Equal(t2)) //true

    t3 := time.Date(2100, 2, 1, 21, 21, 21, 222000000, time.UTC)
    fmt.Println(t1 == t3) //true
}

Why does the reconstituted time return false when compared with the original time?

  • 写回答

1条回答 默认 最新

  • dongwen2794 2016-04-14 06:36
    关注

    time.Time is a struct. When you try to compare them with ==, quoting from the Spec: Comparison operator:

    Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.

    So t1 == t2 will compare all the fields of the 2 Time struct values. The Time struct contains not just the second and nanosecond since a base time, it also contains the location as a pointer: *Location, so == will also compare the location fields. Comparing pointers:

    Pointer values are comparable. Two pointer values are equal if they point to the same variable or if both have value nil. Pointers to distinct zero-size variables may or may not be equal.

    And this is why comparing the times with == gives a false result: 2 locations may denote the same location even if their address is different, and this is your case.

    To prove this:

    fmt.Println("Locations:", t1.Location(), t2.Location())
    fmt.Printf("Location pointers: %p %p
    ", t1.Location(), t2.Location())
    fmt.Println("Locations equal:", t1.Location() == t2.Location())
    

    Output:

    Locations: UTC UTC
    Location pointers: 0x1e2100 0x1e6de0
    Locations equal: false
    

    This is documented in time.Time:

    Note that the Go == operator compares not just the time instant but also the Location. Therefore, Time values should not be used as map or database keys without first guaranteeing that the identical Location has been set for all values, which can be achieved through use of the UTC or Local method.

    If t1 and t2 would also contain the same *Location pointer, they would be equal even if compared with the == operator. This can be ensured by calling Time.UTC() or Time.Local() method on them which returns a time.Time value where the same location pointer (*Location) is used. Or by using the Time.In() method which will set the specified location pointer (after the proper conversion), e.g.:

    t2 = t2.In(t1.Location())
    fmt.Println("Locations:", t1.Location(), t2.Location())
    fmt.Printf("Location pointers: %p %p
    ", t1.Location(), t2.Location())
    fmt.Println("Locations equal:", t1.Location() == t2.Location())
    fmt.Println(t1 == t2)     // Now true
    fmt.Println(t1.Equal(t2)) // Still true
    

    Output:

    Locations: UTC UTC
    Location pointers: 0x1e2100 0x1e2100
    Locations equal: true
    true
    true
    

    Try it on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集