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 }
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开发者_如何学C.in_zone(Time.zone.name)
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 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
精彩评论