timecop icon indicating copy to clipboard operation
timecop copied to clipboard

Date.strptime doesn't work correctly today.

Open mikdiet opened this issue 11 months ago • 7 comments

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 avatar Dec 30 '24 15:12 mikdiet

@mikdiet nice catch. This is a bug.

joshuacronemeyer avatar Dec 30 '24 18:12 joshuacronemeyer

@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.

joshuacronemeyer avatar Dec 30 '24 18:12 joshuacronemeyer

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

joshuacronemeyer avatar Dec 30 '24 19:12 joshuacronemeyer

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.

joshuacronemeyer avatar Dec 30 '24 19:12 joshuacronemeyer

@mikdiet are you interested in submitting a PR for this?

hello @joshuacronemeyer - no, I wasn't intending to fix this myself.

mikdiet avatar Dec 31 '24 08:12 mikdiet

@joshuacronemeyer - first commit to this project, but I think I found a way to test / fix this scenario.

jchapa avatar Jan 03 '25 20:01 jchapa

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

azhuravlev avatar Mar 25 '25 09:03 azhuravlev