#37910 closed enhancement (fixed)
Remove confusing legacy logic in date_i18n()
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 4.7 | Priority: | normal |
Severity: | normal | Version: | 3.0 |
Component: | Date/Time | Keywords: | has-patch commit |
Focuses: | Cc: |
Description
In #37634 it was pointed out that date_i18n()
contains the following line:
$datefunc = $gmt? 'gmdate' : 'date';
However, there is actually no difference between date()
and gmdate()
in WordPress, so that line can be removed. At the time that this logic was added, WordPress was experimenting with setting the default PHP time based on the site's timezone. But in the end it was decided to just always have the default PHP timezome be UTC/GMT. Which means that date()
and time()
always return UTC values, and thus using gmdate()
is unnecessary. (I discovered this just the other day when I was researching a date/time-related bug in a plugin.) See #9588 and [12727].
Retaining this legacy logic is confusing, since it implies that there is some difference in WordPress between using date()
and gmdate()
, when there really is none. date()
should just be used instead.
Note that although that particular line in the function is unnecessary, the $gmt
parameter is still needed for these lines:
<?php if ( false === $i ) { if ( ! $gmt ) $i = current_time( 'timestamp' ); else $i = time(); // we should not let date() interfere with our // specially computed timestamp $gmt = true; }
However, they could be simplified to just:
<?php if ( false === $i ) { $i = current_time( 'timestamp', $gmt ); }
We should probably consider removing every reference to gmdate()
in core in favor of just using date()
, to avoid this confusion. But maybe that would ultimately be for a separate ticket.
Attachments (4)
Change History (27)
#1
@
9 years ago
- Keywords has-patch added
It looks like gmdate()
is used quite a few places in core (~50), so if we want to address that, it should be in a separate ticket I guess.
This ticket was mentioned in Slack in #core by stevenkword. View the logs.
8 years ago
#6
@
8 years ago
37910.2.diff adds some initial tests
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
8 years ago
#8
@
8 years ago
- Owner changed from SergeyBiryukov to swissspidy
- Status changed from reviewing to accepted
#11
@
8 years ago
The unit tests for this have a race condition, and are failing intermittently, see #38381.
#13
@
8 years ago
- Keywords needs-testing added; has-patch has-unit-tests commit removed
- Resolution fixed deleted
- Status changed from closed to reopened
This ticket was mentioned in Slack in #core by helen. View the logs.
8 years ago
#15
follow-up:
↓ 16
@
8 years ago
Tests_Date_I18n::test_adjusts_format_based_on_timezone_string()
was added to cover this part of `date_i18n()` which replaces date format strings based on the site's timezone. So it's not really about comparing the time, but about checking whether that replacement works as expected. Hence the unusual date format string.
The logic in date_i18n()
is still valid, but the test is faulty. I need to replace it with something a bit more flexible.
#16
in reply to:
↑ 15
;
follow-up:
↓ 17
@
8 years ago
Replying to swissspidy:
The logic in
date_i18n()
is still valid, but the test is faulty. I need to replace it with something a bit more flexible.
What about using a timezone which has the same UTC offset all year like America/Regina?
#17
in reply to:
↑ 16
@
8 years ago
Replying to ocean90:
Replying to swissspidy:
The logic in
date_i18n()
is still valid, but the test is faulty. I need to replace it with something a bit more flexible.
What about using a timezone which has the same UTC offset all year like America/Regina?
That… makes sense.
#18
@
8 years ago
- Keywords has-patch added; needs-testing removed
@ocean90 Thanks for the patch! You might not have known, but uploading an attachment doesn't trigger a notification :P
#19
@
8 years ago
- Keywords commit added
Right, I was waiting for the build which took a bit longer and so I forget to comment. And here's a little script to show that America/Regina has the same offset: https://3v4l.org/L59eg
Remove legacy logic and always use
date()
indate_i18n()