【问题标题】:There's one line of Reddit's code (the ranking feature) made me confusedReddit 的一行代码(排名功能)让我很困惑
【发布时间】:2012-12-15 16:26:25
【问题描述】:

代码来自这个页面: https://github.com/reddit/reddit/blob/master/r2/r2/lib/db/_sorts.pyx

这里是sn-p的代码:

cpdef double epoch_seconds(date):
"""Returns the number of seconds from the epoch to date. Should
   match the number returned by the equivalent function in
   postgres."""
td = date - epoch
return td.days * 86400 + td.seconds + (float(td.microseconds) / 1000000)

我认为td.days * 86400 应该等于td.seconds 以及(float(td.microseconds) / 1000000),我想知道他们为什么不简单地将 td.seconds 乘以 3?

【问题讨论】:

    标签: python reddit


    【解决方案1】:

    td.days 是整数天数,一天中的任何部分都会被截断。同样,td.seconds 是该“一天的小数”内的整数秒数,任何一秒的小数都会被截断。所以你必须把组件拼凑起来。

    【讨论】:

      【解决方案2】:

      你的假设是错误的,这就是为什么这看起来很奇怪。 td.days 包含自您正确假设的纪元以来的天数,但 td.seconds 和 td.microseconds 分别包含自一天开始以来的秒数和自一秒开始以来的微秒数。因此,返回值变为自纪元以来的秒数,逗号后面的部分为微秒。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-01-12
        • 1970-01-01
        • 1970-01-01
        • 2014-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多