aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/values/time_zone.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-03-23 22:01:51 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-03-23 22:01:51 -0700
commite094940c2b5a5163504859efd3a0bcc49c40451e (patch)
tree999fba1608b264fb169da1e5682a5dba9208e29f /activesupport/lib/active_support/values/time_zone.rb
parent0bd668fd8867b01c15f943613707b3c7eb59afff (diff)
downloadrails-e094940c2b5a5163504859efd3a0bcc49c40451e.tar.gz
rails-e094940c2b5a5163504859efd3a0bcc49c40451e.tar.bz2
rails-e094940c2b5a5163504859efd3a0bcc49c40451e.zip
Move Numeric#to_utc_offset_s to TimeZone.seconds_to_utc_offset
Diffstat (limited to 'activesupport/lib/active_support/values/time_zone.rb')
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb16
1 files changed, 15 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb
index 836f469df7..516ef2d8f0 100644
--- a/activesupport/lib/active_support/values/time_zone.rb
+++ b/activesupport/lib/active_support/values/time_zone.rb
@@ -170,6 +170,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 +204,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