代码之家  ›  专栏  ›  技术社区  ›  wscourge Kiran Balakrishnan

Time.new和Date.new.strftime(“%A”)为相同的参数返回不同的日期

  •  0
  • wscourge Kiran Balakrishnan  · 技术社区  · 8 年前

    解决 Unlucky Days

    require 'date'
    Time.new(1001,1,1).strftime("%A") # => Thursday
    Date.new(1001,1,1).strftime("%A") # => Wednesday
    

    不是同一天。正确的答案是 Time 一个。

    为什么?

    enter image description here

    1 回复  |  直到 8 年前
        1
  •  4
  •   elliotcm    8 年前

    Date 默认情况下使用儒略日历。当你在处理那些遥远的过去的约会时,你会在日历改革之前有奇怪的行为。

    irb(main):013:0> Time.new(1001,1,1).strftime("%A")
    => "Thursday"
    irb(main):014:0> Date.new(1001,1,1, Date::GREGORIAN).strftime("%A")
    => "Thursday"
    

    更多细节请参见: https://gist.github.com/pixeltrix/e2298822dd89d854444b

    推荐文章