8000 [WebProfilerBundle] Display date/time elements in the user local timezone by javiereguiluz · Pull Request #49012 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WebProfilerBundle] Display date/time elements in the user local timezone #49012

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

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[WebProfilerBundle] Display date/time elements in the user local time…
…zone
  • Loading branch information
javiereguiluz authored and fabpot committed Jan 19, 2023
commit 206c5f02bedba0d20cf4159e90a39b210dc8f314
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
%}
<tr class="log-status-{{ css_class }}" data-type="{{ log.type }}" data-priority="{{ log.priority }}" data-channel="{{ log.channel }}" style="{{ 'event' == log.channel or 'DEBUG' == log.priorityName ? 'display: none' }}">
<td class="log-timestamp">
<time title="{{ log.timestamp|date('r') }}" datetime="{{ log.timestamp|date('c') }}">
<time class="newline" title="{{ log.timestamp|date('r') }}" datetime="{{ log.timestamp|date(constant('\DateTime::RFC3339_EXTENDED')) }}" data-convert-to-user-timezone data-render-as-time data-render-with-millisecond-precision>
{{ log.timestamp|date('H:i:s.v') }}
</time>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,12 +910,49 @@ if (typeof Sfjs === 'undefined' || typeof Sfjs.loadToolbar === 'undefined') {
document.querySelector('#log-filter-priority .filter-active-num').innerText = (priorities.length === selectedPriorities.length) ? 'All' : selectedPriorities.length;
document.querySelector('#log-filter-channel .filter-active-num').innerText = (channels.length === selectedChannels.length) ? 'All' : selectedChannels.length;
},

convertDateTimesToUserTimezone: function() {
const userTimezoneName = Intl.DateTimeFormat().resolvedOptions().timeZone;

document.querySelectorAll('time[data-convert-to-user-timezone]').forEach((timeElement) => {
const iso8601Datetime = timeElement.getAttribute('datetime');
const dateInUserTimezone = new Date(iso8601Datetime);

let options = {};
if (timeElement.hasAttribute('data-render-as-datetime')) {
options = {
year: 'numeric', month: 'long', day: 'numeric',
hour: 'numeric', minute: 'numeric', second: 'numeric'
};
} else if (timeElement.hasAttribute('data-render-as-date')) {
options = { year: 'numeric', month: 'long', day: 'numeric' };
} else if (timeElement.hasAttribute('data-render-as-time')) {
options = { hour: 'numeric', minute: 'numeric', second: 'numeric' };
}

if (timeElement.hasAttribute('data-render-with-millisecond-precision')) {
options.fractionalSecondDigits = 3;
}

/* dates/times are always rendered in English to match the rest of the Profiler interface */
timeElement.textContent = dateInUserTimezone.toLocaleString('en', options);

if (undefined !== userTimezoneName) {
const existingTitle = timeElement.getAttribute('title');
const newTitle = null === existingTitle
? `Date/times shown in your timezone: ${userTimezoneName}`
: existingTitle + ` (date/times shown in your timezone: ${userTimezoneName})`;
timeElement.setAttribute('title', newTitle);
}
});
},
};
})();

Sfjs.addEventListener(document, 'DOMContentLoaded', function() {
Sfjs.createTabs();
Sfjs.createToggles();
Sfjs.convertDateTimesToUserTimezone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this code really be in the JS code that is also loaded by the toolbar ? If think we should have a separate JS file containing profiler-only JS logic (just like we already separate the CSS).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. In a (near) future PR, I'm going to split this JS code and also modernize it a bit.

});
}
/*]]>*/</script>
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
</dd>

<dt>Profiled on</dt>
<dd><time datetime="{{ profile.time|date('c') }}">{{ profile.time|date('r') }}</time></dd>
<dd><time data-convert-to-user-timezone data-render-as-datetime datetime="{{ profile.time|date('c') }}">{{ profile.time|date('r') }}</time></dd>

<dt>Token</dt>
<dd>{{ profile.token }}</dd>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,11 @@ input[type="radio"], input[type="checkbox"] {
box-shadow: none;
}

time[data-render-as-date],
time[data-render-as-time] {
white-space: nowrap;
}

/* Used to hide elements added for accessibility reasons (the !important modifier is needed here) */
.visually-hidden {
border: 0 !important;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@
{{ _self.profile_search_filter(request, result, 'url') }}
</td>
<td class="text-small">
<span class="nowrap">{{ result.time|date('d-M-Y') }}</span>
<span class="nowrap newline">{{ result.time|date('H:i:s') }}</span>
<time data-convert-to-user-timezone data-render-as-date datetime="{{ result.time|date('c') }}">
{{ result.time|date('d-M-Y') }}
</time>
<time class="newline" data-convert-to-user-timezone data-render-as-time datetime="{{ result.time|date('c') }}">
{{ result.time|date('H:i:s') }}
</time>
</td>
<td class="nowrap"><a href="{{ path('_profiler', { token: result.token }) }}">{{ result.token }}</a></td>
</tr>
Expand Down
0