spec/data/zone_spec.rb in barometer-0.5.0 vs spec/data/zone_spec.rb in barometer-0.6.1

- old
+ new

@@ -1,132 +1,362 @@ require 'spec_helper' describe "Data::Zone" do - describe "and class methods" do - - it "responds to now and returns Time object" do - Data::Zone.respond_to?("now").should be_true - Data::Zone.now.is_a?(Time).should be_true - end - - it "responds to today and returns Date object" do - Data::Zone.respond_to?("today").should be_true - Data::Zone.today.is_a?(Date).should be_true - end - - end + # describe "and class methods" do + # + # it "responds to now and returns Time object" do + # Data::Zone.respond_to?("now").should be_true + # Data::Zone.now.is_a?(Time).should be_true + # end + # + # it "responds to today and returns Date object" do + # Data::Zone.respond_to?("today").should be_true + # Data::Zone.today.is_a?(Date).should be_true + # end + # + # end describe "when initialized" do - before(:each) do - @utc = Time.now.utc - @timezone = "Europe/Paris" - @zone = Data::Zone.new(@timezone) - end + describe "with a full zone" do - it "responds to timezone" do - @zone.timezone.should_not be_nil - @zone.timezone.should == @timezone - end + before(:each) do + @utc = Time.now.utc + @timezone = "Europe/Paris" + @zone = Data::Zone.new(@timezone) + end - it "responds to tz" do - lambda { Data::Zone.new("invalid timezone") }.should raise_error(TZInfo::InvalidTimezoneIdentifier) + it "responds to zone_full" do + @zone.zone_full.should_not be_nil + @zone.zone_full.should == @timezone + end + + it "responds to zone_code" do + @zone.zone_code.should be_nil + end + + it "responds to zone_offset" do + @zone.zone_offset.should be_nil + end + + it "responds to tz" do + lambda { Data::Zone.new("invalid timezone") }.should raise_error(ArgumentError) - zone = Data::Zone.new(@timezone) - zone.tz.should_not be_nil - end + zone = Data::Zone.new(@timezone) + zone.tz.should_not be_nil + end - it "responds to code" do - @zone.respond_to?("code").should be_true - zone = Data::Zone.new(@timezone) - zone.tz = nil - zone.tz.should be_nil - zone.code.should == "" + it "responds to full" do + @zone.respond_to?("full").should be_true + zone = Data::Zone.new(@timezone) + zone.tz = nil + zone.tz.should be_nil + zone.full.should == @timezone - zone = Data::Zone.new(@timezone) - zone.code.should == "CEST" - end + zone = Data::Zone.new(@timezone) + zone.full.should == @timezone + end - it "responds to dst?" do - @zone.respond_to?("dst?").should be_true - zone = Data::Zone.new(@timezone) - zone.tz = nil - zone.tz.should be_nil - zone.dst?.should be_nil - end - - it "responds to now" do - @zone.respond_to?("now").should be_true - @zone.now.is_a?(Time).should be_true - end + it "responds to code" do + @zone.respond_to?("code").should be_true + zone = Data::Zone.new(@timezone) + zone.tz = nil + zone.tz.should be_nil + zone.code.should be_nil - it "responds to today" do - @zone.respond_to?("today").should be_true - @zone.today.is_a?(Date).should be_true - end + zone = Data::Zone.new(@timezone) + zone.code.should == "CEST" + end + + it "responds to dst?" do + @zone.respond_to?("dst?").should be_true + zone = Data::Zone.new(@timezone) + zone.tz = nil + zone.tz.should be_nil + zone.dst?.should be_nil + end - it "responds to now" do - Data::Zone.respond_to?("now").should be_true - end + it "responds to now" do + @zone.respond_to?("now").should be_true + @zone.now.is_a?(Time).should be_true + + period = @zone.tz.period_for_utc(Time.now) + actual_now = Time.now.utc + period.utc_total_offset + + now = @zone.now + now.hour.should == actual_now.hour + now.min.should == actual_now.min + now.sec.should == actual_now.sec + now.year.should == actual_now.year + now.month.should == actual_now.month + now.day.should == actual_now.day + end - it "responds to today" do - Data::Zone.respond_to?("today").should be_true + it "responds to today" do + @zone.respond_to?("today").should be_true + @zone.today.is_a?(Date).should be_true + + period = @zone.tz.period_for_utc(Time.now) + actual_now = Time.now.utc + period.utc_total_offset + + now = @zone.today + now.year.should == actual_now.year + now.month.should == actual_now.month + now.day.should == actual_now.day + end + + it "converts local_time to utc" do + local_time = Time.now.utc + utc_time = @zone.local_to_utc(local_time) + + offset = @zone.tz.period_for_utc(local_time).utc_total_offset + utc_time.should == (local_time - offset) + end + + it "converts utc to local_time" do + utc_time = Time.now.utc + local_time = @zone.utc_to_local(utc_time) + + offset = @zone.tz.period_for_utc(local_time).utc_total_offset + utc_time.should == (local_time - offset) + end + end - it "converts local_time to utc" do - local_time = Time.now.utc - utc_time = @zone.local_to_utc(local_time) + describe "with a zone code" do - offset = @zone.tz.period_for_utc(local_time).utc_total_offset - utc_time.should == (local_time - offset) + before(:each) do + @utc = Time.now.utc + @timezone = "EAST" + @zone = Data::Zone.new(@timezone) + end + + it "responds to zone_code" do + @zone.zone_code.should_not be_nil + @zone.zone_code.should == @timezone + end + + it "responds to zone_full" do + @zone.zone_full.should be_nil + end + + it "responds to zone_offset" do + @zone.zone_offset.should be_nil + end + + it "responds to tz" do + zone = Data::Zone.new(@timezone) + zone.tz.should be_nil + end + + it "responds to code" do + @zone.respond_to?("code").should be_true + zone = Data::Zone.new(@timezone) + zone.tz = nil + zone.tz.should be_nil + zone.code.should == @timezone + + zone = Data::Zone.new(@timezone) + zone.code.should == @timezone + end + + it "responds to full" do + @zone.respond_to?("full").should be_true + zone = Data::Zone.new(@timezone) + zone.tz = nil + zone.tz.should be_nil + zone.full.should be_nil + + zone = Data::Zone.new(@timezone) + zone.full.should be_nil + end + + it "responds to now" do + @zone.respond_to?("now").should be_true + @zone.now.is_a?(Time).should be_true + + actual_now = Time.now.utc + (-6*60*60) + + now = @zone.now + now.hour.should == actual_now.hour + now.min.should == actual_now.min + now.sec.should == actual_now.sec + now.year.should == actual_now.year + now.month.should == actual_now.month + now.day.should == actual_now.day + end + + it "responds to today" do + @zone.respond_to?("today").should be_true + @zone.today.is_a?(Date).should be_true + + actual_now = Time.now.utc + (-6*60*60) + + now = @zone.today + now.year.should == actual_now.year + now.month.should == actual_now.month + now.day.should == actual_now.day + end + + it "converts local_time to utc" do + local_time = Time.now.utc + utc_time = @zone.local_to_utc(local_time) + + utc_time.year.should == (local_time - @zone.offset).year + utc_time.month.should == (local_time - @zone.offset).month + utc_time.day.should == (local_time - @zone.offset).day + utc_time.hour.should == (local_time - @zone.offset).hour + utc_time.min.should == (local_time - @zone.offset).min + utc_time.sec.should == (local_time - @zone.offset).sec + end + + it "converts utc to local_time" do + utc_time = Time.now.utc + local_time = @zone.utc_to_local(utc_time) + + local_time.year.should == (utc_time + @zone.offset).year + local_time.month.should == (utc_time + @zone.offset).month + local_time.day.should == (utc_time + @zone.offset).day + local_time.hour.should == (utc_time + @zone.offset).hour + local_time.min.should == (utc_time + @zone.offset).min + local_time.sec.should == (utc_time + @zone.offset).sec + end + end - it "converts utc to local_time" do - utc_time = Time.now.utc - local_time = @zone.utc_to_local(utc_time) + describe "with a zone offset" do - offset = @zone.tz.period_for_utc(local_time).utc_total_offset - utc_time.should == (local_time - offset) + before(:each) do + @utc = Time.now.utc + @timezone = 8.5 + @zone = Data::Zone.new(@timezone) + end + + it "responds to zone_offset" do + @zone.zone_offset.should_not be_nil + @zone.zone_offset.should == @timezone + end + + it "responds to zone_full" do + @zone.zone_full.should be_nil + end + + it "responds to zone_code" do + @zone.zone_code.should be_nil + end + + it "responds to tz" do + zone = Data::Zone.new(@timezone) + zone.tz.should be_nil + end + + it "responds to offset" do + @zone.respond_to?("offset").should be_true + zone = Data::Zone.new(@timezone) + zone.tz = nil + zone.tz.should be_nil + zone.offset.should == (@timezone * 60 * 60) + + zone = Data::Zone.new(@timezone) + zone.offset.should == (@timezone * 60 * 60) + end + + it "responds to full" do + @zone.respond_to?("full").should be_true + zone = Data::Zone.new(@timezone) + zone.tz = nil + zone.tz.should be_nil + zone.full.should be_nil + + zone = Data::Zone.new(@timezone) + zone.full.should be_nil + end + + it "responds to code" do + @zone.respond_to?("code").should be_true + zone = Data::Zone.new(@timezone) + zone.tz = nil + zone.tz.should be_nil + zone.code.should be_nil + + zone = Data::Zone.new(@timezone) + zone.code.should be_nil + end + + it "responds to now" do + @zone.respond_to?("now").should be_true + @zone.now.is_a?(Time).should be_true + + actual_now = Time.now.utc + (@timezone.to_f*60*60) + + now = @zone.now + now.hour.should == actual_now.hour + now.min.should == actual_now.min + now.sec.should == actual_now.sec + now.year.should == actual_now.year + now.month.should == actual_now.month + now.day.should == actual_now.day + end + + it "responds to today" do + @zone.respond_to?("today").should be_true + @zone.today.is_a?(Date).should be_true + + actual_now = Time.now.utc + (@timezone.to_f*60*60) + + now = @zone.today + now.year.should == actual_now.year + now.month.should == actual_now.month + now.day.should == actual_now.day + end + + it "converts local_time to utc" do + local_time = Time.now.utc + utc_time = @zone.local_to_utc(local_time) + + utc_time.year.should == (local_time - @zone.offset).year + utc_time.month.should == (local_time - @zone.offset).month + utc_time.day.should == (local_time - @zone.offset).day + utc_time.hour.should == (local_time - @zone.offset).hour + utc_time.min.should == (local_time - @zone.offset).min + utc_time.sec.should == (local_time - @zone.offset).sec + end + + it "converts utc to local_time" do + utc_time = Time.now.utc + local_time = @zone.utc_to_local(utc_time) + + local_time.year.should == (utc_time + @zone.offset).year + local_time.month.should == (utc_time + @zone.offset).month + local_time.day.should == (utc_time + @zone.offset).day + local_time.hour.should == (utc_time + @zone.offset).hour + local_time.min.should == (utc_time + @zone.offset).min + local_time.sec.should == (utc_time + @zone.offset).sec + end + end end - describe "when manipulating times" do + describe "when detecting zones" do - it "converts a time to utc based on TimeZone Short Code" do - target_zone = "PDT" - target_offset = Time.zone_offset("PDT") - target_time = Time.now - local_time = target_time - local_offset = Time.zone_offset(local_time.zone) - - # converting two times (ie 6am PDT and 6am MDT) to UTC should result - # in two UTC times that are off by the same offset - original_difference = local_offset - target_offset - - target_utc_time = Data::Zone.code_to_utc(target_time, target_zone) - local_utc_time = local_time.utc - - (target_utc_time - local_time.utc).to_i.should == original_difference.to_i + it "recognozes a full time zone format" do + Data::Zone.is_zone_full?("invalid").should be_false + Data::Zone.is_zone_full?("America/New York").should be_true end + + it "matches a zone offset" do + Data::Zone.is_zone_offset?("invalid").should be_false + Data::Zone.is_zone_offset?("MST").should be_false + Data::Zone.is_zone_offset?("10").should be_false + Data::Zone.is_zone_offset?(-10).should be_true + end - it "merges a date and a time to one utc time (biased by TimeZone Short Code)" do - merge_date = "1 March 1990" - merge_time = "5:35 am" - merge_zonecode = "UTC" - - date = Date.parse(merge_date) - time = Time.parse(merge_time) - - utc_time = Data::Zone.merge(merge_time, merge_date, merge_zonecode) - - utc_time.year.should == date.year - utc_time.month.should == date.month - utc_time.day.should == date.day - utc_time.hour.should == time.hour - utc_time.min.should == time.min - utc_time.sec.should == time.sec + it "matches a zone code" do + Data::Zone.is_zone_code?("invalid").should be_false + Data::Zone.is_zone_code?("MST").should be_true + Data::Zone.is_zone_code?("EAST").should be_true end end end \ No newline at end of file