1.week.ago.in_zone(Time.zone) for Rails? [英] 1.week.ago.in_zone(Time.zone) for Rails?

查看:29
本文介绍了1.week.ago.in_zone(Time.zone) for Rails?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不这样做的情况下在特定时区使用那些花哨的时间助手方法?

Is there a way to use those fancy time helper methods in specific time zones without doing this?

Time.use_zone(Time.zone.name) { 1.week.ago }

我应该向哪个类添加 in_zone 方法,以便我可以在不引起太多混乱的情况下执行以下操作?

What class(es) should I add a in_zone method to so I can do the following without causing too much confusion?

1.week.ago.in_zone(Time.zone.name)

还是已经内置了?我想知道,因为我在任何地方都使用 Time.zone.now 并且想要一种更简单的方法来处理其他情况.

Or is this already built in? I'm wondering because I use Time.zone.now everywhere and would like a simpler way of doing it on those other cases.

推荐答案

Rails 有一个方法叫做 in_time_zone 为您执行此操作.它适用于任何 TimeDateActiveSupport::TimeWithZone 对象..

Rails has a method called in_time_zone which does this for you. It works on any Time, Date or ActiveSupport::TimeWithZone object..

1.week.ago.in_time_zone("Tokyo")

您可以通过运行以下命令获取所有可用时区的列表:

You can get a list of all the available time zones by running:

rake time:zones:all

这篇关于1.week.ago.in_zone(Time.zone) for Rails?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆