jhnatr
jhnatr

Reputation: 533

Rails Conversion from ActiveSupport::TimeZone to Country

Given an ActiveSupport::TimeZone object, I'd like to retrieve a country name or code. Is there anything built into rails that can do this? Any gems? I don't see anything in the Rails documentation that jumps out at me as providing this conversion. I understand that there is some ambiguity here, but am just looking for the country of the city specified in the object.

Upvotes: 1

Views: 1743

Answers (2)

tenzin dorjee
tenzin dorjee

Reputation: 326

Instead of using ActiveSupport::TimeZone, you can use TZInfo::Country.get('AU'), where AU is a country code. refer this link get country from time zone

Upvotes: 1

Tom Kadwill
Tom Kadwill

Reputation: 1478

ActiveSupport::TimeZone is a class concerned with timezones, it is not specific to country. Therefore, you can't get the country name or code.

However, ActiveSupport::TimeZone can return its name, which may or may not be a country. For example:

> Time.zone
=> #<ActiveSupport::TimeZone:0x007fdf7d526660 @name="UTC", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @current_period=#<TZInfo::TimezonePeriod: nil,nil,#<TZInfo::TimezoneOffset: 0,0,UTC>>>>
> Time.zone = 'Hawaii'
> Time.zone.name
=> "Hawaii"

Upvotes: 0

Related Questions