既然Date.today
使用系统时间,那么最好的方法是什么它返回与Time.zone.now.to_date
相同的内容,假设Time.zone
设置?
我知道我可以覆盖 Date 类,但不确定是否还有其他我错过了 Ruby 约定,因为我确定我不是第一个遇到此问题。
您正在寻找Date.current
,如果设置了Time.zone
,它将调用Time.zone.today
。这相当于Date
Time.current
(还有DateTime.current
)。
还要记住,这些方法都不在 Ruby 核心中,而是由 ActiveSupport 提供的。