summaryrefslogtreecommitdiff
path: root/spec/ruby/core/time
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/time')
-rw-r--r--spec/ruby/core/time/asctime_spec.rb6
-rw-r--r--spec/ruby/core/time/ctime_spec.rb5
-rw-r--r--spec/ruby/core/time/day_spec.rb15
-rw-r--r--spec/ruby/core/time/dst_spec.rb8
-rw-r--r--spec/ruby/core/time/getgm_spec.rb5
-rw-r--r--spec/ruby/core/time/getutc_spec.rb9
-rw-r--r--spec/ruby/core/time/gm_spec.rb9
-rw-r--r--spec/ruby/core/time/gmt_offset_spec.rb5
-rw-r--r--spec/ruby/core/time/gmt_spec.rb5
-rw-r--r--spec/ruby/core/time/gmtime_spec.rb5
-rw-r--r--spec/ruby/core/time/gmtoff_spec.rb5
-rw-r--r--spec/ruby/core/time/isdst_spec.rb5
-rw-r--r--spec/ruby/core/time/iso8601_spec.rb31
-rw-r--r--spec/ruby/core/time/mday_spec.rb5
-rw-r--r--spec/ruby/core/time/mktime_spec.rb10
-rw-r--r--spec/ruby/core/time/mon_spec.rb5
-rw-r--r--spec/ruby/core/time/month_spec.rb15
-rw-r--r--spec/ruby/core/time/shared/asctime.rb6
-rw-r--r--spec/ruby/core/time/shared/day.rb15
-rw-r--r--spec/ruby/core/time/shared/getgm.rb9
-rw-r--r--spec/ruby/core/time/shared/gm.rb70
-rw-r--r--spec/ruby/core/time/shared/gmt_offset.rb59
-rw-r--r--spec/ruby/core/time/shared/gmtime.rb40
-rw-r--r--spec/ruby/core/time/shared/isdst.rb8
-rw-r--r--spec/ruby/core/time/shared/month.rb15
-rw-r--r--spec/ruby/core/time/shared/to_i.rb16
-rw-r--r--spec/ruby/core/time/shared/xmlschema.rb31
-rw-r--r--spec/ruby/core/time/to_i_spec.rb16
-rw-r--r--spec/ruby/core/time/tv_nsec_spec.rb4
-rw-r--r--spec/ruby/core/time/tv_sec_spec.rb5
-rw-r--r--spec/ruby/core/time/tv_usec_spec.rb4
-rw-r--r--spec/ruby/core/time/utc_offset_spec.rb59
-rw-r--r--spec/ruby/core/time/utc_spec.rb113
-rw-r--r--spec/ruby/core/time/xmlschema_spec.rb7
34 files changed, 297 insertions, 328 deletions
diff --git a/spec/ruby/core/time/asctime_spec.rb b/spec/ruby/core/time/asctime_spec.rb
index a41ee531e4..17e155787a 100644
--- a/spec/ruby/core/time/asctime_spec.rb
+++ b/spec/ruby/core/time/asctime_spec.rb
@@ -1,6 +1,8 @@
require_relative '../../spec_helper'
-require_relative 'shared/asctime'
describe "Time#asctime" do
- it_behaves_like :time_asctime, :asctime
+ it "returns a canonical string representation of time" do
+ t = Time.now
+ t.asctime.should == t.strftime("%a %b %e %H:%M:%S %Y")
+ end
end
diff --git a/spec/ruby/core/time/ctime_spec.rb b/spec/ruby/core/time/ctime_spec.rb
index 57e7cfd9ce..b609b03974 100644
--- a/spec/ruby/core/time/ctime_spec.rb
+++ b/spec/ruby/core/time/ctime_spec.rb
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/asctime'
describe "Time#ctime" do
- it_behaves_like :time_asctime, :ctime
+ it "is an alias of Time#asctime" do
+ Time.instance_method(:ctime).should == Time.instance_method(:asctime)
+ end
end
diff --git a/spec/ruby/core/time/day_spec.rb b/spec/ruby/core/time/day_spec.rb
index 895bcd7a86..3dec17644c 100644
--- a/spec/ruby/core/time/day_spec.rb
+++ b/spec/ruby/core/time/day_spec.rb
@@ -1,6 +1,17 @@
require_relative '../../spec_helper'
-require_relative 'shared/day'
describe "Time#day" do
- it_behaves_like :time_day, :day
+ it "returns the day of the month (1..n) for a local Time" do
+ with_timezone("CET", 1) do
+ Time.local(1970, 1, 1).day.should == 1
+ end
+ end
+
+ it "returns the day of the month for a UTC Time" do
+ Time.utc(1970, 1, 1).day.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).day.should == 1
+ end
end
diff --git a/spec/ruby/core/time/dst_spec.rb b/spec/ruby/core/time/dst_spec.rb
index 436240aae5..42daf86875 100644
--- a/spec/ruby/core/time/dst_spec.rb
+++ b/spec/ruby/core/time/dst_spec.rb
@@ -1,6 +1,10 @@
require_relative '../../spec_helper'
-require_relative 'shared/isdst'
describe "Time#dst?" do
- it_behaves_like :time_isdst, :dst?
+ it "returns whether time is during daylight saving time" do
+ with_timezone("America/Los_Angeles") do
+ Time.local(2007, 9, 9, 0, 0, 0).dst?.should == true
+ Time.local(2007, 1, 9, 0, 0, 0).dst?.should == false
+ end
+ end
end
diff --git a/spec/ruby/core/time/getgm_spec.rb b/spec/ruby/core/time/getgm_spec.rb
index b5d45b1d9f..7698156c8c 100644
--- a/spec/ruby/core/time/getgm_spec.rb
+++ b/spec/ruby/core/time/getgm_spec.rb
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/getgm'
describe "Time#getgm" do
- it_behaves_like :time_getgm, :getgm
+ it "is an alias of Time#getutc" do
+ Time.instance_method(:getgm).should == Time.instance_method(:getutc)
+ end
end
diff --git a/spec/ruby/core/time/getutc_spec.rb b/spec/ruby/core/time/getutc_spec.rb
index 0cd9c17b00..1d49059a71 100644
--- a/spec/ruby/core/time/getutc_spec.rb
+++ b/spec/ruby/core/time/getutc_spec.rb
@@ -1,6 +1,11 @@
require_relative '../../spec_helper'
-require_relative 'shared/getgm'
describe "Time#getutc" do
- it_behaves_like :time_getgm, :getutc
+ 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.getutc.should == Time.gm(2007, 1, 9, 12, 0, 0)
+ end
+ end
end
diff --git a/spec/ruby/core/time/gm_spec.rb b/spec/ruby/core/time/gm_spec.rb
index 26dffbcedc..fbabede6ba 100644
--- a/spec/ruby/core/time/gm_spec.rb
+++ b/spec/ruby/core/time/gm_spec.rb
@@ -1,10 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/gm'
-require_relative 'shared/time_params'
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
+ it "is an alias of Time.utc" do
+ Time.method(:gm).should == Time.method(:utc)
+ end
end
diff --git a/spec/ruby/core/time/gmt_offset_spec.rb b/spec/ruby/core/time/gmt_offset_spec.rb
index df417e6e4e..1769981753 100644
--- a/spec/ruby/core/time/gmt_offset_spec.rb
+++ b/spec/ruby/core/time/gmt_offset_spec.rb
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/gmt_offset'
describe "Time#gmt_offset" do
- it_behaves_like :time_gmt_offset, :gmt_offset
+ it "is an alias of Time#utc_offset" do
+ Time.instance_method(:gmt_offset).should == Time.instance_method(:utc_offset)
+ end
end
diff --git a/spec/ruby/core/time/gmt_spec.rb b/spec/ruby/core/time/gmt_spec.rb
index 840f59e0e8..38e98cc43c 100644
--- a/spec/ruby/core/time/gmt_spec.rb
+++ b/spec/ruby/core/time/gmt_spec.rb
@@ -1,8 +1,7 @@
require_relative '../../spec_helper'
describe "Time#gmt?" do
- it "returns true if time represents a time in UTC (GMT)" do
- Time.now.should_not.gmt?
- Time.now.gmtime.should.gmt?
+ it "is an alias of Time#utc?" do
+ Time.instance_method(:gmt?).should == Time.instance_method(:utc?)
end
end
diff --git a/spec/ruby/core/time/gmtime_spec.rb b/spec/ruby/core/time/gmtime_spec.rb
index d965cd541d..e7e1d4ffb2 100644
--- a/spec/ruby/core/time/gmtime_spec.rb
+++ b/spec/ruby/core/time/gmtime_spec.rb
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/gmtime'
describe "Time#gmtime" do
- it_behaves_like :time_gmtime, :gmtime
+ it "is an alias of Time#utc" do
+ Time.instance_method(:gmtime).should == Time.instance_method(:utc)
+ end
end
diff --git a/spec/ruby/core/time/gmtoff_spec.rb b/spec/ruby/core/time/gmtoff_spec.rb
index fa28520e9e..c7d801a681 100644
--- a/spec/ruby/core/time/gmtoff_spec.rb
+++ b/spec/ruby/core/time/gmtoff_spec.rb
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/gmt_offset'
describe "Time#gmtoff" do
- it_behaves_like :time_gmt_offset, :gmtoff
+ it "is an alias of Time#utc_offset" do
+ Time.instance_method(:gmtoff).should == Time.instance_method(:utc_offset)
+ end
end
diff --git a/spec/ruby/core/time/isdst_spec.rb b/spec/ruby/core/time/isdst_spec.rb
index 173230ca07..759953cca7 100644
--- a/spec/ruby/core/time/isdst_spec.rb
+++ b/spec/ruby/core/time/isdst_spec.rb
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/isdst'
describe "Time#isdst" do
- it_behaves_like :time_isdst, :isdst
+ it "is an alias of Time#dst?" do
+ Time.instance_method(:isdst).should == Time.instance_method(:dst?)
+ end
end
diff --git a/spec/ruby/core/time/iso8601_spec.rb b/spec/ruby/core/time/iso8601_spec.rb
index ad60c3bb32..a6efc57b28 100644
--- a/spec/ruby/core/time/iso8601_spec.rb
+++ b/spec/ruby/core/time/iso8601_spec.rb
@@ -1,6 +1,33 @@
require_relative '../../spec_helper'
-require_relative 'shared/xmlschema'
describe "Time#iso8601" do
- it_behaves_like :time_xmlschema, :iso8601
+ ruby_version_is "3.4" do
+ it "generates ISO-8601 strings in Z for UTC times" do
+ t = Time.utc(1985, 4, 12, 23, 20, 50, 521245)
+ t.iso8601.should == "1985-04-12T23:20:50Z"
+ t.iso8601(2).should == "1985-04-12T23:20:50.52Z"
+ t.iso8601(9).should == "1985-04-12T23:20:50.521245000Z"
+ end
+
+ it "generates ISO-8601 string with timezone offset for non-UTC times" do
+ t = Time.new(1985, 4, 12, 23, 20, 50, "+02:00")
+ t.iso8601.should == "1985-04-12T23:20:50+02:00"
+ t.iso8601(2).should == "1985-04-12T23:20:50.00+02:00"
+ end
+
+ it "year is always at least 4 digits" do
+ t = Time.utc(12, 4, 12)
+ t.iso8601.should == "0012-04-12T00:00:00Z"
+ end
+
+ it "year can be more than 4 digits" do
+ t = Time.utc(40_000, 4, 12)
+ t.iso8601.should == "40000-04-12T00:00:00Z"
+ end
+
+ it "year can be negative" do
+ t = Time.utc(-2000, 4, 12)
+ t.iso8601.should == "-2000-04-12T00:00:00Z"
+ end
+ end
end
diff --git a/spec/ruby/core/time/mday_spec.rb b/spec/ruby/core/time/mday_spec.rb
index 3c21939890..78021785f9 100644
--- a/spec/ruby/core/time/mday_spec.rb
+++ b/spec/ruby/core/time/mday_spec.rb
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/day'
describe "Time#mday" do
- it_behaves_like :time_day, :mday
+ it "is an alias of Time#day" do
+ Time.instance_method(:mday).should == Time.instance_method(:day)
+ end
end
diff --git a/spec/ruby/core/time/mktime_spec.rb b/spec/ruby/core/time/mktime_spec.rb
index 78a6a6e772..83bf12293a 100644
--- a/spec/ruby/core/time/mktime_spec.rb
+++ b/spec/ruby/core/time/mktime_spec.rb
@@ -1,11 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/local'
-require_relative 'shared/time_params'
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
+ it "is an alias of Time.local" do
+ Time.method(:mktime).should == Time.method(:local)
+ end
end
diff --git a/spec/ruby/core/time/mon_spec.rb b/spec/ruby/core/time/mon_spec.rb
index f41b39648b..d57549dadd 100644
--- a/spec/ruby/core/time/mon_spec.rb
+++ b/spec/ruby/core/time/mon_spec.rb
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/month'
describe "Time#mon" do
- it_behaves_like :time_month, :mon
+ it "is an alias of Time#month" do
+ Time.instance_method(:mon).should == Time.instance_method(:month)
+ end
end
diff --git a/spec/ruby/core/time/month_spec.rb b/spec/ruby/core/time/month_spec.rb
index 81e20384ab..eae0e85acd 100644
--- a/spec/ruby/core/time/month_spec.rb
+++ b/spec/ruby/core/time/month_spec.rb
@@ -1,6 +1,17 @@
require_relative '../../spec_helper'
-require_relative 'shared/month'
describe "Time#month" do
- it_behaves_like :time_month, :month
+ it "returns the month of the year for a local Time" do
+ with_timezone("CET", 1) do
+ Time.local(1970, 1).month.should == 1
+ end
+ end
+
+ it "returns the month of the year for a UTC Time" do
+ Time.utc(1970, 1).month.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).month.should == 1
+ end
end
diff --git a/spec/ruby/core/time/shared/asctime.rb b/spec/ruby/core/time/shared/asctime.rb
deleted file mode 100644
index d096666863..0000000000
--- a/spec/ruby/core/time/shared/asctime.rb
+++ /dev/null
@@ -1,6 +0,0 @@
-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
diff --git a/spec/ruby/core/time/shared/day.rb b/spec/ruby/core/time/shared/day.rb
deleted file mode 100644
index 472dc959c1..0000000000
--- a/spec/ruby/core/time/shared/day.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-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
diff --git a/spec/ruby/core/time/shared/getgm.rb b/spec/ruby/core/time/shared/getgm.rb
deleted file mode 100644
index 3576365772..0000000000
--- a/spec/ruby/core/time/shared/getgm.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-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
diff --git a/spec/ruby/core/time/shared/gm.rb b/spec/ruby/core/time/shared/gm.rb
deleted file mode 100644
index 0ee602c837..0000000000
--- a/spec/ruby/core/time/shared/gm.rb
+++ /dev/null
@@ -1,70 +0,0 @@
-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
-
- it "handles fractional usec close to rounding limit" do
- time = Time.send(@method, 2000, 1, 1, 12, 30, 0, 9999r/10000)
-
- time.usec.should == 0
- time.nsec.should == 999
- end
-
- guard -> {
- with_timezone 'right/UTC' do
- (Time.gm(1972, 6, 30, 23, 59, 59) + 1).sec == 60
- end
- } do
- it "handles real leap seconds in zone 'right/UTC'" do
- with_timezone 'right/UTC' do
- time = Time.send(@method, 1972, 6, 30, 23, 59, 60)
-
- time.sec.should == 60
- time.min.should == 59
- time.hour.should == 23
- time.day.should == 30
- time.month.should == 6
- end
- end
- end
-
- it "handles bad leap seconds by carrying values forward" do
- with_timezone 'UTC' do
- time = Time.send(@method, 2017, 7, 5, 23, 59, 60)
- time.sec.should == 0
- time.min.should == 0
- time.hour.should == 0
- time.day.should == 6
- time.month.should == 7
- end
- end
-
- it "handles a value of 60 for seconds by carrying values forward in zone 'UTC'" do
- with_timezone 'UTC' do
- time = Time.send(@method, 1972, 6, 30, 23, 59, 60)
-
- time.sec.should == 0
- time.min.should == 0
- time.hour.should == 0
- time.day.should == 1
- time.month.should == 7
- end
- end
-end
diff --git a/spec/ruby/core/time/shared/gmt_offset.rb b/spec/ruby/core/time/shared/gmt_offset.rb
deleted file mode 100644
index 839566c249..0000000000
--- a/spec/ruby/core/time/shared/gmt_offset.rb
+++ /dev/null
@@ -1,59 +0,0 @@
-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
-
- it "returns 0 when the date is UTC" do
- with_timezone("AST", 3) do
- Time.new.utc.send(@method).should == 0
- 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
diff --git a/spec/ruby/core/time/shared/gmtime.rb b/spec/ruby/core/time/shared/gmtime.rb
deleted file mode 100644
index aa76b436cc..0000000000
--- a/spec/ruby/core/time/shared/gmtime.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-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)
- # Time#== compensates for time zones, so check all parts separately
- t.year.should == 2007
- t.month.should == 1
- t.mday.should == 9
- t.hour.should == 12
- t.min.should == 0
- t.sec.should == 0
- t.zone.should == "UTC"
- 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 FrozenError if the time is not UTC" do
- with_timezone("CST", -6) do
- time = Time.now
- time.freeze
- -> { time.send(@method) }.should.raise(FrozenError)
- end
- end
- end
-end
diff --git a/spec/ruby/core/time/shared/isdst.rb b/spec/ruby/core/time/shared/isdst.rb
deleted file mode 100644
index bc6d139230..0000000000
--- a/spec/ruby/core/time/shared/isdst.rb
+++ /dev/null
@@ -1,8 +0,0 @@
-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
diff --git a/spec/ruby/core/time/shared/month.rb b/spec/ruby/core/time/shared/month.rb
deleted file mode 100644
index 31ca679557..0000000000
--- a/spec/ruby/core/time/shared/month.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-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
diff --git a/spec/ruby/core/time/shared/to_i.rb b/spec/ruby/core/time/shared/to_i.rb
deleted file mode 100644
index 06c966b708..0000000000
--- a/spec/ruby/core/time/shared/to_i.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-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
-
- it "rounds fractional seconds toward zero" do
- t = Time.utc(1960, 1, 1, 0, 0, 0, 999_999)
-
- t.to_f.to_i.should == -315619199
- t.to_i.should == -315619200
- end
-end
diff --git a/spec/ruby/core/time/shared/xmlschema.rb b/spec/ruby/core/time/shared/xmlschema.rb
deleted file mode 100644
index d68c18df36..0000000000
--- a/spec/ruby/core/time/shared/xmlschema.rb
+++ /dev/null
@@ -1,31 +0,0 @@
-describe :time_xmlschema, shared: true do
- ruby_version_is "3.4" do
- it "generates ISO-8601 strings in Z for UTC times" do
- t = Time.utc(1985, 4, 12, 23, 20, 50, 521245)
- t.send(@method).should == "1985-04-12T23:20:50Z"
- t.send(@method, 2).should == "1985-04-12T23:20:50.52Z"
- t.send(@method, 9).should == "1985-04-12T23:20:50.521245000Z"
- end
-
- it "generates ISO-8601 string with timeone offset for non-UTC times" do
- t = Time.new(1985, 4, 12, 23, 20, 50, "+02:00")
- t.send(@method).should == "1985-04-12T23:20:50+02:00"
- t.send(@method, 2).should == "1985-04-12T23:20:50.00+02:00"
- end
-
- it "year is always at least 4 digits" do
- t = Time.utc(12, 4, 12)
- t.send(@method).should == "0012-04-12T00:00:00Z"
- end
-
- it "year can be more than 4 digits" do
- t = Time.utc(40_000, 4, 12)
- t.send(@method).should == "40000-04-12T00:00:00Z"
- end
-
- it "year can be negative" do
- t = Time.utc(-2000, 4, 12)
- t.send(@method).should == "-2000-04-12T00:00:00Z"
- end
- end
-end
diff --git a/spec/ruby/core/time/to_i_spec.rb b/spec/ruby/core/time/to_i_spec.rb
index 54929d1e18..00c4215d31 100644
--- a/spec/ruby/core/time/to_i_spec.rb
+++ b/spec/ruby/core/time/to_i_spec.rb
@@ -1,6 +1,18 @@
require_relative '../../spec_helper'
-require_relative 'shared/to_i'
describe "Time#to_i" do
- it_behaves_like :time_to_i, :to_i
+ it "returns the value of time as an integer number of seconds since epoch" do
+ Time.at(0).to_i.should == 0
+ end
+
+ it "doesn't return an actual number of seconds in time" do
+ Time.at(65.5).to_i.should == 65
+ end
+
+ it "rounds fractional seconds toward zero" do
+ t = Time.utc(1960, 1, 1, 0, 0, 0, 999_999)
+
+ t.to_f.to_i.should == -315619199
+ t.to_i.should == -315619200
+ end
end
diff --git a/spec/ruby/core/time/tv_nsec_spec.rb b/spec/ruby/core/time/tv_nsec_spec.rb
index feb7998b16..802138bf3a 100644
--- a/spec/ruby/core/time/tv_nsec_spec.rb
+++ b/spec/ruby/core/time/tv_nsec_spec.rb
@@ -1,5 +1,7 @@
require_relative '../../spec_helper'
describe "Time#tv_nsec" do
- it "needs to be reviewed for spec completeness"
+ it "is an alias of Time#nsec" do
+ Time.instance_method(:tv_nsec).should == Time.instance_method(:nsec)
+ end
end
diff --git a/spec/ruby/core/time/tv_sec_spec.rb b/spec/ruby/core/time/tv_sec_spec.rb
index f83e1fbfdd..21bdb53ee6 100644
--- a/spec/ruby/core/time/tv_sec_spec.rb
+++ b/spec/ruby/core/time/tv_sec_spec.rb
@@ -1,6 +1,7 @@
require_relative '../../spec_helper'
-require_relative 'shared/to_i'
describe "Time#tv_sec" do
- it_behaves_like :time_to_i, :tv_sec
+ it "is an alias of Time#to_i" do
+ Time.instance_method(:tv_sec).should == Time.instance_method(:to_i)
+ end
end
diff --git a/spec/ruby/core/time/tv_usec_spec.rb b/spec/ruby/core/time/tv_usec_spec.rb
index f0de4f4a9c..e922ee5625 100644
--- a/spec/ruby/core/time/tv_usec_spec.rb
+++ b/spec/ruby/core/time/tv_usec_spec.rb
@@ -1,5 +1,7 @@
require_relative '../../spec_helper'
describe "Time#tv_usec" do
- it "needs to be reviewed for spec completeness"
+ it "is an alias of Time#usec" do
+ Time.instance_method(:tv_usec).should == Time.instance_method(:usec)
+ end
end
diff --git a/spec/ruby/core/time/utc_offset_spec.rb b/spec/ruby/core/time/utc_offset_spec.rb
index 17c031b8c6..8d2fff2012 100644
--- a/spec/ruby/core/time/utc_offset_spec.rb
+++ b/spec/ruby/core/time/utc_offset_spec.rb
@@ -1,6 +1,61 @@
require_relative '../../spec_helper'
-require_relative 'shared/gmt_offset'
describe "Time#utc_offset" do
- it_behaves_like :time_gmt_offset, :utc_offset
+ it "returns the offset in seconds between the timezone of time and UTC" do
+ with_timezone("AST", 3) do
+ Time.new.utc_offset.should == 10800
+ end
+ end
+
+ it "returns 0 when the date is UTC" do
+ with_timezone("AST", 3) do
+ Time.new.utc.utc_offset.should == 0
+ 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.utc_offset.should == -5*60*60
+ (t + 1).utc_offset.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.utc_offset.should == -10*60*60
+ (t + 1).utc_offset.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.utc_offset.should == 13*60*60
+ (t + 1).utc_offset.should == 12*60*60
+ end
+ end
+ end
+
+ it "returns offset as Rational" do
+ Time.new(2010,4,4,1,59,59,7245).utc_offset.should == 7245
+ Time.new(2010,4,4,1,59,59,7245.5).utc_offset.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").utc_offset.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").utc_offset.should == -10800
+ end
+ end
end
diff --git a/spec/ruby/core/time/utc_spec.rb b/spec/ruby/core/time/utc_spec.rb
index ab3c0df657..35c1daa9e5 100644
--- a/spec/ruby/core/time/utc_spec.rb
+++ b/spec/ruby/core/time/utc_spec.rb
@@ -1,6 +1,4 @@
require_relative '../../spec_helper'
-require_relative 'shared/gm'
-require_relative 'shared/gmtime'
require_relative 'shared/time_params'
describe "Time#utc?" do
@@ -11,7 +9,7 @@ describe "Time#utc?" do
it "treats time as UTC what was created in different ways" do
Time.now.utc.utc?.should == true
- Time.now.gmtime.utc?.should == true
+ Time.now.utc.utc?.should == true
Time.now.getgm.utc?.should == true
Time.now.getutc.utc?.should == true
Time.utc(2022).utc?.should == true
@@ -55,12 +53,117 @@ describe "Time#utc?" do
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
+
+ it "creates a time based on given values, interpreted as UTC (GMT)" do
+ Time.utc(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.utc(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.utc(1582, 10, 4, 12).to_i.should == -12220200000 # 2299150j
+ end
+
+ it "interprets Julian-Gregorian gap dates using Gregorian proleptic calendar" do
+ Time.utc(1582, 10, 14, 12).to_i.should == -12219336000 # 2299160j
+ end
+
+ it "interprets post-Gregorian reform dates using Gregorian calendar" do
+ Time.utc(1582, 10, 15, 12).to_i.should == -12219249600 # 2299161j
+ end
+
+ it "handles fractional usec close to rounding limit" do
+ time = Time.utc(2000, 1, 1, 12, 30, 0, 9999r/10000)
+
+ time.usec.should == 0
+ time.nsec.should == 999
+ end
+
+ guard -> {
+ with_timezone 'right/UTC' do
+ (Time.utc(1972, 6, 30, 23, 59, 59) + 1).sec == 60
+ end
+ } do
+ it "handles real leap seconds in zone 'right/UTC'" do
+ with_timezone 'right/UTC' do
+ time = Time.utc(1972, 6, 30, 23, 59, 60)
+
+ time.sec.should == 60
+ time.min.should == 59
+ time.hour.should == 23
+ time.day.should == 30
+ time.month.should == 6
+ end
+ end
+ end
+
+ it "handles bad leap seconds by carrying values forward" do
+ with_timezone 'UTC' do
+ time = Time.utc(2017, 7, 5, 23, 59, 60)
+ time.sec.should == 0
+ time.min.should == 0
+ time.hour.should == 0
+ time.day.should == 6
+ time.month.should == 7
+ end
+ end
+
+ it "handles a value of 60 for seconds by carrying values forward in zone 'UTC'" do
+ with_timezone 'UTC' do
+ time = Time.utc(1972, 6, 30, 23, 59, 60)
+
+ time.sec.should == 0
+ time.min.should == 0
+ time.hour.should == 0
+ time.day.should == 1
+ time.month.should == 7
+ end
+ end
end
describe "Time#utc" do
- it_behaves_like :time_gmtime, :utc
+ 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.utc
+ # Time#== compensates for time zones, so check all parts separately
+ t.year.should == 2007
+ t.month.should == 1
+ t.mday.should == 9
+ t.hour.should == 12
+ t.min.should == 0
+ t.sec.should == 0
+ t.zone.should == "UTC"
+ end
+ end
+
+ it "returns self" do
+ with_timezone("CST", -6) do
+ t = Time.local(2007, 1, 9, 12, 0, 0)
+ t.utc.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.utc.should.equal?(time)
+ end
+
+ it "raises a FrozenError if the time is not UTC" do
+ with_timezone("CST", -6) do
+ time = Time.now
+ time.freeze
+ -> { time.utc }.should.raise(FrozenError)
+ end
+ end
+ end
end
diff --git a/spec/ruby/core/time/xmlschema_spec.rb b/spec/ruby/core/time/xmlschema_spec.rb
index bdf1dc7923..6a26861a45 100644
--- a/spec/ruby/core/time/xmlschema_spec.rb
+++ b/spec/ruby/core/time/xmlschema_spec.rb
@@ -1,6 +1,9 @@
require_relative '../../spec_helper'
-require_relative 'shared/xmlschema'
describe "Time#xmlschema" do
- it_behaves_like :time_xmlschema, :xmlschema
+ ruby_version_is "3.4" do
+ it "is an alias of Time#iso8601" do
+ Time.instance_method(:xmlschema).should == Time.instance_method(:iso8601)
+ end
+ end
end