Skip to content

[Clock] DatePoint created with fixed date should be set at midnight #54497

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

Closed
clement-michelet opened this issue Apr 5, 2024 · 5 comments
Closed

Comments

@clement-michelet
Copy link

clement-michelet commented Apr 5, 2024

Symfony version(s) affected

6.4.5

Description

PHP: 8.1

The DatePoint class extends DateTimeImmutable, so you can use it in your code anywhere a DateTimeImmutable or DateTimeInterface is expected.

I would expect that the behavior of DatePoint does not differ from DateTimeImmutable to use as a pure replacement. However, I cannot use it as it is everywhere (including in test cases) because creating a date with a fixed date keep the current time.

See the repro below.

How to reproduce

<?php

$datePoint = new \Symfony\Component\Clock\DatePoint('2024-01-01');
$immutableDate = new \DateTimeImmutable('2024-01-01');

\PHPUnit\Framework\Assert::assertEquals('2024-01-01 00:00:00', $immutableDate->format('Y-m-d H:i:s')); // Pass
\PHPUnit\Framework\Assert::assertEquals('2024-01-01 00:00:00', $datePoint->format('Y-m-d H:i:s')); // Fails

Possible Solution

  • Add documentation about the different behavior and that DatePoint differs from DateTimeImmutable (not a pure replacement)
  • Or introduce a named constructor keep the different behavior and leave the native constructor

Additional Context

No response

@stof
Copy link
Member

stof commented Apr 5, 2024

Keeping the current type in that case looks like a bug to me.

/cc @nicolas-grekas

@smnandre
Copy link
Member

smnandre commented Apr 5, 2024

In the meanwhile (as temporary solution), you can leverage createFromFormat with the | special character.

$datePoint =  DatePoint::createFromFormat('Y-m-d|', '2024-01-01');
$immutableDate = \DateTimeImmutable::createFromFormat('Y-m-d|', '2024-01-01');

self::assertEquals('2024-01-01 00:00:00', $datePoint->format('Y-m-d H:i:s')); // Pass
self::assertEquals('2024-01-01 00:00:00', $immutableDate->format('Y-m-d H:i:s')); // Pass

@OskarStark
Copy link
Contributor

@stof you mean s/type/time ?

@xabbuh
Copy link
Member

xabbuh commented Apr 9, 2024

see #54530

@stof
Copy link
Member

stof commented Apr 9, 2024

@OskarStark indeed.

xabbuh added a commit that referenced this issue Apr 11, 2024
…odifying the date (xabbuh)

This PR was merged into the 6.4 branch.

Discussion
----------

[Clock] initialize the current time with midnight before modifying the date

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #54497
| License       | MIT

Commits
-------

260b834 initialize the current time with midnight before modifying the date
@xabbuh xabbuh closed this as completed Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants