Last active
August 29, 2015 14:12
-
-
Save vyorkin/b607e34eeb0ac15ee8d9 to your computer and use it in GitHub Desktop.
some dikiy pizdets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[email protected] (main)> ru = TZInfo::Country.get('RU') | |
=> #<TZInfo::Country: RU> | |
[email protected] (main)> ekb = ru.zones[5] | |
=> #<TZInfo::TimezoneProxy: Asia/Yekaterinburg> | |
[email protected] (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> | |
[email protected] (main)> z.to_s | |
=> "(MSK+02:00) Asia/Yekaterinburg" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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