chronos icon indicating copy to clipboard operation
chronos copied to clipboard

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

Open holtkamp opened this issue 1 year ago • 2 comments

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

holtkamp avatar Apr 11 '24 18:04 holtkamp

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.

othercorey avatar Apr 13 '24 03:04 othercorey

@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

holtkamp avatar Apr 15 '24 17:04 holtkamp