diff options
Diffstat (limited to 'spec/ruby/core/time/fixtures/classes.rb')
-rw-r--r-- | spec/ruby/core/time/fixtures/classes.rb | 47 |
1 files changed, 34 insertions, 13 deletions
diff --git a/spec/ruby/core/time/fixtures/classes.rb b/spec/ruby/core/time/fixtures/classes.rb index a648171b32..1a9511b261 100644 --- a/spec/ruby/core/time/fixtures/classes.rb +++ b/spec/ruby/core/time/fixtures/classes.rb @@ -55,31 +55,52 @@ module TimeSpecs end end - class TimezoneWithAbbr < Timezone + Z = Struct.new(:offset, :abbr) + Zone = Struct.new(:std, :dst, :dst_range) + Zones = { + "Asia/Colombo" => Zone[Z[5*3600+30*60, "MMT"], nil, nil], + "Europe/Kiev" => Zone[Z[2*3600, "EET"], Z[3*3600, "EEST"], 4..10], + "PST" => Zone[Z[(-9*60*60), "PST"], nil, nil], + } + + class TimezoneWithName < Timezone + attr_reader :name + def initialize(options) - super - @abbr = options[:abbr] + @name = options[:name] + @std, @dst, @dst_range = *Zones[@name] end - def abbr(time) - @abbr + def dst?(t) + @dst_range&.cover?(t.mon) end - end - class TimezoneWithName < Timezone - def initialize(options) - super - @name = options[:name] + def zone(t) + (dst?(t) ? @dst : @std) end - def name - @name + def utc_offset(t) + zone(t)&.offset || 0 + end + + def abbr(t) + zone(t)&.abbr + end + + def local_to_utc(t) + t - utc_offset(t) + end + + def utc_to_local(t) + t + utc_offset(t) end end class TimeWithFindTimezone < Time def self.find_timezone(name) - TimezoneWithName.new(name: name.to_s, offset: 5*3600+30*60) + TimezoneWithName.new(name: name.to_s) end end + + TimezoneWithAbbr = TimezoneWithName end |