dsieczko
dsieczko

Reputation: 423

How to get tzinfo from ActiveSupport::TimeZones.zones_map?

I'm trying to retrieve an array of all time zones tzinfo but I only need the "America/Los_Angeles" part from each so I can validate timezones being received from an iOS mobile client. Currently running the following:

ActiveSupport::TimeZone.zones_map.values.collect { |z| z.tzinfo }

I get the class TZInfo::TimezoneProxy along with the info I need.

Upvotes: 2

Views: 2115

Answers (3)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241555

The zones in ActiveSupport::Timezones are limited to a subset of 146 "meaningful" zones, though they do not define by what critera they determine which zones are "meaningful".

If you use it to validate IANA time zone identifiers coming from another platform, you will certainly have false negatives, as there are over 500 of them.

Instead, use the TZinfo Gem directly, rather than Rail's strange mutated version of it. See also the section on Rails Time Zone Identifiers in the timezone tag wiki.

Upvotes: 1

Alexander
Alexander

Reputation: 2558

Try following code:

ActiveSupport::TimeZone::MAPPING.values

Upvotes: 4

dsieczko
dsieczko

Reputation: 423

Found the instance method "identifier" in the documentation - http://www.rubydoc.info/gems/tzinfo/TZInfo/TimezoneProxy#_load-class_method

So all I needed was

ActiveSupport::TimeZone.zones_map.values.collect{|z| z.tzinfo.identifier }

Upvotes: 0

Related Questions