Alex Rivera | Logout

ruby get Time in given timezone

Asked 2010-05-28T06:50:07.517
41

In ruby, how can I get current time in a given timezone? I know the offset from UTC, and want to get the current time in the timezone with that offset.

Edit
Report

1 Answer

2

I tried a gem install active_support, it installed activesupport-3.00, which gave me an error:

"You don't have tzinfo installed in your application. Please add it to your Gemfile and run bundle install"

tzinfo is a gem that ActiveSupport uses -- it was a little cleaner for my purposes, doesn't have any external dependencies -- the downside is that it appears to leave everything looking like it is UTC, so if you want your timezones to look correct, gem install activerecord will install everything you need. I'm including this answer mostly for reference for others that run into the same issue / googlability.

(use gem install tzinfo) to install the gem

require 'tzinfo'
zone = TZInfo::Timezone.get('US/Eastern')
puts zone.now

there are a number of different ways to get the timezones, but you can see a list using

TZInfo::Timezone.all
answered 2012-07-19T00:05:40.550

Your Answer