timecop
timecop copied to clipboard
Date.strptime doesn't work correctly today.
In my test I have a line (added just 2 month ago) that used to work, but fails today.
It's not wrapped in any timecop helpers. Actually I could reproduce it in irb:
> Date.strptime('wednesday', '%A')
(irb):2:in `<main>': invalid date (Date::Error)
Date.strptime_without_mock_date("#{year} #{week} #{d[:wday]}", '%Y %W %w', start)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
but original method works.
> Date.strptime_without_mock_date('wednesday', '%A')
Wed, 01 Jan 2025
UPD: the actual string it tries to parse is "2024 53 3" - which corresponds to 32 December 🤣
@mikdiet nice catch. This is a bug.
@mikdiet are you interested in submitting a PR for this?
The problem looks to be that at the end of the year we might need to return a date from next year but our code is always calling Date.strptime_without_mock_date with the current mocked year.
I think the fix is going to be getting the year from the un-mocked method, computing the year offset of the mocked date and then adding it to the result of the un-mocked method.
here is what i was thinking but I am not sure how to write a test for it. I think I need to mock the result of the unmocked time so that we can simulate what we're seeing today (last week of the year) https://github.com/travisjeffery/timecop/pull/436
Ok, never mind that solution isn't quite right... I'll wait to hear if you are going to make a PR. If not I'll figure out how to write a test for this and go for there.
@mikdiet are you interested in submitting a PR for this?
hello @joshuacronemeyer - no, I wasn't intending to fix this myself.
@joshuacronemeyer - first commit to this project, but I think I found a way to test / fix this scenario.
I have a related issue with parsing dates:
irb(main):001:0> Date.strptime("202009", "%G%V")
=> Mon, 24 Feb 2020
irb(main):002:0> require "timecop"
=> true
irb(main):003:0> Date.strptime("202009", "%G%V")
=> Mon, 02 Mar 2020