aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/values/time_zone.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-04-22 15:26:03 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-04-22 15:26:03 +0100
commit5f3f100ce2d689480da85abc88e5e940cf90189e (patch)
tree15c1a05a5308a9eea56d7f0889ac46d9cac5b57c /activesupport/lib/active_support/values/time_zone.rb
parentd758d996d1b66e2a65640f79f01ce2ac674d7ed5 (diff)
parentca49299434bc764b667cd86846d892e91a150ef3 (diff)
downloadrails-5f3f100ce2d689480da85abc88e5e940cf90189e.tar.gz
rails-5f3f100ce2d689480da85abc88e5e940cf90189e.tar.bz2
rails-5f3f100ce2d689480da85abc88e5e940cf90189e.zip
Merge branch 'master' into active_model
Conflicts: activeresource/lib/active_resource/validations.rb
Diffstat (limited to 'activesupport/lib/active_support/values/time_zone.rb')
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb23
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