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

When datetime object DateTimeImmutable, SyslogUdpHandler still doesn't respect TimeZone #1539

Closed
yi-yang-github opened this issue Mar 11, 2021 · 0 comments
Labels
Milestone

Comments

@yi-yang-github
Copy link
Contributor

Monolog version 2

This is a followup to #1350

The old fix mainly is here: 226bc5f#diff-ea99f2625937f8253eb05c4bd9cc5aae307e306bf62665c6a809c126b78606f9R98

Which does

$datetime->setTimezone(new \DateTimeZone('UTC'));
...
$date = $datetime->format($this->dateFormats[$this->rfc]);

However, the $datetime object could be \DateTimeImmutable or \DateTime, when it is \DateTimeImmutable, which is, by definition, immutable and the $datetime->setTimezone(new \DateTimeZone('UTC')) function won't be able to change the original $datetime object.

If I add some print_r() here, you can see it's not changed:

M d H:i:s
before: Monolog\DateTimeImmutable Object
(
    [useMicroseconds:Monolog\DateTimeImmutable:private] => 1
    [date] => 2021-03-11 14:47:24.195319
    [timezone_type] => 3
    [timezone] => America/Vancouver
)
Mar 11 14:47:24
after: Monolog\DateTimeImmutable Object
(
    [useMicroseconds:Monolog\DateTimeImmutable:private] => 1
    [date] => 2021-03-11 14:47:24.195319
    [timezone_type] => 3
    [timezone] => America/Vancouver
)
Mar 11 14:47:24

However, if I add something like

            $dateNew = $datetime->setTimezone(new \DateTimeZone('UTC'));
            $date = $dateNew->format($this->dateFormats[$this->rfc]);

Then it works:

M d H:i:s
before: Monolog\DateTimeImmutable Object
(
    [useMicroseconds:Monolog\DateTimeImmutable:private] => 1
    [date] => 2021-03-11 14:48:19.095968
    [timezone_type] => 3
    [timezone] => America/Vancouver
)
Mar 11 14:48:19
after: Monolog\DateTimeImmutable Object
(
    [useMicroseconds:Monolog\DateTimeImmutable:private] => 1
    [date] => 2021-03-11 22:48:19.095968
    [timezone_type] => 3
    [timezone] => UTC
)
Mar 11 22:48:19
yi-yang-github pushed a commit to yi-yang-github/monolog that referenced this issue Mar 11, 2021
Seldaek added a commit that referenced this issue Apr 4, 2021
#1539 fix timezone when it's DateTimeImmutable
@Seldaek Seldaek added this to the 2.x milestone Apr 4, 2021
@Seldaek Seldaek closed this as completed Apr 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants