Module: Lazier::TimeZone
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/lazier/timezone.rb
Overview
Extensions for timezone objects.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary (collapse)
-
- (Array) aliases
Returns a list of valid aliases (city names) for this timezone (basing on offset).
-
- (String) current_alias
Returns the current alias for this timezone.
-
- (Fixnum|Rational) current_offset(rational = false, date = nil)
Returns the current offset for this timezone, taking care of Daylight Saving Time (DST).
-
- (Fixnum|Rational) dst_correction(rational = false, year = nil)
Return the correction applied to the standard offset the timezone when the Daylight Saving Time (DST) is active.
-
- (String) dst_name(dst_label = nil, year = nil, name = nil)
Returns the name for this zone with Daylight Saving Time (DST) active.
-
- (Fixnum|Rational) dst_offset(rational = false, year = nil, method = :utc_total_offset)
Returns the standard offset for this timezone timezone when the Daylight Saving Time (DST) is active.
-
- (TimezonePeriod) dst_period(year = nil)
Gets a period for this timezone when the Daylight Saving Time (DST) is active (it takes care of different hemispheres).
-
- (Fixnum|Rational) offset(rational = false)
Returns the standard offset for this timezone.
-
- (String) to_str(name = nil, colon = true)
Returns the name for this zone with Daylight Saving Time (DST) active.
-
- (String) to_str_parameterized(with_offset = true, name = nil)
Returns a parameterized string representation for this zone.
-
- (String) to_str_with_dst(dst_label = nil, year = nil, name = nil)
Returns a string representation for this zone with Daylight Saving Time (DST) active.
-
- (String) to_str_with_dst_parameterized(dst_label = nil, year = nil, name = nil)
Returns a parameterized string representation for this zone with Daylight Saving Time (DST) active.
-
- (Boolean) uses_dst?(reference = nil)
Checks if the timezone uses Daylight Saving Time (DST) for that date or year.
Instance Method Details
- (Array) aliases
Returns a list of valid aliases (city names) for this timezone (basing on offset).
147 148 149 150 |
# File 'lib/lazier/timezone.rb', line 147 def aliases reference = self.class::MAPPING.fetch(name, name).gsub("_", " ") @aliases ||= ([reference] + self.class::MAPPING.map { |name, zone| format_alias(name, zone, reference) }).uniq.compact.sort end |
- (String) current_alias
Returns the current alias for this timezone.
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/lazier/timezone.rb', line 166 def current_alias identifier = tzinfo.identifier catch(:alias) do aliases.each do |a| throw(:alias, a) if a == identifier end aliases.first end end |
- (Fixnum|Rational) current_offset(rational = false, date = nil)
Returns the current offset for this timezone, taking care of Daylight Saving Time (DST).
157 158 159 160 161 |
# File 'lib/lazier/timezone.rb', line 157 def current_offset(rational = false, date = nil) date ||= ::DateTime.now rv = (period_for_utc(date.utc).dst? ? dst_offset : offset) rational ? self.class.rationalize_offset(rv) : rv end |
- (Fixnum|Rational) dst_correction(rational = false, year = nil)
Return the correction applied to the standard offset the timezone when the Daylight Saving Time (DST) is active.
218 219 220 |
# File 'lib/lazier/timezone.rb', line 218 def dst_correction(rational = false, year = nil) dst_offset(rational, year, :std_offset) end |
- (String) dst_name(dst_label = nil, year = nil, name = nil)
Returns the name for this zone with Daylight Saving Time (DST) active.
240 241 242 |
# File 'lib/lazier/timezone.rb', line 240 def dst_name(dst_label = nil, year = nil, name = nil) uses_dst?(year) ? "#{name || self.name} #{dst_label || "(DST)"}" : nil end |
- (Fixnum|Rational) dst_offset(rational = false, year = nil, method = :utc_total_offset)
Returns the standard offset for this timezone timezone when the Daylight Saving Time (DST) is active.
228 229 230 231 232 |
# File 'lib/lazier/timezone.rb', line 228 def dst_offset(rational = false, year = nil, method = :utc_total_offset) period = dst_period(year) rv = period ? period.send(method) : 0 rational ? self.class.rationalize_offset(rv) : rv end |
- (TimezonePeriod) dst_period(year = nil)
Gets a period for this timezone when the Daylight Saving Time (DST) is active (it takes care of different hemispheres).
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/lazier/timezone.rb', line 190 def dst_period(year = nil) year ||= ::Date.today.year northern_summer = ::DateTime.civil(year, 7, 15).utc # This is a representation of a summer period in the Northern Hemisphere. southern_summer = ::DateTime.civil(year, 1, 15).utc # This is a representation of a summer period in the Southern Hemisphere. period = period_for_utc(northern_summer) period = period_for_utc(southern_summer) unless period.dst? period.dst? ? period : nil end |
- (Fixnum|Rational) offset(rational = false)
Returns the standard offset for this timezone.
182 183 184 |
# File 'lib/lazier/timezone.rb', line 182 def offset(rational = false) rational ? self.class.rationalize_offset(utc_offset) : utc_offset end |
- (String) to_str(name = nil, colon = true)
Returns the name for this zone with Daylight Saving Time (DST) active.
249 250 251 |
# File 'lib/lazier/timezone.rb', line 249 def to_str(name = nil, colon = true) "(GMT#{formatted_offset(colon)}) #{name || current_alias}" end |
- (String) to_str_parameterized(with_offset = true, name = nil)
Returns a parameterized string representation for this zone.
268 269 270 |
# File 'lib/lazier/timezone.rb', line 268 def to_str_parameterized(with_offset = true, name = nil) ::ActiveSupport::TimeZone.parameterize_zone(name || to_str, with_offset) end |
- (String) to_str_with_dst(dst_label = nil, year = nil, name = nil)
Returns a string representation for this zone with Daylight Saving Time (DST) active.
259 260 261 |
# File 'lib/lazier/timezone.rb', line 259 def to_str_with_dst(dst_label = nil, year = nil, name = nil) uses_dst?(year) ? "(GMT#{self.class.seconds_to_utc_offset(dst_period(year).utc_total_offset)}) #{name || current_alias} #{dst_label || "(DST)"}" : nil end |
- (String) to_str_with_dst_parameterized(dst_label = nil, year = nil, name = nil)
Returns a parameterized string representation for this zone with Daylight Saving Time (DST) active.
278 279 280 281 |
# File 'lib/lazier/timezone.rb', line 278 def to_str_with_dst_parameterized(dst_label = nil, year = nil, name = nil) rv = to_str_with_dst(dst_label, year, name) rv ? ::ActiveSupport::TimeZone.parameterize_zone(rv) : nil end |
- (Boolean) uses_dst?(reference = nil)
Checks if the timezone uses Daylight Saving Time (DST) for that date or year.
205 206 207 208 209 210 211 |
# File 'lib/lazier/timezone.rb', line 205 def uses_dst?(reference = nil) if reference.respond_to?(:year) && reference.respond_to?(:utc) # This is a date like object dst_period(reference.year).present? && period_for_utc(reference.utc).dst? else dst_period(reference).present? end end |