From 4e877936da7c23b51e1ebc54b5590c927af4015f Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Tue, 10 Oct 2017 20:32:12 +0000 Subject: [PATCH 1/6] Time#to_time spec --- library/time/to_time_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 library/time/to_time_spec.rb diff --git a/library/time/to_time_spec.rb b/library/time/to_time_spec.rb new file mode 100644 index 0000000000..c25cab1154 --- /dev/null +++ b/library/time/to_time_spec.rb @@ -0,0 +1,16 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'time' + +ruby_version_is "2.4" do + describe "Time#to_time" do + it "returns itself in the same timezone" do + with_timezone("America/Regina") do + time = Time.new(2012, 2, 21, 10, 11, 12) + time.to_time.should == time + end + + time2 = Time.utc(2012, 2, 21, 10, 11, 12) + time2.to_time.should == time2 + end + end +end From 987780446130174a1a96292ae312ffd45b37b6be Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Wed, 11 Oct 2017 02:01:08 +0000 Subject: [PATCH 2/6] Adds specs for DateTime#to_time Covers the copying of the time from DateTime to Time, and the maintaining of the timezone by not shifting the hours. --- library/datetime/to_time_spec.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/library/datetime/to_time_spec.rb b/library/datetime/to_time_spec.rb index aa2902930c..36f57124dc 100644 --- a/library/datetime/to_time_spec.rb +++ b/library/datetime/to_time_spec.rb @@ -1,6 +1,24 @@ require File.expand_path('../../../spec_helper', __FILE__) require 'date' -describe "DateTime#to_time" do - it "needs to be reviewed for spec completeness" +ruby_version_is "2.4" do + describe "DateTime#to_time" do + it "yields a new Time object" do + DateTime.now.to_time.should be_kind_of(Time) + end + + it "preserves the same time regardless of local time or zone" do + with_timezone("Pactific/Pago_Pago", -11) do + date = DateTime.new(2012, 12, 24, 12, 23, 00, '+03:00') + time = date.to_time + + time.year.should == date.year + time.mon.should == date.mon + time.day.should == date.day + time.hour.should == date.hour + time.min.should == date.min + time.sec.should == date.sec + end + end + end end From d8857135cc9f160d40f6d46a5c106c4df67e7c60 Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Wed, 11 Oct 2017 02:43:37 +0000 Subject: [PATCH 3/6] Adds timezone comparison Comparing timezones in the spec proved slightly challenging due to incompatible equivalence methods, so just strftime %z to validate. --- library/datetime/to_time_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/library/datetime/to_time_spec.rb b/library/datetime/to_time_spec.rb index 36f57124dc..bc64163e90 100644 --- a/library/datetime/to_time_spec.rb +++ b/library/datetime/to_time_spec.rb @@ -12,6 +12,7 @@ date = DateTime.new(2012, 12, 24, 12, 23, 00, '+03:00') time = date.to_time + time.strftime("%z").should == date.strftime("%z") time.year.should == date.year time.mon.should == date.mon time.day.should == date.day From 3e49c44b7635c79c5c50a63f4ad306a26cb6dd9d Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Wed, 11 Oct 2017 12:10:06 +0000 Subject: [PATCH 4/6] Updates for clarity on to_time --- library/datetime/to_time_spec.rb | 32 +++++++++++++++----------------- library/time/to_time_spec.rb | 3 ++- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/library/datetime/to_time_spec.rb b/library/datetime/to_time_spec.rb index bc64163e90..500da94fa4 100644 --- a/library/datetime/to_time_spec.rb +++ b/library/datetime/to_time_spec.rb @@ -1,25 +1,23 @@ require File.expand_path('../../../spec_helper', __FILE__) require 'date' -ruby_version_is "2.4" do - describe "DateTime#to_time" do - it "yields a new Time object" do - DateTime.now.to_time.should be_kind_of(Time) - end +describe "DateTime#to_time" do + it "yields a new Time object" do + DateTime.now.to_time.should be_kind_of(Time) + end - it "preserves the same time regardless of local time or zone" do - with_timezone("Pactific/Pago_Pago", -11) do - date = DateTime.new(2012, 12, 24, 12, 23, 00, '+03:00') - time = date.to_time + it "preserves the same time regardless of local time or zone" do + date = DateTime.new(2012, 12, 24, 12, 23, 00, '+03:00') + time = date.to_time - time.strftime("%z").should == date.strftime("%z") - time.year.should == date.year - time.mon.should == date.mon - time.day.should == date.day - time.hour.should == date.hour - time.min.should == date.min - time.sec.should == date.sec - end + with_timezone("Pactific/Pago_Pago", -11) do + time.utc_offset.should == 3 * 3600 + time.year.should == date.year + time.mon.should == date.mon + time.day.should == date.day + time.hour.should == date.hour + time.min.should == date.min + time.sec.should == date.sec end end end diff --git a/library/time/to_time_spec.rb b/library/time/to_time_spec.rb index c25cab1154..085561876a 100644 --- a/library/time/to_time_spec.rb +++ b/library/time/to_time_spec.rb @@ -4,8 +4,9 @@ ruby_version_is "2.4" do describe "Time#to_time" do it "returns itself in the same timezone" do + time = Time.new(2012, 2, 21, 10, 11, 12) + with_timezone("America/Regina") do - time = Time.new(2012, 2, 21, 10, 11, 12) time.to_time.should == time end From 2956ad1dcc0eaf4566da5c9c7ef90b1084d6ea5e Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Wed, 11 Oct 2017 14:54:10 +0000 Subject: [PATCH 5/6] Version guard on timezone preservation of DateTime#to_time --- library/datetime/to_time_spec.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/library/datetime/to_time_spec.rb b/library/datetime/to_time_spec.rb index 500da94fa4..5946a04008 100644 --- a/library/datetime/to_time_spec.rb +++ b/library/datetime/to_time_spec.rb @@ -6,18 +6,20 @@ DateTime.now.to_time.should be_kind_of(Time) end - it "preserves the same time regardless of local time or zone" do - date = DateTime.new(2012, 12, 24, 12, 23, 00, '+03:00') - time = date.to_time + ruby_version_is "2.4" do + it "preserves the same time regardless of local time or zone" do + date = DateTime.new(2012, 12, 24, 12, 23, 00, '+03:00') + time = date.to_time - with_timezone("Pactific/Pago_Pago", -11) do - time.utc_offset.should == 3 * 3600 - time.year.should == date.year - time.mon.should == date.mon - time.day.should == date.day - time.hour.should == date.hour - time.min.should == date.min - time.sec.should == date.sec + with_timezone("Pactific/Pago_Pago", -11) do + time.utc_offset.should == 3 * 3600 + time.year.should == date.year + time.mon.should == date.mon + time.day.should == date.day + time.hour.should == date.hour + time.min.should == date.min + time.sec.should == date.sec + end end end end From 2ebbac1b741440c68c78e071cf910b860f0484d2 Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Wed, 11 Oct 2017 16:15:21 +0000 Subject: [PATCH 6/6] Moves the to_time call into the with_timezone to verify no timezone shift --- library/datetime/to_time_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/datetime/to_time_spec.rb b/library/datetime/to_time_spec.rb index 5946a04008..f5b7cb8a23 100644 --- a/library/datetime/to_time_spec.rb +++ b/library/datetime/to_time_spec.rb @@ -9,9 +9,10 @@ ruby_version_is "2.4" do it "preserves the same time regardless of local time or zone" do date = DateTime.new(2012, 12, 24, 12, 23, 00, '+03:00') - time = date.to_time with_timezone("Pactific/Pago_Pago", -11) do + time = date.to_time + time.utc_offset.should == 3 * 3600 time.year.should == date.year time.mon.should == date.mon