mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Refine Timezone fixture
This commit is contained in:
parent
6cad064424
commit
751d4ab9c2
3 changed files with 40 additions and 19 deletions
|
@ -235,14 +235,14 @@ describe "Time.at" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "could be a timezone object" do
|
it "could be a timezone object" do
|
||||||
zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo", offset: (5*3600+30*60))
|
zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo")
|
||||||
time = Time.at(@epoch_time, in: zone)
|
time = Time.at(@epoch_time, in: zone)
|
||||||
|
|
||||||
time.utc_offset.should == 5*3600+30*60
|
time.utc_offset.should == 5*3600+30*60
|
||||||
time.zone.should == zone
|
time.zone.should == zone
|
||||||
time.to_i.should == @epoch_time
|
time.to_i.should == @epoch_time
|
||||||
|
|
||||||
zone = TimeSpecs::TimezoneWithName.new(name: "PST", offset: (-9*60*60))
|
zone = TimeSpecs::TimezoneWithName.new(name: "PST")
|
||||||
time = Time.at(@epoch_time, in: zone)
|
time = Time.at(@epoch_time, in: zone)
|
||||||
|
|
||||||
time.utc_offset.should == -9*60*60
|
time.utc_offset.should == -9*60*60
|
||||||
|
|
|
@ -55,31 +55,52 @@ module TimeSpecs
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class TimezoneWithAbbr < Timezone
|
Z = Struct.new(:offset, :abbr)
|
||||||
def initialize(options)
|
Zone = Struct.new(:std, :dst, :dst_range)
|
||||||
super
|
Zones = {
|
||||||
@abbr = options[:abbr]
|
"Asia/Colombo" => Zone[Z[5*3600+30*60, "MMT"], nil, nil],
|
||||||
end
|
"Europe/Kiev" => Zone[Z[2*3600, "EET"], Z[3*3600, "EEST"], 4..10],
|
||||||
|
"PST" => Zone[Z[(-9*60*60), "PST"], nil, nil],
|
||||||
def abbr(time)
|
}
|
||||||
@abbr
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class TimezoneWithName < Timezone
|
class TimezoneWithName < Timezone
|
||||||
|
attr_reader :name
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
super
|
|
||||||
@name = options[:name]
|
@name = options[:name]
|
||||||
|
@std, @dst, @dst_range = *Zones[@name]
|
||||||
end
|
end
|
||||||
|
|
||||||
def name
|
def dst?(t)
|
||||||
@name
|
@dst_range&.cover?(t.mon)
|
||||||
|
end
|
||||||
|
|
||||||
|
def zone(t)
|
||||||
|
(dst?(t) ? @dst : @std)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
class TimeWithFindTimezone < Time
|
class TimeWithFindTimezone < Time
|
||||||
def self.find_timezone(name)
|
def self.find_timezone(name)
|
||||||
TimezoneWithName.new(name: name.to_s, offset: 5*3600+30*60)
|
TimezoneWithName.new(name: name.to_s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
TimezoneWithAbbr = TimezoneWithName
|
||||||
end
|
end
|
||||||
|
|
|
@ -265,7 +265,7 @@ ruby_version_is "2.6" do
|
||||||
|
|
||||||
context "#name method" do
|
context "#name method" do
|
||||||
it "uses the optional #name method for marshaling" do
|
it "uses the optional #name method for marshaling" do
|
||||||
zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo", offset: (5*3600+30*60))
|
zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo")
|
||||||
time = Time.new(2000, 1, 1, 12, 0, 0, zone)
|
time = Time.new(2000, 1, 1, 12, 0, 0, zone)
|
||||||
time_loaded = Marshal.load(Marshal.dump(time))
|
time_loaded = Marshal.load(Marshal.dump(time))
|
||||||
|
|
||||||
|
@ -284,7 +284,7 @@ ruby_version_is "2.6" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "the #abbr method is used by '%Z' in #strftime" do
|
it "the #abbr method is used by '%Z' in #strftime" do
|
||||||
zone = TimeSpecs::TimezoneWithAbbr.new(abbr: "MMT", offset: (5*3600+30*60))
|
zone = TimeSpecs::TimezoneWithAbbr.new(name: "Asia/Colombo")
|
||||||
time = Time.new(2000, 1, 1, 12, 0, 0, zone)
|
time = Time.new(2000, 1, 1, 12, 0, 0, zone)
|
||||||
|
|
||||||
time.strftime("%Z").should == "MMT"
|
time.strftime("%Z").should == "MMT"
|
||||||
|
@ -296,7 +296,7 @@ ruby_version_is "2.6" do
|
||||||
# the necessary methods mentioned above.
|
# the necessary methods mentioned above.
|
||||||
context "subject's class implements .find_timezone method" do
|
context "subject's class implements .find_timezone method" do
|
||||||
it "calls .find_timezone to build a time object at loading marshaled data" do
|
it "calls .find_timezone to build a time object at loading marshaled data" do
|
||||||
zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo", offset: (5*3600+30*60))
|
zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo")
|
||||||
time = TimeSpecs::TimeWithFindTimezone.new(2000, 1, 1, 12, 0, 0, zone)
|
time = TimeSpecs::TimeWithFindTimezone.new(2000, 1, 1, 12, 0, 0, zone)
|
||||||
time_loaded = Marshal.load(Marshal.dump(time))
|
time_loaded = Marshal.load(Marshal.dump(time))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue