Skip to content

Instantly share code, notes, and snippets.

@hosiawak
Created June 7, 2011 21:29
Show Gist options
  • Save hosiawak/1013220 to your computer and use it in GitHub Desktop.
Save hosiawak/1013220 to your computer and use it in GitHub Desktop.
From 04c96cf7f4aefec5cfb4837bf5c13495d21d64ff Mon Sep 17 00:00:00 2001
From: Karol Hosiawa <hosiawak@gmail.com>
Date: Tue, 7 Jun 2011 23:25:38 +0200
Subject: [PATCH] updated Date constants and julian specs
---
spec/ruby/library/date/constants_spec.rb | 69 +++++++++++++++++++++++++++
spec/ruby/library/date/julian_spec.rb | 75 ++++++++++++++++++++++++++----
2 files changed, 134 insertions(+), 10 deletions(-)
diff --git a/spec/ruby/library/date/constants_spec.rb b/spec/ruby/library/date/constants_spec.rb
index cf34039..66f6a58 100644
--- a/spec/ruby/library/date/constants_spec.rb
+++ b/spec/ruby/library/date/constants_spec.rb
@@ -38,4 +38,73 @@ describe "Date constants" do
Date::ABBR_DAYNAMES.should == %w(Sun Mon Tue Wed Thu Fri Sat)
end
+ it "freezes MONTHNAMES, DAYNAMES, ABBR_MONTHNAMES, ABBR_DAYSNAMES" do
+ [Date::MONTHNAMES, Date::DAYNAMES, Date::ABBR_MONTHNAMES, Date::ABBR_DAYNAMES].each do |ary|
+ lambda { ary << "Unknown" }.should raise_error
+ ary.compact.each do |name|
+ lambda { name << "modified" }.should raise_error
+ end
+ end
+ end
+
+ ruby_version_is "" ... "1.8.7" do
+ it "defines UNIXEPOCH" do
+ Date::UNIXEPOCH.should == 2440588
+ end
+ end
+
+ ruby_version_is '1.8.7' do
+ it "defines HALF_DAYS_IN_DAY" do
+ Date::HALF_DAYS_IN_DAY.should == Rational(1, 2)
+ end
+
+ it "defines HOURS_IN_DAY" do
+ Date::HOURS_IN_DAY.should == Rational(1, 24)
+ end
+
+ it "defines MINUTES_IN_DAY" do
+ Date::MINUTES_IN_DAY.should == Rational(1, 1440)
+ end
+
+ it "defines SECONDS_IN_DAY" do
+ Date::SECONDS_IN_DAY.should == Rational(1, 86400)
+ end
+
+ it "defines MILLISECONDS_IN_DAY" do
+ Date::MILLISECONDS_IN_DAY.should == Rational(1, 86400*10**3)
+ end
+
+ it "defines NANOSECONDS_IN_DAY" do
+ Date::NANOSECONDS_IN_DAY.should == Rational(1, 86400*10**9)
+ end
+
+ it "defines MILLISECONDS_IN_SECOND" do
+ Date::MILLISECONDS_IN_SECOND.should == Rational(1, 10**3)
+ end
+
+ it "defines NANOSECONDS_IN_SECOND" do
+ Date::NANOSECONDS_IN_SECOND.should == Rational(1, 10**9)
+ end
+
+ it "defines MJD_EPOCH_IN_AJD" do
+ Date::MJD_EPOCH_IN_AJD.should == Rational(4800001, 2) # 1858-11-17
+ end
+
+ it "defines UNIX_EPOCH_IN_AJD" do
+ Date::UNIX_EPOCH_IN_AJD.should == Rational(4881175, 2) # 1970-01-01
+ end
+
+ it "defines MJD_EPOCH_IN_CJD" do
+ Date::MJD_EPOCH_IN_CJD.should == 2400001
+ end
+
+ it "defines UNIX_EPOCH_IN_CJD" do
+ Date::UNIX_EPOCH_IN_CJD.should == 2440588
+ end
+
+ it "defines LD_EPOCH_IN_CJD" do
+ Date::LD_EPOCH_IN_CJD.should == 2299160
+ end
+ end
+
end
diff --git a/spec/ruby/library/date/julian_spec.rb b/spec/ruby/library/date/julian_spec.rb
index 370a8f3..dff2fba 100644
--- a/spec/ruby/library/date/julian_spec.rb
+++ b/spec/ruby/library/date/julian_spec.rb
@@ -3,11 +3,11 @@ require File.expand_path('../../../spec_helper', __FILE__)
describe "Date#jd" do
- it "should be able to construct a Date object based on the Julian day" do
+ it "constructs a Date object based on the Julian day" do
Date.jd(2454482).should == Date.civil(2008, 1, 16)
end
- it "should be able to determine the Julian day for a Date object" do
+ it "determines the Julian day for a Date object" do
Date.civil(2008, 1, 16).jd.should == 2454482
end
@@ -15,12 +15,12 @@ end
describe "Date#julian?" do
- it "should mark a day before the calendar reform as Julian" do
+ it "marks a day before the calendar reform as Julian" do
Date.civil(1007, 2, 27).julian?.should == true
Date.civil(1907, 2, 27, Date.civil(2000, 1, 1).jd).julian?.should == true
end
- it "should mark a day after the calendar reform as Julian" do
+ it "marks a day after the calendar reform as not Julian" do
Date.civil(2007, 2, 27).julian?.should == false
Date.civil(1007, 2, 27, Date.civil(1000, 1, 1).jd).julian?.should == false
end
@@ -29,7 +29,7 @@ end
describe "Date#julian_leap?" do
- it "should be able to determine whether a year is a leap year in the Julian calendar" do
+ it "determines whether a year is a leap year in the Julian calendar" do
Date.julian_leap?(1900).should == true
Date.julian_leap?(1999).should == false
Date.julian_leap?(2000).should == true
@@ -42,7 +42,7 @@ end
ruby_version_is "" ... "1.9" do
describe "Date#valid_jd?" do
- it "should be able to determine if a day number is a valid Julian day number, true for all numbers" do
+ it "determines if a day number is a valid Julian day number, true for all numbers" do
# This might need to check the type of the jd parameter. Date.valid_jd?(:number) is of course
# bogus but returns itself with the current implementation
Date.valid_jd?(-100).should == -100
@@ -53,10 +53,68 @@ ruby_version_is "" ... "1.9" do
end
end
+ruby_version_is "1.8" do
+ describe "Date.julian?" do
+
+ it "marks the date as not Julian if using the Gregorian calendar" do
+ Date.julian?(Date.civil(1007, 2, 27).jd, Date::GREGORIAN).should == false
+ end
+
+ it "marks the date as Julian if using the Julian calendar" do
+ Date.julian?(Date.civil(1007, 2, 27).jd, Date::JULIAN).should == true
+ end
+
+ it "marks the date before the English Day of Calendar Reform as Julian" do
+ Date.julian?(Date.civil(1752, 9, 13).jd, Date::ENGLAND).should == true
+ end
+
+ it "marks the date after the English Day of Calendar Reform as not Julian" do
+ Date.julian?(Date.civil(1752, 9, 14).jd, Date::ENGLAND).should == false
+ end
+
+ it "marks the date before the Italian Day of Calendar Reform as Julian" do
+ Date.julian?(Date.civil(1582, 10, 4).jd, Date::ITALY).should == true
+ end
+
+ it "marks the date after the Italian Day of Calendar Reform as not Julian" do
+ Date.julian?(Date.civil(1582, 10, 15).jd, Date::ITALY).should == false
+ end
+
+ end
+
+ describe "Date.gregorian?" do
+
+ it "marks the date as Gregorian if using the Gregorian calendar" do
+ Date.gregorian?(Date.civil(1007, 2, 27).jd, Date::GREGORIAN).should == true
+ end
+
+ it "marks the date as not Gregorian if using the Julian calendar" do
+ Date.gregorian?(Date.civil(1007, 2, 27).jd, Date::JULIAN).should == false
+ end
+
+ it "marks the date before the English Day of Calendar Reform as not Gregorian" do
+ Date.gregorian?(Date.civil(1752, 9, 13).jd, Date::ENGLAND).should == false
+ end
+
+ it "marks the date after the English Day of Calendar Reform as Gregorian" do
+ Date.gregorian?(Date.civil(1752, 9, 14).jd, Date::ENGLAND).should == true
+ end
+
+ it "marks the date before the Italian Day of Calendar Reform as not Gregorian" do
+ Date.gregorian?(Date.civil(1582, 10, 4).jd, Date::ITALY).should == false
+ end
+
+ it "marks the date after the Italian Day of Calendar Reform as Gregorian" do
+ Date.gregorian?(Date.civil(1582, 10, 15).jd, Date::ITALY).should == true
+ end
+
+ end
+end
+
ruby_version_is "1.9" do
describe "Date#valid_jd?" do
- it "should be able to determine if a day number is a valid Julian day number, true for all numbers" do
+ it "determines if a day number is a valid Julian day number, true for all numbers" do
# This might need to check the type of the jd parameter. Date.valid_jd?(:number) is of course
# bogus but returns itself with the current implementation
Date.valid_jd?(-100).should == true
@@ -67,6 +125,3 @@ ruby_version_is "1.9" do
end
end
-describe "Date.julian?" do
- it "needs to be reviewed for spec completeness"
-end
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment