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

Respect mock instance in createFromFormat with partial format #1281

Merged
merged 5 commits into from
May 13, 2018

Conversation

imbrish
Copy link
Contributor

@imbrish imbrish commented May 10, 2018

Let's say current time is:

Carbon::now(); // 2018-05-10 16:22:30 America/Toronto

We set the mock instance to:

Carbon::setTestNow('2013-09-01 05:10:15 America/Vancouver');

One would expect that when using Carbon::createFromFormat with a partial format, the missing part will be filled from the mock:

Carbon::createFromFormat('Y-m-d', '2016-06-10'); // 2016-06-10 05:10:15 America/Vancouver
Carbon::createFromFormat('H:i:s', '10:20:30'); // 2013-09-01 10:20:30 America/Vancouver

However as of now it is filled from the actual time instead:

Carbon::createFromFormat('Y-m-d', '2016-06-10'); // 2016-06-10 16:22:30 America/Toronto
Carbon::createFromFormat('H:i:s', '10:20:30'); // 2018-05-10 10:20:30 America/Toronto

This PR fixes that issue, by prepending mocked time to the input. It works with many fancy formats, including unix epoch reset flags !, |. See new tests for more details.

Changes are not backwards compatible, however that's often the case when fixing bugs, which I would assume the old behavior to be. Worst case scenario would be that we break some odd monkey-fixed tests that circumvented the problem by comparing against the real time. We don't need to worry about someone using setTestNow in the business code, right?

The PR includes two additional changes to preserve current behaviors:

  1. Last errors applied to resulting instance come from parsing original (unmodified) format.
  2. Microseconds in the Carbon::create method are explicitly set to 0 by using format !Y-n-j G:i:s instead of previous Y-n-j G:i:s.

Bonus:

  1. Fix creating timezone from an offset for PHP below 5.5.10.

Fixes #1280.

@imbrish imbrish changed the title Respect mock instance when creating from partial format Respect mock instance in createFromFormat with partial format May 11, 2018
@kylekatarnls kylekatarnls added this to the 1.28 milestone May 13, 2018
@kylekatarnls kylekatarnls merged commit a033afb into briannesbitt:master May 13, 2018
@imbrish imbrish deleted the fix-1280 branch May 16, 2018 16:39
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 this pull request may close these issues.

Carbon::createFromFormat using a date without time does not respect Carbon::setTestNow
2 participants