Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.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.

like image 835
Lance Avatar asked Mar 21 '11 15:03

Lance


1 Answers

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
like image 67
idlefingers Avatar answered Nov 16 '22 17:11

idlefingers