cakephp / chronos

A standalone DateTime library originally based off of Carbon
http://book.cakephp.org/chronos
MIT License
1.34k stars 61 forks source link

Chronos::createFromFormat using a date without time does not respect Chronos::setTestNow #452

Open holtkamp opened 2 months ago

holtkamp commented 2 months ago

It seems Chronos::setTestNow() only fixates the time and not the rest of a date, is that intended?

For example, when using Chronos::createFromFormat() with a partial format, things like the day and month are not fixated:

Chronos::setTestNow(new Chronos('2020-12-01 00:00:00'));
$date = Chronos::createFromFormat('m-d', '10-5'); // I would expect this to become "2020-10-05 00:00:00
$date = Chronos::createFromFormat('d', '5'); // I would expect this to become "2020-12-05 00:00:00

Note this is similar to https://github.com/briannesbitt/Carbon/issues/1280

othercorey commented 2 months ago

This is using the default behavior of DateTimeImmutalbe;:createFromFormat(). I don't think we'll be able to detect a time component wasn't included in the format.

holtkamp commented 2 months ago

@othercorey thanks for the swift response. Indeed the native DateTimeImmutable::createFromFormat() does not support providing an alternative clock.

Maybe this can be used as inspiration: https://github.com/briannesbitt/Carbon/pull/1281

Which seem to have landed here after 6 years: https://github.com/briannesbitt/Carbon/blob/8275fbf2f47c8a792cda010d56b7fe62c3eeafbb/src/Carbon/Traits/Creator.php#L602-L613