1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Update docs for formatted_offset

Output of `formatted_offset` is depends on input so it’s not always in +HH:MM format. Possible outputs are “+5:30”, “+530” or provided alternate UTC string [ci skip]
This commit is contained in:
Ronak Jangir 2015-09-28 23:19:09 +05:30
parent 3c52b2000c
commit 25607697bc
3 changed files with 10 additions and 3 deletions

View file

@ -40,6 +40,8 @@ class DateTime
alias_method :to_default_s, :to_s if instance_methods(false).include?(:to_s) alias_method :to_default_s, :to_s if instance_methods(false).include?(:to_s)
alias_method :to_s, :to_formatted_s alias_method :to_s, :to_formatted_s
# Returns a formatted string of the offset from UTC, or an alternative
# string if the time zone is already UTC.
# #
# datetime = DateTime.civil(2000, 1, 1, 0, 0, 0, Rational(-6, 24)) # datetime = DateTime.civil(2000, 1, 1, 0, 0, 0, Rational(-6, 24))
# datetime.formatted_offset # => "-06:00" # datetime.formatted_offset # => "-06:00"

View file

@ -55,7 +55,8 @@ class Time
alias_method :to_default_s, :to_s alias_method :to_default_s, :to_s
alias_method :to_s, :to_formatted_s alias_method :to_s, :to_formatted_s
# Returns the UTC offset as an +HH:MM formatted string. # Returns a formatted string of the offset from UTC, or an alternative
# string if the time zone is already UTC.
# #
# Time.local(2000).formatted_offset # => "-06:00" # Time.local(2000).formatted_offset # => "-06:00"
# Time.local(2000).formatted_offset(false) # => "-0600" # Time.local(2000).formatted_offset(false) # => "-0600"

View file

@ -285,8 +285,12 @@ module ActiveSupport
end end
end end
# Returns the offset of this time zone as a formatted string, of the # Returns a formatted string of the offset from UTC, or an alternative
# format "+HH:MM". # string if the time zone is already UTC.
#
# zone = ActiveSupport::TimeZone['Central Time (US & Canada)']
# zone.formatted_offset # => "-06:00"
# zone.formatted_offset(false) # => "-0600"
def formatted_offset(colon=true, alternate_utc_string = nil) def formatted_offset(colon=true, alternate_utc_string = nil)
utc_offset == 0 && alternate_utc_string || self.class.seconds_to_utc_offset(utc_offset, colon) utc_offset == 0 && alternate_utc_string || self.class.seconds_to_utc_offset(utc_offset, colon)
end end