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

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

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

In which classes should the method be added in_zoneso that I can do the following without causing too much confusion?

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

Or is it already built in? I am interested because I use Time.zone.noweverywhere and would like an easier way to do this in these other cases.

+5
source share
1 answer

Rails has a method in_time_zonethat does this for you. It works with any objects Time, Dateor ActiveSupport::TimeWithZone..

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

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

rake time:zones:all
+12
source

All Articles