Search code examples
ruby-on-railsrubygraphql-ruby

ActiveSupport TimeZone not returning all zones


I'm having an issue like this. Not all zones are returning with:

ActiveSupport::TimeZone.all.sort_by {|t| t.name}.map { |tz|
  #symbol = tz.tzinfo.identifier.gsub(/[^_a-zA-Z0-9]/, '_').squeeze('_').upcase!
  
  tz.to_s #> (GMT+00:00) Edinburgh for example
}

I need to use the .to_s to get the UTC (GMT+00:00). With the above, London is missing and I assume others. This one works great:

ActiveSupport::TimeZone::MAPPING.sort_by {|k,v| k}.map { |k,v|
  #symbol = k.gsub(/[^_a-zA-Z0-9]/, '_').squeeze('_').upcase!
  
  k #> London London is included with this method
}

I cannot use this method because I do not know how to get the (GMT+00:00) in (GMT+00:00) London

Has the bug return? How to get all the zones show for the first example?

Edit.

I'm using GraphQL-ruby. I've created an enum to return a list of time zones:

# Taken from: https://gist.github.com/pedrocarmona/f41d25e631c1144045971c319f1c9e17
class Types::TimeZoneEnumType < Types::BaseEnum
  ActiveSupport::TimeZone.all.sort_by {|t| t.name}.map { |tz|
    symbol = tz.tzinfo.identifier.gsub(/[^_a-zA-Z0-9]/, '_').squeeze('_').upcase
    value("TZ_#{symbol}", tz.to_s)
  }
end

Then inside query_type.rb

[..]

field :time_zones, Types::TimeZoneEnumType, null: false

[..]

Next, inside graphiql, I make the query:

query timeZones{
  __type(name: "TimeZoneEnum") {
    enumValues {
      name
      description
    }
  }
}

Which returns something like, except London:

[
  [..]

  {
    "name": "TZ_AMERICA_LA_PAZ",
    "description": "(GMT-04:00) La Paz"
  },
  {
    "name": "TZ_AMERICA_LIMA",
    "description": "(GMT-05:00) Lima"
  },
  {
    "name": "TZ_EUROPE_LISBON",
    "description": "(GMT+00:00) Lisbon"
  },
  {
    "name": "TZ_EUROPE_LJUBLJANA",
    "description": "(GMT+01:00) Ljubljana"
  },
  {
    "name": "TZ_EUROPE_MADRID",
    "description": "(GMT+01:00) Madrid"
  },
  
  [..]
]

After Ljubljana I should see "London" but it's not there.


Solution

  • If I run

    ActiveSupport::TimeZone.all.sort_by {|t| t.name}.map { |tz| 
      [ tz.tzinfo.identifier.gsub(/[^_a-zA-Z0-9]/, '_').squeeze('_').upcase, tz.to_s ] 
    }.sort
    

    the result includes the entries ["EUROPE_LONDON", "(GMT+00:00) Edinburgh"], ["EUROPE_LONDON", "(GMT+00:00) London"], i.e. EUROPE_LONDON is duplicated.

    I don't know how the GraphQL library is operating, but I'm assuming it's deduplicating the data and returning a single entry for EUROPE_LONDON (enums are normally unique). Moscow is the same - it has values for Moscow and St Petersburg - so you could test by looking at the results for EUROPE_MOSCOW.