lib/lazier/datetime.rb in lazier-1.0.6 vs lib/lazier/datetime.rb in lazier-1.0.7

- old
+ new

@@ -1,8 +1,8 @@ # encoding: utf-8 # -# This file is part of the lazier gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>. +# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>. # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php. # module Lazier # Extensions for date and time objects. @@ -182,11 +182,11 @@ end # Returns the number of months passed between the beginning of the base year and the current date. # # ```ruby - # DateTime.civil(2012, 6, 1).in_months(2011) + # DateTime.civil(2013, 6, 1).in_months(2011) # # => 18 # ``` # # @param base [DateTime] The base year to start computation from. Default to current year. # @return [Fixnum] Returns the number of months passed between the beginning of the base year and the current date. @@ -422,10 +422,23 @@ rv = (self.period_for_utc(date.utc).dst? ? self.dst_offset : self.offset) rational ? self.class.rationalize_offset(rv) : rv end + # Return the current alias for this timezone. + def current_alias + identifier = self.tzinfo.identifier + + catch(:alias) do + self.aliases.each do |a| + throw(:alias, a) if a == identifier + end + + self.aliases.first + end + end + # Returns the standard offset for this timezone. # # @param rational [Boolean] If to return the offset as a Rational. # @return [Fixnum|Rational] The offset of this timezone. def offset(rational = false) @@ -499,11 +512,11 @@ # # @param name [String] The name to use for this zone. Defaults to the zone name. # @param colon [Boolean] If to put the colon in the output string. # @return [String] The name for this zone. def to_str(name = nil, colon = true) - name ||= self.aliases.first + name ||= self.current_alias "(GMT#{self.formatted_offset(colon)}) #{name}" end # Returns a string representation for this zone with Daylight Saving Time (DST) active. # @@ -511,10 +524,10 @@ # @param year [Fixnum] The year to which refer to. Defaults to the current year. # @param name [String] The name to use for this zone. Defaults to the zone name. # @return [String] The string representation for the zone with DST or `nil`, if the timezone doesn't use DST for that year. def to_str_with_dst(dst_label = nil, year = nil, name = nil) dst_label ||= "(DST)" - name ||= self.aliases.first + name ||= self.current_alias if self.uses_dst?(year) then period = self.dst_period(year) offset = self.class.seconds_to_utc_offset(period.utc_total_offset) "(GMT#{offset}) #{name} #{dst_label}" \ No newline at end of file