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

"DateTimeMock::__construct()" side-effect using "idate()" when setting microseconds... #7

Closed
lfmimo opened this issue Apr 2, 2022 · 6 comments · Fixed by #8
Closed

Comments

@lfmimo
Copy link
Contributor

lfmimo commented Apr 2, 2022

Hi,

when using a non GMT DateTimezone and setting microseconds via

$this->setTime(
                idate('H', $this->getTimestamp()),
                idate('i', $this->getTimestamp()),
                idate('s', $this->getTimestamp()),
                (int) ClockMock::getFrozenDateTime()->format('u')
            );

the hour/minute is wrongly modified as idate() works on unix GMT-based timestamp.
Example:

Let's assume 02.04.2022 18:00:00 as Berlin time.

$dt = (new DateTime('now', new DateTimeZone('Europe/Berlin')))

DateTime now contains:

date: 2022-04-02 18:00:00.0 Europe/Berlin (+02:00)

Now using

$dt->setTime(
                idate('H', $dt->getTimestamp()),
                idate('i', $dt->getTimestamp()),
                idate('s', $dt->getTimestamp())
);

$dt contains:

date: 2022-04-02 16:00:00.0 Europe/Berlin (+02:00).

This means the time is now set 2 hours back, but still the Europe/Berlin timezone.

Solution in DateTimeMock:

Use

$this->setTime(
                  (int) $this->format('H'),
                  (int) $this->format('i'),
                  (int) $this->format('s'),
                  (int) ClockMock::getFrozenDateTime()->format('u')
);

as this does't modify hour/minute/second when setting microseconds...

-mimo

@asprega
Copy link
Contributor

asprega commented Apr 4, 2022

Hi! Thanks for your report.

Would you be able to open a pull request with the fix and a test for it?

@lfmimo
Copy link
Contributor Author

lfmimo commented Apr 4, 2022

Sure ;)

@lfmimo
Copy link
Contributor Author

lfmimo commented Apr 4, 2022

Think I don't have sufficient access-rights to the repo:

image

@asprega
Copy link
Contributor

asprega commented Apr 4, 2022

You should first fork the repository and create your own copy, and then open the PR against the main repo, as described here: https://docs.github.com/en/get-started/quickstart/contributing-to-projects

hope this helps!

@lfmimo
Copy link
Contributor Author

lfmimo commented Apr 4, 2022

I've created the PR...

@asprega
Copy link
Contributor

asprega commented Apr 4, 2022

I just released version 0.2.2 including your contribution. Thanks 🎉

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.

2 participants