8

rails中的Date.today和Date.current区别

 3 years ago
source link: http://blog.ilibrary.me/2020/08/25/rails%E4%B8%AD%E7%9A%84date-today%E5%92%8Cdate-current%E5%8C%BA%E5%88%AB
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
rails中的Date.today和Date.current区别 | 垂钓江湖
欢迎转载,请支持原创,保留原文链接:blog.ilibrary.me

Date.today是UTC时间, Date.current是locale时间, Time.current也是locale时间, 1.days.after也是locale时间。所以不要用Date.today, 用另外3种形式. 也不要用Time.now.

在console里面执行$ Date.current, 可以看到如下代码:

def current
  ::Time.zone ? ::Time.zone.today : ::Date.today
end

Date.current会判断时区,然后计算好时区以后返回当地时间。

在console里面执行$ 1.days.later, 可以看到如下代码:

def since(time = ::Time.current)
  sum(1, time)
end

这个也是基于时区来计算的,大部分情况下我们需要的就是基于时区的时间。

注: 美元符查看源码$ Date.currentpry的功能, 需要安装gem pry之后才能正常使用。

大部分情况下我们应该用Date.current, 1.days.afterTime.current,不要用Date.today, 也不要用Time.now.

Rails里面没有Date.now.

扫描二维码分享到微信朋友圈Loading...Please waitqrcode.php?url=%2F2020%2F08%2F25%2Frails%25E4%25B8%25AD%25E7%259A%2584date-today%25E5%2592%258Cdate-current%25E5%258C%25BA%25E5%2588%25AB

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK