2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'shared/parse'
|
|
|
|
require_relative 'shared/parse_us'
|
|
|
|
require_relative 'shared/parse_eu'
|
2017-05-07 08:04:49 -04:00
|
|
|
require 'date'
|
|
|
|
|
|
|
|
describe "Date#parse" do
|
|
|
|
# The space separator is also different, doesn't work for only numbers
|
|
|
|
it "parses a day name into a Date object" do
|
|
|
|
d = Date.parse("friday")
|
|
|
|
d.should == Date.commercial(d.cwyear, d.cweek, 5)
|
|
|
|
end
|
|
|
|
|
2019-10-24 12:35:48 -04:00
|
|
|
it "parses a month name into a Date object" do
|
|
|
|
d = Date.parse("october")
|
|
|
|
d.should == Date.civil(Date.today.year, 10)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "parses a month day into a Date object" do
|
|
|
|
d = Date.parse("5th")
|
|
|
|
d.should == Date.civil(Date.today.year, Date.today.month, 5)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Specs using numbers
|
|
|
|
it "throws an argument error for a single digit" do
|
2019-07-27 06:40:09 -04:00
|
|
|
->{ Date.parse("1") }.should raise_error(ArgumentError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "parses DD as month day number" do
|
|
|
|
d = Date.parse("10")
|
|
|
|
d.should == Date.civil(Date.today.year, Date.today.month, 10)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "parses DDD as year day number" do
|
|
|
|
d = Date.parse("100")
|
|
|
|
if Date.gregorian_leap?(Date.today.year)
|
|
|
|
d.should == Date.civil(Date.today.year, 4, 9)
|
|
|
|
else
|
|
|
|
d.should == Date.civil(Date.today.year, 4, 10)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "parses MMDD as month and day" do
|
|
|
|
d = Date.parse("1108")
|
|
|
|
d.should == Date.civil(Date.today.year, 11, 8)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "parses YYDDD as year and day number in 1969--2068" do
|
|
|
|
d = Date.parse("10100")
|
|
|
|
d.should == Date.civil(2010, 4, 10)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "parses YYMMDD as year, month and day in 1969--2068" do
|
|
|
|
d = Date.parse("201023")
|
|
|
|
d.should == Date.civil(2020, 10, 23)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "parses YYYYDDD as year and day number" do
|
|
|
|
d = Date.parse("1910100")
|
|
|
|
d.should == Date.civil(1910, 4, 10)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "parses YYYYMMDD as year, month and day number" do
|
|
|
|
d = Date.parse("19101101")
|
|
|
|
d.should == Date.civil(1910, 11, 1)
|
|
|
|
end
|
2018-08-03 12:19:40 -04:00
|
|
|
|
|
|
|
it "raises a TypeError trying to parse non-String-like object" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { Date.parse(1) }.should raise_error(TypeError)
|
2022-02-25 12:42:35 -05:00
|
|
|
-> { Date.parse([]) }.should raise_error(TypeError)
|
2018-08-03 12:19:40 -04:00
|
|
|
end
|
2020-03-27 19:22:51 -04:00
|
|
|
|
|
|
|
it "coerces using to_str" do
|
|
|
|
c = Class.new do
|
|
|
|
attr_accessor :string
|
|
|
|
def to_str
|
|
|
|
@string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
o = c.new
|
|
|
|
o.string = "19101101"
|
|
|
|
|
|
|
|
d = Date.parse(o)
|
|
|
|
d.should == Date.civil(1910, 11, 1)
|
|
|
|
|
|
|
|
# parse should not modify string value
|
|
|
|
o.to_str.should == "19101101"
|
|
|
|
end
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Date#parse with '.' separator" do
|
|
|
|
before :all do
|
|
|
|
@sep = '.'
|
|
|
|
end
|
|
|
|
|
|
|
|
it_should_behave_like "date_parse"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Date#parse with '/' separator" do
|
|
|
|
before :all do
|
|
|
|
@sep = '/'
|
|
|
|
end
|
|
|
|
|
|
|
|
it_should_behave_like "date_parse"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Date#parse with ' ' separator" do
|
|
|
|
before :all do
|
|
|
|
@sep = ' '
|
|
|
|
end
|
|
|
|
|
|
|
|
it_should_behave_like "date_parse"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Date#parse with '/' separator US-style" do
|
|
|
|
before :all do
|
|
|
|
@sep = '/'
|
|
|
|
end
|
|
|
|
|
|
|
|
it_should_behave_like "date_parse_us"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Date#parse with '-' separator EU-style" do
|
|
|
|
before :all do
|
|
|
|
@sep = '-'
|
|
|
|
end
|
|
|
|
|
|
|
|
it_should_behave_like "date_parse_eu"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Date#parse(.)" do
|
|
|
|
it "parses YYYY.MM.DD into a Date object" do
|
|
|
|
d = Date.parse("2007.10.01")
|
|
|
|
d.year.should == 2007
|
|
|
|
d.month.should == 10
|
|
|
|
d.day.should == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it "parses DD.MM.YYYY into a Date object" do
|
|
|
|
d = Date.parse("10.01.2007")
|
|
|
|
d.year.should == 2007
|
|
|
|
d.month.should == 1
|
|
|
|
d.day.should == 10
|
|
|
|
end
|
|
|
|
|
|
|
|
it "parses YY.MM.DD into a Date object using the year 20YY" do
|
|
|
|
d = Date.parse("10.01.07")
|
|
|
|
d.year.should == 2010
|
|
|
|
d.month.should == 1
|
|
|
|
d.day.should == 7
|
|
|
|
end
|
|
|
|
|
|
|
|
it "parses YY.MM.DD using the year digits as 20YY when given true as additional argument" do
|
|
|
|
d = Date.parse("10.01.07", true)
|
|
|
|
d.year.should == 2010
|
|
|
|
d.month.should == 1
|
|
|
|
d.day.should == 7
|
|
|
|
end
|
|
|
|
end
|