diff options
Diffstat (limited to 'activesupport/lib/active_support/values/time_zone.rb')
-rw-r--r-- | activesupport/lib/active_support/values/time_zone.rb | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 836f469df7..e2d759aa50 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -1,3 +1,7 @@ +require 'active_support/core_ext/time' +require 'active_support/core_ext/date' +require 'active_support/core_ext/date_time' + # The TimeZone class serves as a wrapper around TZInfo::Timezone instances. It allows us to do the following: # # * Limit the set of zones provided by TZInfo to a meaningful subset of 142 zones. @@ -170,6 +174,20 @@ module ActiveSupport MAPPING.freeze end + UTC_OFFSET_WITH_COLON = '%+03d:%02d' + UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.sub(':', '') + + # Assumes self represents an offset from UTC in seconds (as returned from Time#utc_offset) + # and turns this into an +HH:MM formatted string. Example: + # + # TimeZone.seconds_to_utc_offset(-21_600) # => "-06:00" + def self.seconds_to_utc_offset(seconds, colon = true) + format = colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON + hours = seconds / 3600 + minutes = (seconds.abs % 3600) / 60 + format % [hours, minutes] + end + include Comparable attr_reader :name @@ -190,7 +208,7 @@ module ActiveSupport # Returns the offset of this time zone as a formatted string, of the # format "+HH:MM". def formatted_offset(colon=true, alternate_utc_string = nil) - utc_offset == 0 && alternate_utc_string || utc_offset.to_utc_offset_s(colon) + utc_offset == 0 && alternate_utc_string || self.class.seconds_to_utc_offset(utc_offset, colon) end # Compare this time zone to the parameter. The two are comapred first on @@ -345,14 +363,13 @@ module ActiveSupport "Wellington" ], [ 46_800, "Nuku'alofa" ]]. each do |offset, *places| - places.each do |place| + places.sort.each do |place| place.freeze zone = new(place, offset) ZONES << zone ZONES_MAP[place] = zone end end - ZONES.sort! ZONES.freeze ZONES_MAP.freeze |