Understanding US/Pacific #6

Closed
hemantsingal opened this Issue Oct 14, 2013 · 5 comments

Comments

Projects
None yet
2 participants
@hemantsingal

How do I get rails time zone from "US/Pacific"?
Rails is happy to convert "America/Los_Angeles" but it does not understand obsolete time zones.

I get obsolete time zones in an api I have exposed to iOS and I have no way to convert "US/Pacific" to rails ActiveSupport::TimeZone unlike "America/Los_Angeles" which I can understand through mapping it provides.

@philr

This comment has been minimized.

Show comment
Hide comment
@philr

philr Oct 15, 2013

Member

You should be able to access the access the US/Pacific zone using ActiveSupport::TimeZone, for example:

> ActiveSupport::TimeZone['US/Pacific'].now
=> Tue, 15 Oct 2013 14:46:57 PDT -07:00 

Any zone identifier that isn't included in the ActiveSupport mapping will be looked up in TZInfo (it calls TZInfo::Timezone.get if it doesn't recognize the identifier).

Are you using an old Rails release? Rails version 2.3 and earlier included a cut-down version of TZInfo with just the zones required for the ActiveSupport mapping (therefore omitting US/Pacific). To use zones that aren't defined in ActiveSupport, you'll need to install the tzinfo gem. Version 0.3.38 is the latest version of tzinfo that is compatible with Rails 2.3.

Member

philr commented Oct 15, 2013

You should be able to access the access the US/Pacific zone using ActiveSupport::TimeZone, for example:

> ActiveSupport::TimeZone['US/Pacific'].now
=> Tue, 15 Oct 2013 14:46:57 PDT -07:00 

Any zone identifier that isn't included in the ActiveSupport mapping will be looked up in TZInfo (it calls TZInfo::Timezone.get if it doesn't recognize the identifier).

Are you using an old Rails release? Rails version 2.3 and earlier included a cut-down version of TZInfo with just the zones required for the ActiveSupport mapping (therefore omitting US/Pacific). To use zones that aren't defined in ActiveSupport, you'll need to install the tzinfo gem. Version 0.3.38 is the latest version of tzinfo that is compatible with Rails 2.3.

@philr philr closed this Oct 15, 2013

@hemantsingal

This comment has been minimized.

Show comment
Hide comment
@hemantsingal

hemantsingal Oct 16, 2013

I want to get Rails TimeZone name like I can get from "America/Los_Angeles" by calling:
ActiveSupport::TimeZone::MAPPING.select {|k, v| v == @user_params['time_zone'] }.keys.first

Is there a way to do this for "US/Pacific"?

I want to get Rails TimeZone name like I can get from "America/Los_Angeles" by calling:
ActiveSupport::TimeZone::MAPPING.select {|k, v| v == @user_params['time_zone'] }.keys.first

Is there a way to do this for "US/Pacific"?

@philr

This comment has been minimized.

Show comment
Hide comment
@philr

philr Oct 16, 2013

Member

I don't think there would be a straight-forward way to do this in the general case.

If you're only using a limited number of zones, you may be best off manually working out what the best equivalent zone in Rails is for each of them.

If you check the IANA time zone database, you'll see that US/Pacific is just an alias for America/Los_Angeles (see https://github.com/eggert/tz/blob/master/backward). In other cases though, you may be using time zones that have no equivalent in Rails (either because the current rules are different or because historical rules are different).

Member

philr commented Oct 16, 2013

I don't think there would be a straight-forward way to do this in the general case.

If you're only using a limited number of zones, you may be best off manually working out what the best equivalent zone in Rails is for each of them.

If you check the IANA time zone database, you'll see that US/Pacific is just an alias for America/Los_Angeles (see https://github.com/eggert/tz/blob/master/backward). In other cases though, you may be using time zones that have no equivalent in Rails (either because the current rules are different or because historical rules are different).

@hemantsingal

This comment has been minimized.

Show comment
Hide comment
@hemantsingal

hemantsingal Oct 16, 2013

Right. Is there a way to figure out that US/Pacific is just an alias for America/Los_Angeles?
I can worry about timezones which are neither alias nor in standard list separately.

Right. Is there a way to figure out that US/Pacific is just an alias for America/Los_Angeles?
I can worry about timezones which are neither alias nor in standard list separately.

@philr

This comment has been minimized.

Show comment
Hide comment
@philr

philr Oct 16, 2013

Member

The following function will return the target of a linked zone:

require 'tzinfo'

def linked_to(identifier)
  zone = TZInfo::Timezone.get(identifier)

  if zone.kind_of?(TZInfo::LinkedTimezone)
    link = zone.send(:info).link_to_identifier
    linked_to(link) || link
  else
    nil
  end
end

For example:

> linked_to('US/Pacific')  # a linked zone, returns the target of the link
=> "America/Los_Angeles" 
> linked_to('America/Los_Angeles')  # a fully defined zone, returns nil
=> nil

The code will work with either TZInfo version 0.3.x, or with TZinfo >= 1.0.0 when TZInfo::Data (the tzinfo-data gem) is also installed.

Please note that this uses undocumented and unsupported interfaces in TZInfo, so I would only recommend using it as a one off (i.e. please don't rely on it continuing to work with future TZInfo releases).

Member

philr commented Oct 16, 2013

The following function will return the target of a linked zone:

require 'tzinfo'

def linked_to(identifier)
  zone = TZInfo::Timezone.get(identifier)

  if zone.kind_of?(TZInfo::LinkedTimezone)
    link = zone.send(:info).link_to_identifier
    linked_to(link) || link
  else
    nil
  end
end

For example:

> linked_to('US/Pacific')  # a linked zone, returns the target of the link
=> "America/Los_Angeles" 
> linked_to('America/Los_Angeles')  # a fully defined zone, returns nil
=> nil

The code will work with either TZInfo version 0.3.x, or with TZinfo >= 1.0.0 when TZInfo::Data (the tzinfo-data gem) is also installed.

Please note that this uses undocumented and unsupported interfaces in TZInfo, so I would only recommend using it as a one off (i.e. please don't rely on it continuing to work with future TZInfo releases).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment