-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[4.6] fix: Time::createFromTimestamp() change for PHP 8.4 #9105
[4.6] fix: Time::createFromTimestamp() change for PHP 8.4 #9105
Conversation
4aa7a39
to
3b51c3e
Compare
tests/system/I18n/TimeTest.php
Outdated
$timestamp = 1489762800.654321; | ||
|
||
// The timezone will be UTC if you don't specify. | ||
$time = Time::createFromTimestamp($timestamp); | ||
|
||
// float cannot handle the number precisely. | ||
$this->assertSame('2017-03-17 15:00:00.654300', $time->format('Y-m-d H:i:s.u')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought this was due to float
, but the following test passes. Why?
$timestamp = 1489762800.654321;
$time = \DateTimeImmutable::createFromTimestamp($timestamp);
$this->assertSame('2017-03-17 15:00:00.654321', $time->format('Y-m-d H:i:s.u'));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The u
format expresses the time in microseconds which is a millionth of a second. So, the expressed time is six decimal digits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant the following test passes. Why 654300
and 654321
?
$timestamp = 1489762800.654321;
$time1 = Time::createFromTimestamp($timestamp);
$time2 = \DateTimeImmutable::createFromTimestamp($timestamp);
$this->assertSame('2017-03-17 15:00:00.654300', $time1->format('Y-m-d H:i:s.u'));
$this->assertSame('2017-03-17 15:00:00.654321', $time2->format('Y-m-d H:i:s.u'));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It probably has something to do with the default precision
(which is 14
by default).
In the Time::createFromTimestamp
method, we use:
$time = new static('@' . $timestamp, 'UTC', $locale);
Which will "cut" the ending and make it: .6543
. We can either set ini_set('precision', 16)
at the framework level which is not ideal or use sprintf
here:
$time = new static(sprintf('@%.6f', $timestamp), 'UTC', $locale);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sprintf('@%.6f', $timestamp)
works. Thank you!
3ff50fb
to
2b819ac
Compare
It returns Time with the default timezone as before.
It returns Time with the default timezone as before.
2b819ac
to
97148ef
Compare
The microseconds should be preserved without rounding.
Needs #9107Description
Follow-up #8544
Time::createFromTimestamp()
(without timezone specified) should returnTime
with the timezone UTC, becauseDateTime(Immutable)::createFromTimestamp()
in PHP 8.4 returns an instance with the timezone+00:00
.self
withstatic
.PHP 8.4 introduces new
DateTime(Immutable)::createFromTimestamp()
method.https://php.watch/versions/8.4/datetime-createFromTimestamp
Carbon and Chronos have changed the behaviors.
Checklist: