From f8a581aa71905d60e4e282682faaa21e0ea17e92 Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Thu, 12 Oct 2017 03:13:27 +0000 Subject: [PATCH 01/12] Adds DateTime#to_datetime --- library/datetime/to_datetime_spec.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/datetime/to_datetime_spec.rb b/library/datetime/to_datetime_spec.rb index e289f8ce36..e4db9558f1 100644 --- a/library/datetime/to_datetime_spec.rb +++ b/library/datetime/to_datetime_spec.rb @@ -2,5 +2,8 @@ require 'date' describe "DateTime#to_datetime" do - it "needs to be reviewed for spec completeness" + it "returns itself" do + dt = DateTime.new(2012, 12, 24, 12, 23, 00, '+05:00') + dt.to_datetime.should == dt + end end From 7d32193d4dd34a8b36465f12425e827bbfec6b2e Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Thu, 12 Oct 2017 03:13:40 +0000 Subject: [PATCH 02/12] Adds DateTime#to_date --- library/datetime/to_date_spec.rb | 33 +++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/library/datetime/to_date_spec.rb b/library/datetime/to_date_spec.rb index 915e1ac87a..387eda9229 100644 --- a/library/datetime/to_date_spec.rb +++ b/library/datetime/to_date_spec.rb @@ -2,5 +2,36 @@ require 'date' describe "DateTime#to_date" do - it "needs to be reviewed for spec completeness" + it "returns an instance of Date" do + dt = DateTime.new(2012, 12, 24, 12, 23, 00, '+05:00') + dt.to_date.should be_kind_of(Date) + end + + it "maintains the same year" do + dt = DateTime.new(2012, 12, 24, 12, 23, 00, '+05:00') + dt.to_date.year.should == dt.year + end + + it "maintains the same month" do + dt = DateTime.new(2012, 12, 24, 12, 23, 00, '+05:00') + dt.to_date.mon.should == dt.mon + end + + it "maintains the same day" do + dt = DateTime.new(2012, 12, 24, 12, 23, 00, '+05:00') + dt.to_date.day.should == dt.day + end + + it "maintains the same mday" do + dt = DateTime.new(2012, 12, 24, 12, 23, 00, '+05:00') + dt.to_date.mday.should == dt.mday + end + + it "maintains the same julian day regardless of local time or zone" do + dt = DateTime.new(2012, 12, 24, 12, 23, 00, '+05:00') + + with_timezone("Pactific/Pago_Pago", -11) do + dt.to_date.jd.should == dt.jd + end + end end From 58ddc8afcb72030049c59d86123fb63d09acf280 Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Thu, 12 Oct 2017 03:23:12 +0000 Subject: [PATCH 03/12] Adds DateTime#to_s --- library/datetime/to_s_spec.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/library/datetime/to_s_spec.rb b/library/datetime/to_s_spec.rb index 893cc93283..9d9dfc629f 100644 --- a/library/datetime/to_s_spec.rb +++ b/library/datetime/to_s_spec.rb @@ -2,5 +2,16 @@ require 'date' describe "DateTime#to_s" do - it "needs to be reviewed for spec completeness" + it "returns a new String object" do + dt = DateTime.new(2012, 12, 24, 1, 2, 3, "+03:00") + dt.to_s.should be_kind_of(String) + end + + it "maintains timezone regardless of local time" do + dt = DateTime.new(2012, 12, 24, 1, 2, 3, "+03:00") + + with_timezone("Pactific/Pago_Pago", -11) do + dt.to_s.should == "2012-12-24T01:02:03+03:00" + end + end end From 7db3b7b739e431f3486d109bbcc1edf9e3678b50 Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Thu, 12 Oct 2017 17:31:28 +0000 Subject: [PATCH 04/12] Expands coverage of DateTime#now --- library/datetime/now_spec.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/library/datetime/now_spec.rb b/library/datetime/now_spec.rb index e8c93aa604..8b6e997967 100644 --- a/library/datetime/now_spec.rb +++ b/library/datetime/now_spec.rb @@ -5,4 +5,28 @@ it "creates an instance of DateTime" do DateTime.now.should be_an_instance_of(DateTime) end + + it "sets the current date" do + dt = DateTime.now + d = Date.today + + dt.year.should == d.year + dt.mon.should == d.mon + dt.day.should == d.day + end + + it "sets the current time" do + dt = DateTime.now + t = Time.now + + dt.hour.should == t.hour + dt.min.should == t.min + dt.sec.should == t.sec + end + + it "grabs the local timezone" do + dt = DateTime.now + t = Time.local(dt.year, dt.mon, dt.mday, dt.hour, dt.min, dt.sec) + dt.offset.to_i.should == t.utc_offset + end end From 1408eaa590f2a763934a89e663f2eb172e83ecf4 Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Mon, 16 Oct 2017 00:39:05 +0000 Subject: [PATCH 05/12] Time.now spec checks time --- core/time/shared/now.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/time/shared/now.rb b/core/time/shared/now.rb index f570aeedd2..3add3eaf9d 100644 --- a/core/time/shared/now.rb +++ b/core/time/shared/now.rb @@ -5,4 +5,14 @@ TimeSpecs::SubTime.send(@method).should be_an_instance_of(TimeSpecs::SubTime) TimeSpecs::MethodHolder.send(@method).should be_an_instance_of(Time) end + + it "sets the current time" do + check = `date "+%H:%M:%S"`.strip + time = check.match(/(\d{2})\:(\d{2})\:(\d{2})/) + now = TimeSpecs::MethodHolder.send(@method) + + now.hour.should == time[1].to_i + now.min.should == time[2].to_i + now.sec.should == time[3].to_i + end end From 1e3c3edc4cce7d3b02305d2e55df7bd2660ee836 Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Mon, 16 Oct 2017 00:39:33 +0000 Subject: [PATCH 06/12] Date.today checks against local date --- library/date/today_spec.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/library/date/today_spec.rb b/library/date/today_spec.rb index 09e8ed6006..11bf52cd27 100644 --- a/library/date/today_spec.rb +++ b/library/date/today_spec.rb @@ -2,5 +2,16 @@ require 'date' describe "Date.today" do - it "needs to be reviewed for spec completeness" + it "returns a Date object" do + Date.today.should be_kind_of Date + end + + it "sets Date object to the current date" do + check = `date "+%Y-%m-%d"`.strip + date = check.match(/(\d{4})-(\d{2})-(\d{2})/) + today = Date.today + today.year.should == date[1].to_i + today.mon.should == date[2].to_i + today.day.should == date[3].to_i + end end From f89de839fe4d8d4bedd9cee32d6a95d8af845985 Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Sun, 15 Oct 2017 19:47:44 -0500 Subject: [PATCH 07/12] Time.now preserves local timezone --- core/time/shared/now.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/time/shared/now.rb b/core/time/shared/now.rb index 3add3eaf9d..2eee3df3b9 100644 --- a/core/time/shared/now.rb +++ b/core/time/shared/now.rb @@ -15,4 +15,11 @@ now.min.should == time[2].to_i now.sec.should == time[3].to_i end + + it "preserves the local timezone" do + with_timezone("PDT", -8) do + now = TimeSpecs::MethodHolder.send(@method) + now.utc_offset.should == (-8 * 60 * 60) + end + end end From f410d198c66e2ab55a108e7d135a219c19781ec1 Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Sun, 15 Oct 2017 19:59:55 -0500 Subject: [PATCH 08/12] Better describes the Timezone preservation for DateTime.now --- library/datetime/now_spec.rb | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/library/datetime/now_spec.rb b/library/datetime/now_spec.rb index 8b6e997967..8739feea6b 100644 --- a/library/datetime/now_spec.rb +++ b/library/datetime/now_spec.rb @@ -8,25 +8,28 @@ it "sets the current date" do dt = DateTime.now - d = Date.today + check = `date "+%Y-%m-%d"`.strip + date = check.match(/(\d{4})-(\d{2})-(\d{2})/) - dt.year.should == d.year - dt.mon.should == d.mon - dt.day.should == d.day + dt.year.should == date[1].to_i + dt.mon.should == date[2].to_i + dt.day.should == date[3].to_i end it "sets the current time" do dt = DateTime.now - t = Time.now + check = `date "+%H:%M:%S"`.strip + time = check.match(/(\d{2})\:(\d{2})\:(\d{2})/) - dt.hour.should == t.hour - dt.min.should == t.min - dt.sec.should == t.sec + dt.hour.should == time[1].to_i + dt.min.should == time[2].to_i + dt.sec.should == time[3].to_i end it "grabs the local timezone" do - dt = DateTime.now - t = Time.local(dt.year, dt.mon, dt.mday, dt.hour, dt.min, dt.sec) - dt.offset.to_i.should == t.utc_offset + with_timezone("PDT", -8) do + dt = DateTime.now + dt.zone.should == "-08:00" + end end end From af15365c96ab57dbe96e0da9a46b89237eb4b0c6 Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Thu, 19 Oct 2017 15:07:59 -0500 Subject: [PATCH 09/12] Replace data shell out with Process#clock_gettime --- core/time/shared/now.rb | 9 ++------- library/date/today_spec.rb | 10 +++++----- library/datetime/now_spec.rb | 18 ++++++++---------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/core/time/shared/now.rb b/core/time/shared/now.rb index 2eee3df3b9..c35115fcf4 100644 --- a/core/time/shared/now.rb +++ b/core/time/shared/now.rb @@ -7,16 +7,11 @@ end it "sets the current time" do - check = `date "+%H:%M:%S"`.strip - time = check.match(/(\d{2})\:(\d{2})\:(\d{2})/) now = TimeSpecs::MethodHolder.send(@method) - - now.hour.should == time[1].to_i - now.min.should == time[2].to_i - now.sec.should == time[3].to_i + now.to_f.should be_close(Process.clock_gettime(Process::CLOCK_REALTIME), 10.0) end - it "preserves the local timezone" do + it "uses the local timezone" do with_timezone("PDT", -8) do now = TimeSpecs::MethodHolder.send(@method) now.utc_offset.should == (-8 * 60 * 60) diff --git a/library/date/today_spec.rb b/library/date/today_spec.rb index 11bf52cd27..4edf894774 100644 --- a/library/date/today_spec.rb +++ b/library/date/today_spec.rb @@ -7,11 +7,11 @@ end it "sets Date object to the current date" do - check = `date "+%Y-%m-%d"`.strip - date = check.match(/(\d{4})-(\d{2})-(\d{2})/) today = Date.today - today.year.should == date[1].to_i - today.mon.should == date[2].to_i - today.day.should == date[3].to_i + + now = Time.now + today.year.should == now.year + today.mon.should == now.mon + today.day.should == now.day end end diff --git a/library/datetime/now_spec.rb b/library/datetime/now_spec.rb index 8739feea6b..fad3f40ecc 100644 --- a/library/datetime/now_spec.rb +++ b/library/datetime/now_spec.rb @@ -8,22 +8,20 @@ it "sets the current date" do dt = DateTime.now - check = `date "+%Y-%m-%d"`.strip - date = check.match(/(\d{4})-(\d{2})-(\d{2})/) + date = Date.today - dt.year.should == date[1].to_i - dt.mon.should == date[2].to_i - dt.day.should == date[3].to_i + dt.year.should == date.year + dt.mon.should == date.mon + dt.day.should == date.day end it "sets the current time" do dt = DateTime.now - check = `date "+%H:%M:%S"`.strip - time = check.match(/(\d{2})\:(\d{2})\:(\d{2})/) + time = Time.now - dt.hour.should == time[1].to_i - dt.min.should == time[2].to_i - dt.sec.should == time[3].to_i + dt.hour.should == time.hour + dt.min.should == time.min + dt.sec.should == time.sec end it "grabs the local timezone" do From 6fb7e78a90ad81f1481dede896c9ffdac88c59d2 Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Thu, 19 Oct 2017 16:16:04 -0500 Subject: [PATCH 10/12] Be close enough on time --- library/date/today_spec.rb | 5 +---- library/datetime/now_spec.rb | 8 ++------ 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/library/date/today_spec.rb b/library/date/today_spec.rb index 4edf894774..92ae567468 100644 --- a/library/date/today_spec.rb +++ b/library/date/today_spec.rb @@ -8,10 +8,7 @@ it "sets Date object to the current date" do today = Date.today - now = Time.now - today.year.should == now.year - today.mon.should == now.mon - today.day.should == now.day + (today.to_time - now).should be_close(24 * 60 * 60) end end diff --git a/library/datetime/now_spec.rb b/library/datetime/now_spec.rb index fad3f40ecc..143eae3368 100644 --- a/library/datetime/now_spec.rb +++ b/library/datetime/now_spec.rb @@ -9,7 +9,6 @@ it "sets the current date" do dt = DateTime.now date = Date.today - dt.year.should == date.year dt.mon.should == date.mon dt.day.should == date.day @@ -17,11 +16,8 @@ it "sets the current time" do dt = DateTime.now - time = Time.now - - dt.hour.should == time.hour - dt.min.should == time.min - dt.sec.should == time.sec + now = Time.now + (dt.to_time - now).should be_close(24 * 60 * 60) end it "grabs the local timezone" do From ad1e861e017eab4f2d9193cfdecb27e4bede48d2 Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Thu, 19 Oct 2017 16:31:24 -0500 Subject: [PATCH 11/12] Properly implement be_close on timing assertion --- library/date/today_spec.rb | 2 +- library/datetime/now_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/date/today_spec.rb b/library/date/today_spec.rb index 92ae567468..d487be089f 100644 --- a/library/date/today_spec.rb +++ b/library/date/today_spec.rb @@ -9,6 +9,6 @@ it "sets Date object to the current date" do today = Date.today now = Time.now - (today.to_time - now).should be_close(24 * 60 * 60) + (now - today.to_time).should be_close(0.0, 24 * 60 * 60) end end diff --git a/library/datetime/now_spec.rb b/library/datetime/now_spec.rb index 143eae3368..1344ebbd40 100644 --- a/library/datetime/now_spec.rb +++ b/library/datetime/now_spec.rb @@ -17,7 +17,7 @@ it "sets the current time" do dt = DateTime.now now = Time.now - (dt.to_time - now).should be_close(24 * 60 * 60) + (dt.to_time - now).should be_close(0.0, 10.0) end it "grabs the local timezone" do From 756752fd4dd12cd376c6814ba09cfc1a5443db7a Mon Sep 17 00:00:00 2001 From: Andrew Nordman Date: Thu, 19 Oct 2017 16:36:06 -0500 Subject: [PATCH 12/12] DateTime.now uses be_close to avoid day rollover --- library/datetime/now_spec.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/library/datetime/now_spec.rb b/library/datetime/now_spec.rb index 1344ebbd40..a5bf590aff 100644 --- a/library/datetime/now_spec.rb +++ b/library/datetime/now_spec.rb @@ -7,11 +7,7 @@ end it "sets the current date" do - dt = DateTime.now - date = Date.today - dt.year.should == date.year - dt.mon.should == date.mon - dt.day.should == date.day + (DateTime.now - Date.today).to_f.should be_close(0.0, 1.0) end it "sets the current time" do