Skip to content

Instantly share code, notes, and snippets.

@ch1ago
Created May 15, 2016 13:26
Show Gist options
  • Save ch1ago/05d245d2b4c4b4898cf891b5d2e96692 to your computer and use it in GitHub Desktop.
Save ch1ago/05d245d2b4c4b4898cf891b5d2e96692 to your computer and use it in GitHub Desktop.
timezone:
- info
source for TZInfo is 'cat /usr/share/zoneinfo/zone.tab'
rails uses this as standard ::ActiveSupport::TimeZone.all
rake time:zones:all
- test:
raises an error
Time.zone='Fuck'
gem open tzinfo
- https://github.com/rails/rails/blob/master/activesupport/lib/active_support/values/time_zone.rb
132 ActiveSupport::TimeZone::MAPPING.values.uniq
149 ActiveSupport::TimeZone::MAPPING.values
149 ActiveSupport::TimeZone.send(:zones_map).values.map(&:name)
149 ActiveSupport::TimeZone.all.map(&:name)
- https://github.com/tzinfo/tzinfo/blob/master/lib/tzinfo/timezone.rb
416 TZInfo::Timezone.all_country_zones.size
583 TZInfo::Timezone.all_identifiers
016 TZInfo::Country.get('BR').zones
029 TZInfo::Country.get('US').zones
034 ::ActiveSupport::TimeZone.all.uniq(&:formatted_offset)
034 ::ActiveSupport::TimeZone.all.uniq(&:formatted_offset).map(&:formatted_offset)
- config
config.time_zones = ["UTC", "Midway Island", "American Samoa", "Hawaii", "Alaska", "Pacific Time (US & Canada)"]
config.time_zones = ActiveSupport::TimeZone.send(:zones_map).values.map(&:name)
- Hello
Hello.available_timezones
Hello.all_available_timezones - shortcut for one of the above commands
to work on rails
- externalize the filtering logic found in form helpers to methods of ActiveSupport::TimeZone
# Returns select and option tags for the given object and method, using
# #time_zone_options_for_select to generate the list of option tags.
#
# In addition to the <tt>:include_blank</tt> option documented above,
# this method also supports a <tt>:model</tt> option, which defaults
# to ActiveSupport::TimeZone. This may be used by users to specify a
# different time zone model object. (See +time_zone_options_for_select+
# for more information.)
#
# You can also supply an array of ActiveSupport::TimeZone objects
# as +priority_zones+, so that they will be listed above the rest of the
# (long) list. (You can use ActiveSupport::TimeZone.us_zones as a convenience
# for obtaining a list of the US time zones, or a Regexp to select the zones
# of your choice)
#
# Finally, this method supports a <tt>:default</tt> option, which selects
# a default ActiveSupport::TimeZone if the object's time zone is +nil+.
#
# time_zone_select( "user", "time_zone", nil, include_blank: true)
#
# time_zone_select( "user", "time_zone", nil, default: "Pacific Time (US & Canada)" )
#
# time_zone_select( "user", 'time_zone', ActiveSupport::TimeZone.us_zones, default: "Pacific Time (US & Canada)")
#
# time_zone_select( "user", 'time_zone', [ ActiveSupport::TimeZone['Alaska'], ActiveSupport::TimeZone['Hawaii'] ])
#
# time_zone_select( "user", 'time_zone', /Australia/)
#
# time_zone_select( "user", "time_zone", ActiveSupport::TimeZone.all.sort, model: ActiveSupport::TimeZone)
def time_zone_select(object, method, priority_zones = nil, options = {}, html_options = {})
Tags::TimeZoneSelect.new(object, method, self, priority_zones, options, html_options).render
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment