Way of getting canonical zone without using private methods? #16

Closed
97jaz opened this Issue Feb 14, 2014 · 3 comments

Projects

None yet

2 participants

97jaz commented Feb 14, 2014

It would be useful to be able to canonicalize any instance of TZInfo::Timezone. So, for any linked zone lz, lz.canonical_zone would return the data zone to which it is linked. For any data zone, it would return itself.

Currently, I get can this information, but it seems to require, first, determining whether the zone in question is a linked zone (based on its class) and, second, using the private method info to get at the identifier of its data zone.

In the master branch, it looks like I could load the info from the data source without using private interfaces. However, my dependencies are keeping me on the 0.3 branch for now.

97jaz commented Feb 14, 2014

For my current purposes, I only need the canonical identifier, so I've added the following monkey patch to my project:

require "tzinfo"

module TZInfo
  class DataTimezone
    def canonical_identifier
      identifier
    end
  end

  class LinkedTimezone
    def canonical_identifier
      info.link_to_identifier
    end
  end

  class TimezoneProxy
    def canonical_identifier
      real_timezone.canonical_identifier
    end
  end
end
Owner
philr commented Feb 15, 2014

Thanks for the suggestion. I'll take a look at adding canonical_identifier and/or canonical_zone methods to the TZInfo::Timezone interface. Note that these methods would only be meaningful when used with the TZInfo::Data :ruby data source. Information about links is not present in the zoneinfo files that are used by the :zoneinfo data source supported by TZInfo >= 1.0.0.

Your monkey patch looks to be correct. I might change the LinkedTimezone#canonical_identifier implementation to the following though:

class LinkedTimezone
  def canonical_identifier
    @linked_timezone.canonical_identifier
  end
end

This would allow for the possibility of links via another LinkedTimezone (e.g. LinkedZone1 -> LinkedZone2 -> DataZone). There are no such linked zones in the time zone database at present, and I doubt that any would be added, so this is slightly academic.

@philr philr added the enhancement label Feb 15, 2014
@philr philr self-assigned this Feb 15, 2014
97jaz commented Feb 17, 2014

Thanks, and I like your improvement of LinkedTimezone#canonical_identifier.

@philr philr closed this in 3d84ce4 May 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment