Skip to content

Fix due dates not rendering with a one-off date #29698

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

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 22 additions & 0 deletions web_src/js/features/midnight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Some timestamps are not meant to be localized in the user's timezone, only in their language.
// This is true for due date timestamps. These include a specific year, month, and day combination. If a user in one timezone
// sets the date YYYY-MM-DD, another user should see the same date, regardless of their timezone. So when a relative-time element
// has their datetime attribute specified only as YYYY-MM-DD, we will update it to YYYY-MM-DD midnight in the user's timezone.
// When the date is rendered, the only localization that will happen is the language.

const dateRegex = /^\d{4}-\d{2}-\d{2}$/;

// for all relative-time elements, if their datetime attribute is YYYY-MM-DD, we will update it to YYYY-MM-DD midnight in the user's timezone
export function initMidnightRelativeTime() {
const relativeTimeElements = document.querySelectorAll('relative-time[datetime]');

for (const element of relativeTimeElements) {
const datetimeAttr = element.getAttribute('datetime');

if (dateRegex.test(datetimeAttr)) {
const [year, month, day] = datetimeAttr.split('-');
const userMidnight = new Date(year, month - 1, day);
element.setAttribute('datetime', userMidnight.toISOString());
}
}
}
2 changes: 2 additions & 0 deletions web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ import {initRepoRecentCommits} from './features/recent-commits.js';
import {initRepoDiffCommitBranchesAndTags} from './features/repo-diff-commit.js';
import {initDirAuto} from './modules/dirauto.js';
import {initRepositorySearch} from './features/repo-search.js';
import {initMidnightRelativeTime} from './features/midnight.js';

// Init Gitea's Fomantic settings
initGiteaFomantic();
initDirAuto();

onDomReady(() => {
initMidnightRelativeTime();
initGlobalCommon();

initGlobalTooltips();
Expand Down