gpt4 book ai didi

ruby-on-rails - 如何检查 Rails 中某个范围内是否包含日期时间?

转载 作者:行者123 更新时间:2023-12-05 04:13:31 25 4
gpt4 key购买 nike

使用 Ruby 的 Range#include?具有日期时间范围会引发异常:

> (Time.zone.now.beginning_of_day..Time.zone.now.end_of_day).include?(Time.zone.now)
TypeError: can't iterate from ActiveSupport::TimeWithZone

在使用 Rails 时 Range#overlaps?作品:

> (Time.zone.now.beginning_of_day..Time.zone.now.end_of_day).overlaps?(Time.zone.now..Time.zone.now)
=> true

我不喜欢这样一个事实,即我必须创建一个单元素范围以使用 overlaps? 检查是否包含 有没有一种方法可以将 include? 与日期时间范围一起使用在 Rails 中?

最佳答案

啊哈!

这是怎么回事?

Time.zone.now.between?(Time.zone.now.beginning_of_day,Time.zone.now.end_of_day)

结果:

2.3.0 :001 > Time.zone.now.between?(Time.zone.now.beginning_of_day,Time.zone.tomorrow.end_of_day)
=> true
2.3.0 :002 > Time.zone.tomorrow.between?(Time.zone.now.beginning_of_day,Time.zone.now.end_of_day)
=> false
2.3.0 :003 > Time.zone.yesterday.between?(Time.zone.now.beginning_of_day,Time.zone.now.end_of_day)

引用:http://apidock.com/rails/ActiveSupport/TimeWithZone/between%3F

关于ruby-on-rails - 如何检查 Rails 中某个范围内是否包含日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37426125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com