Skip to content
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

Date.strptime returns wrong month for %Y #377

Closed
krzyzak opened this issue Feb 1, 2022 · 6 comments · Fixed by #379
Closed

Date.strptime returns wrong month for %Y #377

krzyzak opened this issue Feb 1, 2022 · 6 comments · Fixed by #379

Comments

@krzyzak
Copy link

krzyzak commented Feb 1, 2022

Hey,

Today I found issue with Date.strptime: it falls back to current month, rather than returning Jan 1st, when parsing just %Y:

Date.strptime('2000', "%Y")
=> Sat, 01 Jan 2000
require 'timecop'
=> true
Date.strptime('2000', "%Y")
=> Tue, 01 Feb 2000

Seems that the issue lies in time_extensions.rb.

@joshuacronemeyer
Copy link
Collaborator

thanks for the bug report @krzyzak are you interested in sending a PR with tests that demonstrate the fix?

@augustoccesar
Copy link

I was looking into opening a PR for this issue, but from the short investigation that I did it seems that this is a very explicit expected behaviour.

d = Date._strptime(str, fmt) || Date.strptime_without_mock_date(str, fmt)
now = Time.now.to_date
year = d[:year] || now.year
mon = d[:mon] || now.mon

I guess to change to the expected behaviour described in this issue it would need to change to something like

d = Date._strptime(str, fmt) || Date.strptime_without_mock_date(str, fmt)
now = Time.now.to_date
year = d[:year] || now.year
mon = d[:mon] || 1

which feels like a large change from the perspective of expectation of how this method works (on timecop).

So I think that the main question is: Should the timecop Date.strptime behaves like the Ruby Date.strptime, or this an expected/desired behaviour to default to the current month?

@joshuacronemeyer
Copy link
Collaborator

Thanks guys. The Timecop implementation of strptime is a work in progress. Our implementation is incomplete and we're slowly chipping away at it. It should behave like the ruby implementation.

checkout

#TODO Support these formats
# '%G %V %w',
# '%G %V %u',
# '%C %g %V %w',
# '%C %g %V %u',
# '%Y %U %w',
# '%Y %U %u',
# '%Y %W %w',
# '%Y %W %u',
# '%C %y %U %w',
# '%C %y %U %u',
# '%C %y %W %w',
# '%C %y %W %u',
to see a bunch of time format stuff we haven't handled yet. The scenario highlighted by this issue is just one more to add to that list.

@joshuacronemeyer joshuacronemeyer linked a pull request Feb 27, 2022 that will close this issue
@joshuacronemeyer
Copy link
Collaborator

@krzyzak and @augustoccesar i'd appreciate if you'd have a look at my PR #379 for this issue. I'm open to suggestions on ways to better implement this.

@krzyzak
Copy link
Author

krzyzak commented Mar 7, 2022

@joshuacronemeyer yes, that indeed fixes my issue. Is there an option to release new gem version?

@joshuacronemeyer
Copy link
Collaborator

@krzyzak thanks for asking. I just released 0.9.5 that includes this change. Would appreciate you bumping your version and testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants