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

normalizeInterval should be improved #3

Closed
nyamsprod opened this issue Jan 28, 2019 · 2 comments
Closed

normalizeInterval should be improved #3

nyamsprod opened this issue Jan 28, 2019 · 2 comments

Comments

@nyamsprod
Copy link

nyamsprod commented Jan 28, 2019

private function normalizeInterval(DateInterval $value): DateInterval

This method should be improved to guard against DST and other timezone shenanigans...

        $now = new DateTimeImmutable();
        $then = $now->add($value);
        return $now->diff($then);

should be

        $now = new DateTimeImmutable('@'.time());
        $then = $now->add($value);
        return $now->diff($then);

this way your reference DateTimeImmutable object is created against a timestamp (aka) GMT which is not affected by Timezone trickery 😉 .

Maybe refactor your code to always use this code everywhere instead of repeating it everywhere it would make the code more maintainable too. If I'm correct this pattern is used in many places currently.

PS: if you are lazy .. you could even use DateTimeImmutable('@0') as a private static variable 👍 anyway this part should be improved.

@nyamsprod
Copy link
Author

of course this refactoring can only be done once your class is made final #2

@jeromegamez
Copy link
Owner

Thanks again! New release incoming :)

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

No branches or pull requests

2 participants