Calls to Joomla\CMS\HTML\HTMLHelper::date()
(or Joomla\CMS\HTML\HTMLHelper::_('date')
if you're Doing It Right) can be somewhat expensive because it always calls Joomla\CMS\Factory::getUser()
which causes a read action from the session API. This method is used quite liberally to format date/time strings to a desired timezone, so it should be a performant method to the best of our ability. Additionally, it's quite a bit of overhead if you don't need to convert timezones on a date/time string. So, there are a couple of actionable points with this issue:
First, there is only one code path that requires a call to Joomla\CMS\Factory::getUser()
so we shouldn't make that call every time the method is executed.
Second, we should review the core uses of this method and depending on the use case decide if:
Right now, this code snippet is massively more performant (and performance matters a lot in places like the action logs exports, as many recent issues have shown, not to mention the method is normally used in all layouts where date/time strings are displayed to users, so it gets used quite frequently):
use Joomla\CMS\Date\Date;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
// This ...
$date = new Date($time, new \DateTimeZone('UTC'));
$date->format(Text::_('DATE_FORMAT_LC6'));
// ... is roughly equivalent to ...
HTMLHelper::_('date', $time, Text::_('DATE_FORMAT_LC6'), 'UTC');
// ... but without the performance overhead.
Labels |
Added:
?
|
When you're aiming for user timezone conversion, then yes, the JHtml helper should be used. My main point though is that not every date/time display in Joomla actually needs to go through this process, and that is what we can/should review. Though with other patches the performance penalty is getting negated, it still doesn't hurt to do a scan and make sure we aren't needlessly converting.
Labels |
Added:
J3 Issue
|
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2019-02-19 02:01:01 |
Closed_By | ⇒ | mbabker |
This code snippet formats the date into UTC. But mostly you want to have the date in the user timezone on display in a page. So there is no way around to get the timezone of the user somehow.