代码之家  ›  专栏  ›  技术社区  ›  dreeves

带有数字参数的绝对时间行为异常

  •  0
  • dreeves  · 技术社区  · 15 年前

    这很奇怪:

    DateList@AbsoluteTime[596523]
    

    收益率

    {2078, 7, 2, 2, 42, 9.7849}
    

    但是

    DateList@AbsoluteTime[596524]
    

    收益率

    {1942, 5, 26, 20, 28, 39.5596}
    

    问题是:怎么了? 请注意,带有数字参数的绝对时间是未记录的。

    (我想我现在知道它在做什么了,但我认为这是有用的,作为一个StackOverflow问题,以供将来参考;我很好奇,是否有什么原因导致这个神奇的596523数字。)

    PS:在Mathematica中编写这些用于转换Unix时间和从Unix时间转换的实用程序函数时遇到了这个问题:

    (* Using Unix time (an integer) instead of Mathematica's AbsoluteTime...      *)
    tm[x___] := AbsoluteTime[x]            (* tm is an alias for AbsoluteTime.    *)
    uepoch = tm[{1970}, TimeZone->0];      (* unixtm works analogously to tm.     *)
    unixtm[x___] := Round[tm[x]-uepoch]    (* tm & unixtm convert between unix &  *)
    unixtm[x_?NumericQ] := Round[x-uepoch] (*  mma epoch time when given numeric  *)
    tm[t_?NumericQ] := t+uepoch            (*  args. Ie, they're inverses.        *)
    
    2 回复  |  直到 14 年前
        1
  •  5
  •   ragfield    15 年前

    如果将596524和596523从小时转换为秒(乘以3600),您将看到较大的数字大于2^31-1(最大32位带符号整数值),而较小的数字则不是。

        2
  •  2
  •   dreeves    14 年前

    打电话 AbsoluteTime[x] 其中x是整数,是(未记录)的快捷方式 AbsoluteTime[TimeZone->x] . 那么x的正常值应该是-12到+12,但是如果你告诉它的话,它可以很高兴地从UTC加上或减去50万。 我不知道为什么它突然在596524发疯了。 但我想一个合理的答案是“不要这样做!”.

    附:另一个答案揭示了魔法的奥秘596524小时。它是 (2^31-1)/3600 ,即可以存储在无符号32位整数中的最大秒数。