Skip to content

Instantly share code, notes, and snippets.

@vyorkin
Last active August 29, 2015 14:12
Show Gist options
  • Save vyorkin/b607e34eeb0ac15ee8d9 to your computer and use it in GitHub Desktop.
Save vyorkin/b607e34eeb0ac15ee8d9 to your computer and use it in GitHub Desktop.
some dikiy pizdets
4.1.8@2.1.5 (main)> ru = TZInfo::Country.get('RU')
=> #<TZInfo::Country: RU>
4.1.8@2.1.5 (main)> ekb = ru.zones[5]
=> #<TZInfo::TimezoneProxy: Asia/Yekaterinburg>
4.1.8@2.1.5 (main)> z = MskTimeZone.create(ekb.name, ekb.current_period.utc_offset, ekb)
=> #<MskTimeZone:0x007f82f4547a30 @name="Asia/Yekaterinburg", @utc_offset=18000, @tzinfo=#<TZInfo::TimezoneProxy: Asia/Yekaterinburg>, @current_period=nil>
4.1.8@2.1.5 (main)> z.to_s
=> "(MSK+02:00) Asia/Yekaterinburg"
class I18nTimeZone < ActiveSupport::TimeZone
class << self
def all
TZInfo::Timezone.all.map do |tzinfo|
utc_offset = tzinfo.current_period.utc_offset
create(tzinfo.name, utc_offset, tzinfo)
end
end
end
def to_s
"(GMT#{formatted_offset}) #{translated_name}"
end
protected
def translated_name
I18n.t(name, scope: :timezones, default: name)
end
end
######################
class MskTimeZone < I18nTimeZone
@@moscow_timezone = ActiveSupport::TimeZone.new('Europe/Moscow')
class << self
def utc_seconds_to_msk_offset(utc_seconds, colon = true)
seconds = utc_seconds - @@moscow_timezone.utc_offset
seconds_to_utc_offset(seconds, colon)
end
end
def formatted_offset(colon=true, alternate_utc_string = nil)
utc_offset == 0 && alternate_utc_string ||
self.class.utc_seconds_to_msk_offset(utc_offset, colon)
end
def to_s
"(MSK#{formatted_offset}) #{translated_name}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment