1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Move spec/rubyspec to spec/ruby for consistency

* Other ruby implementations use the spec/ruby directory.
  [Misc #13792] [ruby-core:82287]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2017-09-20 20:18:52 +00:00
parent 75bfc6440d
commit 1d15d5f080
4370 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,56 @@
# -*- encoding: binary -*-
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#_dump" do
before :each do
@local = Time.at(946812800)
@t = Time.at(946812800)
@t = @t.gmtime
@s = @t.send(:_dump)
end
it "is a private method" do
Time.should have_private_instance_method(:_dump, false)
end
# http://redmine.ruby-lang.org/issues/show/627
it "preserves the GMT flag" do
@t.gmt?.should == true
dump = @t.send(:_dump).unpack("VV").first
((dump >> 30) & 0x1).should == 1
@local.gmt?.should == false
dump = @local.send(:_dump).unpack("VV").first
((dump >> 30) & 0x1).should == 0
end
it "dumps a Time object to a bytestring" do
@s.should be_an_instance_of(String)
@s.should == [3222863947, 2235564032].pack("VV")
end
it "dumps an array with a date as first element" do
high = 1 << 31 |
(@t.gmt? ? 1 : 0) << 30 |
(@t.year - 1900) << 14 |
(@t.mon - 1) << 10 |
@t.mday << 5 |
@t.hour
high.should == @s.unpack("VV").first
end
it "dumps an array with a time as second element" do
low = @t.min << 26 |
@t.sec << 20 |
@t.usec
low.should == @s.unpack("VV").last
end
it "dumps like MRI's marshaled time format" do
t = Time.utc(2000, 1, 15, 20, 1, 1, 203).localtime
t.send(:_dump).should == "\364\001\031\200\313\000\020\004"
end
end

View file

@ -0,0 +1,54 @@
# -*- encoding: binary -*-
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time._load" do
it "is a private method" do
Time.should have_private_method(:_load, false)
end
# http://redmine.ruby-lang.org/issues/show/627
it "loads a time object in the new format" do
t = Time.local(2000, 1, 15, 20, 1, 1)
t = t.gmtime
high = 1 << 31 |
(t.gmt? ? 1 : 0) << 30 |
(t.year - 1900) << 14 |
(t.mon - 1) << 10 |
t.mday << 5 |
t.hour
low = t.min << 26 |
t.sec << 20 |
t.usec
Time.send(:_load, [high, low].pack("VV")).should == t
end
it "loads a time object in the old UNIX timestamp based format" do
t = Time.local(2000, 1, 15, 20, 1, 1, 203)
timestamp = t.to_i
high = timestamp & ((1 << 31) - 1)
low = t.usec
Time.send(:_load, [high, low].pack("VV")).should == t
end
it "loads MRI's marshaled time format" do
t = Marshal.load("\004\bu:\tTime\r\320\246\e\200\320\001\r\347")
t.utc
t.to_s.should == "2010-10-22 16:57:48 UTC"
end
with_feature :encoding do
it "treats the data as binary data" do
data = "\x04\bu:\tTime\r\fM\x1C\xC0\x00\x00\xD0\xBE"
data.force_encoding Encoding::UTF_8
t = Marshal.load(data)
t.to_s.should == "2013-04-08 12:47:45 UTC"
end
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/asctime', __FILE__)
describe "Time#asctime" do
it_behaves_like(:time_asctime, :asctime)
end

View file

@ -0,0 +1,145 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time.at" do
describe "passed Numeric" do
it "returns a Time object representing the given number of Integer seconds since 1970-01-01 00:00:00 UTC" do
Time.at(1184027924).getgm.asctime.should == "Tue Jul 10 00:38:44 2007"
end
it "returns a Time object representing the given number of Float seconds since 1970-01-01 00:00:00 UTC" do
t = Time.at(10.5)
t.usec.should == 500000.0
t.should_not == Time.at(10)
end
it "returns a non-UTC Time" do
Time.at(1184027924).utc?.should == false
end
it "returns a subclass instance on a Time subclass" do
c = Class.new(Time)
t = c.at(0)
t.should be_an_instance_of(c)
end
it "roundtrips a Rational produced by #to_r" do
t = Time.now()
t2 = Time.at(t.to_r)
t2.should == t
t2.usec.should == t.usec
t2.nsec.should == t.nsec
end
describe "passed BigDecimal" do
it "doesn't round input value" do
require 'bigdecimal'
Time.at(BigDecimal.new('1.1')).to_f.should == 1.1
end
end
end
describe "passed Time" do
it "creates a new time object with the value given by time" do
t = Time.now
Time.at(t).inspect.should == t.inspect
end
it "creates a dup time object with the value given by time" do
t1 = Time.new
t2 = Time.at(t1)
t1.object_id.should_not == t2.object_id
end
it "returns a UTC time if the argument is UTC" do
t = Time.now.getgm
Time.at(t).utc?.should == true
end
it "returns a non-UTC time if the argument is non-UTC" do
t = Time.now
Time.at(t).utc?.should == false
end
it "returns a subclass instance" do
c = Class.new(Time)
t = c.at(Time.now)
t.should be_an_instance_of(c)
end
end
describe "passed non-Time, non-Numeric" do
it "raises a TypeError with a String argument" do
lambda { Time.at("0") }.should raise_error(TypeError)
end
it "raises a TypeError with a nil argument" do
lambda { Time.at(nil) }.should raise_error(TypeError)
end
describe "with an argument that responds to #to_int" do
it "coerces using #to_int" do
o = mock('integer')
o.should_receive(:to_int).and_return(0)
Time.at(o).should == Time.at(0)
end
end
describe "with an argument that responds to #to_r" do
it "coerces using #to_r" do
o = mock_numeric('rational')
o.should_receive(:to_r).and_return(Rational(5, 2))
Time.at(o).should == Time.at(Rational(5, 2))
end
end
end
describe "passed [Integer, Numeric]" do
it "returns a Time object representing the given number of seconds and Integer microseconds since 1970-01-01 00:00:00 UTC" do
t = Time.at(10, 500000)
t.tv_sec.should == 10
t.tv_usec.should == 500000
end
it "returns a Time object representing the given number of seconds and Float microseconds since 1970-01-01 00:00:00 UTC" do
t = Time.at(10, 500.500)
t.tv_sec.should == 10
t.tv_nsec.should == 500500
end
end
describe "with a second argument that responds to #to_int" do
it "coerces using #to_int" do
o = mock('integer')
o.should_receive(:to_int).and_return(10)
Time.at(0, o).should == Time.at(0, 10)
end
end
describe "with a second argument that responds to #to_r" do
it "coerces using #to_r" do
o = mock_numeric('rational')
o.should_receive(:to_r).and_return(Rational(5, 2))
Time.at(0, o).should == Time.at(0, Rational(5, 2))
end
end
describe "passed [Integer, nil]" do
it "raises a TypeError" do
lambda { Time.at(0, nil) }.should raise_error(TypeError)
end
end
describe "passed [Integer, String]" do
it "raises a TypeError" do
lambda { Time.at(0, "0") }.should raise_error(TypeError)
end
end
describe "passed [Time, Integer]" do
# #8173
it "raises a TypeError" do
lambda { Time.at(Time.now, 500000) }.should raise_error(TypeError)
end
end
end

View file

@ -0,0 +1,94 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#<=>" do
it "returns 1 if the first argument is a point in time after the second argument" do
(Time.now <=> Time.at(0)).should == 1
end
it "returns 1 if the first argument is a point in time after the second argument (down to a millisecond)" do
(Time.at(0, 1000) <=> Time.at(0, 0)).should == 1
(Time.at(1202778512, 1000) <=> Time.at(1202778512, 999)).should == 1
end
it "returns 1 if the first argument is a point in time after the second argument (down to a microsecond)" do
(Time.at(0, 100) <=> Time.at(0, 0)).should == 1
(Time.at(1202778512, 100) <=> Time.at(1202778512, 99)).should == 1
end
it "returns 0 if time is the same as other" do
(Time.at(1202778513) <=> Time.at(1202778513)).should == 0
(Time.at(100, 100) <=> Time.at(100, 100)).should == 0
end
it "returns -1 if the first argument is a point in time before the second argument" do
(Time.at(0) <=> Time.now).should == -1
(Time.at(100, 100) <=> Time.at(101, 100)).should == -1
end
it "returns -1 if the first argument is a point in time before the second argument (down to a millisecond)" do
(Time.at(0, 0) <=> Time.at(0, 1000)).should == -1
end
it "returns -1 if the first argument is a point in time before the second argument (down to a microsecond)" do
(Time.at(0, 0) <=> Time.at(0, 100)).should == -1
end
it "returns 1 if the first argument is a fraction of a microsecond after the second argument" do
(Time.at(100, Rational(1,1000)) <=> Time.at(100, 0)).should == 1
end
it "returns 0 if time is the same as other, including fractional microseconds" do
(Time.at(100, Rational(1,1000)) <=> Time.at(100, Rational(1,1000))).should == 0
end
it "returns -1 if the first argument is a fraction of a microsecond before the second argument" do
(Time.at(100, 0) <=> Time.at(100, Rational(1,1000))).should == -1
end
describe "given a non-Time argument" do
it "returns nil if argument <=> self returns nil" do
t = Time.now
obj = mock('time')
obj.should_receive(:<=>).with(t).and_return(nil)
(t <=> obj).should == nil
end
it "returns -1 if argument <=> self is greater than 0" do
t = Time.now
r = mock('r')
r.should_receive(:>).with(0).and_return(true)
obj = mock('time')
obj.should_receive(:<=>).with(t).and_return(r)
(t <=> obj).should == -1
end
it "returns 1 if argument <=> self is not greater than 0 and is less than 0" do
t = Time.now
r = mock('r')
r.should_receive(:>).with(0).and_return(false)
r.should_receive(:<).with(0).and_return(true)
obj = mock('time')
obj.should_receive(:<=>).with(t).and_return(r)
(t <=> obj).should == 1
end
it "returns 0 if argument <=> self is neither greater than 0 nor less than 0" do
t = Time.now
r = mock('r')
r.should_receive(:>).with(0).and_return(false)
r.should_receive(:<).with(0).and_return(false)
obj = mock('time')
obj.should_receive(:<=>).with(t).and_return(r)
(t <=> obj).should == 0
end
it "returns nil if argument also uses an inverse comparison for <=>" do
t = Time.now
r = mock('r')
def r.<=>(other); other <=> self; end
r.should_receive(:<=>).once
(t <=> r).should be_nil
end
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/asctime', __FILE__)
describe "Time#ctime" do
it_behaves_like(:time_asctime, :ctime)
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/day', __FILE__)
describe "Time#day" do
it_behaves_like(:time_day, :day)
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/isdst', __FILE__)
describe "Time#dst?" do
it_behaves_like(:time_isdst, :dst?)
end

View file

@ -0,0 +1,46 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#dup" do
it "returns a Time object that represents the same time" do
t = Time.at(100)
t.dup.tv_sec.should == t.tv_sec
end
it "copies the gmt state flag" do
Time.now.gmtime.dup.gmt?.should == true
end
it "returns an independent Time object" do
t = Time.now
t2 = t.dup
t.gmtime
t2.gmt?.should == false
end
it "returns a subclass instance" do
c = Class.new(Time)
t = c.now
t.should be_an_instance_of(c)
t.dup.should be_an_instance_of(c)
end
it "returns a clone of Time instance" do
c = Time.dup
t = c.now
t.should be_an_instance_of(c)
t.should_not be_an_instance_of(Time)
t.dup.should be_an_instance_of(c)
t.dup.should_not be_an_instance_of(Time)
end
it "does not copy frozen status from the original" do
t = Time.now
t.freeze
t2 = t.dup
t2.frozen?.should be_false
end
end

View file

@ -0,0 +1,29 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#eql?" do
it "returns true if self and other have the same whole number of seconds" do
Time.at(100).should eql(Time.at(100))
end
it "returns false if self and other have differing whole numbers of seconds" do
Time.at(100).should_not eql(Time.at(99))
end
it "returns true if self and other have the same number of microseconds" do
Time.at(100, 100).should eql(Time.at(100, 100))
end
it "returns false if self and other have differing numbers of microseconds" do
Time.at(100, 100).should_not eql(Time.at(100, 99))
end
it "returns false if self and other have differing fractional microseconds" do
Time.at(100, Rational(100,1000)).should_not eql(Time.at(100, Rational(99,1000)))
end
it "returns false when given a non-time value" do
Time.at(100, 100).should_not eql("100")
Time.at(100, 100).should_not eql(100)
Time.at(100, 100).should_not eql(100.1)
end
end

View file

@ -0,0 +1,12 @@
module TimeSpecs
class SubTime < Time; end
class MethodHolder
class << self
define_method(:now, &Time.method(:now))
define_method(:new, &Time.method(:new))
end
end
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#friday?" do
it "returns true if time represents Friday" do
Time.local(2000, 1, 7).friday?.should == true
end
it "returns false if time doesn't represent Friday" do
Time.local(2000, 1, 1).friday?.should == false
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/getgm', __FILE__)
describe "Time#getgm" do
it_behaves_like(:time_getgm, :getgm)
end

View file

@ -0,0 +1,98 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#getlocal" do
it "returns a new time which is the local representation of time" do
# Testing with America/Regina here because it doesn't have DST.
with_timezone("CST", -6) do
t = Time.gm(2007, 1, 9, 12, 0, 0)
t.localtime.should == Time.local(2007, 1, 9, 6, 0, 0)
end
end
it "returns a Time with UTC offset specified as an Integer number of seconds" do
t = Time.gm(2007, 1, 9, 12, 0, 0).getlocal(3630)
t.should == Time.new(2007, 1, 9, 13, 0, 30, 3630)
t.utc_offset.should == 3630
end
platform_is_not :windows do
it "returns a new time with the correct utc_offset according to the set timezone" do
t = Time.new(2005, 2, 27, 22, 50, 0, -3600)
t.utc_offset.should == -3600
with_timezone("America/New_York") do
t.getlocal.utc_offset.should == -18000
end
end
end
describe "with an argument that responds to #to_int" do
it "coerces using #to_int" do
o = mock('integer')
o.should_receive(:to_int).and_return(3630)
t = Time.gm(2007, 1, 9, 12, 0, 0).getlocal(o)
t.should == Time.new(2007, 1, 9, 13, 0, 30, 3630)
t.utc_offset.should == 3630
end
end
it "returns a Time with a UTC offset of the specified number of Rational seconds" do
t = Time.gm(2007, 1, 9, 12, 0, 0).getlocal(Rational(7201, 2))
t.should == Time.new(2007, 1, 9, 13, 0, Rational(1, 2), Rational(7201, 2))
t.utc_offset.should eql(Rational(7201, 2))
end
describe "with an argument that responds to #to_r" do
it "coerces using #to_r" do
o = mock_numeric('rational')
o.should_receive(:to_r).and_return(Rational(7201, 2))
t = Time.gm(2007, 1, 9, 12, 0, 0).getlocal(o)
t.should == Time.new(2007, 1, 9, 13, 0, Rational(1, 2), Rational(7201, 2))
t.utc_offset.should eql(Rational(7201, 2))
end
end
it "returns a Time with a UTC offset specified as +HH:MM" do
t = Time.gm(2007, 1, 9, 12, 0, 0).getlocal("+01:00")
t.should == Time.new(2007, 1, 9, 13, 0, 0, 3600)
t.utc_offset.should == 3600
end
it "returns a Time with a UTC offset specified as -HH:MM" do
t = Time.gm(2007, 1, 9, 12, 0, 0).getlocal("-01:00")
t.should == Time.new(2007, 1, 9, 11, 0, 0, -3600)
t.utc_offset.should == -3600
end
describe "with an argument that responds to #to_str" do
it "coerces using #to_str" do
o = mock('string')
o.should_receive(:to_str).and_return("+01:00")
t = Time.gm(2007, 1, 9, 12, 0, 0).getlocal(o)
t.should == Time.new(2007, 1, 9, 13, 0, 0, 3600)
t.utc_offset.should == 3600
end
end
it "raises ArgumentError if the String argument is not of the form (+|-)HH:MM" do
t = Time.now
lambda { t.getlocal("3600") }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the String argument is not in an ASCII-compatible encoding" do
t = Time.now
lambda { t.getlocal("-01:00".encode("UTF-16LE")) }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the argument represents a value less than or equal to -86400 seconds" do
t = Time.new
t.getlocal(-86400 + 1).utc_offset.should == (-86400 + 1)
lambda { t.getlocal(-86400) }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the argument represents a value greater than or equal to 86400 seconds" do
t = Time.new
t.getlocal(86400 - 1).utc_offset.should == (86400 - 1)
lambda { t.getlocal(86400) }.should raise_error(ArgumentError)
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/getgm', __FILE__)
describe "Time#getutc" do
it_behaves_like(:time_getgm, :getutc)
end

View file

@ -0,0 +1,10 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/gm', __FILE__)
require File.expand_path('../shared/time_params', __FILE__)
describe "Time.gm" do
it_behaves_like(:time_gm, :gm)
it_behaves_like(:time_params, :gm)
it_behaves_like(:time_params_10_arg, :gm)
it_behaves_like(:time_params_microseconds, :gm)
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/gmt_offset', __FILE__)
describe "Time#gmt_offset" do
it_behaves_like(:time_gmt_offset, :gmt_offset)
end

View file

@ -0,0 +1,8 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#gmt?" do
it "returns true if time represents a time in UTC (GMT)" do
Time.now.gmt?.should == false
Time.now.gmtime.gmt?.should == true
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/gmtime', __FILE__)
describe "Time#gmtime" do
it_behaves_like(:time_gmtime, :gmtime)
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/gmt_offset', __FILE__)
describe "Time#gmtoff" do
it_behaves_like(:time_gmt_offset, :gmtoff)
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#hash" do
it "returns a Fixnum" do
Time.at(100).hash.should be_an_instance_of(Fixnum)
end
it "is stable" do
Time.at(1234).hash.should == Time.at(1234).hash
end
end

View file

@ -0,0 +1,17 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#hour" do
it "returns the hour of the day (0..23) for a local Time" do
with_timezone("CET", 1) do
Time.local(1970, 1, 1, 1).hour.should == 1
end
end
it "returns the hour of the day for a UTC Time" do
Time.utc(1970, 1, 1, 0).hour.should == 0
end
it "returns the hour of the day for a Time with a fixed offset" do
Time.new(2012, 1, 1, 0, 0, 0, -3600).hour.should == 0
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/inspect', __FILE__)
describe "Time#inspect" do
it_behaves_like :inspect, :inspect
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/isdst', __FILE__)
describe "Time#isdst" do
it_behaves_like(:time_isdst, :isdst)
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/local', __FILE__)
require File.expand_path('../shared/time_params', __FILE__)
describe "Time.local" do
it_behaves_like(:time_local, :local)
it_behaves_like(:time_local_10_arg, :local)
it_behaves_like(:time_params, :local)
it_behaves_like(:time_params_10_arg, :local)
it_behaves_like(:time_params_microseconds, :local)
end

View file

@ -0,0 +1,127 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#localtime" do
it "converts self to local time, modifying the receiver" do
# Testing with America/Regina here because it doesn't have DST.
with_timezone("CST", -6) do
t = Time.gm(2007, 1, 9, 12, 0, 0)
t.localtime
t.should == Time.local(2007, 1, 9, 6, 0, 0)
end
end
it "returns self" do
t = Time.gm(2007, 1, 9, 12, 0, 0)
t.localtime.should equal(t)
end
it "converts time to the UTC offset specified as an Integer number of seconds" do
t = Time.gm(2007, 1, 9, 12, 0, 0)
t.localtime(3630)
t.should == Time.new(2007, 1, 9, 13, 0, 30, 3630)
t.utc_offset.should == 3630
end
describe "on a frozen time" do
it "does not raise an error if already in the right time zone" do
time = Time.now
time.freeze
time.localtime.should equal(time)
end
it "raises a RuntimeError if the time has a different time zone" do
time = Time.gm(2007, 1, 9, 12, 0, 0)
time.freeze
lambda { time.localtime }.should raise_error(RuntimeError)
end
end
describe "with an argument that responds to #to_int" do
it "coerces using #to_int" do
o = mock('integer')
o.should_receive(:to_int).and_return(3630)
t = Time.gm(2007, 1, 9, 12, 0, 0)
t.localtime(o)
t.should == Time.new(2007, 1, 9, 13, 0, 30, 3630)
t.utc_offset.should == 3630
end
end
it "returns a Time with a UTC offset of the specified number of Rational seconds" do
t = Time.gm(2007, 1, 9, 12, 0, 0)
t.localtime(Rational(7201, 2))
t.should == Time.new(2007, 1, 9, 13, 0, Rational(1, 2), Rational(7201, 2))
t.utc_offset.should eql(Rational(7201, 2))
end
describe "with an argument that responds to #to_r" do
it "coerces using #to_r" do
o = mock_numeric('rational')
o.should_receive(:to_r).and_return(Rational(7201, 2))
t = Time.gm(2007, 1, 9, 12, 0, 0)
t.localtime(o)
t.should == Time.new(2007, 1, 9, 13, 0, Rational(1, 2), Rational(7201, 2))
t.utc_offset.should eql(Rational(7201, 2))
end
end
it "returns a Time with a UTC offset specified as +HH:MM" do
t = Time.gm(2007, 1, 9, 12, 0, 0)
t.localtime("+01:00")
t.should == Time.new(2007, 1, 9, 13, 0, 0, 3600)
t.utc_offset.should == 3600
end
it "returns a Time with a UTC offset specified as -HH:MM" do
t = Time.gm(2007, 1, 9, 12, 0, 0)
t.localtime("-01:00")
t.should == Time.new(2007, 1, 9, 11, 0, 0, -3600)
t.utc_offset.should == -3600
end
platform_is_not :windows do
it "changes the timezone according to the set one" do
t = Time.new(2005, 2, 27, 22, 50, 0, -3600)
t.utc_offset.should == -3600
with_timezone("America/New_York") do
t.localtime
end
t.utc_offset.should == -18000
end
end
describe "with an argument that responds to #to_str" do
it "coerces using #to_str" do
o = mock('string')
o.should_receive(:to_str).and_return("+01:00")
t = Time.gm(2007, 1, 9, 12, 0, 0)
t.localtime(o)
t.should == Time.new(2007, 1, 9, 13, 0, 0, 3600)
t.utc_offset.should == 3600
end
end
it "raises ArgumentError if the String argument is not of the form (+|-)HH:MM" do
t = Time.now
lambda { t.localtime("3600") }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the String argument is not in an ASCII-compatible encoding" do
t = Time.now
lambda { t.localtime("-01:00".encode("UTF-16LE")) }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the argument represents a value less than or equal to -86400 seconds" do
t = Time.new
t.localtime(-86400 + 1).utc_offset.should == (-86400 + 1)
lambda { t.localtime(-86400) }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the argument represents a value greater than or equal to 86400 seconds" do
t = Time.new
t.localtime(86400 - 1).utc_offset.should == (86400 - 1)
lambda { t.localtime(86400) }.should raise_error(ArgumentError)
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/day', __FILE__)
describe "Time#mday" do
it_behaves_like(:time_day, :mday)
end

View file

@ -0,0 +1,17 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#min" do
it "returns the minute of the hour (0..59) for a local Time" do
with_timezone("CET", 1) do
Time.local(1970, 1, 1, 0, 0).min.should == 0
end
end
it "returns the minute of the hour for a UTC Time" do
Time.utc(1970, 1, 1, 0, 0).min.should == 0
end
it "returns the minute of the hour for a Time with a fixed offset" do
Time.new(2012, 1, 1, 0, 0, 0, -3600).min.should == 0
end
end

View file

@ -0,0 +1,103 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#-" do
it "decrements the time by the specified amount" do
(Time.at(100) - 100).should == Time.at(0)
(Time.at(100) - Time.at(99)).should == 1.0
end
it "understands negative subtractions" do
t = Time.at(100) - -1.3
t.usec.should == 300000
t.to_i.should == 101
end
#see [ruby-dev:38446]
it "accepts arguments that can be coerced into Rational" do
(obj = mock_numeric('10')).should_receive(:to_r).and_return(Rational(10))
(Time.at(100) - obj).should == Time.at(90)
end
it "raises a TypeError if given argument is a coercible String" do
lambda { Time.now - "1" }.should raise_error(TypeError)
lambda { Time.now - "0.1" }.should raise_error(TypeError)
lambda { Time.now - "1/3" }.should raise_error(TypeError)
end
it "raises TypeError on argument that can't be coerced" do
lambda { Time.now - Object.new }.should raise_error(TypeError)
lambda { Time.now - "stuff" }.should raise_error(TypeError)
end
it "raises TypeError on nil argument" do
lambda { Time.now - nil }.should raise_error(TypeError)
end
it "tracks microseconds" do
time = Time.at(0.777777)
time -= 0.654321
time.usec.should == 123456
time -= 1
time.usec.should == 123456
end
it "tracks microseconds from a Rational" do
time = Time.at(Rational(777_777, 1_000_000))
time -= Rational(654_321, 1_000_000)
time.usec.should == 123_456
time -= Rational(123_456, 1_000_000)
time.usec.should == 0
end
it "tracks nanoseconds" do
time = Time.at(Rational(999_999_999, 1_000_000_000))
time -= Rational(876_543_210, 1_000_000_000)
time.nsec.should == 123_456_789
time -= Rational(123_456_789, 1_000_000_000)
time.nsec.should == 0
end
it "maintains precision" do
time = Time.at(10) - Rational(1_000_000_000_000_001, 1_000_000_000_000_000)
time.should_not == Time.at(9)
end
it "maintains microseconds precision" do
time = Time.at(10) - Rational(1, 1_000_000)
time.usec.should == 999_999
end
it "maintains nanoseconds precision" do
time = Time.at(10) - Rational(1, 1_000_000_000)
time.nsec.should == 999_999_999
end
it "maintains subseconds precision" do
time = Time.at(0) - Rational(1_000_000_000_000_001, 1_000_000_000_000_000)
time.subsec.should == Rational(999_999_999_999_999, 1_000_000_000_000_000)
end
it "returns a UTC time if self is UTC" do
(Time.utc(2012) - 10).utc?.should == true
end
it "returns a non-UTC time if self is non-UTC" do
(Time.local(2012) - 10).utc?.should == false
end
it "returns a time with the same fixed offset as self" do
(Time.new(2012, 1, 1, 0, 0, 0, 3600) - 10).utc_offset.should == 3600
end
it "does not return a subclass instance" do
c = Class.new(Time)
x = c.now + 1
x.should be_an_instance_of(Time)
end
it "returns a time with nanoseconds precision between two time objects" do
time1 = Time.utc(2000, 1, 2, 23, 59, 59, Rational(999999999, 1000))
time2 = Time.utc(2000, 1, 2, 0, 0, 0, Rational(1, 1000))
(time1 - time2).should == 86_399.999999998
end
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/local', __FILE__)
require File.expand_path('../shared/time_params', __FILE__)
describe "Time.mktime" do
it_behaves_like(:time_local, :mktime)
it_behaves_like(:time_local_10_arg, :mktime)
it_behaves_like(:time_params, :mktime)
it_behaves_like(:time_params_10_arg, :mktime)
it_behaves_like(:time_params_microseconds, :mktime)
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/month', __FILE__)
describe "Time#mon" do
it_behaves_like(:time_month, :mon)
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#monday?" do
it "returns true if time represents Monday" do
Time.local(2000, 1, 3).monday?.should == true
end
it "returns false if time doesn't represent Monday" do
Time.local(2000, 1, 1).monday?.should == false
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/month', __FILE__)
describe "Time#month" do
it_behaves_like(:time_month, :month)
end

View file

@ -0,0 +1,99 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/now', __FILE__)
require File.expand_path('../shared/local', __FILE__)
require File.expand_path('../shared/time_params', __FILE__)
describe "Time.new" do
it_behaves_like(:time_now, :new)
end
describe "Time.new" do
it_behaves_like(:time_local, :new)
it_behaves_like(:time_params, :new)
end
describe "Time.new with a utc_offset argument" do
it "returns a non-UTC time" do
Time.new(2000, 1, 1, 0, 0, 0, 0).utc?.should == false
end
it "returns a Time with a UTC offset of the specified number of Integer seconds" do
Time.new(2000, 1, 1, 0, 0, 0, 123).utc_offset.should == 123
end
describe "with an argument that responds to #to_int" do
it "coerces using #to_int" do
o = mock('integer')
o.should_receive(:to_int).and_return(123)
Time.new(2000, 1, 1, 0, 0, 0, o).utc_offset.should == 123
end
end
it "returns a Time with a UTC offset of the specified number of Rational seconds" do
Time.new(2000, 1, 1, 0, 0, 0, Rational(5, 2)).utc_offset.should eql(Rational(5, 2))
end
describe "with an argument that responds to #to_r" do
it "coerces using #to_r" do
o = mock_numeric('rational')
o.should_receive(:to_r).and_return(Rational(5, 2))
Time.new(2000, 1, 1, 0, 0, 0, o).utc_offset.should eql(Rational(5, 2))
end
end
it "returns a Time with a UTC offset specified as +HH:MM" do
Time.new(2000, 1, 1, 0, 0, 0, "+05:30").utc_offset.should == 19800
end
it "returns a Time with a UTC offset specified as -HH:MM" do
Time.new(2000, 1, 1, 0, 0, 0, "-04:10").utc_offset.should == -15000
end
describe "with an argument that responds to #to_str" do
it "coerces using #to_str" do
o = mock('string')
o.should_receive(:to_str).and_return("+05:30")
Time.new(2000, 1, 1, 0, 0, 0, o).utc_offset.should == 19800
end
end
it "returns a local Time if the argument is nil" do
with_timezone("PST", -8) do
t = Time.new(2000, 1, 1, 0, 0, 0, nil)
t.utc_offset.should == -28800
t.zone.should == "PST"
end
end
# [Bug #8679], r47676
it "disallows a value for minutes greater than 59" do
lambda {
Time.new(2000, 1, 1, 0, 0, 0, "+01:60")
}.should raise_error(ArgumentError)
lambda {
Time.new(2000, 1, 1, 0, 0, 0, "+01:99")
}.should raise_error(ArgumentError)
end
it "raises ArgumentError if the String argument is not of the form (+|-)HH:MM" do
lambda { Time.new(2000, 1, 1, 0, 0, 0, "3600") }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the hour value is greater than 23" do
lambda { Time.new(2000, 1, 1, 0, 0, 0, "+24:00") }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the String argument is not in an ASCII-compatible encoding" do
lambda { Time.new(2000, 1, 1, 0, 0, 0, "-04:10".encode("UTF-16LE")) }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the argument represents a value less than or equal to -86400 seconds" do
Time.new(2000, 1, 1, 0, 0, 0, -86400 + 1).utc_offset.should == (-86400 + 1)
lambda { Time.new(2000, 1, 1, 0, 0, 0, -86400) }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the argument represents a value greater than or equal to 86400 seconds" do
Time.new(2000, 1, 1, 0, 0, 0, 86400 - 1).utc_offset.should == (86400 - 1)
lambda { Time.new(2000, 1, 1, 0, 0, 0, 86400) }.should raise_error(ArgumentError)
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/now', __FILE__)
describe "Time.now" do
it_behaves_like(:time_now, :now)
end

View file

@ -0,0 +1,27 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#nsec" do
it "returns 0 for a Time constructed with a whole number of seconds" do
Time.at(100).nsec.should == 0
end
it "returns the nanoseconds part of a Time constructed with a Float number of seconds" do
Time.at(10.75).nsec.should == 750_000_000
end
it "returns the nanoseconds part of a Time constructed with an Integer number of microseconds" do
Time.at(0, 999_999).nsec.should == 999_999_000
end
it "returns the nanoseconds part of a Time constructed with an Float number of microseconds" do
Time.at(0, 3.75).nsec.should == 3750
end
it "returns the nanoseconds part of a Time constructed with a Rational number of seconds" do
Time.at(Rational(3, 2)).nsec.should == 500_000_000
end
it "returns the nanoseconds part of a Time constructed with an Rational number of microseconds" do
Time.at(0, Rational(99, 10)).nsec.should == 9900
end
end

View file

@ -0,0 +1,100 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#+" do
it "increments the time by the specified amount" do
(Time.at(0) + 100).should == Time.at(100)
end
it "is a commutative operator" do
(Time.at(1.1) + 0.9).should == Time.at(0.9) + 1.1
end
it "adds a negative Float" do
t = Time.at(100) + -1.3
t.usec.should == 699999
t.to_i.should == 98
end
it "raises a TypeError if given argument is a coercible String" do
lambda { Time.now + "1" }.should raise_error(TypeError)
lambda { Time.now + "0.1" }.should raise_error(TypeError)
lambda { Time.now + "1/3" }.should raise_error(TypeError)
end
it "increments the time by the specified amount as rational numbers" do
(Time.at(Rational(11, 10)) + Rational(9, 10)).should == Time.at(2)
end
it "accepts arguments that can be coerced into Rational" do
(obj = mock_numeric('10')).should_receive(:to_r).and_return(Rational(10))
(Time.at(100) + obj).should == Time.at(110)
end
it "raises TypeError on argument that can't be coerced into Rational" do
lambda { Time.now + Object.new }.should raise_error(TypeError)
lambda { Time.now + "stuff" }.should raise_error(TypeError)
end
it "returns a UTC time if self is UTC" do
(Time.utc(2012) + 10).utc?.should == true
end
it "returns a non-UTC time if self is non-UTC" do
(Time.local(2012) + 10).utc?.should == false
end
it "returns a time with the same fixed offset as self" do
(Time.new(2012, 1, 1, 0, 0, 0, 3600) + 10).utc_offset.should == 3600
end
it "does not return a subclass instance" do
c = Class.new(Time)
x = c.now + 1
x.should be_an_instance_of(Time)
end
it "raises TypeError on Time argument" do
lambda { Time.now + Time.now }.should raise_error(TypeError)
end
it "raises TypeError on nil argument" do
lambda { Time.now + nil }.should raise_error(TypeError)
end
#see [ruby-dev:38446]
it "tracks microseconds" do
time = Time.at(0)
time += Rational(123_456, 1_000_000)
time.usec.should == 123_456
time += Rational(654_321, 1_000_000)
time.usec.should == 777_777
end
it "tracks nanoseconds" do
time = Time.at(0)
time += Rational(123_456_789, 1_000_000_000)
time.nsec.should == 123_456_789
time += Rational(876_543_210, 1_000_000_000)
time.nsec.should == 999_999_999
end
it "maintains precision" do
t = Time.at(0) + Rational(8_999_999_999_999_999, 1_000_000_000_000_000)
t.should_not == Time.at(9)
end
it "maintains microseconds precision" do
time = Time.at(0) + Rational(8_999_999_999_999_999, 1_000_000_000_000_000)
time.usec.should == 999_999
end
it "maintains nanoseconds precision" do
time = Time.at(0) + Rational(8_999_999_999_999_999, 1_000_000_000_000_000)
time.nsec.should == 999_999_999
end
it "maintains subseconds precision" do
time = Time.at(0) + Rational(8_999_999_999_999_999, 1_000_000_000_000_000)
time.subsec.should == Rational(999_999_999_999_999, 1_000_000_000_000_000)
end
end

View file

@ -0,0 +1,33 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#round" do
before do
@time = Time.utc(2010, 3, 30, 5, 43, "25.123456789".to_r)
@subclass = Class.new(Time).now
end
it "defaults to rounding to 0 places" do
@time.round.should == Time.utc(2010, 3, 30, 5, 43, 25.to_r)
end
it "rounds to 0 decimal places with an explicit argument" do
@time.round(0).should == Time.utc(2010, 3, 30, 5, 43, 25.to_r)
end
it "rounds to 7 decimal places with an explicit argument" do
@time.round(7).should == Time.utc(2010, 3, 30, 5, 43, "25.1234568".to_r)
end
it "returns an instance of Time, even if #round is called on a subclass" do
@subclass.round.should be_an_instance_of(Time)
end
it "copies own timezone to the returning value" do
@time.zone.should == @time.round.zone
with_timezone "JST-9" do
time = Time.at 0, 1
time.zone.should == time.round.zone
end
end
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#saturday?" do
it "returns true if time represents Saturday" do
Time.local(2000, 1, 1).saturday?.should == true
end
it "returns false if time doesn't represent Saturday" do
Time.local(2000, 1, 2).saturday?.should == false
end
end

View file

@ -0,0 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#sec" do
it "returns the second of the minute(0..60) for time" do
Time.at(0).sec.should == 0
end
end

View file

@ -0,0 +1,6 @@
describe :time_asctime, shared: true do
it "returns a canonical string representation of time" do
t = Time.now
t.send(@method).should == t.strftime("%a %b %e %H:%M:%S %Y")
end
end

View file

@ -0,0 +1,15 @@
describe :time_day, shared: true do
it "returns the day of the month (1..n) for a local Time" do
with_timezone("CET", 1) do
Time.local(1970, 1, 1).send(@method).should == 1
end
end
it "returns the day of the month for a UTC Time" do
Time.utc(1970, 1, 1).send(@method).should == 1
end
it "returns the day of the month for a Time with a fixed offset" do
Time.new(2012, 1, 1, 0, 0, 0, -3600).send(@method).should == 1
end
end

View file

@ -0,0 +1,9 @@
describe :time_getgm, shared: true do
it "returns a new time which is the utc representation of time" do
# Testing with America/Regina here because it doesn't have DST.
with_timezone("CST", -6) do
t = Time.local(2007, 1, 9, 6, 0, 0)
t.send(@method).should == Time.gm(2007, 1, 9, 12, 0, 0)
end
end
end

View file

@ -0,0 +1,22 @@
describe :time_gm, shared: true do
it "creates a time based on given values, interpreted as UTC (GMT)" do
Time.send(@method, 2000,"jan",1,20,15,1).inspect.should == "2000-01-01 20:15:01 UTC"
end
it "creates a time based on given C-style gmtime arguments, interpreted as UTC (GMT)" do
time = Time.send(@method, 1, 15, 20, 1, 1, 2000, :ignored, :ignored, :ignored, :ignored)
time.inspect.should == "2000-01-01 20:15:01 UTC"
end
it "interprets pre-Gregorian reform dates using Gregorian proleptic calendar" do
Time.send(@method, 1582, 10, 4, 12).to_i.should == -12220200000 # 2299150j
end
it "interprets Julian-Gregorian gap dates using Gregorian proleptic calendar" do
Time.send(@method, 1582, 10, 14, 12).to_i.should == -12219336000 # 2299160j
end
it "interprets post-Gregorian reform dates using Gregorian calendar" do
Time.send(@method, 1582, 10, 15, 12).to_i.should == -12219249600 # 2299161j
end
end

View file

@ -0,0 +1,53 @@
describe :time_gmt_offset, shared: true do
it "returns the offset in seconds between the timezone of time and UTC" do
with_timezone("AST", 3) do
Time.new.send(@method).should == 10800
end
end
platform_is_not :windows do
it "returns the correct offset for US Eastern time zone around daylight savings time change" do
# "2010-03-14 01:59:59 -0500" + 1 ==> "2010-03-14 03:00:00 -0400"
with_timezone("EST5EDT") do
t = Time.local(2010,3,14,1,59,59)
t.send(@method).should == -5*60*60
(t + 1).send(@method).should == -4*60*60
end
end
it "returns the correct offset for Hawaii around daylight savings time change" do
# "2010-03-14 01:59:59 -1000" + 1 ==> "2010-03-14 02:00:00 -1000"
with_timezone("Pacific/Honolulu") do
t = Time.local(2010,3,14,1,59,59)
t.send(@method).should == -10*60*60
(t + 1).send(@method).should == -10*60*60
end
end
it "returns the correct offset for New Zealand around daylight savings time change" do
# "2010-04-04 02:59:59 +1300" + 1 ==> "2010-04-04 02:00:00 +1200"
with_timezone("Pacific/Auckland") do
t = Time.local(2010,4,4,1,59,59) + (60 * 60)
t.send(@method).should == 13*60*60
(t + 1).send(@method).should == 12*60*60
end
end
end
it "returns offset as Rational" do
Time.new(2010,4,4,1,59,59,7245).send(@method).should == 7245
Time.new(2010,4,4,1,59,59,7245.5).send(@method).should == Rational(14491,2)
end
context 'given positive offset' do
it 'returns a positive offset' do
Time.new(2013,3,17,nil,nil,nil,"+03:00").send(@method).should == 10800
end
end
context 'given negative offset' do
it 'returns a negative offset' do
Time.new(2013,3,17,nil,nil,nil,"-03:00").send(@method).should == -10800
end
end
end

View file

@ -0,0 +1,33 @@
describe :time_gmtime, shared: true do
it "converts self to UTC, modifying the receiver" do
# Testing with America/Regina here because it doesn't have DST.
with_timezone("CST", -6) do
t = Time.local(2007, 1, 9, 6, 0, 0)
t.send(@method)
t.should == Time.gm(2007, 1, 9, 12, 0, 0)
end
end
it "returns self" do
with_timezone("CST", -6) do
t = Time.local(2007, 1, 9, 12, 0, 0)
t.send(@method).should equal(t)
end
end
describe "on a frozen time" do
it "does not raise an error if already in UTC" do
time = Time.gm(2007, 1, 9, 12, 0, 0)
time.freeze
time.send(@method).should equal(time)
end
it "raises a RuntimeError if the time is not UTC" do
with_timezone("CST", -6) do
time = Time.now
time.freeze
lambda { time.send(@method) }.should raise_error(RuntimeError)
end
end
end
end

View file

@ -0,0 +1,23 @@
# -*- encoding: us-ascii -*-
describe :inspect, shared: true do
it "formats the local time following the pattern 'yyyy-MM-dd HH:mm:ss Z'" do
with_timezone("PST", +1) do
Time.local(2000, 1, 1, 20, 15, 1).send(@method).should == "2000-01-01 20:15:01 +0100"
end
end
it "formats the UTC time following the pattern 'yyyy-MM-dd HH:mm:ss UTC'" do
Time.utc(2000, 1, 1, 20, 15, 1).send(@method).should == "2000-01-01 20:15:01 UTC"
end
it "formats the fixed offset time following the pattern 'yyyy-MM-dd HH:mm:ss +/-HHMM'" do
Time.new(2000, 1, 1, 20, 15, 01, 3600).send(@method).should == "2000-01-01 20:15:01 +0100"
end
with_feature :encoding do
it "returns a US-ASCII encoded string" do
Time.now.send(@method).encoding.should equal(Encoding::US_ASCII)
end
end
end

View file

@ -0,0 +1,8 @@
describe :time_isdst, shared: true do
it "dst? returns whether time is during daylight saving time" do
with_timezone("America/Los_Angeles") do
Time.local(2007, 9, 9, 0, 0, 0).send(@method).should == true
Time.local(2007, 1, 9, 0, 0, 0).send(@method).should == false
end
end
end

View file

@ -0,0 +1,45 @@
describe :time_local, shared: true do
it "creates a time based on given values, interpreted in the local time zone" do
with_timezone("PST", -8) do
Time.send(@method, 2000, "jan", 1, 20, 15, 1).to_a.should ==
[1, 15, 20, 1, 1, 2000, 6, 1, false, "PST"]
end
end
platform_is_not :windows do
describe "timezone changes" do
it "correctly adjusts the timezone change to 'CEST' on 'Europe/Amsterdam'" do
with_timezone("Europe/Amsterdam") do
Time.send(@method, 1940, 5, 16).to_a.should ==
[0, 40, 1, 16, 5, 1940, 4, 137, true, "CEST"]
end
end
end
end
end
describe :time_local_10_arg, shared: true do
it "creates a time based on given C-style gmtime arguments, interpreted in the local time zone" do
with_timezone("PST", -8) do
Time.send(@method, 1, 15, 20, 1, 1, 2000, :ignored, :ignored, :ignored, :ignored).to_a.should ==
[1, 15, 20, 1, 1, 2000, 6, 1, false, "PST"]
end
end
platform_is_not :windows do
it "creates the correct time just before dst change" do
with_timezone("America/New_York") do
time = Time.send(@method, 0, 30, 1, 30, 10, 2005, 0, 0, true, ENV['TZ'])
time.utc_offset.should == -4 * 3600
end
end
it "creates the correct time just after dst change" do
with_timezone("America/New_York") do
time = Time.send(@method, 0, 30, 1, 30, 10, 2005, 0, 0, false, ENV['TZ'])
time.utc_offset.should == -5 * 3600
end
end
end
end

View file

@ -0,0 +1,15 @@
describe :time_month, shared: true do
it "returns the month of the year for a local Time" do
with_timezone("CET", 1) do
Time.local(1970, 1).send(@method).should == 1
end
end
it "returns the month of the year for a UTC Time" do
Time.utc(1970, 1).send(@method).should == 1
end
it "returns the four digit year for a Time with a fixed offset" do
Time.new(2012, 1, 1, 0, 0, 0, -3600).send(@method).should == 1
end
end

View file

@ -0,0 +1,8 @@
require File.expand_path('../../fixtures/classes', __FILE__)
describe :time_now, shared: true do
it "creates a subclass instance if called on a subclass" do
TimeSpecs::SubTime.send(@method).should be_an_instance_of(TimeSpecs::SubTime)
TimeSpecs::MethodHolder.send(@method).should be_an_instance_of(Time)
end
end

View file

@ -0,0 +1,260 @@
describe :time_params, shared: true do
it "accepts 1 argument (year)" do
Time.send(@method, 2000).should ==
Time.send(@method, 2000, 1, 1, 0, 0, 0)
end
it "accepts 2 arguments (year, month)" do
Time.send(@method, 2000, 2).should ==
Time.send(@method, 2000, 2, 1, 0, 0, 0)
end
it "accepts 3 arguments (year, month, day)" do
Time.send(@method, 2000, 2, 3).should ==
Time.send(@method, 2000, 2, 3, 0, 0, 0)
end
it "accepts 4 arguments (year, month, day, hour)" do
Time.send(@method, 2000, 2, 3, 4).should ==
Time.send(@method, 2000, 2, 3, 4, 0, 0)
end
it "accepts 5 arguments (year, month, day, hour, minute)" do
Time.send(@method, 2000, 2, 3, 4, 5).should ==
Time.send(@method, 2000, 2, 3, 4, 5, 0)
end
it "accepts a too big day of the month by going to the next month" do
Time.send(@method, 1999, 2, 31).should ==
Time.send(@method, 1999, 3, 3)
end
it "raises a TypeError if the year is nil" do
lambda { Time.send(@method, nil) }.should raise_error(TypeError)
end
it "accepts nil month, day, hour, minute, and second" do
Time.send(@method, 2000, nil, nil, nil, nil, nil).should ==
Time.send(@method, 2000)
end
it "handles a String year" do
Time.send(@method, "2000").should ==
Time.send(@method, 2000)
end
it "coerces the year with #to_int" do
m = mock(:int)
m.should_receive(:to_int).and_return(1)
Time.send(@method, m).should == Time.send(@method, 1)
end
it "handles a String month given as a numeral" do
Time.send(@method, 2000, "12").should ==
Time.send(@method, 2000, 12)
end
it "handles a String month given as a short month name" do
Time.send(@method, 2000, "dec").should ==
Time.send(@method, 2000, 12)
end
it "coerces the month with #to_str" do
(obj = mock('12')).should_receive(:to_str).and_return("12")
Time.send(@method, 2008, obj).should ==
Time.send(@method, 2008, 12)
end
it "coerces the month with #to_int" do
m = mock(:int)
m.should_receive(:to_int).and_return(1)
Time.send(@method, 2008, m).should == Time.send(@method, 2008, 1)
end
it "handles a String day" do
Time.send(@method, 2000, 12, "15").should ==
Time.send(@method, 2000, 12, 15)
end
it "coerces the day with #to_int" do
m = mock(:int)
m.should_receive(:to_int).and_return(1)
Time.send(@method, 2008, 1, m).should == Time.send(@method, 2008, 1, 1)
end
it "handles a String hour" do
Time.send(@method, 2000, 12, 1, "5").should ==
Time.send(@method, 2000, 12, 1, 5)
end
it "coerces the hour with #to_int" do
m = mock(:int)
m.should_receive(:to_int).and_return(1)
Time.send(@method, 2008, 1, 1, m).should == Time.send(@method, 2008, 1, 1, 1)
end
it "handles a String minute" do
Time.send(@method, 2000, 12, 1, 1, "8").should ==
Time.send(@method, 2000, 12, 1, 1, 8)
end
it "coerces the minute with #to_int" do
m = mock(:int)
m.should_receive(:to_int).and_return(1)
Time.send(@method, 2008, 1, 1, 0, m).should == Time.send(@method, 2008, 1, 1, 0, 1)
end
it "handles a String second" do
Time.send(@method, 2000, 12, 1, 1, 1, "8").should ==
Time.send(@method, 2000, 12, 1, 1, 1, 8)
end
it "coerces the second with #to_int" do
m = mock(:int)
m.should_receive(:to_int).and_return(1)
Time.send(@method, 2008, 1, 1, 0, 0, m).should == Time.send(@method, 2008, 1, 1, 0, 0, 1)
end
it "interprets all numerals as base 10" do
Time.send(@method, "2000", "08", "08", "08", "08", "08").should ==
Time.send(@method, 2000, 8, 8, 8, 8, 8)
Time.send(@method, "2000", "09", "09", "09", "09", "09").should ==
Time.send(@method, 2000, 9, 9, 9, 9, 9)
end
it "handles fractional seconds as a Float" do
t = Time.send(@method, 2000, 1, 1, 20, 15, 1.75)
t.sec.should == 1
t.usec.should == 750000
end
it "handles fractional seconds as a Rational" do
t = Time.send(@method, 2000, 1, 1, 20, 15, Rational(99, 10))
t.sec.should == 9
t.usec.should == 900000
end
it "handles years from 0 as such" do
0.upto(2100) do |year|
t = Time.send(@method, year)
t.year.should == year
end
end
it "accepts various year ranges" do
Time.send(@method, 1801, 12, 31, 23, 59, 59).wday.should == 4
Time.send(@method, 3000, 12, 31, 23, 59, 59).wday.should == 3
end
it "raises an ArgumentError for out of range month" do
lambda {
Time.send(@method, 2008, 13, 31, 23, 59, 59)
}.should raise_error(ArgumentError)
end
it "raises an ArgumentError for out of range day" do
lambda {
Time.send(@method, 2008, 12, 32, 23, 59, 59)
}.should raise_error(ArgumentError)
end
it "raises an ArgumentError for out of range hour" do
lambda {
Time.send(@method, 2008, 12, 31, 25, 59, 59)
}.should raise_error(ArgumentError)
end
it "raises an ArgumentError for out of range minute" do
lambda {
Time.send(@method, 2008, 12, 31, 23, 61, 59)
}.should raise_error(ArgumentError)
end
it "raises an ArgumentError for out of range second" do
lambda {
Time.send(@method, 2008, 12, 31, 23, 59, 61)
}.should raise_error(ArgumentError)
end
it "raises ArgumentError when given 9 arguments" do
lambda { Time.send(@method, *[0]*9) }.should raise_error(ArgumentError)
end
it "raises ArgumentError when given 11 arguments" do
lambda { Time.send(@method, *[0]*11) }.should raise_error(ArgumentError)
end
it "returns subclass instances" do
c = Class.new(Time)
c.send(@method, 2008, "12").should be_an_instance_of(c)
end
end
describe :time_params_10_arg, shared: true do
it "handles string arguments" do
Time.send(@method, "1", "15", "20", "1", "1", "2000", :ignored, :ignored,
:ignored, :ignored).should ==
Time.send(@method, 1, 15, 20, 1, 1, 2000, :ignored, :ignored, :ignored, :ignored)
end
it "handles float arguments" do
Time.send(@method, 1.0, 15.0, 20.0, 1.0, 1.0, 2000.0, :ignored, :ignored,
:ignored, :ignored).should ==
Time.send(@method, 1, 15, 20, 1, 1, 2000, :ignored, :ignored, :ignored, :ignored)
end
it "raises an ArgumentError for out of range values" do
lambda {
Time.send(@method, 61, 59, 23, 31, 12, 2008, :ignored, :ignored, :ignored, :ignored)
}.should raise_error(ArgumentError) # sec
lambda {
Time.send(@method, 59, 61, 23, 31, 12, 2008, :ignored, :ignored, :ignored, :ignored)
}.should raise_error(ArgumentError) # min
lambda {
Time.send(@method, 59, 59, 25, 31, 12, 2008, :ignored, :ignored, :ignored, :ignored)
}.should raise_error(ArgumentError) # hour
lambda {
Time.send(@method, 59, 59, 23, 32, 12, 2008, :ignored, :ignored, :ignored, :ignored)
}.should raise_error(ArgumentError) # day
lambda {
Time.send(@method, 59, 59, 23, 31, 13, 2008, :ignored, :ignored, :ignored, :ignored)
}.should raise_error(ArgumentError) # month
end
end
describe :time_params_microseconds, shared: true do
it "handles microseconds" do
t = Time.send(@method, 2000, 1, 1, 20, 15, 1, 123)
t.usec.should == 123
end
it "handles fractional microseconds as a Float" do
t = Time.send(@method, 2000, 1, 1, 20, 15, 1, 1.75)
t.usec.should == 1
t.nsec.should == 1750
end
it "handles fractional microseconds as a Rational" do
t = Time.send(@method, 2000, 1, 1, 20, 15, 1, Rational(99, 10))
t.usec.should == 9
t.nsec.should == 9900
end
it "ignores fractional seconds if a passed whole number of microseconds" do
t = Time.send(@method, 2000, 1, 1, 20, 15, 1.75, 2)
t.sec.should == 1
t.usec.should == 2
t.nsec.should == 2000
end
it "ignores fractional seconds if a passed fractional number of microseconds" do
t = Time.send(@method, 2000, 1, 1, 20, 15, 1.75, Rational(99, 10))
t.sec.should == 1
t.usec.should == 9
t.nsec.should == 9900
end
end

View file

@ -0,0 +1,9 @@
describe :time_to_i, shared: true do
it "returns the value of time as an integer number of seconds since epoch" do
Time.at(0).send(@method).should == 0
end
it "doesn't return an actual number of seconds in time" do
Time.at(65.5).send(@method).should == 65
end
end

View file

@ -0,0 +1,52 @@
# encoding: utf-8
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../../../shared/time/strftime_for_date', __FILE__)
require File.expand_path('../../../shared/time/strftime_for_time', __FILE__)
describe "Time#strftime" do
before :all do
@new_date = lambda { |y,m,d| Time.gm(y,m,d) }
@new_time = lambda { |*args| Time.gm(*args) }
@new_time_in_zone = lambda { |zone,offset,*args|
with_timezone(zone, offset) do
Time.new(*args)
end
}
@new_time_with_offset = lambda { |y,m,d,h,min,s,offset|
Time.new(y,m,d,h,min,s,offset)
}
@time = @new_time[2001, 2, 3, 4, 5, 6]
end
it_behaves_like :strftime_date, :strftime
it_behaves_like :strftime_time, :strftime
# Differences with date
it "requires an argument" do
lambda { @time.strftime }.should raise_error(ArgumentError)
end
# %Z is zone name or empty for Time
it "should be able to show the timezone if available" do
@time.strftime("%Z").should == @time.zone
with_timezone("UTC", 0) do
Time.gm(2000).strftime("%Z").should == "UTC"
end
Time.new(2000, 1, 1, 0, 0, 0, 42).strftime("%Z").should == ""
end
# %v is %e-%^b-%Y for Time
it "should be able to show the commercial week" do
@time.strftime("%v").should == " 3-FEB-2001"
@time.strftime("%v").should == @time.strftime('%e-%^b-%Y')
end
# Date/DateTime round at creation time, but Time does it in strftime.
it "rounds an offset to the nearest second when formatting with %z" do
time = @new_time_with_offset[2012, 1, 1, 0, 0, 0, Rational(36645, 10)]
time.strftime("%::z").should == "+01:01:05"
end
end

View file

@ -0,0 +1,27 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#subsec" do
it "returns 0 as a Fixnum for a Time with a whole number of seconds" do
Time.at(100).subsec.should eql(0)
end
it "returns the fractional seconds as a Rational for a Time constructed with a Rational number of seconds" do
Time.at(Rational(3, 2)).subsec.should eql(Rational(1, 2))
end
it "returns the fractional seconds as a Rational for a Time constructed with a Float number of seconds" do
Time.at(10.75).subsec.should eql(Rational(3, 4))
end
it "returns the fractional seconds as a Rational for a Time constructed with an Integer number of microseconds" do
Time.at(0, 999999).subsec.should eql(Rational(999999, 1000000))
end
it "returns the fractional seconds as a Rational for a Time constructed with an Rational number of microseconds" do
Time.at(0, Rational(9, 10)).subsec.should eql(Rational(9, 10000000))
end
it "returns the fractional seconds as a Rational for a Time constructed with an Float number of microseconds" do
Time.at(0, 0.75).subsec.should eql(Rational(3, 4000000))
end
end

View file

@ -0,0 +1,19 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#succ" do
it "returns a new time one second later than time" do
-> {
@result = Time.at(100).succ
}.should complain(/Time#succ is obsolete/)
@result.should == Time.at(101)
end
it "returns a new instance" do
t1 = Time.at(100)
t2 = nil
-> {
t2 = t1.succ
}.should complain(/Time#succ is obsolete/)
t1.object_id.should_not == t2.object_id
end
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#sunday?" do
it "returns true if time represents Sunday" do
Time.local(2000, 1, 2).sunday?.should == true
end
it "returns false if time doesn't represent Sunday" do
Time.local(2000, 1, 1).sunday?.should == false
end
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#thursday?" do
it "returns true if time represents Thursday" do
Time.local(2000, 1, 6).thursday?.should == true
end
it "returns false if time doesn't represent Thursday" do
Time.local(2000, 1, 1).thursday?.should == false
end
end

View file

@ -0,0 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time" do
it "includes Comparable" do
Time.include?(Comparable).should == true
end
end

View file

@ -0,0 +1,12 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#to_a" do
platform_is_not :windows do
it "returns a 10 element array representing the deconstructed time" do
# Testing with America/Regina here because it doesn't have DST.
with_timezone("America/Regina") do
Time.at(0).to_a.should == [0, 0, 18, 31, 12, 1969, 3, 365, false, "CST"]
end
end
end
end

View file

@ -0,0 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#to_f" do
it "returns the float number of seconds + usecs since the epoch" do
Time.at(100, 100).to_f.should == 100.0001
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/to_i', __FILE__)
describe "Time#to_i" do
it_behaves_like(:time_to_i, :to_i)
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#to_r" do
it "returns the a Rational representing seconds and subseconds since the epoch" do
Time.at(Rational(11, 10)).to_r.should eql(Rational(11, 10))
end
it "returns a Rational even for a whole number of seconds" do
Time.at(2).to_r.should eql(Rational(2))
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/inspect', __FILE__)
describe "Time#to_s" do
it_behaves_like :inspect, :to_s
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#tuesday?" do
it "returns true if time represents Tuesday" do
Time.local(2000, 1, 4).tuesday?.should == true
end
it "returns false if time doesn't represent Tuesday" do
Time.local(2000, 1, 1).tuesday?.should == false
end
end

View file

@ -0,0 +1,5 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#tv_nsec" do
it "needs to be reviewed for spec completeness"
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/to_i', __FILE__)
describe "Time#tv_sec" do
it_behaves_like(:time_to_i, :tv_sec)
end

View file

@ -0,0 +1,5 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#tv_usec" do
it "needs to be reviewed for spec completeness"
end

View file

@ -0,0 +1,39 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#usec" do
it "returns 0 for a Time constructed with a whole number of seconds" do
Time.at(100).usec.should == 0
end
it "returns the microseconds part of a Time constructed with a Float number of seconds" do
Time.at(10.75).usec.should == 750_000
end
it "returns the microseconds part of a Time constructed with an Integer number of microseconds" do
Time.at(0, 999_999).usec.should == 999_999
end
it "returns the microseconds part of a Time constructed with an Float number of microseconds > 1" do
Time.at(0, 3.75).usec.should == 3
end
it "returns 0 for a Time constructed with an Float number of microseconds < 1" do
Time.at(0, 0.75).usec.should == 0
end
it "returns the microseconds part of a Time constructed with a Rational number of seconds" do
Time.at(Rational(3, 2)).usec.should == 500_000
end
it "returns the microseconds part of a Time constructed with an Rational number of microseconds > 1" do
Time.at(0, Rational(99, 10)).usec.should == 9
end
it "returns 0 for a Time constructed with an Rational number of microseconds < 1" do
Time.at(0, Rational(9, 10)).usec.should == 0
end
it "returns the microseconds for time created by Time#local" do
Time.local(1,2,3,4,5,Rational(6.78)).usec.should == 780000
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/gmt_offset', __FILE__)
describe "Time#utc_offset" do
it_behaves_like(:time_gmt_offset, :utc_offset)
end

View file

@ -0,0 +1,21 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/gm', __FILE__)
require File.expand_path('../shared/gmtime', __FILE__)
require File.expand_path('../shared/time_params', __FILE__)
describe "Time#utc?" do
it "returns true if time represents a time in UTC (GMT)" do
Time.now.utc?.should == false
end
end
describe "Time.utc" do
it_behaves_like(:time_gm, :utc)
it_behaves_like(:time_params, :utc)
it_behaves_like(:time_params_10_arg, :utc)
it_behaves_like(:time_params_microseconds, :utc)
end
describe "Time#utc" do
it_behaves_like(:time_gmtime, :utc)
end

View file

@ -0,0 +1,9 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#wday" do
it "returns an integer representing the day of the week, 0..6, with Sunday being 0" do
with_timezone("GMT", 0) do
Time.at(0).wday.should == 4
end
end
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#wednesday?" do
it "returns true if time represents Wednesday" do
Time.local(2000, 1, 5).wednesday?.should == true
end
it "returns false if time doesn't represent Wednesday" do
Time.local(2000, 1, 1).wednesday?.should == false
end
end

View file

@ -0,0 +1,21 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#yday" do
it "returns an integer representing the day of the year, 1..366" do
with_timezone("UTC") do
Time.at(9999999).yday.should == 116
end
end
it 'returns the correct value for each day of each month' do
mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
yday = 1
mdays.each_with_index do |days, month|
days.times do |day|
Time.new(2014, month+1, day+1).yday.should == yday
yday += 1
end
end
end
end

View file

@ -0,0 +1,17 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#year" do
it "returns the four digit year for a local Time as an Integer" do
with_timezone("CET", 1) do
Time.local(1970).year.should == 1970
end
end
it "returns the four digit year for a UTC Time as an Integer" do
Time.utc(1970).year.should == 1970
end
it "returns the four digit year for a Time with a fixed offset" do
Time.new(2012, 1, 1, 0, 0, 0, -3600).year.should == 2012
end
end

View file

@ -0,0 +1,90 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Time#zone" do
platform_is_not :windows do
it "returns the time zone used for time" do
with_timezone("America/New_York") do
Time.new(2001, 1, 1, 0, 0, 0).zone.should == "EST"
Time.new(2001, 7, 1, 0, 0, 0).zone.should == "EDT"
%w[EST EDT].should include Time.now.zone
end
end
end
it "returns nil for a Time with a fixed offset" do
Time.new(2001, 1, 1, 0, 0, 0, "+05:00").zone.should == nil
end
platform_is_not :windows do
it "returns the correct timezone for a local time" do
t = Time.new(2005, 2, 27, 22, 50, 0, -3600)
with_timezone("America/New_York") do
t.getlocal.zone.should == "EST"
end
end
end
it "returns nil when getting the local time with a fixed offset" do
t = Time.new(2005, 2, 27, 22, 50, 0, -3600)
with_timezone("America/New_York") do
t.getlocal("+05:00").zone.should be_nil
end
end
describe "Encoding.default_internal is set" do
before :each do
@encoding = Encoding.default_internal
Encoding.default_internal = Encoding::UTF_8
end
after :each do
Encoding.default_internal = @encoding
end
it "returns an ASCII string" do
t = Time.new(2005, 2, 27, 22, 50, 0, -3600)
with_timezone("America/New_York") do
t.getlocal.zone.encoding.should == Encoding::US_ASCII
end
end
it "doesn't raise errors for a Time with a fixed offset" do
lambda {
Time.new(2001, 1, 1, 0, 0, 0, "+05:00").zone
}.should_not raise_error
end
end
it "returns UTC when called on a UTC time" do
Time.now.utc.zone.should == "UTC"
end
platform_is_not :aix, :windows do
it "defaults to UTC when bad zones given" do
with_timezone("hello-foo") do
Time.now.utc_offset.should == 0
end
with_timezone("1,2") do
Time.now.utc_offset.should == 0
end
with_timezone("Sun,Fri,2") do
Time.now.utc_offset.should == 0
end
end
end
platform_is :windows do
# See https://bugs.ruby-lang.org/issues/13591#note-11
it "defaults to UTC when bad zones given" do
with_timezone("1,2") do
Time.now.utc_offset.should == 0
end
with_timezone("12") do
Time.now.utc_offset.should == 0
end
end
end
end