mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-22 08:42:32 -05:00
16eb85adfb
Before this patch, we were using `Date` getter/setter methods that worked with local time to get a list of Sundays that are in the range of some start date and end date. The problem with this was that the Sundays are in Unix epoch time and when we changed the "startDate" argument that was passed to make sure it is on a Sunday, this change would be reflected when we convert it to Unix epoch time. More specifically, I observed that we may get different Unix epochs depending on your timezone when the returned list should rather be timezone-agnostic. This led to issues in US timezones that caused the contributor, code frequency, and recent commit charts to not show any chart data. This fix resolves this by using getter/setter methods that work with UTC since it isn't dependent on timezones. Fixes #30851. --------- Co-authored-by: Sam Fisher <fisher@3echelon.local> (cherry picked from commit 22c7b3a74459833b86783e84d4708c8934d34e58)
72 lines
2.1 KiB
JavaScript
72 lines
2.1 KiB
JavaScript
import dayjs from 'dayjs';
|
|
import utc from 'dayjs/plugin/utc.js';
|
|
import {getCurrentLocale} from '../utils.js';
|
|
|
|
dayjs.extend(utc);
|
|
|
|
/**
|
|
* Returns an array of millisecond-timestamps of start-of-week days (Sundays)
|
|
*
|
|
* @param startConfig The start date. Can take any type that `Date` accepts.
|
|
* @param endConfig The end date. Can take any type that `Date` accepts.
|
|
*/
|
|
export function startDaysBetween(startDate, endDate) {
|
|
const start = dayjs.utc(startDate);
|
|
const end = dayjs.utc(endDate);
|
|
|
|
let current = start;
|
|
|
|
// Ensure the start date is a Sunday
|
|
while (current.day() !== 0) {
|
|
current = current.add(1, 'day');
|
|
}
|
|
|
|
const startDays = [];
|
|
while (current.isBefore(end)) {
|
|
startDays.push(current.valueOf());
|
|
current = current.add(1, 'week');
|
|
}
|
|
|
|
return startDays;
|
|
}
|
|
|
|
export function firstStartDateAfterDate(inputDate) {
|
|
if (!(inputDate instanceof Date)) {
|
|
throw new Error('Invalid date');
|
|
}
|
|
const dayOfWeek = inputDate.getUTCDay();
|
|
const daysUntilSunday = 7 - dayOfWeek;
|
|
const resultDate = new Date(inputDate.getTime());
|
|
resultDate.setUTCDate(resultDate.getUTCDate() + daysUntilSunday);
|
|
return resultDate.valueOf();
|
|
}
|
|
|
|
export function fillEmptyStartDaysWithZeroes(startDays, data) {
|
|
const result = {};
|
|
|
|
for (const startDay of startDays) {
|
|
result[startDay] = data[startDay] || {'week': startDay, 'additions': 0, 'deletions': 0, 'commits': 0};
|
|
}
|
|
|
|
return Object.values(result);
|
|
}
|
|
|
|
let dateFormat;
|
|
|
|
// format a Date object to document's locale, but with 24h format from user's current locale because this
|
|
// option is a personal preference of the user, not something that the document's locale should dictate.
|
|
export function formatDatetime(date) {
|
|
if (!dateFormat) {
|
|
// TODO: replace `hour12` with `Intl.Locale.prototype.getHourCycles` once there is broad browser support
|
|
dateFormat = new Intl.DateTimeFormat(getCurrentLocale(), {
|
|
day: 'numeric',
|
|
month: 'short',
|
|
year: 'numeric',
|
|
hour: 'numeric',
|
|
hour12: !Number.isInteger(Number(new Intl.DateTimeFormat([], {hour: 'numeric'}).format())),
|
|
minute: '2-digit',
|
|
timeZoneName: 'short',
|
|
});
|
|
}
|
|
return dateFormat.format(date);
|
|
}
|