【问题标题】:Chronic not selecting correct 1st monday of the month慢性未选择正确的每月第一个星期一
【发布时间】:2020-06-08 16:24:38
【问题描述】:
def monday?
  require 'chronic'
  today = Date.today
  1st_monday = Chronic.parse('1st monday of this month', now: today.beginning_of_month).to_date
  3rd_monday = Chronic.parse('3th monday of this month', now: today.beginning_of_month).to_date
  return today == 1st_monday || today == 3rd_monday
end

我已经定义了这个方法来选择每个月的第一个或第三个星期一。今天出现了这个问题,因为 Chronic 选择 6 月 8 日作为本月的第一个星期一,这是错误的,因为 6 月 1 日是第一个星期一。知道如何解决这个问题吗? 会不会是因为这个月有五个星期一,让慢性病搞混了?

【问题讨论】:

  • 有趣的是Chronic.parse('1st monday of this June') 工作正常。也许可以试试Chronic.parse("1st monday of this #{today.strftime('%B')}") 之类的东西,但实际上并不需要这样做。
  • 看起来像是 Chronic 上存在的从未解决的错误:github.com/mojombo/chronic/issues/295
  • 这是关于 Rails,而不是 Ruby。

标签: ruby-on-rails scheduled-tasks chronic


【解决方案1】:

似乎是慢性宝石中的一个错误。猜猜你必须使用这个解决方案:

month = Date.today.strftime('%B')
=>"June"

Chronic.parse("3st monday of this #{month}")

=> 2020-06-15 12:00:00 -0400

【讨论】:

  • 我每天使用sidekiq运行这个方法,这个方法检查它是第一个星期一还是第三个
  • 查看更新的答案,如果过去无法解析,则返回 nil,因此您可以回退到未来。
  • Chronic.parse('本月第三个星期一', context: :past) || Chronic.parse('3rd monday of this month') 我试过了,它返回了 6 月 29 日,即本月的第 5 个星期一。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 2017-09-25
相关资源
最近更新 更多