-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(434) strptime_with_mock_date :: Fix year boundary #437
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module DateStrptimeYearBoundaryScenarios | ||
#calling freeze and travel tests are making the date Time.local(1992,1,1) | ||
|
||
# Test for wday | ||
def test_date_strptime_year_boundary_with_day_of_week | ||
assert_equal Date.strptime('Thursday', '%A'), Date.new(1992, 1, 2) | ||
assert_equal Date.strptime('Monday', '%A'), Date.new(1991, 12, 30) | ||
end | ||
|
||
# Test for cwday | ||
def test_date_strptime_year_boundary_with_iso_day_of_week | ||
assert_equal Date.strptime('4', '%u'), Date.new(1992, 1, 2) | ||
assert_equal Date.strptime('1', '%u'), Date.new(1991, 12, 30) | ||
end | ||
|
||
# Test for wnum1 | ||
def test_date_strptime_year_boundary_with_wnum1 | ||
assert_equal Date.strptime('52', '%W'), Date.new(1992, 12, 28) | ||
assert_equal Date.strptime('1', '%W'), Date.new(1992, 01, 6) | ||
end | ||
|
||
# Test for wnum0 | ||
def test_date_strptime_year_boundary_with_wnum0 | ||
assert_equal Date.strptime('52', '%U'), Date.new(1992, 12, 27) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Date.new(1992,12,27) is sort of a magic number, I had to do research to understand why it was the right date. What do you think of using Date.strptime('1992-52', '%Y-%U') instead of newing up that date? This comment is general to all the magic dates in here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW if you implement this idea then for the cweek tests you have to use %G not %Y like Date.strptime('1992-52', '%G-%V'). I spent a bit of time scratching my head at this
before i realized that the iso8601 commercial week has its own year concept too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I like this idea. 2 caveats:
For these specific tests, if I don't change the other strptime tests, then I think we could safely improve readability making the change you suggested. |
||
assert_equal Date.strptime('1', '%U'), Date.new(1992, 01, 5) | ||
end | ||
|
||
# Test for cweek | ||
def test_date_strptime_year_boundary_with_cweek | ||
# TODO: year-ambiguous cweek is not handled correctly during year boundaries: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i do not understand this TODO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you referenced this in your other comment, but I think in general we're just not handling cweek well in this library. I'm not sure if that's something we want to tackle in this PR. Some examples (from
Going to poke at this a little more and hopefully find a cleaner solve |
||
# assert_equal Date.strptime('52', '%V'), Date.new(1992, 12, 21) | ||
# assert_equal Date.strptime('53', '%V'), Date.new(1991, 12, 28) | ||
assert_equal Date.strptime('52', '%V'), Date.new(1992, 12, 28) | ||
assert_equal Date.strptime('1', '%V'), Date.new(1992, 01, 6) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require_relative "test_helper" | ||
require 'timecop' | ||
require_relative 'date_strptime_year_boundary_scenarios' | ||
|
||
class TestTimecop < Minitest::Test | ||
def setup | ||
t = Time.local(1992,1,1) | ||
Timecop.freeze(t) | ||
end | ||
|
||
def teardown | ||
Timecop.return | ||
end | ||
|
||
# Test for Date | ||
include DateStrptimeYearBoundaryScenarios | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require_relative "test_helper" | ||
require 'timecop' | ||
require_relative 'date_strptime_year_boundary_scenarios' | ||
|
||
class TestTimecop < Minitest::Test | ||
def setup | ||
t = Time.local(1992,1,1) | ||
Timecop.travel(t) | ||
end | ||
|
||
def teardown | ||
Timecop.return | ||
end | ||
|
||
# Test for Date | ||
include DateStrptimeYearBoundaryScenarios | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the to_date calls on now are not necessary. it will be easier to read without those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch